[jboss-cvs] JBossAS SVN: r90311 - projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/zip.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 17 06:08:31 EDT 2009


Author: alesj
Date: 2009-06-17 06:08:31 -0400 (Wed, 17 Jun 2009)
New Revision: 90311

Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/zip/ZipUtils.java
Log:
Impl proper ZipUtils.

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/zip/ZipUtils.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/zip/ZipUtils.java	2009-06-17 10:07:22 UTC (rev 90310)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/zip/ZipUtils.java	2009-06-17 10:08:31 UTC (rev 90311)
@@ -21,21 +21,98 @@
  */
 package org.jboss.virtual.spi.zip;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.logging.Logger;
 import org.jboss.virtual.spi.zip.jdk.JDKZipFactory;
-import org.jboss.virtual.spi.zip.jzipfile.JZipFileZipFactory;
 
 /**
  * Zip utils.
+ * This is the entry point to the ZipFactory.
  *
  * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  */
 public class ZipUtils
 {
-   private static ZipFactory factory = new JZipFileZipFactory();
-//   private static ZipFactory factory = new JDKZipFactory();
+   private static final Logger log = Logger.getLogger(ZipUtils.class);
+   public static final String KEY = ZipFactory.class.getName();
 
+   private static final Map<String, String> mappings;
+   private static ZipFactory factory;
+
+   static
+   {
+      mappings = new HashMap<String, String>();
+      mappings.put("jzipfile", "org.jboss.virtual.spi.zip.jzipfile.JZipFileZipFactory");
+      mappings.put("truezip", "org.jboss.virtual.spi.zip.truezip.TrueZipFactory");
+   }
+
+   /**
+    * Get the zip factory.
+    *
+    * @return the zip factory
+    */
    public static ZipFactory getFactory()
    {
+      if (factory == null)
+         init();
+
       return factory;
    }
+
+   /**
+    * Initialize zip factory.
+    */
+   private static void init()
+   {
+      SecurityManager sm = System.getSecurityManager();
+      if (sm == null)
+      {
+         factory = createZipFactory();
+      }
+      else
+      {
+         PrivilegedAction<ZipFactory> action = new PrivilegedAction<ZipFactory>()
+         {
+            public ZipFactory run()
+            {
+               return createZipFactory();
+            }
+         };
+         factory = AccessController.doPrivileged(action);
+      }
+   }
+
+   /**
+    * Instantiate zip factory.
+    *
+    * @return the zip factory
+    */
+   private static ZipFactory createZipFactory()
+   {
+      String factoryClass = System.getProperty(KEY);
+      if (factoryClass != null)
+      {
+         String mappingClass = mappings.get(factoryClass);
+         if (mappingClass != null)
+            factoryClass = mappingClass;
+
+         try
+         {
+            Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(factoryClass);
+            Object result = clazz.newInstance();
+            log.debug("Using custom ZipFactory - " + result.getClass().getName());
+            return ZipFactory.class.cast(result);
+         }
+         catch (Exception e)
+         {
+            log.warn("Exception instantiating ZipFactory: " + e);
+         }
+      }
+      log.debug("Using default ZipFactory - " + JDKZipFactory.class.getName());
+      return new JDKZipFactory();
+   }
 }
\ No newline at end of file




More information about the jboss-cvs-commits mailing list