[jboss-svn-commits] JBoss Common SVN: r4754 - in shrinkwrap/trunk/extension-classloader: src/main/java/org/jboss/shrinkwrap/classloader and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jul 16 12:00:43 EDT 2010


Author: aslak
Date: 2010-07-16 12:00:42 -0400 (Fri, 16 Jul 2010)
New Revision: 4754

Removed:
   shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ImmediateScheduledExecutorService.java
Modified:
   shrinkwrap/trunk/extension-classloader/pom.xml
   shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoader.java
Log:
SHRINKWRAP-161 Changed ClassLoader to have a internal URLStreamHandler to avoid relying on a subset of VFS. 


Modified: shrinkwrap/trunk/extension-classloader/pom.xml
===================================================================
--- shrinkwrap/trunk/extension-classloader/pom.xml	2010-07-16 14:46:07 UTC (rev 4753)
+++ shrinkwrap/trunk/extension-classloader/pom.xml	2010-07-16 16:00:42 UTC (rev 4754)
@@ -25,10 +25,6 @@
   <!-- 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 -->
@@ -42,11 +38,6 @@
       <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>
@@ -71,34 +62,6 @@
       <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 -->

Deleted: shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ImmediateScheduledExecutorService.java
===================================================================
--- shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ImmediateScheduledExecutorService.java	2010-07-16 14:46:07 UTC (rev 4753)
+++ shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ImmediateScheduledExecutorService.java	2010-07-16 16:00:42 UTC (rev 4754)
@@ -1,240 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, 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.util.Collection;
-import java.util.List;
-import java.util.concurrent.Callable;
-import java.util.concurrent.Delayed;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-/**
- * {@link ScheduledExecutorService} implementation which ignores
- * all scheduling operations and immediately dispatches invocations
- * to an underlying {@link ExecutorService}
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- */
-class ImmediateScheduledExecutorService implements ScheduledExecutorService
-{
-   //-------------------------------------------------------------------------------------||
-   // Instance Members -------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Delegate for all operations
-    */
-   private final ExecutorService delegate;
-
-   //-------------------------------------------------------------------------------------||
-   // Constructors -----------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Creates a new instance using the specified delegate
-    * @param delegate
-    * @throws IllegalArgumentException If the delegate is not specified
-    */
-   ImmediateScheduledExecutorService(final ExecutorService delegate) throws IllegalArgumentException
-   {
-      // Precondition checks
-      if (delegate == null)
-      {
-         throw new IllegalArgumentException("Delegate " + ExecutorService.class.getSimpleName() + " must be specified");
-      }
-
-      // Set
-      this.delegate = delegate;
-   }
-
-   //-------------------------------------------------------------------------------------||
-   // Required Implementations -----------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   private static class ImmediateScheduledFuture<V> implements ScheduledFuture<V>
-   {
-
-      /**
-       * Delegate through which all {@link Future} contracts will
-       * be fulfilled 
-       */
-      private final Future<V> delegate;
-
-      ImmediateScheduledFuture(final Future<V> delegate)
-      {
-         assert delegate != null : "Delegate Future must be specified";
-         this.delegate = delegate;
-      }
-
-      /**
-       * Returns a delay of 0, always
-       * @see java.util.concurrent.Delayed#getDelay(java.util.concurrent.TimeUnit)
-       */
-      public long getDelay(final TimeUnit unit)
-      {
-         // No delay
-         return 0;
-      }
-
-      /**
-       * Compare our delay of 0 to the incoming {@link Delayed}
-       * @see java.lang.Comparable#compareTo(java.lang.Object)
-       */
-      public int compareTo(final Delayed o)
-      {
-         final TimeUnit unit = TimeUnit.SECONDS;
-         return new Long(this.getDelay(unit)).compareTo(o.getDelay(unit));
-      }
-
-      /*
-       * Delegate methods only below this marker
-       */
-
-      public boolean cancel(boolean mayInterruptIfRunning)
-      {
-         return delegate.cancel(mayInterruptIfRunning);
-      }
-
-      public V get() throws InterruptedException, ExecutionException
-      {
-         try
-         {
-            return this.delegate.get();
-         }
-         catch (final Exception e)
-         {
-            throw new ExecutionException(e);
-         }
-      }
-
-      public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
-      {
-         return get();
-      }
-
-      public boolean isCancelled()
-      {
-         return this.delegate.isCancelled();
-      }
-
-      public boolean isDone()
-      {
-         return this.delegate.isDone();
-      }
-
-   }
-
-   @SuppressWarnings("unchecked")
-   public ScheduledFuture<?> schedule(final Runnable command, final long delay, final TimeUnit unit)
-   {
-      final Future<?> future = this.submit(command);
-      return new ImmediateScheduledFuture(future);
-   }
-
-   public <V> ScheduledFuture<V> schedule(final Callable<V> callable, final long delay, final TimeUnit unit)
-   {
-      final Future<V> future = this.submit(callable);
-      return new ImmediateScheduledFuture<V>(future);
-   }
-
-   public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
-   {
-      return this.schedule(command, period, unit);
-   }
-
-   public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
-   {
-      return this.schedule(command, delay, unit);
-   }
-
-   /*
-    * Everything below this marker simply passes control along to the delegate
-    */
-
-   public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
-   {
-      return delegate.awaitTermination(timeout, unit);
-   }
-
-   public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
-   {
-      return delegate.invokeAll(tasks);
-   }
-
-   public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
-         throws InterruptedException
-   {
-      return delegate.invokeAll(tasks, timeout, unit);
-   }
-
-   public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
-   {
-      return delegate.invokeAny(tasks);
-   }
-
-   public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
-         throws InterruptedException, ExecutionException, TimeoutException
-   {
-      return delegate.invokeAny(tasks, timeout, unit);
-   }
-
-   public boolean isShutdown()
-   {
-      return delegate.isShutdown();
-   }
-
-   public boolean isTerminated()
-   {
-      return delegate.isTerminated();
-   }
-
-   public void shutdown()
-   {
-      delegate.shutdown();
-   }
-
-   public List<Runnable> shutdownNow()
-   {
-      return delegate.shutdownNow();
-   }
-
-   public <T> Future<T> submit(Callable<T> task)
-   {
-      return delegate.submit(task);
-   }
-
-   public Future<?> submit(Runnable task)
-   {
-      return delegate.submit(task);
-   }
-
-   public <T> Future<T> submit(Runnable task, T result)
-   {
-      return delegate.submit(task, result);
-   }
-
-   public void execute(Runnable command)
-   {
-      delegate.execute(command);
-   }
-}

