[jboss-cvs] JBossAS SVN: r80924 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/plugins/cache and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Nov 13 06:24:28 EST 2008


Author: alesj
Date: 2008-11-13 06:24:27 -0500 (Thu, 13 Nov 2008)
New Revision: 80924

Added:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractExceptionHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/ExceptionHandler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/NameExceptionHandler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ExceptionHandlerTestCase.java
   projects/vfs/trunk/src/test/resources/vfs/test/zipeinit.jar
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/PreInitializeVFSContexts.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVFSContext.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileSystemContext.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContext.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java
Log:
[JBVFS-80]; add exception handler.

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java	2008-11-13 10:46:58 UTC (rev 80923)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -32,6 +32,7 @@
 import org.jboss.virtual.spi.VFSContextFactory;
 import org.jboss.virtual.spi.VFSContextFactoryLocator;
 import org.jboss.virtual.spi.VirtualFileHandler;
+import org.jboss.virtual.spi.ExceptionHandler;
 import org.jboss.virtual.spi.cache.VFSCacheFactory;
 import org.jboss.virtual.spi.cache.VFSCache;
 import org.jboss.util.file.ArchiveBrowser;
@@ -55,6 +56,19 @@
    }
 
    /**
+    * Create a new VFS.
+    *
+    * @param context the context
+    * @throws IllegalArgumentException for a null context
+    */
+   public VFS(VFSContext context)
+   {
+      if (context == null)
+         throw new IllegalArgumentException("Null name");
+      this.context = context;
+   }
+
+   /**
     * Initialize VFS protocol handlers package property. 
     */
    public static void init()
@@ -90,6 +104,16 @@
    }
 
    /**
+    * Set exception handler.
+    *
+    * @param exceptionHandler the exception handler.
+    */
+   public void setExceptionHandler(ExceptionHandler exceptionHandler)
+   {
+      context.setExceptionHandler(exceptionHandler);
+   }
+
+   /**
     * Get the virtual file system for a root uri
     * 
     * @param rootURI the root URI
@@ -222,19 +246,6 @@
    }
 
    /**
-    * Create a new VFS.
-    * 
-    * @param context the context
-    * @throws IllegalArgumentException for a null context
-    */
-   public VFS(VFSContext context)
-   {
-      if (context == null)
-         throw new IllegalArgumentException("Null name");
-      this.context = context;
-   }
-   
-   /**
     * Get the root file of this VFS
     * 
     * @return the root

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/PreInitializeVFSContexts.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/PreInitializeVFSContexts.java	2008-11-13 10:46:58 UTC (rev 80923)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/PreInitializeVFSContexts.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -24,9 +24,12 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 import org.jboss.logging.Logger;
 import org.jboss.virtual.VFS;
+import org.jboss.virtual.spi.ExceptionHandler;
 
 /**
  * Initialize vfs contexts - performance improvements.
@@ -36,7 +39,7 @@
 public class PreInitializeVFSContexts
 {
    private Logger log = Logger.getLogger(PreInitializeVFSContexts.class);
-   private List<URL> initializedVFSContexts;
+   private Map<URL, ExceptionHandler> initializedVFSContexts;
    private boolean holdReference;
    private List<VFS> references;
 
@@ -52,9 +55,14 @@
          if (holdReference)
             references = new ArrayList<VFS>();
 
-         for (URL url : initializedVFSContexts)
+         for (Map.Entry<URL, ExceptionHandler> entry : initializedVFSContexts.entrySet())
          {
-            VFS vfs = VFS.getVFS(url);
+            VFS vfs = VFS.getVFS(entry.getKey());
+
+            ExceptionHandler eh = entry.getValue();
+            if (eh != null)
+               vfs.setExceptionHandler(eh);
+
             log.debug("Initialized Virtual File: " + vfs.getRoot());
             if (holdReference)
             {
@@ -90,6 +98,20 @@
     */
    public void setInitializedVFSContexts(List<URL> initializedVFSContexts)
    {
+      this.initializedVFSContexts = new HashMap<URL, ExceptionHandler>();
+      for (URL url : initializedVFSContexts)
+      {
+         this.initializedVFSContexts.put(url, null);
+      }
+   }
+
+   /**
+    * Set URLs that need to be initialized before anything else.
+    *
+    * @param initializedVFSContexts the URLs to be initialized
+    */
+   public void setInitializedVFSContexts(Map<URL, ExceptionHandler> initializedVFSContexts)
+   {
       this.initializedVFSContexts = initializedVFSContexts;
    }
 

