Author: jacopoc
Date: Fri Sep 12 13:49:17 2014
New Revision: 1624549

URL: http://svn.apache.org/r1624549
Log:
Removed the custom CachedClassLoader and related artifacts in favour of a 
simpler bootstrap process and a simpler classpath tree; now the classpath tree 
is made of: bootstrap classloader (primordial+ext) -> system classloader (aka 
applications or classpath classloader) -> two custom classloaders (one loads 
the framework/base classes and one loads all the other classes) -> Webapp 
classloaders (one per web application).
Junit tests are included. The two custom classloaders could be merged into one 
but this will be addressed in a second phase because it will require some 
changes to the bootstrap componenr and process.


Removed:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/config/ClasspathLoader.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/CachedClassLoader.java
    
ofbiz/trunk/framework/entity/src/META-INF/services/org.ofbiz.base.util.CachedClassLoader$Init
    
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/CachedClassLoaderInit.java
Modified:
    ofbiz/trunk/framework/base/ofbiz-component.xml
    
ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ClassLoaderContainer.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java
    
ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
    ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Config.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java

Modified: ofbiz/trunk/framework/base/ofbiz-component.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/ofbiz-component.xml?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/ofbiz-component.xml (original)
+++ ofbiz/trunk/framework/base/ofbiz-component.xml Fri Sep 12 13:49:17 2014
@@ -28,7 +28,7 @@ under the License.
 
     <test-suite loader="main" location="testdef/basetests.xml"/>
 
-    <!-- load the cached classloader container (always second) -->
+    <!-- load the classloader container -->
     <container name="classloader-container" 
loaders="main,rmi,pos,install,test" 
class="org.ofbiz.base.container.ClassLoaderContainer"/>
 
     <!-- load the naming (JNDI) server -->

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ClassLoaderContainer.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ClassLoaderContainer.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ClassLoaderContainer.java
 (original)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ClassLoaderContainer.java
 Fri Sep 12 13:49:17 2014
@@ -18,36 +18,22 @@
  
*******************************************************************************/
 package org.ofbiz.base.container;
 