Modified: 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	2010-07-16 14:46:07 UTC (rev 4753)
+++ shrinkwrap/trunk/extension-classloader/src/main/java/org/jboss/shrinkwrap/classloader/ShrinkWrapClassLoader.java	2010-07-16 16:00:42 UTC (rev 4754)
@@ -18,26 +18,18 @@
 
 import java.io.Closeable;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
-import java.util.Set;
-import java.util.UUID;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.logging.Level;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import org.jboss.shrinkwrap.api.Archive;
-import org.jboss.shrinkwrap.api.Configuration;
-import org.jboss.shrinkwrap.spi.Configurable;
-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;
+import org.jboss.shrinkwrap.api.ArchivePath;
+import org.jboss.shrinkwrap.api.ArchivePaths;
 
 /**
  * Extension that will create a ClassLoader based on a Array of Archives
@@ -60,22 +52,7 @@
    // Instance Members -------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
-   /**
-    * All open VFS handles to close when {@link ShrinkWrapClassLoader#close()}
-    * is invoked
-    */
-   private Set<Closeable> vfsHandlesToClose = new LinkedHashSet<Closeable>();
-
-   /**
-    * {@link ExecutorService}s to shutdown when {@link ShrinkWrapClassLoader#close()}
-    * is invoked
-    */
-   private Set<ExecutorService> executorServicesToShutdown = new HashSet<ExecutorService>();
-
-   /**
-    *  
-    */
-   private ScheduledExecutorService scheduledExecutorService = null;
+   private Map<URL, InputStream> openedStreams = new HashMap<URL, InputStream>();
    
    //-------------------------------------------------------------------------------------||
    // Constructors -----------------------------------------------------------------------||
@@ -87,34 +64,8 @@
     * be searched in the order specified for classes and resources after
     * first searching in the parent class loader.
     * 
-    * @param service {@link ScheduledExecutorService} used internally to handle mounting
-    *   of the {@link Archive}s such that they may be read
     * @param archives the {@link Archive}s from which to load classes and resources
     */
