You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/01/10 09:55:47 UTC
git commit: DELTASPIKE-25 cleanup and improved handling of file urls
Updated Branches:
refs/heads/master 1880190f9 -> c26a4285d
DELTASPIKE-25 cleanup and improved handling of file urls
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/c26a4285
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/c26a4285
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/c26a4285
Branch: refs/heads/master
Commit: c26a4285d7217c19fb84683df8348256cf95f210
Parents: 1880190
Author: gpetracek <gp...@apache.org>
Authored: Tue Jan 10 09:53:34 2012 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Tue Jan 10 09:53:34 2012 +0100
----------------------------------------------------------------------
.../impl/activation/TestClassDeactivation.java | 4 +-
.../org/apache/deltaspike/test/util/FileUtils.java | 57 +++
.../core/api/provider/BeanManagerProviderTest.java | 4 +-
.../impl/activation/TestClassDeactivation.java | 6 +-
.../testutil/ShrinkWrapArchiveUtil.java | 345 ---------------
.../deltaspike/integration/util/FileUtils.java | 57 +++
.../integration/util/ShrinkWrapArchiveUtil.java | 345 +++++++++++++++
7 files changed, 466 insertions(+), 352 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c26a4285/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/TestClassDeactivation.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/TestClassDeactivation.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/TestClassDeactivation.java
index fca5b45..f5af85d 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/TestClassDeactivation.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/activation/TestClassDeactivation.java
@@ -21,6 +21,7 @@ package org.apache.deltaspike.test.core.impl.activation;
import org.apache.deltaspike.core.impl.util.ClassDeactivation;
import org.apache.deltaspike.test.core.api.provider.TestBean;
import org.apache.deltaspike.test.core.api.temptestutil.ShrinkWrapArchiveUtil;
+import org.apache.deltaspike.test.util.FileUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -30,7 +31,6 @@ import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
-import java.io.File;
import java.net.URL;
/**
@@ -54,7 +54,7 @@ public class TestClassDeactivation
new String[]{"org.apache.deltaspike.test.core.impl.activation"},
null))
.addClass(TestBean.class)
- .addAsResource(new File(fileUrl.getFile()))
+ .addAsResource(FileUtils.getFileForURL(fileUrl.toString()))
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c26a4285/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/util/FileUtils.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/util/FileUtils.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/util/FileUtils.java
new file mode 100644
index 0000000..f1ab790
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/util/FileUtils.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.test.util;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * Some basic utils
+ */
+public class FileUtils
+{
+ private FileUtils()
+ {
+ // prevent instantiation
+ }
+
+ /**
+ * @param url the target URL
+ * @return a file created based on the given URL
+ */
+ public static File getFileForURL(String url)
+ {
+ //fix for wls
+ if(!url.startsWith("file:/")) {
+ url = "file:/" + url;
+ }
+
+ url = url.replaceAll("%20", " ");
+
+ try
+ {
+ return new File( (new URL(url)).getFile());
+ }
+ catch (MalformedURLException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c26a4285/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/api/provider/BeanManagerProviderTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/api/provider/BeanManagerProviderTest.java b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/api/provider/BeanManagerProviderTest.java
index 3aafca8..5682325 100644
--- a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/api/provider/BeanManagerProviderTest.java
+++ b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/api/provider/BeanManagerProviderTest.java
@@ -24,7 +24,7 @@ import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
import org.apache.deltaspike.integration.FullProfileCategory;
import org.apache.deltaspike.integration.SeCategory;
import org.apache.deltaspike.integration.WebProfileCategory;
-import org.apache.deltaspike.integration.testutil.ShrinkWrapArchiveUtil;
+import org.apache.deltaspike.integration.util.ShrinkWrapArchiveUtil;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -50,7 +50,7 @@ public class BeanManagerProviderTest
return ShrinkWrap.create(WebArchive.class)
.addAsLibraries(ShrinkWrapArchiveUtil.getArchives(null,
"META-INF/beans.xml",
- new String[] {"org.apache.deltaspike.core"},
+ new String[]{"org.apache.deltaspike.core"},
null))
.addClass(TestBean.class)
// Must add the categories to the archive
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c26a4285/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/impl/activation/TestClassDeactivation.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/impl/activation/TestClassDeactivation.java b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/impl/activation/TestClassDeactivation.java
index 632e9fc..baa0f64 100644
--- a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/impl/activation/TestClassDeactivation.java
+++ b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/core/impl/activation/TestClassDeactivation.java
@@ -22,7 +22,8 @@ import org.apache.deltaspike.core.impl.util.ClassDeactivation;
import org.apache.deltaspike.integration.FullProfileCategory;
import org.apache.deltaspike.integration.SeCategory;
import org.apache.deltaspike.integration.WebProfileCategory;
-import org.apache.deltaspike.integration.testutil.ShrinkWrapArchiveUtil;
+import org.apache.deltaspike.integration.util.FileUtils;
+import org.apache.deltaspike.integration.util.ShrinkWrapArchiveUtil;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -33,7 +34,6 @@ import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
-import java.io.File;
import java.net.URL;
/**
@@ -57,7 +57,7 @@ public class TestClassDeactivation
"META-INF/beans.xml",
new String[]{"org.apache.deltaspike.core"},
null))
- .addAsResource(new File(fileUrl.getFile()))
+ .addAsResource(FileUtils.getFileForURL(fileUrl.toString()))
.addClasses(ActivatedClass.class, DeactivatedClass.class, TestClassDeactivator.class)
// Must add the categories to the archive
.addClasses(SeCategory.class, WebProfileCategory.class, FullProfileCategory.class)
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c26a4285/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/testutil/ShrinkWrapArchiveUtil.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/testutil/ShrinkWrapArchiveUtil.java b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/testutil/ShrinkWrapArchiveUtil.java
deleted file mode 100644
index fd06a0d..0000000
--- a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/testutil/ShrinkWrapArchiveUtil.java
+++ /dev/null
@@ -1,345 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.deltaspike.integration.testutil;
-
-
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.jar.JarInputStream;
-import java.util.logging.Logger;
-import java.util.zip.ZipEntry;
-
-
-/**
- * Lots of neat little helpers to more easily create JavaArchives from marker files on the classpath.
- * This should finally get moved to ShrinkWrap core!
- *
- * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
- * @deprecated This class should get moved to ShrinkWrap itself!
- */
-public class ShrinkWrapArchiveUtil
-{
- private static final Logger LOG = Logger.getLogger(ShrinkWrapArchiveUtil.class.getName());
-
- /**
- * Resolve all markerFiles from the current ClassPath and package the root nodes
- * into a JavaArchive.
- *
- * @param classLoader to use
- * @param markerFile
- * @param includeIfPackageExists if not null, we will only create JavaArchives if the given package exists
- * @param excludeIfPackageExists if not null, we will <b>not</b> create JavaArchives if the given package exists.
- * This has a higher precedence than includeIfPackageExists.
- * @return
- */
- public static JavaArchive[] getArchives(ClassLoader classLoader,
- String markerFile,
- String[] includeIfPackageExists,
- String[] excludeIfPackageExists)
- {
- if (classLoader == null)
- {
- classLoader = ShrinkWrapArchiveUtil.class.getClassLoader();
- }
-
- try
- {
- Enumeration<URL> foundFiles = classLoader.getResources(markerFile);
-
- List<JavaArchive> archives = new ArrayList<JavaArchive>();
-
- while (foundFiles.hasMoreElements())
- {
- URL foundFile = foundFiles.nextElement();
- LOG.fine("Evaluating Java ClassPath URL " + foundFile.toExternalForm());
-
- JavaArchive archive = createArchive(foundFile, markerFile, includeIfPackageExists, excludeIfPackageExists);
- if (archive != null)
- {
- LOG.info("Adding Java ClassPath URL as JavaArchive " + foundFile.toExternalForm());
- archives.add(archive);
- }
- }
-
- return archives.toArray(new JavaArchive[archives.size()]);
- }
- catch (IOException ioe)
- {
- throw new RuntimeException(ioe);
- }
-
- }
-
- private static JavaArchive createArchive(URL foundFile, String markerFile,
- String[] includeIfPackageExists, String[] excludeIfPackageExists)
- throws IOException
- {
- String urlString = foundFile.toString();
- int idx = urlString.lastIndexOf(markerFile);
- urlString = urlString.substring(0, idx);
-
- String jarUrlPath = isJarUrl(urlString);
- if (jarUrlPath != null)
- {
- return addJarArchive((new URL(ensureCorrectUrlFormat(jarUrlPath))).openStream(),
- includeIfPackageExists, excludeIfPackageExists);
- }
- else
- {
- File f = new File((new URL(ensureCorrectUrlFormat(urlString))).getFile());
- if (!f.exists())
- {
- // try a fallback if the URL contains %20 -> spaces
- if (urlString.contains("%20"))
- {
- urlString = urlString.replaceAll("%20", " ");
- f = new File((new URL(ensureCorrectUrlFormat(urlString))).getFile());
- }
-
- }
-
- return addFileArchive(f, includeIfPackageExists, excludeIfPackageExists);
- }
- }
-
- private static JavaArchive addJarArchive(InputStream inputStream,
- String[] includeIfPackageExists,
- String[] excludeIfPackageExists)
- throws IOException
- {
- JavaArchive ret = null;
- JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class);
-
- if (includeIfPackageExists == null)
- {
- // no include rule, thus add it immediately
- ret = javaArchive;
- }
-
- JarInputStream jar = new JarInputStream(inputStream);
- try
- {
- for (ZipEntry jarEntry = jar.getNextEntry(); jarEntry != null; jarEntry = jar.getNextEntry())
- {
- String entryName = jarEntry.getName();
-
- if (jarEntry.isDirectory())
- {
- // exclude rule
- if (excludeIfPackageExists(entryName, excludeIfPackageExists))
- {
- return null;
- }
-
- if (ret == null && includeIfPackageExists(entryName, includeIfPackageExists))
- {
- ret = javaArchive;
- }
-
- continue;
- }
-
- if (entryName.endsWith(".class"))
- {
- String className = pathToClassName(entryName.substring(0, entryName.length() - (".class".length())));
- javaArchive.addClass(className);
- }
- else
- {
- javaArchive.addAsResource(entryName);
- }
- }
- }
- finally
- {
- try
- {
- jar.close();
- }
- catch (IOException ignored)
- {
- // all fine
- }
- }
-
- return ret;
- }
-
- private static JavaArchive addFileArchive(File archiveBasePath,
- String[] includeIfPackageExists,
- String[] excludeIfPackageExists)
- throws IOException
- {
- if (!archiveBasePath.exists())
- {
- return null;
- }
-
- JavaArchive ret = null;
- JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class);
-
- if (includeIfPackageExists == null)
- {
- // no include rule, thus add it immediately
- ret = javaArchive;
- }
-
- int basePathLength = archiveBasePath.getAbsolutePath().length() + 1;
-
- for (File archiveEntry : collectArchiveEntries(archiveBasePath))
- {
- String entryName = archiveEntry.getAbsolutePath().substring(basePathLength);
-
- // exclude rule
- if (excludeIfPackageExists(entryName, excludeIfPackageExists))
- {
- return null;
- }
-
- // include rule
- if (ret == null && includeIfPackageExists(entryName, includeIfPackageExists))
- {
- ret = javaArchive;
- }
-
- if (entryName.endsWith(".class"))
- {
- String className = pathToClassName(entryName.substring(0, entryName.length() - (".class".length())));
-
- javaArchive.addClass(className);
- }
- else
- {
- javaArchive.addAsResource(entryName.replace('\\', '/'));
- }
- }
-
- return ret;
- }
-
- private static List<File> collectArchiveEntries(File archiveBasePath)
- {
- if (archiveBasePath.isDirectory())
- {
- List<File> archiveEntries = new ArrayList<File>();
- File[] files = archiveBasePath.listFiles();
-
- for (File file : files)
- {
- if (file.isDirectory())
- {
- archiveEntries.addAll(collectArchiveEntries(file));
- }
- else
- {
- archiveEntries.add(file);
- }
- }
-
- return archiveEntries;
- }
-
- return Collections.EMPTY_LIST;
- }
-
-
- private static boolean excludeIfPackageExists(String jarEntryName, String[] excludeOnPackages)
- {
- if (excludeOnPackages != null)
- {
- String packageName = pathToClassName(jarEntryName);
-
- for (String excludeOnPackage : excludeOnPackages)
- {
- if (packageName.startsWith(excludeOnPackage))
- {
- return true;
- }
- }
- }
-
- return false;
- }
-
- private static boolean includeIfPackageExists(String jarEntryName, String[] includeOnPackages)
- {
- if (includeOnPackages == null)
- {
- return true;
- }
-
- String packageName = pathToClassName(jarEntryName);
-
- for (String includeOnPackage : includeOnPackages)
- {
- if (packageName.startsWith(includeOnPackage))
- {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * check if the given url path is a Jar
- *
- * @param urlPath
- * @return
- */
- private static String isJarUrl(String urlPath)
- {
- // common prefixes of the url are: jar: (tomcat), zip: (weblogic) and wsjar: (websphere)
- final int jarColon = urlPath.indexOf(':');
- if (urlPath.endsWith("!/") && jarColon > 0)
- {
- urlPath = urlPath.substring(jarColon + 1, urlPath.length() - 2);
- return urlPath;
- }
-
- return null;
- }
-
- private static String ensureCorrectUrlFormat(String url)
- {
- //fix for wls
- if (!url.startsWith("file:/"))
- {
- url = "file:/" + url;
- }
- return url;
- }
-
- private static String pathToClassName(String pathName)
- {
- return pathName.replace('/', '.').replace('\\', '.'); // replace unix and windows separators
- }
-
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c26a4285/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/FileUtils.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/FileUtils.java b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/FileUtils.java
new file mode 100644
index 0000000..93c40be
--- /dev/null
+++ b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/FileUtils.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.integration.util;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * Some basic utils
+ */
+public class FileUtils
+{
+ private FileUtils()
+ {
+ // prevent instantiation
+ }
+
+ /**
+ * @param url the target URL
+ * @return a file created based on the given URL
+ */
+ public static File getFileForURL(String url)
+ {
+ //fix for wls
+ if(!url.startsWith("file:/")) {
+ url = "file:/" + url;
+ }
+
+ url = url.replaceAll("%20", " ");
+
+ try
+ {
+ return new File( (new URL(url)).getFile());
+ }
+ catch (MalformedURLException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/c26a4285/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/ShrinkWrapArchiveUtil.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/ShrinkWrapArchiveUtil.java b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/ShrinkWrapArchiveUtil.java
new file mode 100644
index 0000000..add28aa
--- /dev/null
+++ b/deltaspike/core/integration-test/src/test/java/org/apache/deltaspike/integration/util/ShrinkWrapArchiveUtil.java
@@ -0,0 +1,345 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.deltaspike.integration.util;
+
+
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.jar.JarInputStream;
+import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+
+
+/**
+ * Lots of neat little helpers to more easily create JavaArchives from marker files on the classpath.
+ * This should finally get moved to ShrinkWrap core!
+ *
+ * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
+ * @deprecated This class should get moved to ShrinkWrap itself!
+ */
+public class ShrinkWrapArchiveUtil
+{
+ private static final Logger LOG = Logger.getLogger(ShrinkWrapArchiveUtil.class.getName());
+
+ /**
+ * Resolve all markerFiles from the current ClassPath and package the root nodes
+ * into a JavaArchive.
+ *
+ * @param classLoader to use
+ * @param markerFile
+ * @param includeIfPackageExists if not null, we will only create JavaArchives if the given package exists
+ * @param excludeIfPackageExists if not null, we will <b>not</b> create JavaArchives if the given package exists.
+ * This has a higher precedence than includeIfPackageExists.
+ * @return
+ */
+ public static JavaArchive[] getArchives(ClassLoader classLoader,
+ String markerFile,
+ String[] includeIfPackageExists,
+ String[] excludeIfPackageExists)
+ {
+ if (classLoader == null)
+ {
+ classLoader = ShrinkWrapArchiveUtil.class.getClassLoader();
+ }
+
+ try
+ {
+ Enumeration<URL> foundFiles = classLoader.getResources(markerFile);
+
+ List<JavaArchive> archives = new ArrayList<JavaArchive>();
+
+ while (foundFiles.hasMoreElements())
+ {
+ URL foundFile = foundFiles.nextElement();
+ LOG.fine("Evaluating Java ClassPath URL " + foundFile.toExternalForm());
+
+ JavaArchive archive = createArchive(foundFile, markerFile, includeIfPackageExists, excludeIfPackageExists);
+ if (archive != null)
+ {
+ LOG.info("Adding Java ClassPath URL as JavaArchive " + foundFile.toExternalForm());
+ archives.add(archive);
+ }
+ }
+
+ return archives.toArray(new JavaArchive[archives.size()]);
+ }
+ catch (IOException ioe)
+ {
+ throw new RuntimeException(ioe);
+ }
+
+ }
+
+ private static JavaArchive createArchive(URL foundFile, String markerFile,
+ String[] includeIfPackageExists, String[] excludeIfPackageExists)
+ throws IOException
+ {
+ String urlString = foundFile.toString();
+ int idx = urlString.lastIndexOf(markerFile);
+ urlString = urlString.substring(0, idx);
+
+ String jarUrlPath = isJarUrl(urlString);
+ if (jarUrlPath != null)
+ {
+ return addJarArchive((new URL(ensureCorrectUrlFormat(jarUrlPath))).openStream(),
+ includeIfPackageExists, excludeIfPackageExists);
+ }
+ else
+ {
+ File f = new File((new URL(ensureCorrectUrlFormat(urlString))).getFile());
+ if (!f.exists())
+ {
+ // try a fallback if the URL contains %20 -> spaces
+ if (urlString.contains("%20"))
+ {
+ urlString = urlString.replaceAll("%20", " ");
+ f = new File((new URL(ensureCorrectUrlFormat(urlString))).getFile());
+ }
+
+ }
+
+ return addFileArchive(f, includeIfPackageExists, excludeIfPackageExists);
+ }
+ }
+
+ private static JavaArchive addJarArchive(InputStream inputStream,
+ String[] includeIfPackageExists,
+ String[] excludeIfPackageExists)
+ throws IOException
+ {
+ JavaArchive ret = null;
+ JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class);
+
+ if (includeIfPackageExists == null)
+ {
+ // no include rule, thus add it immediately
+ ret = javaArchive;
+ }
+
+ JarInputStream jar = new JarInputStream(inputStream);
+ try
+ {
+ for (ZipEntry jarEntry = jar.getNextEntry(); jarEntry != null; jarEntry = jar.getNextEntry())
+ {
+ String entryName = jarEntry.getName();
+
+ if (jarEntry.isDirectory())
+ {
+ // exclude rule
+ if (excludeIfPackageExists(entryName, excludeIfPackageExists))
+ {
+ return null;
+ }
+
+ if (ret == null && includeIfPackageExists(entryName, includeIfPackageExists))
+ {
+ ret = javaArchive;
+ }
+
+ continue;
+ }
+
+ if (entryName.endsWith(".class"))
+ {
+ String className = pathToClassName(entryName.substring(0, entryName.length() - (".class".length())));
+ javaArchive.addClass(className);
+ }
+ else
+ {
+ javaArchive.addAsResource(entryName);
+ }
+ }
+ }
+ finally
+ {
+ try
+ {
+ jar.close();
+ }
+ catch (IOException ignored)
+ {
+ // all fine
+ }
+ }
+
+ return ret;
+ }
+
+ private static JavaArchive addFileArchive(File archiveBasePath,
+ String[] includeIfPackageExists,
+ String[] excludeIfPackageExists)
+ throws IOException
+ {
+ if (!archiveBasePath.exists())
+ {
+ return null;
+ }
+
+ JavaArchive ret = null;
+ JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class);
+
+ if (includeIfPackageExists == null)
+ {
+ // no include rule, thus add it immediately
+ ret = javaArchive;
+ }
+
+ int basePathLength = archiveBasePath.getAbsolutePath().length() + 1;
+
+ for (File archiveEntry : collectArchiveEntries(archiveBasePath))
+ {
+ String entryName = archiveEntry.getAbsolutePath().substring(basePathLength);
+
+ // exclude rule
+ if (excludeIfPackageExists(entryName, excludeIfPackageExists))
+ {
+ return null;
+ }
+
+ // include rule
+ if (ret == null && includeIfPackageExists(entryName, includeIfPackageExists))
+ {
+ ret = javaArchive;
+ }
+
+ if (entryName.endsWith(".class"))
+ {
+ String className = pathToClassName(entryName.substring(0, entryName.length() - (".class".length())));
+
+ javaArchive.addClass(className);
+ }
+ else
+ {
+ javaArchive.addAsResource(entryName.replace('\\', '/'));
+ }
+ }
+
+ return ret;
+ }
+
+ private static List<File> collectArchiveEntries(File archiveBasePath)
+ {
+ if (archiveBasePath.isDirectory())
+ {
+ List<File> archiveEntries = new ArrayList<File>();
+ File[] files = archiveBasePath.listFiles();
+
+ for (File file : files)
+ {
+ if (file.isDirectory())
+ {
+ archiveEntries.addAll(collectArchiveEntries(file));
+ }
+ else
+ {
+ archiveEntries.add(file);
+ }
+ }
+
+ return archiveEntries;
+ }
+
+ return Collections.EMPTY_LIST;
+ }
+
+
+ private static boolean excludeIfPackageExists(String jarEntryName, String[] excludeOnPackages)
+ {
+ if (excludeOnPackages != null)
+ {
+ String packageName = pathToClassName(jarEntryName);
+
+ for (String excludeOnPackage : excludeOnPackages)
+ {
+ if (packageName.startsWith(excludeOnPackage))
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private static boolean includeIfPackageExists(String jarEntryName, String[] includeOnPackages)
+ {
+ if (includeOnPackages == null)
+ {
+ return true;
+ }
+
+ String packageName = pathToClassName(jarEntryName);
+
+ for (String includeOnPackage : includeOnPackages)
+ {
+ if (packageName.startsWith(includeOnPackage))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * check if the given url path is a Jar
+ *
+ * @param urlPath
+ * @return
+ */
+ private static String isJarUrl(String urlPath)
+ {
+ // common prefixes of the url are: jar: (tomcat), zip: (weblogic) and wsjar: (websphere)
+ final int jarColon = urlPath.indexOf(':');
+ if (urlPath.endsWith("!/") && jarColon > 0)
+ {
+ urlPath = urlPath.substring(jarColon + 1, urlPath.length() - 2);
+ return urlPath;
+ }
+
+ return null;
+ }
+
+ private static String ensureCorrectUrlFormat(String url)
+ {
+ //fix for wls
+ if (!url.startsWith("file:/"))
+ {
+ url = "file:/" + url;
+ }
+ return url;
+ }
+
+ private static String pathToClassName(String pathName)
+ {
+ return pathName.replace('/', '.').replace('\\', '.'); // replace unix and windows separators
+ }
+
+
+}