[jboss-cvs] JBossAS SVN: r95154 - branches/vfs3-integration/server/src/main/java/org/jboss/web/deployers.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Oct 19 21:17:21 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-10-19 21:17:21 -0400 (Mon, 19 Oct 2009)
New Revision: 95154

Modified:
   branches/vfs3-integration/server/src/main/java/org/jboss/web/deployers/WARStructure.java
Log:
Port WARStructure to VFS3

Modified: branches/vfs3-integration/server/src/main/java/org/jboss/web/deployers/WARStructure.java
===================================================================
--- branches/vfs3-integration/server/src/main/java/org/jboss/web/deployers/WARStructure.java	2009-10-20 01:16:44 UTC (rev 95153)
+++ branches/vfs3-integration/server/src/main/java/org/jboss/web/deployers/WARStructure.java	2009-10-20 01:17:21 UTC (rev 95154)
@@ -22,8 +22,10 @@
 package org.jboss.web.deployers;
 
 import java.io.IOException;
+import java.io.Closeable;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.ScheduledExecutorService;
 
 import org.jboss.deployers.spi.DeploymentException;
 import org.jboss.deployers.spi.structure.ContextInfo;
@@ -32,11 +34,15 @@
 import org.jboss.vfs.VirtualFile;
 import org.jboss.vfs.VirtualFileFilter;
 import org.jboss.vfs.VisitorAttributes;
-import org.jboss.vfs.plugins.vfs.helpers.SuffixMatchFilter;
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.TempFileProvider;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.util.SuffixMatchFilter;
+import org.jboss.logging.Logger;
 
 /**
  * WARStructure.
- * 
+ *
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision$
@@ -45,7 +51,7 @@
 {
    /** The default filter which allows jars/jar directories */
    public static final VirtualFileFilter DEFAULT_WEB_INF_LIB_FILTER = new SuffixMatchFilter(".jar", VisitorAttributes.DEFAULT);
-   
+
    /** The web-inf/lib filter */
    private VirtualFileFilter webInfLibFilter = DEFAULT_WEB_INF_LIB_FILTER;
 
@@ -54,6 +60,7 @@
 
    /** Whether to include web-inf in the classpath */
    private boolean includeWebInfInClasspath;
+   private ScheduledExecutorService scheduledExecutorService;
 
    /**
     * Sets the default relative order 1000.
@@ -65,8 +72,28 @@
    }
 
    /**
+    * Get the scheduled executor service.
+    *
+    * @return the scheduled executor service
+    */
+   public ScheduledExecutorService getScheduledExecutorService()
+   {
+      return scheduledExecutorService;
+   }
+
+   /**
+    * Set the scheduled executor service.
+    *
+    * @param scheduledExecutorService the scheduled executor service
+    */
+   public void setScheduledExecutorService(final ScheduledExecutorService scheduledExecutorService)
+   {
+      this.scheduledExecutorService = scheduledExecutorService;
+   }
+
+   /**
     * Get the webInfLibFilter.
-    * 
+    *
     * @return the webInfLibFilter.
     */
    public VirtualFileFilter getWebInfLibFilter()
@@ -76,7 +103,7 @@
 
    /**
     * Set the webInfLibFilter.
-    * 
+    *
     * @param webInfLibFilter the webInfLibFilter.
     * @throws IllegalArgumentException for a null filter
     */