-   public ShrinkWrapClassLoader(final ScheduledExecutorService service, final Archive<?>... archives)
-   {
-      super(new URL[]{});
-
-      if (service == null)
-      {
-         throw new IllegalArgumentException("ScheduledExecutorService must be specified");
-      }
-      if (archives == null)
-      {
-         throw new IllegalArgumentException("Archives must be specified");
-      }
-      scheduledExecutorService = service;
-      addArchives(archives);
-   }
-
-   /**
-    * Constructs a new ShrinkWrapClassLoader for the specified {@link Archive}s using the
-    * default delegation parent <code>ClassLoader</code>. The {@link Archive}s will
-    * be searched in the order specified for classes and resources after
-    * first searching in the parent class loader.
-    * 
-    * @param archives the {@link Archive}s from which to load classes and resources
-    */
    public ShrinkWrapClassLoader(final Archive<?>... archives)
    {
       super(new URL[]{});
@@ -155,99 +106,69 @@
 
    private void addArchive(final Archive<?> archive)
    {
-      // Grab or make a ScheduledExecutorService to back the mounting process
-      final Configuration configuration = archive.as(Configurable.class).getConfiguration();
-      final ExecutorService executorServiceFromArchiveConfig = configuration.getExecutorService();
-      ScheduledExecutorService scheduledService;
-      if (executorServiceFromArchiveConfig != null)
+      try
       {
-         if (executorServiceFromArchiveConfig instanceof ScheduledExecutorService)
+         addURL(new URL(null, "archive:" + archive.getName() + "/", new URLStreamHandler()
          {
-            scheduledService = (ScheduledExecutorService) executorServiceFromArchiveConfig;
-            if (log.isLoggable(Level.FINER))
+            @Override
+            protected URLConnection openConnection(final URL u) throws IOException
             {
-               log.log(Level.FINER, "Using " + scheduledService + " from archive configuration for mounting "
-                     + archive.toString());
+               return new URLConnection(u)
+               {
+                  @Override
+                  public void connect() throws IOException
+                  {
+                  }
+                  
+                  @Override
+                  public InputStream getInputStream() throws IOException
+                  {
+                     synchronized (openedStreams)
+                     {
+                        InputStream input = openedStreams.get(u);
+                        if(input == null)
+                        {
+                           ArchivePath path = convertToArchivePath(u);
+                           input = archive.get(path).getAsset().openStream();
+                           openedStreams.put(u, input);
+                        }
+                        return input;
+                     }
+                  }
+                  
+                  private ArchivePath convertToArchivePath(URL url)
+                  {
+                     String path = url.getPath();
+                     path = path.replace(archive.getName(), "");
+                     
+                     return ArchivePaths.create(path);
+                  }
+               };
             }
-         }
-         else
-         {
-            scheduledService = new ImmediateScheduledExecutorService(executorServiceFromArchiveConfig);
-            if (log.isLoggable(Level.FINER))
-            {
-               log.log(Level.FINER, "Wrapping " + executorServiceFromArchiveConfig + " from archive as "
-                     + scheduledService + " configuration for mounting " + archive.toString());
-            }
-         }
+         }));
       }
-      else if(this.scheduledExecutorService != null)
+      catch (Exception e) 
       {
-         scheduledService = this.scheduledExecutorService;
+         throw new RuntimeException("Could not create URL for archive: " + archive.getName(), e);
       }
-      else
-      {
-         scheduledService = Executors.newScheduledThreadPool(2);
-         this.executorServicesToShutdown.add(scheduledService);
-         if (log.isLoggable(Level.FINER))
-         {
-            log.log(Level.FINER, "Created " + scheduledService + " for mounting " + archive.toString());
-         }
-      }
-
-      try
-      {
-         final TempFileProvider tempFileProvider = TempFileProvider.create("shrinkwrap-classloader", scheduledService);
-         final TempDir tempDir = tempFileProvider.createTempDir(archive.getName());
-         final VirtualFile virtualFile = VFS.getChild(UUID.randomUUID().toString()).getChild(archive.getName());
-
-         final Closeable handle = VFS.mount(virtualFile, new ArchiveFileSystem(archive, tempDir));
-
-         // add to list of resources to cleanup during close()
-         vfsHandlesToClose.add(handle);
-         vfsHandlesToClose.add(tempDir);
-         vfsHandlesToClose.add(tempFileProvider);
-         
-
-         addURL(virtualFile.toURL());
-
-      }
-      catch (final IOException e)
-      {
-         throw new RuntimeException("Could not create ClassLoader from archive: " + archive.getName(), e);
-      }
    }
-
-   /**
-    * {@inheritDoc}
-    * @see java.io.Closeable#close()
-    */
+   
    public void close() throws IOException
    {
-      // Close all opened resources
-      synchronized (vfsHandlesToClose)
+      synchronized (openedStreams)
       {
-         for (final Closeable handle : vfsHandlesToClose)
+         for(InputStream stream : openedStreams.values())
          {
             try
             {
-               handle.close();
+               stream.close();
             }
-            catch (final IOException e)
+            catch (Exception e) 
             {
-               log.warning("Could not close VFS handle: " + e);
+               log.warning("Could not close opened inputstream: " + e);
             }
          }
-         vfsHandlesToClose.clear();
+         openedStreams.clear();
       }
-      
-      // Shutdown all created Executor Services.
-      synchronized (executorServicesToShutdown)
-      {
-         for (final ExecutorService executorService : executorServicesToShutdown)
-         {
-            executorService.shutdownNow();
-         }
-         executorServicesToShutdown.clear();
-      }
    }
-}
+}
\ No newline at end of file



More information about the jboss-svn-commits mailing list