返回vfs而不是文件

时间:2013-11-19 13:05:26

标签: java jboss classpath vfs

我将我的Web应用程序从JBOSS 5升级到JBOSS 7.在我的Web应用程序中获取包含特定文件的jar我使用以下代码行。


static final Pattern _URLJarNamePattern = Pattern.compile(".*[/\\\\](.+)\\.(jar|zip)\\!/.*");;
 try {
                urls = Thread.currentThread().getContextClassLoader().getResources("META-INF/config.properties");
            } catch (IOException e) {
                throw new AWGenericException(e);
            }
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                Matcher m = _URLJarNamePattern.matcher(url.toExternalForm());
                if (m.matches()) {
                    String jarName = m.group(1);
                     System.out.println(jarName);
                    _AWJarUrlsByName.put(jarName, url);
                }
            }

在我的旧案例(JBOSS 5服务器)中,url字符串如下所示:


jar:file:/D:/JBOSS5/3.2.0/server/default/deploy/appl.war/WEB-INF/lib/app.myapp.jar!/META-INF/config.properties

这里当我在JBOSS 5中运行上面的代码时,它根据我输入的模式成功运行并输出jar文件名(这里是app.myapp

但是在JBOSS 7的情况下,url字符串如下所示,根据上述模式导致输出错误


vfs:/E:/Servers/jbossas7/standalone/deployments/appl.war/WEB-INF/lib/app.myapp.jar/META-INF/config.properties,

我的问题是为什么它为同一代码提供了两个输出?是否有可能在没有任何代码更改的情况下修复它?

1 个答案:

答案 0 :(得分:0)

JBoss AS 7使用VFS。有关如何访问VFS'绝对网址'

,请参阅https://community.jboss.org/thread/170388#715560
while (urls.hasMoreElements()) {
    URL vfsUrl = urls.nextElement();
    URLConnection conn = vfsUrl.openConnection();
    VirtualFile vf = (VirtualFile)conn.getContent();
    //not sure if 'url' points to a vfs 'content' dir 
    //which parent dir has the actual file you are interested
    //yet, it is file:// not vfs:// path in url, now
    URL url = VFSUtils.getPhysicalURL(vf);
    Matcher m = _URLJarNamePattern.matcher(url.toExternalForm());
    if (m.matches()) {
        String jarName = m.group(1);
         System.out.println(jarName);
        _AWJarUrlsByName.put(jarName, url);
    }
}

如果你试图在JBoss AS 7中获得appl.war的classpath jar URL,那么至少在Tomcat 7中是可能的:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URLClassLoader cl = (URLClassLoader) classLoader;//classcast exception in JBoss AS 7
URL[] urls = cl.getURLs();

对于JBoss AS 7类路径jar,以下片段创建了appl.war的类路径的URL数组

http://pastebin.com/YjNwdePr