craigmcc    01/08/04 15:26:37

  Modified:    digester/src/java/org/apache/commons/digester
                        CallMethodRule.java Digester.java
                        ObjectCreateRule.java SetNextRule.java
                        SetTopRule.java
  Log:
  Make it possible to set the class loader to be used for instantiating
  application objects.  If you call setClassLoader(), this will override
  the default behavior that is based on the useContextClassLoader property.
  
  Revision  Changes    Path
  1.4       +6 -17     
jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java
  
  Index: CallMethodRule.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CallMethodRule.java       2001/07/19 20:55:52     1.3
  +++ CallMethodRule.java       2001/08/04 22:26:37     1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java,v
 1.3 2001/07/19 20:55:52 sanders Exp $
  - * $Revision: 1.3 $
  - * $Date: 2001/07/19 20:55:52 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java,v
 1.4 2001/08/04 22:26:37 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2001/08/04 22:26:37 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
    *
    * @author Craig McClanahan
    * @author Scott Sanders
  - * @version $Revision: 1.3 $ $Date: 2001/07/19 20:55:52 $
  + * @version $Revision: 1.4 $ $Date: 2001/08/04 22:26:37 $
    */
   
   public class CallMethodRule extends Rule {
  @@ -129,19 +129,8 @@
               this.paramTypes = new Class[paramTypes.length];
               for (int i = 0; i < this.paramTypes.length; i++) {
                   try {
  -
  -                    // Check to see if the context class loader is set, and if so, 
use
  -                    // it (only if allowed to by the associated digester), as it may
  -                    // be set in server-side environments and Class.forName() may
  -                    // cause issues
  -                    ClassLoader ctxLoader =
  -                        Thread.currentThread().getContextClassLoader();
  -                    if (ctxLoader!=null && digester.getUseContextClassLoader()) {
  -                        this.paramTypes[i] = ctxLoader.loadClass(paramTypes[i]);
  -                    } else {
  -                        this.paramTypes[i] = Class.forName(paramTypes[i]);
  -                    }
  -
  +                    this.paramTypes[i] =
  +                        digester.getClassLoader().loadClass(paramTypes[i]);
                   } catch (ClassNotFoundException e) {
                       this.paramTypes[i] = null; // Will cause NPE later
                   }
  
  
  
  1.9       +59 -11    
jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java
  
  Index: Digester.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Digester.java     2001/08/04 22:04:15     1.8
  +++ Digester.java     2001/08/04 22:26:37     1.9
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
 1.8 2001/08/04 22:04:15 craigmcc Exp $
  - * $Revision: 1.8 $
  - * $Date: 2001/08/04 22:04:15 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
 1.9 2001/08/04 22:26:37 craigmcc Exp $
  + * $Revision: 1.9 $
  + * $Date: 2001/08/04 22:26:37 $
    *
    * ====================================================================
    *
  @@ -106,7 +106,7 @@
    *
    * @author Craig McClanahan
    * @author Scott Sanders
  - * @version $Revision: 1.8 $ $Date: 2001/08/04 22:04:15 $
  + * @version $Revision: 1.9 $ $Date: 2001/08/04 22:26:37 $
    */
   
   public class Digester extends DefaultHandler {
  @@ -144,13 +144,6 @@
   
   
       /**
  -     * Do we want to use the Context ClassLoader when loading classes
  -     * for instantiating new objects?  Default is <code>false</code>.
  -     */
  -    protected boolean useContextClassLoader = false;
  -
  -
  -    /**
        * The body text of the current element.
        */
       protected StringBuffer bodyText = new StringBuffer();
  @@ -163,6 +156,15 @@
   
   
       /**
  +     * The class loader to use for instantiating application objects.
  +     * If not specified, the context class loader, or the class loader
  +     * used to load Digester itself, is used, based on the value of the
  +     * <code>useContextClassLoader</code> variable.
  +     */
  +    protected ClassLoader classLoader = null;
  +
  +
  +    /**
        * The debugging detail level of this component.
        */
       protected int debug = 0;
  @@ -235,6 +237,13 @@
   
   
       /**
  +     * Do we want to use the Context ClassLoader when loading classes
  +     * for instantiating new objects?  Default is <code>false</code>.
  +     */
  +    protected boolean useContextClassLoader = false;
  +
  +
  +    /**
        * Do we want to use a validating parser?
        */
       protected boolean validating = false;
  @@ -248,6 +257,45 @@
   
   
       // ----------------------------------------------------------- Properties
  +
  +
  +    /**
  +     * Return the class loader to be used for instantiating application objects
  +     * when required.  This is determined based upon the following rules:
  +     * <ul>
  +     * <li>The class loader set by <code>setClassLoader()</code>, if any</li>
  +     * <li>The thread context class loader, if it exists and the
  +     *     <code>useContextClassLoader</code> property is set to true</li>
  +     * <li>The class loader used to load the Digester class itself.
  +     * </ul>
  +     */
  +    public ClassLoader getClassLoader() {
  +
  +        if (this.classLoader != null)
  +            return (this.classLoader);
  +        if (this.useContextClassLoader) {
  +            ClassLoader classLoader =
  +                Thread.currentThread().getContextClassLoader();
  +            if (classLoader != null)
  +                return (classLoader);
  +        }
  +        return (this.getClass().getClassLoader());
  +
  +    }
  +
  +
  +    /**
  +     * Set the class loader to be used for instantiating application objects
  +     * when required.
  +     *
  +     * @param classLoader The new class loader to use, or <code>null</code>
  +     *  to revert to the standard rules
  +     */
  +    public void setClassLoader(ClassLoader classLoader) {
  +
  +        this.classLoader = classLoader;
  +
  +    }
   
   
       /**
  
  
  
  1.4       +6 -18     
jakarta-commons/digester/src/java/org/apache/commons/digester/ObjectCreateRule.java
  
  Index: ObjectCreateRule.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/ObjectCreateRule.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ObjectCreateRule.java     2001/07/19 20:55:52     1.3
  +++ ObjectCreateRule.java     2001/08/04 22:26:37     1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/ObjectCreateRule.java,v
 1.3 2001/07/19 20:55:52 sanders Exp $
  - * $Revision: 1.3 $
  - * $Date: 2001/07/19 20:55:52 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/ObjectCreateRule.java,v
 1.4 2001/08/04 22:26:37 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2001/08/04 22:26:37 $
    *
    * ====================================================================
    *
  @@ -74,7 +74,7 @@
    *
    * @author Craig McClanahan
    * @author Scott Sanders
  - * @version $Revision: 1.3 $ $Date: 2001/07/19 20:55:52 $
  + * @version $Revision: 1.4 $ $Date: 2001/08/04 22:26:37 $
    */
   
   public class ObjectCreateRule extends Rule {
  @@ -151,20 +151,8 @@
            digester.log("New " + realClassName);
   
        // Instantiate the new object and push it on the context stack
  -    Class clazz = null;
  -
  -    // Check to see if the context class loader is set, and if so, use
  -    // it (only if allowed to by the associated digester), as it may
  -    // be set in server-side environments and Class.forName() may
  -    // cause issues
  -    ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
  -    if (ctxLoader!=null && digester.getUseContextClassLoader()) {
  -        clazz = ctxLoader.loadClass(realClassName);
  -    } else {
  -        clazz = Class.forName(realClassName);
  -    }
  -     
  -    Object instance = clazz.newInstance();
  +        Class clazz = digester.getClassLoader().loadClass(realClassName);
  +        Object instance = clazz.newInstance();
        digester.push(instance);
   
       }
  
  
  
  1.4       +8 -21     
jakarta-commons/digester/src/java/org/apache/commons/digester/SetNextRule.java
  
  Index: SetNextRule.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetNextRule.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SetNextRule.java  2001/07/19 20:55:52     1.3
  +++ SetNextRule.java  2001/08/04 22:26:37     1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetNextRule.java,v
 1.3 2001/07/19 20:55:52 sanders Exp $
  - * $Revision: 1.3 $
  - * $Date: 2001/07/19 20:55:52 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetNextRule.java,v
 1.4 2001/08/04 22:26:37 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2001/08/04 22:26:37 $
    *
    * ====================================================================
    *
  @@ -75,7 +75,7 @@
    *
    * @author Craig McClanahan
    * @author Scott Sanders
  - * @version $Revision: 1.3 $ $Date: 2001/07/19 20:55:52 $
  + * @version $Revision: 1.4 $ $Date: 2001/08/04 22:26:37 $
    */
   
   public class SetNextRule extends Rule {
  @@ -151,24 +151,11 @@
   
        // Call the specified method
        Class paramTypes[] = new Class[1];
  -     if (paramType != null) {
  -
  -        // Check to see if the context class loader is set, and if so, use
  -        // it (only if allowed to by the associated digester), as it may
  -        // be set in server-side environments and Class.forName() may
  -        // cause issues
  -        ClassLoader ctxLoader =
  -            Thread.currentThread().getContextClassLoader();
  -        if (ctxLoader!=null && digester.getUseContextClassLoader()) {
  -            paramTypes[0] = ctxLoader.loadClass(paramType);
  -        } else {
  -            paramTypes[0] = Class.forName(paramType);
  -        }
  -
  -     } else {
  +     if (paramType != null)
  +            paramTypes[0] =
  +                digester.getClassLoader().loadClass(paramType);
  +        else
            paramTypes[0] = child.getClass();
  -    }
  -
        Method method = parent.getClass().getMethod(methodName, paramTypes);
        method.invoke(parent, new Object[] { child });
   
  
  
  
  1.4       +9 -22     
jakarta-commons/digester/src/java/org/apache/commons/digester/SetTopRule.java
  
  Index: SetTopRule.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetTopRule.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SetTopRule.java   2001/07/19 20:55:52     1.3
  +++ SetTopRule.java   2001/08/04 22:26:37     1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetTopRule.java,v
 1.3 2001/07/19 20:55:52 sanders Exp $
  - * $Revision: 1.3 $
  - * $Date: 2001/07/19 20:55:52 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetTopRule.java,v
 1.4 2001/08/04 22:26:37 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2001/08/04 22:26:37 $
    *
    * ====================================================================
    *
  @@ -74,7 +74,7 @@
    *
    * @author Craig McClanahan
    * @author Scott Sanders
  - * @version $Revision: 1.3 $ $Date: 2001/07/19 20:55:52 $
  + * @version $Revision: 1.4 $ $Date: 2001/08/04 22:26:37 $
    */
   
   public class SetTopRule extends Rule {
  @@ -150,24 +150,11 @@
   
        // Call the specified method
        Class paramTypes[] = new Class[1];
  -     if (paramType != null) {
  -
  -        // Check to see if the context class loader is set, and if so, use
  -        // it (only if allowed to by the associated digester), as it may
  -        // be set in server-side environments and Class.forName() may
  -        // cause issues
  -        ClassLoader ctxLoader =
  -            Thread.currentThread().getContextClassLoader();
  -        if (ctxLoader!=null && digester.getUseContextClassLoader()) {
  -            paramTypes[0] = ctxLoader.loadClass(paramType);
  -        } else {
  -            paramTypes[0] = Class.forName(paramType);
  -        }
  -
  -    } else {
  -         paramTypes[0] = child.getClass();
  -    }
  -
  +     if (paramType != null)
  +            paramTypes[0] =
  +                digester.getClassLoader().loadClass(paramType);
  +        else
  +            paramTypes[0] = child.getClass();
        Method method = parent.getClass().getMethod(methodName, paramTypes);
        method.invoke(parent, new Object[] { child });
   
  
  
  

Reply via email to