openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gerdo...@apache.org
Subject svn commit: r959732 - in /openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans: config/WebBeansFinder.java util/ClassUtil.java
Date Thu, 01 Jul 2010 17:35:51 GMT
Author: gerdogdu
Date: Thu Jul  1 17:35:50 2010
New Revision: 959732

URL: http://svn.apache.org/viewvc?rev=959732&view=rev
Log:
Update how our loading classes for singletons. Using Class.forName instead of loadClass

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansFinder.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansFinder.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansFinder.java?rev=959732&r1=959731&r2=959732&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansFinder.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansFinder.java
Thu Jul  1 17:35:50 2010
@@ -19,10 +19,13 @@
 package org.apache.webbeans.config;
 
 import java.util.HashMap;
+import java.util.IdentityHashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.webbeans.exception.WebBeansException;
 import org.apache.webbeans.util.Asserts;
+import org.apache.webbeans.util.ClassUtil;
 import org.apache.webbeans.util.WebBeansUtil;
 
 /**
@@ -39,6 +42,8 @@ public final class WebBeansFinder
      * Values --> Maps of singleton class name with object
      */
     private static Map<ClassLoader, Map<String, Object>> singletonMap = new HashMap<ClassLoader,
Map<String,Object>>();
+    
+    private static Map<Object, ClassLoader> objectToClassLoaderMap = new IdentityHashMap<Object,
ClassLoader>();
 
     /**
      * No instantiate.
@@ -85,13 +90,20 @@ public final class WebBeansFinder
                 try
                 {
                     //Load class
-                    Class<?> clazz = classLoader.loadClass(singletonName);
+                    Class<?> clazz = ClassUtil.getClassFromName(singletonName);
+                    if(clazz == null)
+                    {
+                        throw new ClassNotFoundException("Class with name: " + singletonName
+ " is not found in the system");
+                    }
                     
                     //Create instance
                     object = clazz.newInstance();
 
                     //Save it
                     managerMap.put(singletonName, object);
+                    
+                    //Save it object --> classloader
+                    objectToClassLoaderMap.put(object, classLoader);
 
                 }
                 catch (InstantiationException e)
@@ -145,8 +157,34 @@ public final class WebBeansFinder
         Asserts.assertNotNull(classLoader, "classloader is null");
         synchronized (singletonMap)
         {
-            singletonMap.remove(classLoader);
+            Map<String, Object> objects = singletonMap.remove(classLoader);
+            if(objects != null)
+            {
+                for(Entry<String, Object> entry : objects.entrySet())
+                {
+                    objectToClassLoaderMap.remove(entry.getValue());
+                }
+            }
+        }
+    }
+    
+    /**
+     * Gets classloader with given singelton instance.
+     * @param object singleton instance
+     * @return the classloader that instance is created within
+     */
+    public static ClassLoader getSingletonClassLoader(Object object)
+    {
+        Asserts.assertNotNull(object, "object is null");
+        synchronized (objectToClassLoaderMap)
+        {
+            if(objectToClassLoaderMap.containsKey(object))
+            {
+                return objectToClassLoaderMap.get(object);
+            }              
         }
+        
+        return null;
     }
 
 }
\ No newline at end of file

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java?rev=959732&r1=959731&r2=959732&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
Thu Jul  1 17:35:50 2010
@@ -185,12 +185,11 @@ public final class ClassUtil
     public static Class<?> getClassFromName(String name)
     {
         Class<?> clazz = null;
-
+        ClassLoader loader = null;
         try
         {
-            ClassLoader loader = WebBeansUtil.getCurrentClassLoader();
-            clazz = loader.loadClass(name);
-
+            loader = WebBeansUtil.getCurrentClassLoader();
+            clazz = Class.forName(name, true , loader);
             return clazz;
 
         }
@@ -198,7 +197,8 @@ public final class ClassUtil
         {
             try
             {
-                clazz = ClassUtil.class.getClassLoader().loadClass(name);
+                loader = ClassUtil.class.getClassLoader(); 
+                clazz = Class.forName(name, true , loader);
 
                 return clazz;
 
@@ -207,7 +207,8 @@ public final class ClassUtil
             {
                 try
                 {
-                    clazz = ClassLoader.getSystemClassLoader().loadClass(name);
+                    loader = ClassLoader.getSystemClassLoader();
+                    clazz = Class.forName(name, true , loader);
 
                     return clazz;
 



Mime
View raw message