-import java.net.URL;
-
-import org.ofbiz.base.start.Classpath;
-import org.ofbiz.base.util.CachedClassLoader;
 import org.ofbiz.base.util.Debug;
 
 /**
- * ClassLoader Container; Created a CachedClassLoader for use by all following 
containers
+ * ClassLoader Container
  *
  */
 public class ClassLoaderContainer implements Container {
 
     public static final String module = ClassLoaderContainer.class.getName();
-    protected static CachedClassLoader cl = null;
     public static Integer portOffset = 0;
     private String name;
 
     @Override
     public void init(String[] args, String name, String configFile) throws 
ContainerException {
         this.name = name;
-        ClassLoader parent = Thread.currentThread().getContextClassLoader();
-        if (parent == null) {
-            parent = Classpath.class.getClassLoader();
-        }
-        if (parent == null) {
-            parent = ClassLoader.getSystemClassLoader();
-        }
 
-        cl = new CachedClassLoader(new URL[0], parent);
-        
         if (args != null) {
             for (String argument : args) {
                 // arguments can prefix w/ a '-'. Just strip them off
@@ -75,8 +61,6 @@ public class ClassLoaderContainer implem
             }
         }
         
-        Thread.currentThread().setContextClassLoader(cl);
-        Debug.logInfo("CachedClassLoader created", module);
     }
 
     /**
@@ -99,11 +83,4 @@ public class ClassLoaderContainer implem
         return name;
     }
 
-    public static ClassLoader getClassLoader() {
-        if (cl != null) {
-            return cl;
-        } else {
-            return ClassLoader.getSystemClassLoader();
-        }
-    }
 }

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java 
(original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java Fri Sep 
12 13:49:17 2014
@@ -22,13 +22,12 @@ import java.io.Serializable;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.TimeZone;
 
-import javolution.util.FastMap;
-
 import org.ofbiz.base.conversion.ConversionException;
 import org.ofbiz.base.conversion.Converter;
 import org.ofbiz.base.conversion.Converters;
@@ -47,11 +46,48 @@ public class ObjectType {
 
     public static final Object NULL = new NullObject();
 
-    protected static FastMap<String, Class<?>> classCache = 
FastMap.newInstance();
-
     public static final String LANG_PACKAGE = "java.lang."; // We will test 
both the raw value and this + raw value
     public static final String SQL_PACKAGE = "java.sql.";   // We will test 
both the raw value and this + raw value
 
+    private static final Map<String, String> classAlias = new HashMap<String, 
String>();
+    private static final Map<String, Class> primitives = new HashMap<String, 
Class>();
+
+    static {
+        classAlias.put("Object", "java.lang.Object");
+        classAlias.put("String", "java.lang.String");
+        classAlias.put("Boolean", "java.lang.Boolean");
+        classAlias.put("BigDecimal", "java.math.BigDecimal");
+        classAlias.put("Double", "java.lang.Double");
+        classAlias.put("Float", "java.lang.Float");
+        classAlias.put("Long", "java.lang.Long");
+        classAlias.put("Integer", "java.lang.Integer");
+        classAlias.put("Short", "java.lang.Short");
+        classAlias.put("Byte", "java.lang.Byte");
+        classAlias.put("Character", "java.lang.Character");
+        classAlias.put("Timestamp", "java.sql.Timestamp");
+        classAlias.put("Time", "java.sql.Time");
+        classAlias.put("Date", "java.sql.Date");
+        classAlias.put("Locale", "java.util.Locale");
+        classAlias.put("Collection", "java.util.Collection");
+        classAlias.put("List", "java.util.List");
+        classAlias.put("Set", "java.util.Set");
+        classAlias.put("Map", "java.util.Map");
+        classAlias.put("HashMap", "java.util.HashMap");
+        classAlias.put("TimeZone", "java.util.TimeZone");
+        classAlias.put("TimeDuration", "org.ofbiz.base.util.TimeDuration");
+        classAlias.put("GenericValue", "org.ofbiz.entity.GenericValue");
+        classAlias.put("GenericPK", "org.ofbiz.entity.GenericPK");
+        classAlias.put("GenericEntity", "org.ofbiz.entity.GenericEntity");
+        primitives.put("boolean", Boolean.TYPE);
+        primitives.put("short", Short.TYPE);
+        primitives.put("int", Integer.TYPE);
+        primitives.put("long", Long.TYPE);
+        primitives.put("float", Float.TYPE);
+        primitives.put("double", Double.TYPE);
+        primitives.put("byte", Byte.TYPE);
+        primitives.put("char", Character.TYPE);
+    }
+
     /**
      * Loads a class with the current thread's context classloader.
      * @param className The name of the class to load
@@ -59,25 +95,23 @@ public class ObjectType {
      * @throws ClassNotFoundException
      */
     public static Class<?> loadClass(String className) throws 
ClassNotFoundException {
-        int genericsStart = className.indexOf("<");
-        if (genericsStart != -1) className = className.substring(0, 
genericsStart);
-
-        // small block to speed things up by putting using preloaded classes 
for common objects, this turns out to help quite a bit...
-        Class<?> theClass = 
CachedClassLoader.globalClassNameClassMap.get(className);
-
-        if (theClass != null) return theClass;
-
         return loadClass(className, null);
     }
 
     /**
-     * Loads a class with the current thread's context classloader.
+     * Loads a class with the specified classloader.
      * @param className The name of the class to load
      * @param loader The ClassLoader to use
      * @return The requested class
      * @throws ClassNotFoundException
      */
     public static Class<?> loadClass(String className, ClassLoader loader) 
throws ClassNotFoundException {
+        Class<?> theClass = null;
+        // if it is a primitive type, return the object from the "primitives" 
map
+        if (primitives.containsKey(className)) {
+            return primitives.get(className);
+        }
+
         int genericsStart = className.indexOf("<");
         if (genericsStart != -1) className = className.substring(0, 
genericsStart);
 
@@ -98,26 +132,14 @@ public class ObjectType {
             }
         }
 
-        // small block to speed things up by putting using preloaded classes 
for common objects, this turns out to help quite a bit...
-        Class<?> theClass = 
CachedClassLoader.globalClassNameClassMap.get(className);
-
-        if (theClass != null) return theClass;
+        // if className is an alias (e.g. "String") then replace it with the 
proper class name (e.g. "java.lang.String")
+        if (classAlias.containsKey(className)) {
+            className = classAlias.get(className);
+        }
 
         if (loader == null) loader = 
Thread.currentThread().getContextClassLoader();
 
-        try {
-            theClass = Class.forName(className, true, loader);
-        } catch (Exception e) {
-            theClass = classCache.get(className);
-            if (theClass == null) {
-                theClass = Class.forName(className);
-                if (theClass != null) {
-                    if (classCache.putIfAbsent(className, theClass) == null) {
-                        if (Debug.verboseOn()) Debug.logVerbose("Loaded Class: 
" + theClass.getName(), module);
-                    }
-                }
-            }
-        }
+        theClass = Class.forName(className, true, loader);
 
         return theClass;
     }

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilIO.java Fri Sep 12 
13:49:17 2014
@@ -43,7 +43,6 @@ import java.nio.charset.Charset;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
-import org.ofbiz.base.container.ClassLoaderContainer;
 import org.ofbiz.base.conversion.Converter;
 import org.ofbiz.base.conversion.Converters;
 import org.ofbiz.base.json.JSON;
@@ -378,7 +377,7 @@ public final class UtilIO {
             for (i = offset; i < length && buffer[i] != ':'; i++);
             if (i > offset && i < length) {
                 String className = new String(buffer, offset, i);
-                Class<?> type = Class.forName(className, true, 
ClassLoaderContainer.getClassLoader());
+                Class<?> type = Class.forName(className);
                 if (buffer[length - 1] == '\n') {
                     length--;
                 }

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java 
(original)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java 
Fri Sep 12 13:49:17 2014
@@ -194,6 +194,58 @@ public class ObjectTypeTests extends Gen
         assertSame(label + ":to-java.lang.Object", toConvert, 
simpleTypeConvert(toConvert, "java.lang.Object", null, null, null, true));
     }
 
+    public void testLoadClassWithNonExistentClass() {
+        Exception exception = null;
+        try {
+            ObjectType.loadClass("foobarbaz");
+        } catch (Exception e) {
+            exception = e;
+        }
+        assertTrue("Exception thrown by loadClass(\"foobarbaz\") is not 
ClassNotFoundException", exception instanceof ClassNotFoundException);
+    }
+
+    public void testLoadClassWithPrimitives() {
+        try {
+            Class<?> theClass;
+            theClass = ObjectType.loadClass("boolean");
+            assertEquals("Wrong class returned by loadClass(\"boolean\")", 
(Boolean.TYPE).getName(), theClass.getName());
+            theClass = ObjectType.loadClass("short");
+            assertEquals("Wrong class returned by loadClass(\"short\")", 
(Short.TYPE).getName(), theClass.getName());
+            theClass = ObjectType.loadClass("int");
+            assertEquals("Wrong class returned by loadClass(\"int\")", 
(Integer.TYPE).getName(), theClass.getName());
+            theClass = ObjectType.loadClass("long");
+            assertEquals("Wrong class returned by loadClass(\"long\")", 
(Long.TYPE).getName(), theClass.getName());
+            theClass = ObjectType.loadClass("float");
+            assertEquals("Wrong class returned by loadClass(\"float\")", 
(Float.TYPE).getName(), theClass.getName());
+            theClass = ObjectType.loadClass("double");
+            assertEquals("Wrong class returned by loadClass(\"double\")", 
(Double.TYPE).getName(), theClass.getName());
+            theClass = ObjectType.loadClass("byte");
+            assertEquals("Wrong class returned by loadClass(\"byte\")", 
(Byte.TYPE).getName(), theClass.getName());
+            theClass = ObjectType.loadClass("char");
+            assertEquals("Wrong class returned by loadClass(\"char\")", 
(Character.TYPE).getName(), theClass.getName());
+        } catch (Exception e) {
+            fail("Exception thrown by loadClass: " + e.getMessage());
+        }
+    }
+
+    public void testLoadClassWithAlias() {
+        try {
+            Class<?> theClass;
+            // first try with a class full name
+            theClass = ObjectType.loadClass("java.lang.String");
+            assertEquals("Wrong class returned by 
loadClass(\"java.lang.String\")", "java.lang.String", theClass.getName());
+            // now try with some aliases
+            theClass = ObjectType.loadClass("String");
+            assertEquals("Wrong class returned by loadClass(\"String\")", 
"java.lang.String", theClass.getName());
+            theClass = ObjectType.loadClass("Object");
+            assertEquals("Wrong class returned by loadClass(\"Object\")", 
"java.lang.Object", theClass.getName());
+            theClass = ObjectType.loadClass("Date");
+            assertEquals("Wrong class returned by loadClass(\"Date\")", 
"java.sql.Date", theClass.getName());
+        } catch (Exception e) {
+            fail("Exception thrown by loadClass: " + e.getMessage());
+        }
+    }
+
     public void testClassNotFound() {
         GeneralException caught = null;
         try {

Modified: 
ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
 (original)
+++ 
ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
 Fri Sep 12 13:49:17 2014
@@ -678,7 +678,7 @@ public class CatalinaContainer implement
 
         context.setJ2EEApplication(J2EE_APP);
         context.setJ2EEServer(J2EE_SERVER);
-        context.setLoader(new 
WebappLoader(ClassLoaderContainer.getClassLoader()));
+        context.setLoader(new 
WebappLoader(Thread.currentThread().getContextClassLoader()));
 
         context.setCookies(appInfo.isSessionCookieAccepted());
         context.addParameter("cookies", appInfo.isSessionCookieAccepted() ? 
"true" : "false");

Modified: ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Config.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Config.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Config.java (original)
+++ ofbiz/trunk/framework/start/src/org/ofbiz/base/start/Config.java Fri Sep 12 
13:49:17 2014
@@ -270,9 +270,10 @@ public class Config {
         if (this.baseLib != null) {
             loadLibs(classPath, this.baseLib, true);
         }
-        // load the ofbiz-base.jar
+        // load the ofbiz-base.jar and the ofbiz-base-test.jar
         if (this.baseJar != null) {
             classPath.addComponent(this.baseJar);
+            classPath.addComponent(this.baseJar.substring(0, 
this.baseJar.indexOf(".jar")) + "-test.jar");
         }
         // load the base schema directory
         if (this.baseDtd != null) {

Modified: 
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java 
(original)
+++ 
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java 
Fri Sep 12 13:49:17 2014
@@ -35,14 +35,12 @@ import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.ofbiz.base.util.CachedClassLoader;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.StringUtil;
 import org.ofbiz.base.util.UtilGenerics;
 import org.ofbiz.base.util.UtilHttp;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilObject;
-import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.entity.Delegator;
 import org.ofbiz.entity.DelegatorFactory;
@@ -65,7 +63,6 @@ public class ContextFilter implements Fi
     public static final String module = ContextFilter.class.getName();
     public static final String FORWARDED_FROM_SERVLET = 
"_FORWARDED_FROM_SERVLET_";
 
-    protected ClassLoader localCachedClassLoader = null;
     protected FilterConfig config = null;
     protected boolean debug = false;
 
@@ -78,10 +75,6 @@ public class ContextFilter implements Fi
         // puts all init-parameters in ServletContext attributes for easier 
parameterization without code changes
         this.putAllInitParametersInAttributes();
 
-        // initialize the cached class loader for this application
-        ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        localCachedClassLoader = new CachedClassLoader(loader, (String) 
config.getServletContext().getAttribute("webSiteId"));
-
         // set debug
         this.debug = "true".equalsIgnoreCase(config.getInitParameter("debug"));
         if (!debug) {
@@ -111,11 +104,6 @@ public class ContextFilter implements Fi
         // Debug.logInfo("Running ContextFilter.doFilter", module);
 
         // ----- Servlet Object Setup -----
-        // set the cached class loader for more speedy running in this thread
-        String disableCachedClassloader = 
config.getInitParameter("disableCachedClassloader");
-        if (disableCachedClassloader == null || 
!"Y".equalsIgnoreCase(disableCachedClassloader)) {
-            
Thread.currentThread().setContextClassLoader(localCachedClassLoader);
-        }
 
         // set the ServletContext in the request for future use
         httpRequest.setAttribute("servletContext", config.getServletContext());

Modified: 
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java?rev=1624549&r1=1624548&r2=1624549&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java 
(original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/webdav/WebDavServlet.java 
Fri Sep 12 13:49:17 2014
@@ -30,7 +30,6 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
-import org.ofbiz.base.util.CachedClassLoader;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.entity.Delegator;
 import org.ofbiz.entity.DelegatorFactory;
@@ -61,8 +60,6 @@ public class WebDavServlet extends Gener
     public void init(ServletConfig config) throws ServletException{
         try {
             super.init(config);
-            ClassLoader loader = new 
CachedClassLoader(Thread.currentThread().getContextClassLoader(), null);
-            Thread.currentThread().setContextClassLoader(loader);
             ServletContext context = this.getServletContext();
             String delegatorName = 
context.getInitParameter("entityDelegatorName");
             this.delegator = DelegatorFactory.getDelegator(delegatorName);
@@ -70,7 +67,7 @@ public class WebDavServlet extends Gener
             this.dispatcher = 
ServiceContainer.getLocalDispatcher(dispatcherName, this.delegator);
             this.security = SecurityFactory.getInstance(this.delegator);
             String factoryClassName = 
context.getInitParameter("requestHandlerFactoryClass");
-            this.handlerFactory = (RequestHandlerFactory) 
loader.loadClass(factoryClassName).newInstance();
+            this.handlerFactory = (RequestHandlerFactory) 
Class.forName(factoryClassName).newInstance();
         } catch (Exception e) {
             Debug.logError(e, "Error while initializing WebDAV servlet: ", 
module);
             throw new ServletException(e);


Reply via email to