Copied: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractExceptionHandler.java (from rev 80791, projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVFSContext.java)
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractExceptionHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractExceptionHandler.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -0,0 +1,41 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.virtual.plugins.context;
+
+import org.jboss.logging.Logger;
+import org.jboss.virtual.spi.ExceptionHandler;
+
+/**
+ * AbstractExceptionHandler
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public abstract class AbstractExceptionHandler implements ExceptionHandler
+{
+   /** The log */
+   protected final Logger log = Logger.getLogger(getClass());
+
+   public void handleZipEntriesInitException(Exception e, String name)
+   {
+      throw new RuntimeException(e);
+   }
+}
\ No newline at end of file


Property changes on: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractExceptionHandler.java
___________________________________________________________________
Name: svn:mergeinfo
   + 

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVFSContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVFSContext.java	2008-11-13 10:46:58 UTC (rev 80923)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVFSContext.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -22,10 +22,10 @@
 package org.jboss.virtual.plugins.context;
 
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.MalformedURLException;
 import java.util.List;
 import java.util.Map;
 
@@ -35,6 +35,7 @@
 import org.jboss.virtual.VirtualFile;
 import org.jboss.virtual.VirtualFileFilter;
 import org.jboss.virtual.VisitorAttributes;
+import org.jboss.virtual.spi.ExceptionHandler;
 import org.jboss.virtual.spi.VFSContext;
 import org.jboss.virtual.spi.VirtualFileHandler;
 import org.jboss.virtual.spi.VirtualFileHandlerVisitor;
@@ -44,6 +45,7 @@
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @author Scott.Stark at jboss.org
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public abstract class AbstractVFSContext implements VFSContext
@@ -63,6 +65,9 @@
    /** Root's peer within another context */
    private VirtualFileHandler rootPeer;
 
+   /** The exception handler */
+   private ExceptionHandler exceptionHandler;
+
    /**
     * Create a new AbstractVFSContext.
     * 
@@ -148,6 +153,33 @@
       return new URL(sb.toString());
    }
 
+   /**
+    * Merge source context with this.
+    *
+    * @param target the vfs context source
+    */
+   protected void mergeContexts(VFSContext target)
+   {
+      if (target.getExceptionHandler() == null)
+         target.setExceptionHandler(getExceptionHandler());
+
+      mergeOptions(target);
+   }
+
+   /**
+    * Merge options.
+    *
+    * @param target the vfs context target
+    */
+   private void mergeOptions(VFSContext target)
+   {
+      Map<String, String> sourceOptions = getOptions();
+      if (sourceOptions != null && sourceOptions.isEmpty() == false)
+      {
+         target.getOptions().putAll(sourceOptions);
+      }
+   }
+
    public List<VirtualFileHandler> getChildren(VirtualFileHandler parent, boolean ignoreErrors) throws IOException
    {
       if (parent == null)
@@ -319,7 +351,17 @@
          }
       }
    }
-   
+
+   public ExceptionHandler getExceptionHandler()
+   {
+      return exceptionHandler;
+   }
+
+   public void setExceptionHandler(ExceptionHandler exceptionHandler)
+   {
+      this.exceptionHandler = exceptionHandler;
+   }
+
    @Override
    public String toString()
    {

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileSystemContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileSystemContext.java	2008-11-13 10:46:58 UTC (rev 80923)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileSystemContext.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -278,6 +278,7 @@
 
       delegatorUrl = setOptionsToURL(delegatorUrl);
       ZipEntryContext ctx = new ZipEntryContext(delegatorUrl, delegator, fileUrl);
+      mergeContexts(ctx);
 
       VirtualFileHandler handler = ctx.getRoot();
       delegator.setDelegate(handler);
@@ -304,7 +305,7 @@
          throw new IllegalArgumentException("Null uri");
 
       VirtualFileHandler handler = null;
-      if( VFSUtils.isLink(file.getName()) )
+      if(VFSUtils.isLink(file.getName()))
       {
          handler = createLinkHandler(parent, file, null);
       }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-11-13 10:46:58 UTC (rev 80923)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -56,6 +56,7 @@
 import org.jboss.virtual.plugins.context.DelegatingHandler;
 import org.jboss.virtual.plugins.context.jar.JarUtils;
 import org.jboss.virtual.plugins.copy.AbstractCopyMechanism;
+import org.jboss.virtual.spi.ExceptionHandler;
 import org.jboss.virtual.spi.VirtualFileHandler;
 
 /**
@@ -488,7 +489,11 @@
       }
       catch (Exception ex)
       {
-         throw new RuntimeException("Failed to read zip file: " + zipSource, ex);
+         ExceptionHandler eh = getExceptionHandler();
+         if (eh != null)
+            eh.handleZipEntriesInitException(ex, zipSource.getName());
+         else
+            throw new RuntimeException("Failed to read zip file: " + zipSource, ex);
       }
       finally
       {
@@ -518,6 +523,8 @@
 
       delegatorUrl = setOptionsToURL(delegatorUrl);
       ZipEntryContext ctx = new ZipEntryContext(delegatorUrl, delegator, fileUrl, true);
+      mergeContexts(ctx);
+
       VirtualFileHandler handler = ctx.getRoot();
       delegator.setDelegate(handler);
 
@@ -546,6 +553,8 @@
 
       delegatorUrl = setOptionsToURL(delegatorUrl);
       ZipEntryContext ctx = new ZipEntryContext(delegatorUrl, delegator, wrapper, false);
+      mergeContexts(ctx);
+
       VirtualFileHandler handler = ctx.getRoot();
       delegator.setDelegate(handler);
 

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/ExceptionHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/ExceptionHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/ExceptionHandler.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -0,0 +1,48 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.virtual.spi;
+
+/**
+ * VFS exception handler.
+ *
+ * Custom exception handling methods shoud be added here
+ * and used at the pointcut where exception is thrown.
+ *
+ * Its abstract super class should implement them all
+ * hence back compatibility is assured by noop.
+ *
+ * Real handlers should just override some methods that
+ * they know how to handle. 
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface ExceptionHandler
+{
+   /**
+    * Handle zip entries initializaion exception.
+    *
+    * @param e the cause exception
+    * @param name the file name
+    * @throws RuntimeException if not handled
+    */
+   void handleZipEntriesInitException(Exception e, String name);
+}

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContext.java	2008-11-13 10:46:58 UTC (rev 80923)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContext.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -113,4 +113,18 @@
     * @throws IllegalArgumentException if the handler or visitor is null
     */
    void visit(VirtualFileHandler handler, VirtualFileHandlerVisitor visitor) throws IOException;
