[jboss-cvs] JBossAS SVN: r103059 - projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Mar 26 14:13:06 EDT 2010


Author: alesj
Date: 2010-03-26 14:13:05 -0400 (Fri, 26 Mar 2010)
New Revision: 103059

Added:
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DirectRootResourceOwnerFinder.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ReflectProviderUtilFactory.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinderUtilFactory.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/UtilFactory.java
Modified:
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ClassResourceOwnerFinder.java
   projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentUtilsFactory.java
Log:
Fix utils handling.

Modified: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ClassResourceOwnerFinder.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ClassResourceOwnerFinder.java	2010-03-26 17:56:21 UTC (rev 103058)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ClassResourceOwnerFinder.java	2010-03-26 18:13:05 UTC (rev 103059)
@@ -29,12 +29,18 @@
 import org.jboss.classloading.spi.visitor.ResourceContext;
 
 /**
- * Direct class source location fnder.
+ * Direct class source location finder.
  *
  * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
  */
 public class ClassResourceOwnerFinder implements ResourceOwnerFinder
 {
+   public static ResourceOwnerFinder INSTANCE = new ClassResourceOwnerFinder();
+   
+   private ClassResourceOwnerFinder()
+   {
+   }
+
    /**
     * This one loads the class.
     *

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-26 17:56:21 UTC (rev 103058)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DeploymentUtilsFactory.java	2010-03-26 18:13:05 UTC (rev 103059)
@@ -5,10 +5,7 @@
 import java.util.WeakHashMap;
 
 import org.jboss.classloading.spi.dependency.Module;
-import org.jboss.deployers.spi.deployer.helpers.AttachmentLocator;
 import org.jboss.deployers.structure.spi.DeploymentUnit;
-import org.jboss.reflect.plugins.introspection.ReflectionUtils;
-import org.jboss.scanning.plugins.visitor.JavassistReflectProvider;
 import org.jboss.scanning.plugins.visitor.ReflectProvider;
 
 /**
@@ -20,23 +17,23 @@
 public class DeploymentUtilsFactory
 {
    /** The default impls */
-   private static Map<Class<?>, Class<?>> defaults = new WeakHashMap<Class<?>, Class<?>>();
+   private static Map<Class<?>, UtilFactory<?>> defaults = new WeakHashMap<Class<?>, UtilFactory<?>>();
 
    static
    {
-      addImplementation(ReflectProvider.class, JavassistReflectProvider.class);
-      addImplementation(ResourceOwnerFinder.class, CachingResourceOwnerFinder.class);
+      addImplementation(ReflectProvider.class, new ReflectProviderUtilFactory());
+      addImplementation(ResourceOwnerFinder.class, new ResourceOwnerFinderUtilFactory());
    }
 
    /**
     * Add the util impl.
     *
     * @param iface the interface
-    * @param implementation the impl
+    * @param factory the util factory
     */
-   public static <T> void addImplementation(Class<T> iface, Class<? extends T> implementation)
+   public static <T> void addImplementation(Class<T> iface, UtilFactory<T> factory)
    {
-      defaults.put(iface, implementation);
+      defaults.put(iface, factory);
    }
 
    /**
@@ -63,31 +60,24 @@
       if (utilType == null)
          throw new IllegalArgumentException("Null util type");
 
-      T util = AttachmentLocator.searchAncestors(unit, utilType);
+      // group util per module
+      DeploymentUnit moduleUnit = unit;
+      while(moduleUnit != null && moduleUnit.isAttachmentPresent(Module.class) == false)
+         moduleUnit = moduleUnit.getParent();
+
+      if (moduleUnit == null)
+         throw new IllegalArgumentException("No module in unit: " + unit);
+
+      T util = moduleUnit.getAttachment(utilType);
       if (util == null)
       {
-         Class<?> defUtilClass = defaults.get(utilType);
-         if (defUtilClass == null)
-            throw new IllegalArgumentException("No default impl defined for " + utilType);
+         UtilFactory factory = defaults.get(utilType);
+         if (factory == null)
+            throw new IllegalArgumentException("No util factory defined for " + utilType);
 
-         try
-         {
-            Object instance = ReflectionUtils.newInstance(defUtilClass);
-            util = utilType.cast(instance);
-         }
-         catch (Throwable t)
-         {
-            throw new RuntimeException(t);
-         }
+         Object instance = factory.create(moduleUnit);
+         util = utilType.cast(instance);
 
-         DeploymentUnit moduleUnit = unit;
-         while(moduleUnit != null && moduleUnit.isAttachmentPresent(Module.class) == false)
-            moduleUnit = moduleUnit.getParent();
-
-         if (moduleUnit == null)
-            throw new IllegalArgumentException("No module in unit: " + unit);
-
-         // group util per module
          moduleUnit.addAttachment(utilType, util);
       }
       return util;

Copied: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DirectRootResourceOwnerFinder.java (from rev 102929, projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ClassResourceOwnerFinder.java)
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DirectRootResourceOwnerFinder.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/DirectRootResourceOwnerFinder.java	2010-03-26 18:13:05 UTC (rev 103059)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.scanning.plugins.helpers;
+
+import java.lang.reflect.Method;
+import java.net.URL;
+
+import org.jboss.classloading.plugins.vfs.VFSResourceVisitor;
+import org.jboss.classloading.spi.visitor.ResourceContext;
+import org.jboss.vfs.VirtualFile;
+
+/**
+ * Direct root finder.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class DirectRootResourceOwnerFinder implements ResourceOwnerFinder
+{
+   private static ResourceOwnerFinder instance;
+   private static Method currentRootGetter;
+
+   static
+   {
+      try
+      {
+         Class<?> clazz = VFSResourceVisitor.class;
+         currentRootGetter = clazz.getDeclaredMethod("getCurrentRoot");
+      }
+      catch (Throwable ignored)
+      {
+      }
+   }
+
+   private DirectRootResourceOwnerFinder()
+   {
+   }
+
+   public static boolean isValid()
+   {
+      return currentRootGetter != null;
+   }
+
+   public static ResourceOwnerFinder getInstance()
+   {
+      if (instance == null)
+         instance = new DirectRootResourceOwnerFinder();
+      return instance;
+   }
+
+   /**
+    * This one loads the class.
+    *
+    * @param resource the resource
+    * @return the owner url
+    */
+   public URL findOwnerURL(ResourceContext resource)
+   {
+      try
+      {
+         VirtualFile file = (VirtualFile) currentRootGetter.invoke(null);
+         return file.toURL();
+      }
+      catch (Exception e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+}
\ No newline at end of file

Added: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ReflectProviderUtilFactory.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ReflectProviderUtilFactory.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ReflectProviderUtilFactory.java	2010-03-26 18:13:05 UTC (rev 103059)
@@ -0,0 +1,18 @@
+package org.jboss.scanning.plugins.helpers;
+
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.scanning.plugins.visitor.JavassistReflectProvider;
+import org.jboss.scanning.plugins.visitor.ReflectProvider;
+
+/**
+ * Reflect provider factory.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class ReflectProviderUtilFactory implements UtilFactory<ReflectProvider>
+{
+   public ReflectProvider create(DeploymentUnit unit)
+   {
+      return new JavassistReflectProvider();
+   }
+}
\ No newline at end of file

Added: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinderUtilFactory.java
===================================================================
--- projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinderUtilFactory.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/ResourceOwnerFinderUtilFactory.java	2010-03-26 18:13:05 UTC (rev 103059)
@@ -0,0 +1,19 @@
+package org.jboss.scanning.plugins.helpers;
+
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+
+/**
+ * Resource owner finder factory.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class ResourceOwnerFinderUtilFactory implements UtilFactory<ResourceOwnerFinder>
+{
+   public ResourceOwnerFinder create(DeploymentUnit unit)
+   {
+      if (DirectRootResourceOwnerFinder.isValid())
+         return DirectRootResourceOwnerFinder.getInstance();
+      else
+         return new CachingResourceOwnerFinder();
+   }
+}
\ No newline at end of file

Copied: projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/UtilFactory.java (from rev 102988, 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/UtilFactory.java	                        (rev 0)
+++ projects/scanning/trunk/scanning-impl/src/main/java/org/jboss/scanning/plugins/helpers/UtilFactory.java	2010-03-26 18:13:05 UTC (rev 103059)
@@ -0,0 +1,20 @@
+package org.jboss.scanning.plugins.helpers;
+
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+
+/**
+ * Create deployment util.
+ *
+ * @param <T> exact util type
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface UtilFactory<T>
+{
+   /**
+    * Create util.
+    *
+    * @param unit the deployment unit
+    * @return new util
+    */
+   T create(DeploymentUnit unit);
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list