[jboss-svn-commits] JBoss Common SVN: r4706 - in shrinkwrap/trunk: extension-classloader and 13 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jul 8 17:32:06 EDT 2010
Author: aslak
Date: 2010-07-08 17:32:05 -0400 (Thu, 08 Jul 2010)
New Revision: 4706
Added:
shrinkwrap/trunk/extension-classloader/
shrinkwrap/trunk/extension-classloader/pom.xml
shrinkwrap/trunk/extension-classloader/src/
shrinkwrap/trunk/extension-classloader/src/main/
shrinkwrap/trunk/extension-classloader/src/main/java/
shrinkwrap/trunk/extension-classloader/src/main/java/org/
shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/
shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/
shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/
shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoader.java
shrinkwrap/trunk/extension-classloader/src/main/resources/
shrinkwrap/trunk/extension-classloader/src/test/
shrinkwrap/trunk/extension-classloader/src/test/java/
shrinkwrap/trunk/extension-classloader/src/test/java/org/
shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/
shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/
shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/
shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/LoadedTestClass.java
shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoaderTestCase.java
shrinkwrap/trunk/extension-classloader/src/test/resources/
Modified:
shrinkwrap/trunk/pom.xml
Log:
SHRINKWRAP-161 Added ShrinkWrap extension that use Archives as backing for a ClassLoader
Property changes on: shrinkwrap/trunk/extension-classloader
___________________________________________________________________
Name: svn:ignore
+ target
eclipse-target
target-eclipse
bin
.settings
.classpath
.project
Added: shrinkwrap/trunk/extension-classloader/pom.xml
===================================================================
--- shrinkwrap/trunk/extension-classloader/pom.xml (rev 0)
+++ shrinkwrap/trunk/extension-classloader/pom.xml 2010-07-08 21:32:05 UTC (rev 4706)
@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="UTF-8"?>
+ <!--
+ vi:ts=2:sw=2:expandtab:
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <!-- Parent -->
+ <parent>
+ <groupId>org.jboss.shrinkwrap</groupId>
+ <artifactId>shrinkwrap-build</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <relativePath>../build/pom.xml</relativePath>
+ </parent>
+
+ <!-- Model Version -->
+ <modelVersion>4.0.0</modelVersion>
+
+ <!-- Artifact Configuration -->
+ <artifactId>shrinkwrap-extension-classloader</artifactId>
+ <name>ShrinkWrap Extension ClassLoader</name>
+ <description>ShrinkWrap Extension for creating a ClassLoader based on a Archive</description>
+
+
+ <!-- Properties -->
+ <properties>
+
+ <!-- Versioning -->
+ <version.jboss.logging>2.2.0.CR1</version.jboss.logging>
+ <version.org.jboss.logmanager>1.2.0.CR1</version.org.jboss.logmanager>
+
+ </properties>
+
+ <!-- Dependencies -->
+ <dependencies>
+
+ <!--
+ org.jboss.shrinkwrap
+ -->
+ <dependency>
+ <groupId>org.jboss.shrinkwrap</groupId>
+ <artifactId>shrinkwrap-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.shrinkwrap</groupId>
+ <artifactId>shrinkwrap-extension-vfs3</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.jboss.shrinkwrap</groupId>
+ <artifactId>shrinkwrap-impl-base</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!--
+ External Projects
+ -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- org.jboss:jboss-vfs -->
+
+ <dependency>
+ <groupId>org.jboss.logging</groupId>
+ <artifactId>jboss-logging-spi</artifactId>
+ <version>${version.jboss.logging}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.logging</groupId>
+ <artifactId>jboss-logging-jdk</artifactId>
+ <version>${version.jboss.logging}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.logmanager</groupId>
+ <artifactId>jboss-logmanager</artifactId>
+ <version>${version.org.jboss.logmanager}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.logging</groupId>
+ <artifactId>jboss-logging-logmanager</artifactId>
+ <version>${version.jboss.logging}</version>
+ <scope>test</scope>
+ </dependency>
+
+
+ </dependencies>
+
+ <!-- Build Configuration -->
+ <build>
+
+ <plugins>
+
+ </plugins>
+
+ </build>
+
+</project>
+
Added: shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoader.java
===================================================================
--- shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoader.java (rev 0)
+++ shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoader.java 2010-07-08 21:32:05 UTC (rev 4706)
@@ -0,0 +1,146 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.shrinkwrap.classloader;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.vfs3.ArchiveFileSystem;
+import org.jboss.vfs.TempDir;
+import org.jboss.vfs.TempFileProvider;
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VirtualFile;
+
+/**
+ * Extension that will create a ClassLoader based on a Array of Archives
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ShrinkWrapClassLoader extends URLClassLoader implements Closeable
+{
+ private Set<Closeable> vfsHandlers = new HashSet<Closeable>();
+
+ private Set<ExecutorService> executorServices = new HashSet<ExecutorService>();
+
+ /**
+ * @param archives
+ */
+ public ShrinkWrapClassLoader(final Archive<?>... archives)
+ {
+ super(new URL[]{});
+
+ if(archives == null)
+ {
+ throw new IllegalArgumentException("Archives must be specified");
+ }
+ addArchives(archives);
+ }
+
+ /**
+ * @param parent
+ * @param archives
+ */
+ public ShrinkWrapClassLoader(ClassLoader parent, final Archive<?>... archives)
+ {
+ super(new URL[]{});
+
+ if(archives == null)
+ {
+ throw new IllegalArgumentException("Archives must be specified");
+ }
+ addArchives(archives);
+ }
+
+ protected void addArchives(Archive<?>[] archives)
+ {
+ for(Archive<?> archive : archives)
+ {
+ addArchive(archive);
+ }
+ }
+
+ protected void addArchive(Archive<?> archive)
+ {
+ // TODO: Wrap a ExecutorService in a ScheduledExecutorService
+ //Configuration configuration = archive.as(Configurable.class).getConfiguration();
+ ScheduledExecutorService executorService = null; //configuration.getExecutorService();
+ if(executorService == null)
+ {
+ executorService = Executors.newScheduledThreadPool(2);
+
+ // TODO: only add to 'managed' executor services if it was created here..
+
+ // add to list of resources to cleanup during close()
+ executorServices.add(executorService);
+ }
+
+ try
+ {
+ TempFileProvider tempFileProvider = TempFileProvider.create("shrinkwrap-classloader", executorService);
+
+ final TempDir tempDir = tempFileProvider.createTempDir(archive.getName());
+ final VirtualFile virtualFile = VFS.getChild(UUID.randomUUID().toString()).getChild(archive.getName());
+
+ Closeable handle = VFS.mount(virtualFile, new ArchiveFileSystem(archive, tempDir));
+
+ // add to list of resources to cleanup during close()
+ vfsHandlers.add(handle);
+
+ addURL(virtualFile.toURL());
+
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Could not create ClassLoader from archive: " + archive.getName(), e);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.Closeable#close()
+ */
+ public void close() throws IOException
+ {
+ // Unmount all VFS3 mount points
+ for(Closeable handle : vfsHandlers)
+ {
+ try
+ {
+ handle.close();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace(); // TODO: handle exception
+ }
+ }
+
+ // Shutdown all created Executor Services.
+ for(ExecutorService executorService : executorServices)
+ {
+ executorService.shutdownNow();
+ }
+ }
+}
Added: shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/LoadedTestClass.java
===================================================================
--- shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/LoadedTestClass.java (rev 0)
+++ shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/LoadedTestClass.java 2010-07-08 21:32:05 UTC (rev 4706)
@@ -0,0 +1,28 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.shrinkwrap.classloader;
+
+/**
+ * LoadedTestClass
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class LoadedTestClass
+{
+
+}
Added: shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoaderTestCase.java
===================================================================
--- shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoaderTestCase.java (rev 0)
+++ shrinkwrap/trunk/extension-classloader/src/test/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoaderTestCase.java 2010-07-08 21:32:05 UTC (rev 4706)
@@ -0,0 +1,80 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.shrinkwrap.classloader;
+
+import java.net.URL;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * ShrinkWrapClassLoaderTestCase
+ *
+ * @author <a href="mailto:aslak at redhat.com">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ShrinkWrapClassLoaderTestCase
+{
+
+ private Archive<?> createArchive()
+ {
+ return ShrinkWrap.create(JavaArchive.class)
+ .as(ExplodedImporter.class)
+ .importDirectory("target/test-classes")
+ .as(JavaArchive.class);
+ }
+
+ @Test
+ public void shouldBeAbleToLoadClassFromArchive() throws Exception
+ {
+ ShrinkWrapClassLoader archiveClassLoader = new ShrinkWrapClassLoader(createArchive());
+
+ try
+ {
+ Class<?> loadedTestClass = archiveClassLoader.loadClass(
+ "org.jboss.shrinkwrap.classloader.LoadedTestClass");
+
+ Assert.assertNotNull(loadedTestClass);
+ }
+ finally
+ {
+ archiveClassLoader.close();
+ }
+ }
+
+ @Test
+ public void shouldBeAbleToLoadResourceFromArchive() throws Exception
+ {
+ ShrinkWrapClassLoader archiveClassLoader = new ShrinkWrapClassLoader(createArchive());
+
+ try
+ {
+ URL resource = archiveClassLoader.getResource(
+ "org/jboss/shrinkwrap/classloader/LoadedTestClass.class");
+
+ Assert.assertNotNull(resource);
+ }
+ finally
+ {
+ archiveClassLoader.close();
+ }
+ }
+}
\ No newline at end of file
Modified: shrinkwrap/trunk/pom.xml
===================================================================
--- shrinkwrap/trunk/pom.xml 2010-07-08 14:29:34 UTC (rev 4705)
+++ shrinkwrap/trunk/pom.xml 2010-07-08 21:32:05 UTC (rev 4706)
@@ -77,6 +77,7 @@
<module>extension-tar</module>
<module>extension-vfs3</module>
<module>extension-vdf</module>
+ <module>extension-classloader</module>
</modules>
<repositories>
More information about the jboss-svn-commits
mailing list