+
+   /**
+    * Get the exception handler.
+    *
+    * @return the exception handler
+    */
+   ExceptionHandler getExceptionHandler();
+
+   /**
+    * Set exception handler.
+    *
+    * @param exceptionHandler the exception handler.
+    */
+   void setExceptionHandler(ExceptionHandler exceptionHandler);
 }

Added: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/NameExceptionHandler.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/NameExceptionHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/NameExceptionHandler.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -0,0 +1,46 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.virtual.support;
+
+import org.jboss.virtual.plugins.context.AbstractExceptionHandler;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class NameExceptionHandler extends AbstractExceptionHandler
+{
+   private String name;
+
+   public NameExceptionHandler(String name)
+   {
+      this.name = name;
+   }
+
+   @Override
+   public void handleZipEntriesInitException(Exception e, String name)
+   {
+      if (name.contains(this.name) == false)
+         super.handleZipEntriesInitException(e, name);
+      else
+         log.warn("Exception while initializing zip: " + name, e);
+   }
+}

Added: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ExceptionHandlerTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ExceptionHandlerTestCase.java	                        (rev 0)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ExceptionHandlerTestCase.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -0,0 +1,60 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.virtual.test;
+
+import java.net.URL;
+import java.util.List;
+
+import junit.framework.Test;
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.test.virtual.support.NameExceptionHandler;
+
+/**
+ * ExceptionHandlerTestCase.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ExceptionHandlerTestCase extends AbstractVFSTest
+{
+   public static Test suite()
+   {
+      return suite(ExceptionHandlerTestCase.class);
+   }
+
+   public ExceptionHandlerTestCase(String name)
+   {
+      super(name, true);
+   }
+
+   public void testZipEntriesInit() throws Exception
+   {
+      URL url = getResource("/vfs/test");
+      VFS vfs = VFS.getVFS(url);
+      vfs.setExceptionHandler(new NameExceptionHandler("_sqljdbc.jar"));
+      VirtualFile root = vfs.getRoot();
+      VirtualFile zipeinit = root.findChild("zipeinit.jar");
+      VirtualFile child = zipeinit.findChild("sqljdbc.jar");
+      List<VirtualFile> children = child.getChildren();
+      assertTrue(children.isEmpty());
+   }
+}

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java	2008-11-13 10:46:58 UTC (rev 80923)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java	2008-11-13 11:24:27 UTC (rev 80924)
@@ -91,6 +91,8 @@
       suite.addTest(TimedCacheTestCase.suite());
       suite.addTest(SoftRefCacheTestCase.suite());
       suite.addTest(WeakRefCacheTestCase.suite());
+      // exception handler
+      suite.addTest(ExceptionHandlerTestCase.suite());
 
       return suite;
    }

Added: projects/vfs/trunk/src/test/resources/vfs/test/zipeinit.jar
===================================================================
(Binary files differ)


Property changes on: projects/vfs/trunk/src/test/resources/vfs/test/zipeinit.jar
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream




More information about the jboss-cvs-commits mailing list