1

我将我的 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,

我的问题是为什么它为相同的代码提供两个输出?是否可以在不更改任何代码的情况下修复它?

4

1 回答 1

0

JBoss AS 7 使用 VFS 。请参阅https://community.jboss.org/thread/170388#715560了解如何访问 VFS 'absolute url'

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);
    }
}

如果您尝试获取 appl.war 的类路径 jar URL,则在 JBoss AS 7 中是不可能的,但至少在 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

于 2014-02-14T06:39:29.347 回答