jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r440026 - /jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
Date Mon, 04 Sep 2006 11:01:30 GMT
Author: fmeschbe
Date: Mon Sep  4 04:01:27 2006
New Revision: 440026

URL: http://svn.apache.org/viewvc?view=rev&rev=440026
Log:
JCR-561 Add support to provide custom classloader for class instantiation from configuration

Modified:
    jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java

Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java?view=diff&rev=440026&r1=440025&r2=440026
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
(original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/BeanConfig.java
Mon Sep  4 04:01:27 2006
@@ -28,6 +28,16 @@
  */
 public class BeanConfig {
 
+    /** The default class loader used by all instances of this class */
+    private static ClassLoader DEFAULT_CLASS_LOADER =
+        BeanConfig.class.getClassLoader();
+    
+    /**
+     * The current class loader used by this instance to create instances of
+     * configured classes.
+     */
+    private ClassLoader classLoader = getDefaultClassLoader();
+    
     /**
      * The class name of the configured bean.
      */
@@ -87,7 +97,9 @@
      */
     public Object newInstance() throws ConfigurationException {
         try {
-            Object object = Class.forName(getClassName()).newInstance();
+            Class objectClass =
+                Class.forName(getClassName(), true, getClassLoader());
+            Object object = objectClass.newInstance();
             BeanMap map = new BeanMap(object);
             Iterator iterator = map.keyIterator();
             while (iterator.hasNext()) {
@@ -113,4 +125,66 @@
         }
     }
 
+    //---------- Configurable class loader support ----------------------------
+    
+    /**
+     * Returns the current <code>ClassLoader</code> used to instantiate objects
+     * in the {@link #newInstance()} method.
+     * 
+     * @see #newInstance()
+     * @see #setClassLoader(ClassLoader)
+     * @see #getDefaultClassLoader()
+     * @see #setDefaultClassLoader(ClassLoader)
+     */
+    public ClassLoader getClassLoader() {
+        return classLoader;
+    }
+    
+    /**
+     * Sets the <code>ClassLoader</code> used to instantiate objects in the
+     * {@link #newInstance()} method.
+     * 
+     * @param classLoader The class loader to set on this instance. If this is
+     *      <code>null</code> the system class loader will be used, which may
+     *      lead to unexpected class loading failures.
+     *      
+     * @see #newInstance()
+     * @see #getClassLoader()
+     * @see #getDefaultClassLoader()
+     * @see #setDefaultClassLoader(ClassLoader)
+     */
+    public void setClassLoader(ClassLoader classLoader) {
+        this.classLoader = classLoader;
+    }
+    
+    /**
+     * Returns the current <code>ClassLoader</code> used for new instances of
+     * this class as the loader used to instantiate objects in the
+     * {@link #newInstance()} method.
+     * 
+     * @see #newInstance()
+     * @see #getClassLoader()
+     * @see #setClassLoader(ClassLoader)
+     * @see #setDefaultClassLoader(ClassLoader)
+     */
+    public static ClassLoader getDefaultClassLoader() {
+        return DEFAULT_CLASS_LOADER;
+    }
+    
+    /**
+     * Sets the <code>ClassLoader</code> used for new instances of this class
as
+     * the loader to instantiate objects in the {@link #newInstance()} method.
+     * 
+     * @param classLoader The class loader to set as the default class loader.
+     *      If this is <code>null</code> the system class loader will be used,
+     *      which may lead to unexpected class loading failures. 
+     *      
+     * @see #newInstance()
+     * @see #getClassLoader()
+     * @see #setClassLoader(ClassLoader)
+     * @see #getDefaultClassLoader()
+     */
+    public static void setDefaultClassLoader(ClassLoader classLoader) {
+        DEFAULT_CLASS_LOADER = classLoader;
+    }
 }



Mime
View raw message