[jboss-cvs] JBossAS SVN: r96254 - projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 11 04:05:28 EST 2009


Author: alesj
Date: 2009-11-11 04:05:28 -0500 (Wed, 11 Nov 2009)
New Revision: 96254

Added:
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/LazyVirtualFileHandler.java
Modified:
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleHandler.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleVFSContext.java
Log:
Delay reflect hack as long as possible.

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleHandler.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleHandler.java	2009-11-11 08:33:58 UTC (rev 96253)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleHandler.java	2009-11-11 09:05:28 UTC (rev 96254)
@@ -31,6 +31,8 @@
 import java.util.List;
 
 import org.jboss.osgi.framework.bundle.OSGiBundleState;
+import org.jboss.virtual.VFSUtils;
+import org.jboss.virtual.VirtualFile;
 import org.jboss.virtual.plugins.context.AbstractVirtualFileHandler;
 import org.jboss.virtual.spi.VFSContext;
 import org.jboss.virtual.spi.VirtualFileHandler;
@@ -42,16 +44,30 @@
  */
 class BundleHandler extends AbstractVirtualFileHandler
 {
+   private VirtualFile file;
+   private OSGiBundleState bundleState;
    private VirtualFileHandler handler;
-   private OSGiBundleState bundleState;
 
-   public BundleHandler(VFSContext context, VirtualFileHandler parent, VirtualFileHandler handler, OSGiBundleState bundleState)
+   public BundleHandler(VFSContext context, VirtualFile file, OSGiBundleState bundleState) throws IOException
    {
-      super(context, parent, handler.getName());
-      this.handler = handler;
+      this(context, LazyVirtualFileHandler.create(file.getParent()), file, bundleState);
+   }
+
+   public BundleHandler(VFSContext context, VirtualFileHandler parent, VirtualFile file, OSGiBundleState bundleState) throws IOException
+   {
+      super(context, parent, file.getName());
+      this.file = file;
       this.bundleState = bundleState;
    }
 
+   protected VirtualFileHandler getHandler() throws IOException
+   {
+      if (handler == null)
+         handler = LazyVirtualFileHandler.create(file);
+
+      return handler;
+   }
+
    protected String getRelativePath(boolean checkEnd)
    {
       String path = handler.getLocalPathName();
@@ -60,6 +76,14 @@
       return path;
    }
 
+   protected VirtualFileHandler createChildHandler(VirtualFile child) throws IOException
+   {
+      if (handler == null)
+         return new BundleHandler(getVFSContext(), child, bundleState);
+      else
+         return new BundleHandler(getVFSContext(), handler, child, bundleState);
+   }
+
    public URI toURI() throws URISyntaxException
    {
       return new URI("bundle", Long.toString(bundleState.getBundleId()), getRelativePath(false), null);
@@ -74,20 +98,20 @@
    {
       bundleState.getResource(getRelativePath(false)); // permission check
       
-      return handler.openStream();
+      return file.openStream();
    }
 
    public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
    {
       bundleState.findEntries(getRelativePath(false), null, false); // permission check
 
-      List<VirtualFileHandler> children = handler.getChildren(ignoreErrors);
+      List<VirtualFile> children = file.getChildren();
       if (children != null && children.isEmpty() == false)
       {
          List<VirtualFileHandler> handlers = new ArrayList<VirtualFileHandler>(children.size());
-         for (VirtualFileHandler child : children)
+         for (VirtualFile child : children)
          {
-            handlers.add(new BundleHandler(getVFSContext(), handler, child, bundleState));
+            handlers.add(createChildHandler(child));
          }
          return handlers;
       }
@@ -101,39 +125,39 @@
       if (entry == null)
          return null;
 
-      VirtualFileHandler child = handler.getChild(path); // the child should exist, since entry does
-      return new BundleHandler(getVFSContext(), handler, child, bundleState);
+      VirtualFile child = file.getChild(path); // the child should exist, since entry does
+      return createChildHandler(child);
    }
 
    //---------------------------------------------------------
 
    public long getSize() throws IOException
    {
-      return handler.getSize();
+      return file.getSize();
    }
 
    public boolean exists() throws IOException
    {
-      return handler.exists();
+      return file.exists();
    }
 
    public boolean isLeaf() throws IOException
    {
-      return handler.isLeaf();
+      return file.isLeaf();
    }
 
    public boolean isHidden() throws IOException
    {
-      return handler.isHidden();
+      return file.isHidden();
    }
 
    public boolean removeChild(String name) throws IOException
    {
-      return handler.removeChild(name);
+      return getHandler().removeChild(name);
    }
 
    public boolean isNested() throws IOException
    {
-      return handler.isNested();
+      return VFSUtils.isNestedFile(file);
    }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleVFSContext.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleVFSContext.java	2009-11-11 08:33:58 UTC (rev 96253)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleVFSContext.java	2009-11-11 09:05:28 UTC (rev 96254)
@@ -22,10 +22,8 @@
 package org.jboss.osgi.framework.vfs;
 
 import java.io.IOException;
-import java.lang.reflect.Method;
 import java.net.URI;
-import java.security.AccessController;
-import java.security.PrivilegedExceptionAction;
+import java.net.URL;
 
 import org.jboss.deployers.structure.spi.DeploymentUnit;
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
@@ -43,30 +41,6 @@
  */
 public class BundleVFSContext extends AbstractVFSContext
 {
-   private static final Method getHandler;
-
-   static
-   {
-      try
-      {
-         PrivilegedExceptionAction<Method> action = new PrivilegedExceptionAction<Method>()
-         {
-            public Method run() throws Exception
-            {
-               Method method = VirtualFile.class.getDeclaredMethod("getHandler");
-               method.setAccessible(true);
-               return method;
-            }
-         };
-         getHandler = AccessController.doPrivileged(action);
-
-      }
-      catch (Exception e)
-      {
-         throw new RuntimeException(e);
-      }
-   }
-
    private String host;
    private VirtualFileHandler root;
 
@@ -83,28 +57,22 @@
       if (abs == null)
          throw new IllegalArgumentException("No such bundle: " + id);
 
+      String path = rootURI.getPath();
+      if (path == null)
+         path = "";
+
       OSGiBundleState bundleState = OSGiBundleState.class.cast(abs); // should be able to cast, as it's not system
+      URL resource = bundleState.getEntry(path); // permission check
+      if (resource == null)
+         throw new IllegalArgumentException("No such resource: " + path + " in bundle: " + bundleState);
+
       DeploymentUnit unit = bundleState.getDeploymentUnit();
       if (unit instanceof VFSDeploymentUnit == false)
          throw new IllegalArgumentException("Cannot handle non VFS deployments: " + unit);
 
-      String path = rootURI.getPath();
-      if (path == null)
-         path = "";
-
       VFSDeploymentUnit vdu = VFSDeploymentUnit.class.cast(unit);
-      VirtualFile file = vdu.getFile(path);
-      try
-      {
-         VirtualFileHandler handler = (VirtualFileHandler)getHandler.invoke(file);
-         root = new BundleHandler(this, handler.getParent(), handler, bundleState);
-      }
-      catch (Exception e)
-      {
-         IOException ioe = new IOException();
-         ioe.initCause(e);
-         throw ioe;
-      }
+      VirtualFile file = vdu.getFile(path); // should exist, resource != null
+      root = new BundleHandler(this, file, bundleState);
    }
 
    public String getName()

Copied: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/LazyVirtualFileHandler.java (from rev 96232, projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/BundleHandler.java)
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/LazyVirtualFileHandler.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/vfs/LazyVirtualFileHandler.java	2009-11-11 09:05:28 UTC (rev 96254)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.osgi.framework.vfs;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.security.AccessController;
+import java.security.PrivilegedExceptionAction;
+
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+/**
+ * Create a lazy VirtualFileHandler based off VirtualFile
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+class LazyVirtualFileHandler
+{
+   private static final Method getHandler;
+
+   static
+   {
+      try
+      {
+         PrivilegedExceptionAction<Method> action = new PrivilegedExceptionAction<Method>()
+         {
+            public Method run() throws Exception
+            {
+               Method method = VirtualFile.class.getDeclaredMethod("getHandler");
+               method.setAccessible(true);
+               return method;
+            }
+         };
+         getHandler = AccessController.doPrivileged(action);
+
+      }
+      catch (Exception e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   /**
+    * Create a VFH proxy, delaying the actual reflect hack.
+    *
+    * @param file the VFH owner
+    * @return VFH proxy
+    * @throws IOException for any error
+    */
+   static VirtualFileHandler create(VirtualFile file) throws IOException
+   {
+      if (file == null)
+         return null;
+
+      ClassLoader cl = LazyVirtualFileHandler.class.getClassLoader();
+      Object proxy = Proxy.newProxyInstance(cl, new Class<?>[]{VirtualFileHandler.class}, new ProxyHandler(file));
+      return VirtualFileHandler.class.cast(proxy);
+   }
+   
+   private static class ProxyHandler implements InvocationHandler
+   {
+      private VirtualFile file;
+      private volatile VirtualFileHandler handler;
+
+      private ProxyHandler(VirtualFile file)
+      {
+         this.file = file;
+      }
+
+      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+      {
+         return method.invoke(getHandler(), args);
+      }
+
+      private VirtualFileHandler getHandler() throws Exception
+      {
+         if (handler == null)
+            handler = (VirtualFileHandler)getHandler.invoke(file);
+
+         return handler;
+      }
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list