You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by hl...@apache.org on 2003/09/10 22:38:04 UTC

cvs commit: jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl ClassFactoryClassLoader.java MethodFabImpl.java ClassFactoryImpl.java ClassFabImpl.java

hlship      2003/09/10 13:38:04

  Modified:    hivemind/src/java/org/apache/commons/hivemind/service/impl
                        MethodFabImpl.java ClassFactoryImpl.java
                        ClassFabImpl.java
  Added:       hivemind/src/java/org/apache/commons/hivemind/service/impl
                        ClassFactoryClassLoader.java
  Log:
  Apply fix by Essl Christian to handle more complex class loader issues inside Tomcat.
  
  Revision  Changes    Path
  1.7       +2 -2      jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/MethodFabImpl.java
  
  Index: MethodFabImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/MethodFabImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- MethodFabImpl.java	2 Sep 2003 21:13:04 -0000	1.6
  +++ MethodFabImpl.java	10 Sep 2003 20:38:03 -0000	1.7
  @@ -74,7 +74,7 @@
    * @author Howard Lewis Ship
    * @version $Id$
    */
  -public class MethodFabImpl implements MethodFab
  +class MethodFabImpl implements MethodFab
   {
       private ClassFactoryImpl _factory;
       private ClassPool _pool;
  
  
  
  1.9       +33 -15    jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFactoryImpl.java
  
  Index: ClassFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFactoryImpl.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ClassFactoryImpl.java	10 Sep 2003 15:00:47 -0000	1.8
  +++ ClassFactoryImpl.java	10 Sep 2003 20:38:03 -0000	1.9
  @@ -65,14 +65,12 @@
   import javassist.CtClass;
   import javassist.LoaderClassPath;
   import javassist.NotFoundException;
  -import javassist.bytecode.ClassFile;
  -import javassist.bytecode.ClassFileWriter;
   
   import org.apache.commons.hivemind.ApplicationRuntimeException;
   import org.apache.commons.hivemind.HiveMind;
   import org.apache.commons.hivemind.Module;
  -import org.apache.commons.hivemind.service.*;
   import org.apache.commons.hivemind.service.ClassFab;
  +import org.apache.commons.hivemind.service.ClassFabUtils;
   import org.apache.commons.hivemind.service.ClassFactory;
   
   /**
  @@ -84,13 +82,26 @@
   public class ClassFactoryImpl implements ClassFactory
   {
       /**
  -     * Map of ClassPool, keyed on module id.
  +     * Map of ModuleData, keyed on module id.
        */
  -    private Map _poolMap = new HashMap();
  +    private Map _moduleDataMap = new HashMap();
  +
  +    /**
  +     * Stores the Javassist ClassPool and the special ClassFactoryClassLoader
  +     * for a module.
  +     */
  +    private static class ModuleData
  +    {
  +        ClassPool _pool;
  +        ClassFactoryClassLoader _loader;
  +    }
   
       public ClassFab newClass(String name, Class superClass, Module module)
       {
  -        ClassPool pool = findPool(module);
  +        ModuleData data = findModuleData(module);
  +
  +        ClassPool pool = data._pool;
  +        ClassFactoryClassLoader loader = data._loader;
   
           CtClass ctSuperClass = getClass(pool, superClass);
   
  @@ -98,7 +109,7 @@
           {
               CtClass ctNewClass = pool.makeClass(name, ctSuperClass);
   
  -            return new ClassFabImpl(this, ctNewClass);
  +            return new ClassFabImpl(this, ctNewClass, loader);
           }
           catch (Exception ex)
           {
  @@ -129,22 +140,29 @@
           }
       }
   
  -    private synchronized ClassPool findPool(Module module)
  +    private synchronized ModuleData findModuleData(Module module)
       {
           String id = module.getModuleId();
   
  -        ClassPool result = (ClassPool) _poolMap.get(id);
  +        ModuleData result = (ModuleData) _moduleDataMap.get(id);
   
           if (result == null)
           {
  -            result = new ClassPool(null);
  +            ClassPool pool = new ClassPool(null);
  +
  +            ClassLoader moduleLoader = module.getClassResolver().getClassLoader();
  +            ClassPath path = new LoaderClassPath(moduleLoader);
  +
  +            pool.appendClassPath(path);
  +
  +            ClassFactoryClassLoader loader = new ClassFactoryClassLoader(moduleLoader);
   
  -            ClassLoader loader = module.getClassResolver().getClassLoader();
  -            ClassPath path = new LoaderClassPath(loader);
  +            result = new ModuleData();
   
  -            result.appendClassPath(path);
  +            result._pool = pool;
  +            result._loader = loader;
   
  -            _poolMap.put(id, result);
  +            _moduleDataMap.put(id, result);
           }
   
           return result;
  
  
  
  1.9       +12 -12    jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFabImpl.java
  
  Index: ClassFabImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFabImpl.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ClassFabImpl.java	30 Aug 2003 14:29:54 -0000	1.8
  +++ ClassFabImpl.java	10 Sep 2003 20:38:03 -0000	1.9
  @@ -57,7 +57,6 @@
   
   package org.apache.commons.hivemind.service.impl;
   
  -import java.io.PrintWriter;
   import java.lang.reflect.Modifier;
   
   import javassist.CannotCompileException;
  @@ -66,8 +65,6 @@
   import javassist.CtConstructor;
   import javassist.CtField;
   import javassist.CtMethod;
  -import javassist.bytecode.ClassFile;
  -import javassist.bytecode.ClassFileWriter;
   
   import org.apache.commons.hivemind.ApplicationRuntimeException;
   import org.apache.commons.hivemind.HiveMind;
  @@ -81,19 +78,21 @@
    * @author Howard Lewis Ship
    * @version $Id$
    */
  -public class ClassFabImpl implements ClassFab
  +class ClassFabImpl implements ClassFab
   {
       private ClassFactoryImpl _factory;
       private CtClass _ctClass;
       private ClassPool _pool;
  +    private ClassFactoryClassLoader _loader;
   
       private static final boolean WRITE_CLASS = Boolean.getBoolean("hivemind.write-class");
   
  -    public ClassFabImpl(ClassFactoryImpl factory, CtClass ctClass)
  +    public ClassFabImpl(ClassFactoryImpl factory, CtClass ctClass, ClassFactoryClassLoader loader)
       {
           _factory = factory;
           _ctClass = ctClass;
           _pool = ctClass.getClassPool();
  +        _loader = loader;
       }
   
       public void addInterface(Class interfaceClass)
  @@ -209,20 +208,21 @@
   
       public Class createClass()
       {
  +        String className = _ctClass.getName();
  +
           try
           {
               if (WRITE_CLASS)
                   _ctClass.writeFile();
   
  -            return _pool.writeAsClass(_ctClass.getName());
  +            byte[] bytecode = _pool.write(className);
  +
  +            return _loader.loadClass(className, bytecode);
           }
  -        catch (Exception ex)
  +        catch (Throwable ex)
           {
               throw new ApplicationRuntimeException(
  -                HiveMind.format(
  -                    "ClassFabImpl.unable-to-write-class",
  -                    _ctClass.getName(),
  -                    ex.getMessage()),
  +                HiveMind.format("ClassFabImpl.unable-to-write-class", className, ex.getMessage()),
                   ex);
           }
       }
  
  
  
  1.1                  jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFactoryClassLoader.java
  
  Index: ClassFactoryClassLoader.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.hivemind.service.impl;
  
  /**
   * ClassLoader used to properly instantiate newly created classes.
   *
   * @author Howard Lewis Ship / Essl Christian
   * @version $Id: ClassFactoryClassLoader.java,v 1.1 2003/09/10 20:38:03 hlship Exp $
   */
  class ClassFactoryClassLoader extends ClassLoader
  {
      public ClassFactoryClassLoader(ClassLoader parent)
      {
          super(parent);
      }
  
      public Class loadClass(String name, byte[] bytecodes)
      {
          Class result = defineClass(name, bytecodes, 0, bytecodes.length);
  
          resolveClass(result);
  
          return result;
      }
  }