@@ -119,115 +146,89 @@
 
    public boolean determineStructure(StructureContext structureContext) throws DeploymentException
    {
-      ContextInfo context = null;
+      boolean ok = false;
       VirtualFile file = structureContext.getFile();
-      try
-      {
-         boolean trace = log.isTraceEnabled();
+      final Logger log = WARStructure.log;
+      boolean trace = log.isTraceEnabled();
 
-         // the WEB-INF
-         VirtualFile webinf = null;
-
-         if (isLeaf(file) == false)
+      // the WEB-INF
+      final List<Closeable> handles = new ArrayList<Closeable>();
+      if (! file.isDirectory()) try {
+         handles.add(VFS.mountZipExpanded(file, file, TempFileProvider.create(file.getName(), scheduledExecutorService)));
+      } catch (IOException e) {
+         DeploymentException.rethrowAsDeploymentException("Failed to mount WAR archive", e);
+      }
+      try {
+         final VirtualFile webinf = file.getChild("WEB-INF");
+         if (file.getName().endsWith(".war") == false)
          {
-            // We require either a WEB-INF or the name ends in .war
-            if (file.getName().endsWith(".war") == false)
+            if (webinf.exists())
             {
-               try
-               {
-                  webinf = file.getChild("WEB-INF");
-                  if (webinf != null)
-                  {
-                     if (trace)
-                        log.trace("... ok - directory has a WEB-INF subdirectory");
-                  }
-                  else
-                  {
-                     if (trace)
-                        log.trace("... no - doesn't look like a war and no WEB-INF subdirectory.");
-                     return false;
-                  }
-               }
-               catch (IOException e)
-               {
-                  log.warn("Exception while checking if file is a war: " + e);
-                  return false;
-               }
+               if (trace)
+                  log.trace("... ok - directory has a WEB-INF subdirectory");
             }
-            else if (trace)
+            else
             {
-               log.trace("... ok - name ends in .war.");
+               if (trace)
+                  log.trace("... no - doesn't look like a war and no WEB-INF subdirectory.");
+               return false;
             }
-
-            List<String> metaDataLocations = new ArrayList<String>();
-            metaDataLocations.add("WEB-INF");
-            
-            // Add all children as locations for TLDs (except classes and lib), recursively
-            webinf = file.getChild("WEB-INF");
-            if (webinf != null)
-            {
-               List<VirtualFile> children = webinf.getChildren();
-               for (VirtualFile child : children)
-               {
-                  if (!isLeaf(child) && (!"lib".equals(child.getName())) && (!"classes".equals(child.getName())))
-                  {
-                     metaDataLocations.add("WEB-INF/" + child.getName());
-                     addPathsRecursively(metaDataLocations, child, "WEB-INF/" + child.getName());
-                  }
-               }
-            }
-
-            // Check for WEB-INF/classes
-            VirtualFile classes = null;
+         }
+         else if (trace)
+         {
+            log.trace("... ok - name ends in .war.");
+         }
+         List<String> metaDataLocations = new ArrayList<String>();
+         metaDataLocations.add("WEB-INF");
+         // Check for WEB-INF/classes
+         final VirtualFile classes = file.getChild("WEB-INF/classes");
+         // Check for a META-INF for metadata
+         if (classes.isDirectory())
+         {
+            metaDataLocations.add("WEB-INF/classes/META-INF");
+         }
+         // Check for jars in WEB-INF/lib
+         List<VirtualFile> archives = null;
+         final VirtualFile webinfLib = file.getChild("WEB-INF/lib");
+         if (webinfLib.isDirectory())
+         {
             try
             {
-               // The classpath contains WEB-INF/classes
-               classes = file.getChild("WEB-INF/classes");
-               
-               // Check for a META-INF for metadata
-               // FIXME: This is not spec legal, descriptors could be loaded from this location
-               if (classes != null)
-                  metaDataLocations.add("WEB-INF/classes/META-INF");
-            }
-            catch(IOException e)
-            {
-               log.warn("Exception while looking for classes, " + file.getPathName() + ", " + e);
-            }
-
-            // Check for jars in WEB-INF/lib
-            List<VirtualFile> archives = null;
-            try
-            {
-               VirtualFile webinfLib = file.getChild("WEB-INF/lib");
-               if (webinfLib != null)
+               archives = webinfLib.getChildren(webInfLibFilter);
+               // Add the jars' META-INF for metadata
+               for (VirtualFile jar : archives)
                {
-                  archives = webinfLib.getChildren(webInfLibFilter);
-                  // Add the jars' META-INF for metadata
-                  for (VirtualFile jar : archives)
-                  {
-                     // either same as plain lib filter, null or accepts the jar
-                     if (webInfLibMetaDataFilter == null || webInfLibMetaDataFilter == webInfLibFilter || webInfLibMetaDataFilter.accepts(jar))
-                        metaDataLocations.add("WEB-INF/lib/" + jar.getName() + "/META-INF");
+                  // mount up the JAR if it is an archive
+                  // todo - is it a fatal error if the mount fails, or should we just skip the entry?
+                  if (! jar.isDirectory()) {
+                     try {
+                        handles.add(VFS.mountZip(jar, jar, TempFileProvider.create(jar.getName(), scheduledExecutorService)));
+                     } catch (IOException e) {
+                        log.warn("Failed to mount file " + jar.getName() + ": " + e);
+                        // try the next one
+                        continue;
+                     }
                   }
+                  // either same as plain lib filter, null or accepts the jar
+                  if (webInfLibMetaDataFilter == null || webInfLibMetaDataFilter == webInfLibFilter || webInfLibMetaDataFilter.accepts(jar))
+                     metaDataLocations.add("WEB-INF/lib/" + jar.getName() + "/META-INF");
                }
             }
             catch (IOException e)
             {
                log.warn("Exception looking for WEB-INF/lib, " + file.getPathName() + ", " + e);
             }
-            
-            // Create a context for this war file and all its metadata locations
-            context = createContext(structureContext, metaDataLocations.toArray(new String[metaDataLocations.size()]));
-
+         }
+         // Create a context for this war file and all its metadata locations
+         final ContextInfo context = createContext(structureContext, metaDataLocations.toArray(new String[metaDataLocations.size()]));
+         try {
             // Add the war manifest classpath entries
             addClassPath(structureContext, file, false, true, context);
-
             // Add WEB-INF/classes if present
             if (classes != null)
                addClassPath(structureContext, classes, true, false, context);
             else if (trace)
                log.trace("No WEB-INF/classes for: " + file.getPathName());
-
             // and the top level jars in WEB-INF/lib
             if (archives != null)
             {
@@ -238,45 +239,24 @@
             {
                log.trace("No WEB-INF/lib for: " + file.getPathName());
             }
-
             // do we include WEB-INF in classpath
             if (includeWebInfInClasspath && webinf != null)
             {
                addClassPath(structureContext, webinf, true, false, context);
             }
-
             // There are no subdeployments for wars
-            return true;
+            return ok = true;
+         } finally {
+            if (! ok) {
+               // Remove the invalid context
+               structureContext.removeChild(context);
+            }
          }
-         else
-         {
-            if (trace)
-               log.trace("... no - not a directory or an archive.");
-            return false;
+      } finally {
+         if (! ok) {
+            // Undo all mounts
+            VFSUtils.safeClose(handles);
          }
       }
-      catch (Exception e)
-      {
-         // Remove the invalid context
-         if (context != null)
-            structureContext.removeChild(context);
-
-         throw DeploymentException.rethrowAsDeploymentException("Error determining structure: " + file.getName(), e);
-      }
    }
-   
-   protected void addPathsRecursively(List<String> metaDataLocations, VirtualFile parent, String path)
-   throws IOException
-   {
-      List<VirtualFile> children = parent.getChildren();
-      for (VirtualFile child : children)
-      {
-         if (!isLeaf(child))
-         {
-            metaDataLocations.add(path + "/" + child.getName());
-            addPathsRecursively(metaDataLocations, child, path + "/" + child.getName());
-         }
-      }
-   }
-   
 }




More information about the jboss-cvs-commits mailing list