[jboss-cvs] JBossAS SVN: r102988 - in projects/scanning/trunk: scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Mar 25 17:49:18 EDT 2010


Author: alesj
Date: 2010-03-25 17:49:17 -0400 (Thu, 25 Mar 2010)
New Revision: 102988

Added:
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/LazyUtilsProxyHandler.java
Modified:
   projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/web/plugins/ResourcesIndexScanningPluginFactory.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentUtilsFactory.java
Log:
Lazy utils lookup.

Modified: projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/web/plugins/ResourcesIndexScanningPluginFactory.java
===================================================================
--- projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/web/plugins/ResourcesIndexScanningPluginFactory.java	2010-03-25 21:24:21 UTC (rev 102987)
+++ projects/scanning/trunk/plugins/src/main/java/org/jboss/scanning/web/plugins/ResourcesIndexScanningPluginFactory.java	2010-03-25 21:49:17 UTC (rev 102988)
@@ -47,7 +47,7 @@
    {
       ReflectProvider provider = DeploymentUtilsFactory.getProvider(unit);
       ResourceOwnerFinder finder = DeploymentUtilsFactory.getFinder(unit);
-      AnnotationIndex index = null; // TODO -- lazy
+      AnnotationIndex index = DeploymentUtilsFactory.getLazyUtilProxy(unit, AnnotationIndex.class);
       return new ResourcesIndexScanningPlugin(provider, finder, index);
    }
 }
\ No newline at end of file

Modified: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentUtilsFactory.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentUtilsFactory.java	2010-03-25 21:24:21 UTC (rev 102987)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentUtilsFactory.java	2010-03-25 21:49:17 UTC (rev 102988)
@@ -1,5 +1,6 @@
 package org.jboss.scanning.plugins.helpers;
 
+import java.lang.reflect.Proxy;
 import java.util.Map;
 import java.util.WeakHashMap;
 
@@ -93,6 +94,21 @@
    }
 
    /**
+    * Wrap util lookup in lazy lookup.
+    *
+    * @param unit the deployment unit
+    * @param utilType the util type
+    * @return lazy util proxy
+    */
+   public static <T> T getLazyUtilProxy(DeploymentUnit unit, Class<T> utilType)
+   {
+      // null check is in handler
+      LazyUtilsProxyHandler<T> handler = new LazyUtilsProxyHandler<T>(unit, utilType);
+      Object proxy = Proxy.newProxyInstance(unit.getClassLoader(), new Class[]{utilType}, handler);
+      return utilType.cast(proxy);
+   }
+
+   /**
     * Get reflect provider.
     *
     * @param unit the depoyment unit

Copied: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/LazyUtilsProxyHandler.java (from rev 102987, projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentUtilsFactory.java)
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/LazyUtilsProxyHandler.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/LazyUtilsProxyHandler.java	2010-03-25 21:49:17 UTC (rev 102988)
@@ -0,0 +1,48 @@
+package org.jboss.scanning.plugins.helpers;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+import org.jboss.deployers.spi.deployer.helpers.AttachmentLocator;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+
+/**
+ * The lazy utils proxy.
+ *
+ * @param <T> exact util type
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+class LazyUtilsProxyHandler<T> implements InvocationHandler
+{
+   private DeploymentUnit unit;
+   private Class<T> type;
+   private T util;
+
+   public LazyUtilsProxyHandler(DeploymentUnit unit, Class<T> type)
+   {
+      if (unit == null)
+         throw new IllegalArgumentException("Null unit");
+      if (type == null)
+         throw new IllegalArgumentException("Null util type");
+
+      this.unit = unit;
+      this.type = type;
+   }
+
+   private T getUtil()
+   {
+      if (util == null)
+      {
+         T tmp = AttachmentLocator.searchAncestors(unit, type);
+         if (tmp == null)
+            throw new IllegalArgumentException("No such util: " + type);
+         util = tmp;
+      }
+      return util;
+   }
+
+   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+   {
+      return method.invoke(getUtil(), args);
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list