Author: alessio.soldano(a)jboss.com
Date: 2008-10-30 15:40:43 -0400 (Thu, 30 Oct 2008)
New Revision: 8602
Added:
common/trunk/src/test/java/org/jboss/test/ws/common/ResourceLoaderAdapterTestCase.java
common/trunk/src/test/java/org/jboss/test/ws/common/URLLoaderAdapterTestCase.java
Modified:
common/trunk/src/main/java/org/jboss/wsf/common/ResourceLoaderAdapter.java
common/trunk/src/main/java/org/jboss/wsf/common/URLLoaderAdapter.java
Log:
- [JBWS-2375] Adding getChildren and getName to ResourceLoaderAdapter and
URLLoaderAdapter
- adding testcases for ResourceLoaderAdapter and URLLoaderAdapter
Modified: common/trunk/src/main/java/org/jboss/wsf/common/ResourceLoaderAdapter.java
===================================================================
--- common/trunk/src/main/java/org/jboss/wsf/common/ResourceLoaderAdapter.java 2008-10-30
19:37:40 UTC (rev 8601)
+++ common/trunk/src/main/java/org/jboss/wsf/common/ResourceLoaderAdapter.java 2008-10-30
19:40:43 UTC (rev 8602)
@@ -25,7 +25,15 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import org.jboss.logging.Logger;
import org.jboss.wsf.spi.deployment.UnifiedVirtualFile;
/**
@@ -33,12 +41,15 @@
* If no classload is set, the the thread context classloader will be used.
*
* @author Heiko.Braun(a)jboss.org
+ * @author alessio.soldano(a)jboss.com
* @since 25.01.2007
*/
public class ResourceLoaderAdapter implements UnifiedVirtualFile
{
private URL resourceURL;
private ClassLoader loader;
+ private static Logger log = Logger.getLogger(ResourceLoaderAdapter.class);
+ private static final String jarFileSeparator = "/";
public ResourceLoaderAdapter()
{
@@ -112,4 +123,116 @@
throw new IllegalStateException("UnifiedVirtualFile not
initialized");
return resourceURL;
}
+
+ public List<UnifiedVirtualFile> getChildren() throws IOException
+ {
+ if (null == this.resourceURL)
+ throw new IllegalStateException("UnifiedVirtualFile not
initialized");
+ List<UnifiedVirtualFile> list = new LinkedList<UnifiedVirtualFile>();
+ if (resourceURL.getProtocol().equals("jar"))
+ {
+ String urlString = resourceURL.toExternalForm();
+ String jarRoot = urlString.substring(4, urlString.indexOf("ar!") +
2);
+ String path = urlString.contains("!") ?
urlString.substring(urlString.lastIndexOf("!") + 2) : "";
+ if (path.endsWith(jarFileSeparator))
+ path = path.substring(0, path.lastIndexOf(jarFileSeparator));
+
+ try
+ {
+ //get the jar and open it
+ String folder = jarRoot.substring(5,jarRoot.lastIndexOf(File.separator));
+ String filename = jarRoot.substring(jarRoot.lastIndexOf(File.separator)+1);
+ final File jar = new File(folder, filename);
+
+ PrivilegedAction<JarFile> action = new
PrivilegedAction<JarFile>()
+ {
+ public JarFile run()
+ {
+ try
+ {
+ return new JarFile(jar);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+ JarFile jarFile = AccessController.doPrivileged(action);
+
+ if (jar.canRead())
+ {
+ Enumeration<JarEntry> entries = jarFile.entries();
+ List<String> pathMatch = new LinkedList<String>();
+ List<String> finalMatch = new LinkedList<String>();
+ while (entries.hasMoreElements())
+ {
+ JarEntry entry = entries.nextElement();
+ String name = entry.getName();
+ //keep entries starting with the current resource path (exclude inner
classes and the current file)
+ if (name.startsWith(path + jarFileSeparator) && (name.length()
> path.length() + 1) && !name.contains("$"))
+ pathMatch.add(name.substring(path.length() + 1));
+ }
+ for (String s : pathMatch)
+ {
+ //do not go deeper than the current dir
+ if (!s.contains(jarFileSeparator) || s.indexOf(jarFileSeparator) ==
s.length() - 1)
+ finalMatch.add(s);
+ }
+ for (String s : finalMatch)
+ {
+ URL sUrl = new URL(urlString + jarFileSeparator + s);
+ list.add(new ResourceLoaderAdapter(loader, sUrl));
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ log.error("Cannot get children for resource: " + resourceURL, e);
+ }
+ }
+ else //std file/dir
+ {
+ try
+ {
+ File file = new File(resourceURL.toURI());
+ if (file.exists() && file.isDirectory())
+ {
+ File[] files = file.listFiles();
+ if (files != null)
+ {
+ for (File f : files)
+ {
+ list.add(new ResourceLoaderAdapter(loader, f.toURL()));
+ }
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ log.error("Cannot get children for resource: " + resourceURL, e);
+ }
+ }
+ return list;
+ }
+
+ public String getName()
+ {
+ if (null == this.resourceURL)
+ throw new IllegalStateException("UnifiedVirtualFile not
initialized");
+ String name = null;
+ try
+ {
+ String filename = resourceURL.getFile();
+ File f = new File(filename);
+ name = f.getName();
+ if (f.isDirectory() || (resourceURL.getProtocol().equals("jar")
&& filename.endsWith(jarFileSeparator)))
+ name = name + jarFileSeparator;
+ }
+ catch (Exception e)
+ {
+ log.error("Cannot get name for resource: " + resourceURL);
+ }
+ return name;
+ }
}
Modified: common/trunk/src/main/java/org/jboss/wsf/common/URLLoaderAdapter.java
===================================================================
--- common/trunk/src/main/java/org/jboss/wsf/common/URLLoaderAdapter.java 2008-10-30
19:37:40 UTC (rev 8601)
+++ common/trunk/src/main/java/org/jboss/wsf/common/URLLoaderAdapter.java 2008-10-30
19:40:43 UTC (rev 8602)
@@ -26,7 +26,15 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import org.jboss.logging.Logger;
import org.jboss.wsf.spi.deployment.UnifiedVirtualFile;
/**
@@ -35,6 +43,7 @@
*
*
* @author Heiko.Braun(a)jboss.org
+ * @author alessio.soldano(a)jboss.com
* @since 25.01.2007
*/
public class URLLoaderAdapter implements UnifiedVirtualFile
@@ -42,6 +51,8 @@
private URL rootURL;
private URL resourceURL;
private transient URLClassLoader loader;
+ private static Logger log = Logger.getLogger(URLLoaderAdapter.class);
+ private static final String jarFileSeparator = "/";
public URLLoaderAdapter(URL rootURL)
{
@@ -121,4 +132,116 @@
}
return loader;
}
+
+ public List<UnifiedVirtualFile> getChildren() throws IOException
+ {
+ List<UnifiedVirtualFile> list = new LinkedList<UnifiedVirtualFile>();
+
+ URL url = toURL();
+
+ if (url.getProtocol().equals("jar"))
+ {
+ String urlString = url.toExternalForm();
+ String jarRoot = urlString.substring(4, urlString.indexOf("ar!") +
2);
+ String path = urlString.contains("!") ?
urlString.substring(urlString.lastIndexOf("!") + 2) : "";
+ if (path.endsWith(jarFileSeparator))
+ path = path.substring(0, path.lastIndexOf(jarFileSeparator));
+
+ try
+ {
+ String folder = jarRoot.substring(5,jarRoot.lastIndexOf(File.separator));
+ String filename = jarRoot.substring(jarRoot.lastIndexOf(File.separator)+1);
+ final File jar = new File(folder, filename);
+
+ PrivilegedAction<JarFile> action = new
PrivilegedAction<JarFile>()
+ {
+ public JarFile run()
+ {
+ try
+ {
+ return new JarFile(jar);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+ JarFile jarFile = AccessController.doPrivileged(action);
+
+ if (jar.canRead())
+ {
+ Enumeration<JarEntry> entries = jarFile.entries();
+ List<String> pathMatch = new LinkedList<String>();
+ List<String> finalMatch = new LinkedList<String>();
+ while (entries.hasMoreElements())
+ {
+ JarEntry entry = entries.nextElement();
+ String name = entry.getName();
+ //keep entries starting with the current resource path (exclude inner
classes and the current file)
+ if (name.startsWith(path + jarFileSeparator) && (name.length()
> path.length() + 1) && !name.contains("$"))
+ pathMatch.add(name.substring(path.length() + 1));
+ }
+
+ for (String s : pathMatch)
+ {
+ //do not go deeper than the current dir
+ if (!s.contains(jarFileSeparator) || s.indexOf(jarFileSeparator) ==
s.length() - 1)
+ finalMatch.add(s);
+ }
+ for (String s : finalMatch)
+ {
+ URL sUrl = new URL(urlString + jarFileSeparator + s);
+ list.add(new URLLoaderAdapter(rootURL, loader, sUrl));
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ log.error("Cannot get children for resource: " + url);
+ }
+ }
+ else //std file/dir
+ {
+ try
+ {
+ File file = new File(url.toURI());
+ if (file.exists() && file.isDirectory())
+ {
+ File[] files = file.listFiles();
+ if (files != null)
+ {
+ for (File f : files)
+ {
+ list.add(new URLLoaderAdapter(rootURL, loader, f.toURL()));
+ }
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ log.error("Cannot get children for resource: " + url, e);
+ }
+ }
+ return list;
+ }
+
+ public String getName()
+ {
+ String name = null;
+ try
+ {
+ String filename = toURL().getFile();
+ File f = new File(filename);
+ name = f.getName();
+ if (f.isDirectory() || (toURL().getProtocol().equals("jar") &&
filename.endsWith(jarFileSeparator)))
+ name = name + jarFileSeparator;
+ }
+ catch (Exception e)
+ {
+ log.error("Cannot get name for resource: " + toURL(), e);
+ }
+ return name;
+ }
}
Added:
common/trunk/src/test/java/org/jboss/test/ws/common/ResourceLoaderAdapterTestCase.java
===================================================================
---
common/trunk/src/test/java/org/jboss/test/ws/common/ResourceLoaderAdapterTestCase.java
(rev 0)
+++
common/trunk/src/test/java/org/jboss/test/ws/common/ResourceLoaderAdapterTestCase.java 2008-10-30
19:40:43 UTC (rev 8602)
@@ -0,0 +1,93 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.test.ws.common;
+
+import java.io.File;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.jboss.wsf.common.ResourceLoaderAdapter;
+import org.jboss.wsf.spi.deployment.UnifiedVirtualFile;
+
+/**
+ * Test the ResourceLoaderAdapter
+ *
+ * @author alessio.soldano(a)jboss.org
+ * @since 30-Oct-2008
+ */
+public class ResourceLoaderAdapterTestCase extends TestCase
+{
+ public void testWithJar() throws Exception
+ {
+ //Getting the classloader for UnifiedVirtualFile which lives in SPI -> external
jar (from the maven repo)
+ ClassLoader cl = UnifiedVirtualFile.class.getClassLoader();
+ ResourceLoaderAdapter ula = new ResourceLoaderAdapter(cl);
+
+ UnifiedVirtualFile deployment =
ula.findChild("org/jboss/wsf/spi/deployment/");
+ assertNotNull(deployment);
+ assertTrue(deployment.toURL().toExternalForm().contains("jar!")); //check
we got a URL to a jar
+ assertEquals("deployment/", deployment.getName());
+ List<UnifiedVirtualFile> children = deployment.getChildren();
+ assertNotNull(children);
+ assertTrue(children.size() > 0);
+ UnifiedVirtualFile unifiedVirtualFile = null;
+ UnifiedVirtualFile integration = null;
+ for (UnifiedVirtualFile uvf : children)
+ {
+ if (uvf.getName().equals(UnifiedVirtualFile.class.getSimpleName() +
".class"))
+ unifiedVirtualFile = uvf;
+ else if (uvf.getName().equals("integration/"))
+ integration = uvf;
+ }
+ assertNotNull(unifiedVirtualFile);
+ assertNotNull(integration);
+ assertTrue(unifiedVirtualFile.getChildren().size() == 0);
+ }
+
+ public void testWithDir() throws Exception
+ {
+ //Getting the classloader for ResourceLoaderAdapter which lives in COMMON ->
target/classes in this project
+ ClassLoader cl = ResourceLoaderAdapter.class.getClassLoader();
+ ResourceLoaderAdapter ula = new ResourceLoaderAdapter(cl);
+
+ UnifiedVirtualFile common = ula.findChild("org/jboss/wsf/common/");
+ assertNotNull(common);
+ assertTrue(common.toURL().toExternalForm().contains("target" +
File.separator + "classes")); //check we got a URL to dir
+ assertEquals("common/", common.getName());
+ List<UnifiedVirtualFile> children = common.getChildren();
+ assertNotNull(children);
+ assertTrue(children.size() > 0);
+ UnifiedVirtualFile resourceLoaderAdapter = null;
+ UnifiedVirtualFile utils = null;
+ for (UnifiedVirtualFile uvf : children)
+ {
+ if (uvf.getName().equals(ResourceLoaderAdapter.class.getSimpleName() +
".class"))
+ resourceLoaderAdapter = uvf;
+ else if (uvf.getName().equals("utils/"))
+ utils = uvf;
+ }
+ assertNotNull(resourceLoaderAdapter);
+ assertNotNull(utils);
+ assertTrue(resourceLoaderAdapter.getChildren().size() == 0);
+ }
+}
Property changes on:
common/trunk/src/test/java/org/jboss/test/ws/common/ResourceLoaderAdapterTestCase.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: common/trunk/src/test/java/org/jboss/test/ws/common/URLLoaderAdapterTestCase.java
===================================================================
--- common/trunk/src/test/java/org/jboss/test/ws/common/URLLoaderAdapterTestCase.java
(rev 0)
+++
common/trunk/src/test/java/org/jboss/test/ws/common/URLLoaderAdapterTestCase.java 2008-10-30
19:40:43 UTC (rev 8602)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.test.ws.common;
+
+import java.io.File;
+import java.net.URL;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.jboss.wsf.common.URLLoaderAdapter;
+import org.jboss.wsf.spi.deployment.UnifiedVirtualFile;
+
+/**
+ * Test the URLLoaderAdapter
+ *
+ * @author alessio.soldano(a)jboss.org
+ * @since 30-Oct-2008
+ */
+public class URLLoaderAdapterTestCase extends TestCase
+{
+ public void testWithJar() throws Exception
+ {
+ //Getting the SPI jar url
+ ClassLoader cl = UnifiedVirtualFile.class.getClassLoader();
+ URL rootURL = getJarUrl(cl.getResource("org/jboss/wsf/spi/"));
+ assertNotNull(rootURL);
+ URLLoaderAdapter ula = new URLLoaderAdapter(rootURL);
+
+ UnifiedVirtualFile deployment =
ula.findChild("org/jboss/wsf/spi/deployment/");
+ assertNotNull(deployment);
+ assertTrue(deployment.toURL().toExternalForm().contains("jar!")); //check
we got a URL to a jar
+ assertEquals("deployment/", deployment.getName());
+ List<UnifiedVirtualFile> children = deployment.getChildren();
+ assertNotNull(children);
+ assertTrue(children.size() > 0);
+ UnifiedVirtualFile unifiedVirtualFile = null;
+ UnifiedVirtualFile integration = null;
+ for (UnifiedVirtualFile uvf : children)
+ {
+ if (uvf.getName().equals(UnifiedVirtualFile.class.getSimpleName() +
".class"))
+ unifiedVirtualFile = uvf;
+ else if (uvf.getName().equals("integration/"))
+ integration = uvf;
+ }
+ assertNotNull(unifiedVirtualFile);
+ assertNotNull(integration);
+ assertTrue(unifiedVirtualFile.getChildren().size() == 0);
+ }
+
+ public void testWithDir() throws Exception
+ {
+ ClassLoader cl = UnifiedVirtualFile.class.getClassLoader();
+ URL rootURL = cl.getResource("org/jboss/wsf/common/");
+ assertNotNull(rootURL);
+ URLLoaderAdapter ula = new URLLoaderAdapter(rootURL);
+
+ UnifiedVirtualFile common = ula.findChild("org/jboss/wsf/common/");
+ assertNotNull(common);
+ assertTrue(common.toURL().toExternalForm().contains("target" +
File.separator + "classes")); //check we got a URL to dir
+ assertEquals("common/", common.getName());
+ List<UnifiedVirtualFile> children = common.getChildren();
+ assertNotNull(children);
+ assertTrue(children.size() > 0);
+ UnifiedVirtualFile urlLoaderAdapter = null;
+ UnifiedVirtualFile utils = null;
+ for (UnifiedVirtualFile uvf : children)
+ {
+ if (uvf.getName().equals(URLLoaderAdapter.class.getSimpleName() +
".class"))
+ urlLoaderAdapter = uvf;
+ else if (uvf.getName().equals("utils/"))
+ utils = uvf;
+ }
+ assertNotNull(urlLoaderAdapter);
+ assertNotNull(utils);
+ assertTrue(urlLoaderAdapter.getChildren().size() == 0);
+ }
+
+ private static URL getJarUrl(URL url) throws Exception
+ {
+ String urlString = url.toExternalForm();
+ String jarRoot = urlString.substring(4, urlString.indexOf("ar!") + 2);
+ return new URL(jarRoot);
+ }
+}
Property changes on:
common/trunk/src/test/java/org/jboss/test/ws/common/URLLoaderAdapterTestCase.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF