[jboss-cvs] JBossAS SVN: r58704 - trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/deployers

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Nov 28 11:10:44 EST 2006


Author: remy.maucherat at jboss.com
Date: 2006-11-28 11:10:42 -0500 (Tue, 28 Nov 2006)
New Revision: 58704

Modified:
   trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/deployers/VFSDirContext.java
Log:
- Add the dir context wrapper for VirtualFile.

Modified: trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/deployers/VFSDirContext.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/deployers/VFSDirContext.java	2006-11-28 14:25:59 UTC (rev 58703)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/tc6/deployers/VFSDirContext.java	2006-11-28 16:10:42 UTC (rev 58704)
@@ -18,25 +18,27 @@
 
 package org.jboss.web.tomcat.tc6.deployers;
 
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Date;
-import java.util.Enumeration;
 import java.util.Hashtable;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipException;
-import java.util.zip.ZipFile;
+import java.util.Iterator;
 
 import javax.naming.CompositeName;
 import javax.naming.Name;
+import javax.naming.NameAlreadyBoundException;
+import javax.naming.NameNotFoundException;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.NotContextException;
 import javax.naming.OperationNotSupportedException;
+import javax.naming.directory.AttributeModificationException;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.DirContext;
+import javax.naming.directory.InvalidAttributesException;
+import javax.naming.directory.InvalidSearchControlsException;
+import javax.naming.directory.InvalidSearchFilterException;
 import javax.naming.directory.ModificationItem;
 import javax.naming.directory.SearchControls;
 
@@ -47,6 +49,7 @@
 import org.apache.naming.resources.Resource;
 import org.apache.naming.resources.ResourceAttributes;
 import org.jboss.logging.Logger;
+import org.jboss.virtual.VirtualFile;
 
 /**
  * VFS Directory Context implementation.
@@ -62,89 +65,50 @@
     // ----------------------------------------------------------- Constructors
 
 
-    /**
-     * Builds a WAR directory context using the given environment.
-     */
-    public VFSDirContext() {
-        super();
-    }
+	   /**
+	    * Builds a WAR directory context using the given environment.
+	    */
+	   public VFSDirContext() {
+		   super();
+	   }
 
 
-    /**
-     * Builds a WAR directory context using the given environment.
-     */
-    public VFSDirContext(Hashtable env) {
-        super(env);
-    }
+	   /**
+	    * Builds a WAR directory context using the given environment.
+	    */
+	   public VFSDirContext(Hashtable env) {
+		   super(env);
+	   }
 
 
-    /**
-     * Constructor used for returning fake subcontexts.
-     */
-    protected VFSDirContext(ZipFile base, Entry entries) {
-        this.base = base;
-        this.entries = entries;
-    }
+	   /**
+	    * Constructor used for returning fake subcontexts.
+	    */
+	   protected VFSDirContext(VirtualFile base) {
+		   this.base = base;
+	   }
 
 
     // ----------------------------------------------------- Instance Variables
 
 
     /**
-     * The WAR file.
+     * The virtual file.
      */
-    protected ZipFile base = null;
+    protected VirtualFile base = null;
 
 
-    /**
-     * WAR entries.
-     */
-    protected Entry entries = null;
-
-
     // ------------------------------------------------------------- Properties
-
-
+    
+    
     /**
-     * Set the document root.
-     * 
-     * @param docBase The new document root
-     * 
-     * @exception IllegalArgumentException if the specified value is not
-     *  supported by this implementation
-     * @exception IllegalArgumentException if this would create a
-     *  malformed URL
+     * Set the virtual file root.
      */
-    public void setDocBase(String docBase) {
-
-	// Validate the format of the proposed document root
-	if (docBase == null)
-	    throw new IllegalArgumentException
-		(sm.getString("resources.null"));
-	if (!(docBase.endsWith(".war")))
-	    throw new IllegalArgumentException
-		(sm.getString("warResources.notWar"));
-
-	// Calculate a File object referencing this document base directory
-	File base = new File(docBase);
-
-	// Validate that the document base is an existing directory
-	if (!base.exists() || !base.canRead() || base.isDirectory())
-	    throw new IllegalArgumentException
-		(sm.getString("warResources.invalidWar", docBase));
-        try {
-            this.base = new ZipFile(base);
-        } catch (Exception e) {
-	    throw new IllegalArgumentException
-		(sm.getString("warResources.invalidWar", e.getMessage()));
-        }
-        super.setDocBase(docBase);
-
-        loadEntries();
-
+    public void setVirtualFile(VirtualFile base) {
+        this.base = base;
     }
-
-
+    
+    
     // --------------------------------------------------------- Public Methods
 
 
@@ -153,14 +117,8 @@
      */
     public void release() {
 
-        entries = null;
         if (base != null) {
-            try {
-                base.close();
-            } catch (IOException e) {
-                log.warn
-                    ("Exception closing WAR File " + base.getName(), e);
-            }
+            base.close();
         }
         base = null;
         super.release();
@@ -198,15 +156,30 @@
         throws NamingException {
         if (name.isEmpty())
             return this;
-        Entry entry = treeLookup(name);
+        VirtualFile entry = null;
+        try {
+            entry = treeLookup(name);
+        } catch (IOException e) {
+            NamingException ex = new NamingException
+                (sm.getString("resources.notFound", name));
+            ex.initCause(e);
+            throw ex;
+        }
         if (entry == null)
             throw new NamingException
                 (sm.getString("resources.notFound", name));
-        ZipEntry zipEntry = entry.getEntry();
-        if (zipEntry.isDirectory())
-            return new VFSDirContext(base, entry);
-        else
-            return new WARResource(entry.getEntry());
+        
+        try {
+            if (!entry.isLeaf())
+                return new VFSDirContext(entry);
+            else
+                return new VFSResource(entry);
+        } catch (IOException e) {
+            NamingException ex = new NamingException
+                (sm.getString("resources.notFound", name));
+            ex.initCause(e);
+            throw ex;
+        }
     }
 
 
@@ -281,13 +254,36 @@
      */
     public NamingEnumeration list(Name name)
         throws NamingException {
-        if (name.isEmpty())
-            return new NamingContextEnumeration(list(entries).iterator());
-        Entry entry = treeLookup(name);
+        if (name.isEmpty()) {
+            try {
+                return new NamingContextEnumeration(list(base).iterator());
+            } catch (IOException e) {
+                NamingException ex = new NamingException
+                    (sm.getString("resources.notFound", name));
+                ex.initCause(e);
+                throw ex;
+            }
+        }
+        VirtualFile entry = null;
+        try {
+            entry = treeLookup(name);
+        } catch (IOException e) {
+            NamingException ex = new NamingException
+                (sm.getString("resources.notFound", name));
+            ex.initCause(e);
+            throw ex;
+        }
         if (entry == null)
             throw new NamingException
                 (sm.getString("resources.notFound", name));
-        return new NamingContextEnumeration(list(entry).iterator());
+        try {
+            return new NamingContextEnumeration(list(entry).iterator());
+        } catch (IOException e) {
+            NamingException ex = new NamingException
+                (sm.getString("resources.notFound", name));
+            ex.initCause(e);
+            throw ex;
+        }
     }
 
 
@@ -325,15 +321,38 @@
      */
     public NamingEnumeration listBindings(Name name)
         throws NamingException {
-        if (name.isEmpty())
-            return new NamingContextBindingsEnumeration(list(entries).iterator(),
-                    this);
-        Entry entry = treeLookup(name);
+        if (name.isEmpty()) {
+            try {
+                return new NamingContextBindingsEnumeration(list(base).iterator(),
+                        this);
+            } catch (IOException e) {
+                NamingException ex = new NamingException
+                    (sm.getString("resources.notFound", name));
+                ex.initCause(e);
+                throw ex;
+            }
+        }
+        VirtualFile entry = null;
+        try {
+            entry = treeLookup(name);
+        } catch (IOException e) {
+            NamingException ex = new NamingException
+                (sm.getString("resources.notFound", name));
+            ex.initCause(e);
+            throw ex;
+        }
         if (entry == null)
             throw new NamingException
                 (sm.getString("resources.notFound", name));
-        return new NamingContextBindingsEnumeration(list(entry).iterator(),
-                this);
+        try {
+            return new NamingContextBindingsEnumeration(list(entry).iterator(),
+                    this);
+        } catch (IOException e) {
+            NamingException ex = new NamingException
+                (sm.getString("resources.notFound", name));
+            ex.initCause(e);
+            throw ex;
+        }
     }
 
 
@@ -440,25 +459,38 @@
     public Attributes getAttributes(Name name, String[] attrIds)
         throws NamingException {
         
-        Entry entry = null;
-        if (name.isEmpty())
-            entry = entries;
-        else
-            entry = treeLookup(name);
+        VirtualFile entry = null;
+        if (name.isEmpty()) {
+            entry = base;
+        } else {
+            try {
+                entry = treeLookup(name);
+            } catch (IOException e) {
+                NamingException ex = new NamingException
+                    (sm.getString("resources.notFound", name));
+                ex.initCause(e);
+                throw ex;
+            }
+        }
         if (entry == null)
             throw new NamingException
                 (sm.getString("resources.notFound", name));
         
-        ZipEntry zipEntry = entry.getEntry();
-
         ResourceAttributes attrs = new ResourceAttributes();
-        attrs.setCreationDate(new Date(zipEntry.getTime()));
-        attrs.setName(entry.getName());
-        if (!zipEntry.isDirectory())
-            attrs.setResourceType("");
-        attrs.setContentLength(zipEntry.getSize());
-        attrs.setLastModified(zipEntry.getTime());
-        
+        try {
+            attrs.setCreationDate(new Date(entry.getLastModified()));
+            attrs.setName(entry.getName());
+            if (entry.isLeaf())
+                attrs.setResourceType("");
+            attrs.setContentLength(entry.getSize());
+            attrs.setLastModified(entry.getLastModified());
+        } catch (IOException e) {
+            NamingException ex = new NamingException
+            (sm.getString("resources.notFound", name));
+            ex.initCause(e);
+            throw ex;
+        }
+
         return attrs;
         
     }
@@ -714,113 +746,44 @@
 
 
     /**
-     * Normalize the name of an entry read from the Zip.
-     */
-    protected String normalize(ZipEntry entry) {
-
-        String result = "/" + entry.getName();
-        if (entry.isDirectory()) {
-            result = result.substring(0, result.length() - 1);
-        }
-        return result;
-
-    }
-
-
-    /**
-     * Constructs a tree of the entries contained in a WAR file.
-     */
-    protected void loadEntries() {
-
-        try {
-
-            Enumeration entryList = base.entries();
-            entries = new Entry("/", new ZipEntry("/"));
-            
-            while (entryList.hasMoreElements()) {
-                
-                ZipEntry entry = (ZipEntry) entryList.nextElement();
-                String name = normalize(entry);
-                int pos = name.lastIndexOf('/');
-                // Check that parent entries exist and, if not, create them.
-                // This fixes a bug for war files that don't record separate
-                // zip entries for the directories.
-                int currentPos = -1;
-                int lastPos = 0;
-                while ((currentPos = name.indexOf('/', lastPos)) != -1) {
-                    Name parentName = new CompositeName(name.substring(0, lastPos));
-                    Name childName = new CompositeName(name.substring(0, currentPos));
-                    String entryName = name.substring(lastPos, currentPos);
-                    // Parent should have been created in last cycle through
-                    // this loop
-                    Entry parent = treeLookup(parentName);
-                    Entry child = treeLookup(childName);
-                    if (child == null) {
-                        // Create a new entry for missing entry and strip off
-                        // the leading '/' character and appended on by the
-                        // normalize method and add '/' character to end to
-                        // signify that it is a directory entry
-                        String zipName = name.substring(1, currentPos) + "/";
-                        child = new Entry(entryName, new ZipEntry(zipName));
-                        if (parent != null)
-                            parent.addChild(child);
-                    }
-                    // Increment lastPos
-                    lastPos = currentPos + 1;
-                }
-                String entryName = name.substring(pos + 1, name.length());
-                Name compositeName = new CompositeName(name.substring(0, pos));
-                Entry parent = treeLookup(compositeName);
-                Entry child = new Entry(entryName, entry);
-                if (parent != null)
-                    parent.addChild(child);
-                
-            }
-
-        } catch (Exception e) {
-        }
-
-    }
-
-
-    /**
      * Entry tree lookup.
      */
-    protected Entry treeLookup(Name name) {
+    protected VirtualFile treeLookup(Name name)
+        throws IOException {
         if (name.isEmpty())
-            return entries;
-        Entry currentEntry = entries;
+            return base;
+        VirtualFile currentFile = base;
         for (int i = 0; i < name.size(); i++) {
             if (name.get(i).length() == 0)
                 continue;
-            currentEntry = currentEntry.getChild(name.get(i));
-            if (currentEntry == null)
+            currentFile = currentFile.findChild(name.get(i));
+            if (currentFile == null)
                 return null;
         }
-        return currentEntry;
+        return currentFile;
     }
 
 
     /**
      * List children as objects.
      */
-    protected ArrayList list(Entry entry) {
+    protected ArrayList<NamingEntry> list(VirtualFile entry)
+        throws IOException {
         
-        ArrayList entries = new ArrayList();
-        Entry[] children = entry.getChildren();
-        Arrays.sort(children);
+        ArrayList<NamingEntry> entries = new ArrayList<NamingEntry>();
+        Iterator<VirtualFile> children = entry.getChildren().iterator();
         NamingEntry namingEntry = null;
-        
-        for (int i = 0; i < children.length; i++) {
-            ZipEntry current = children[i].getEntry();
+
+        while (children.hasNext()) {
+            VirtualFile current = children.next();
             Object object = null;
-            if (current.isDirectory()) {
-                object = new VFSDirContext(base, children[i]);
+            if (!current.isLeaf()) {
+                object = new VFSDirContext(current);
             } else {
-                object = new WARResource(current);
+                object = new VFSResource(current);
             }
             namingEntry = new NamingEntry
-                (children[i].getName(), object, NamingEntry.ENTRY);
+                (current.getName(), object, NamingEntry.ENTRY);
             entries.add(namingEntry);
         }
         
@@ -829,82 +792,6 @@
     }
 
 
-    // ---------------------------------------------------- Entries Inner Class
-
-
-    /**
-     * Entries structure.
-     */
-    protected class Entry implements Comparable {
-
-
-        // -------------------------------------------------------- Constructor
-        
-        
-        public Entry(String name, ZipEntry entry) {
-            this.name = name;
-            this.entry = entry;
-        }
-        
-        
-        // --------------------------------------------------- Member Variables
-        
-        
-        protected String name = null;
-        
-        
-        protected ZipEntry entry = null;
-        
-        
-        protected Entry children[] = new Entry[0];
-        
-        
-        // ----------------------------------------------------- Public Methods
-        
-        
-        public int compareTo(Object o) {
-            if (!(o instanceof Entry))
-                return (+1);
-            return (name.compareTo(((Entry) o).getName()));
-        }
-
-        public ZipEntry getEntry() {
-            return entry;
-        }
-        
-        
-        public String getName() {
-            return name;
-        }
-        
-        
-        public void addChild(Entry entry) {
-            Entry[] newChildren = new Entry[children.length + 1];
-            for (int i = 0; i < children.length; i++)
-                newChildren[i] = children[i];
-            newChildren[children.length] = entry;
-            children = newChildren;
-        }
-
-
-        public Entry[] getChildren() {
-            return children;
-        }
-
-
-        public Entry getChild(String name) {
-            for (int i = 0; i < children.length; i++) {
-                if (children[i].name.equals(name)) {
-                    return children[i];
-                }
-            }
-            return null;
-        }
-
-
-    }
-
-
     // ------------------------------------------------ WARResource Inner Class
 
 
@@ -912,13 +799,13 @@
      * This specialized resource implementation avoids opening the IputStream
      * to the WAR right away.
      */
-    protected class WARResource extends Resource {
+    protected class VFSResource extends Resource {
         
         
         // -------------------------------------------------------- Constructor
         
         
-        public WARResource(ZipEntry entry) {
+        public VFSResource(VirtualFile entry) {
             this.entry = entry;
         }
         
@@ -926,7 +813,7 @@
         // --------------------------------------------------- Member Variables
         
         
-        protected ZipEntry entry;
+        protected VirtualFile entry;
         
         
         // ----------------------------------------------------- Public Methods
@@ -939,12 +826,8 @@
          */
         public InputStream streamContent()
             throws IOException {
-            try {
-                if (binaryContent == null) {
-                    inputStream = base.getInputStream(entry);
-                }
-            } catch (ZipException e) {
-                throw new IOException(e.getMessage());
+            if (binaryContent == null) {
+                inputStream = entry.openStream();
             }
             return super.streamContent();
         }




More information about the jboss-cvs-commits mailing list