Return-Path: Delivered-To: apmail-db-ojb-dev-archive@www.apache.org Received: (qmail 42853 invoked from network); 1 Jun 2004 19:15:30 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 1 Jun 2004 19:15:30 -0000 Received: (qmail 67424 invoked by uid 500); 1 Jun 2004 19:15:42 -0000 Delivered-To: apmail-db-ojb-dev-archive@db.apache.org Received: (qmail 67385 invoked by uid 500); 1 Jun 2004 19:15:41 -0000 Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "OJB Developers List" Reply-To: "OJB Developers List" Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 67370 invoked by uid 500); 1 Jun 2004 19:15:41 -0000 Received: (qmail 67351 invoked by uid 99); 1 Jun 2004 19:15:41 -0000 Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Tue, 01 Jun 2004 12:15:41 -0700 Received: (qmail 42802 invoked by uid 1797); 1 Jun 2004 19:15:24 -0000 Date: 1 Jun 2004 19:15:24 -0000 Message-ID: <20040601191524.42801.qmail@minotaur.apache.org> From: tomdz@apache.org To: db-ojb-cvs@apache.org Subject: cvs commit: db-ojb/src/java/org/apache/ojb/broker/util/interceptor InterceptorFactory.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N tomdz 2004/06/01 12:15:24 Modified: src/java/org/apache/ojb/broker/util IdentityMapFactory.java ClassHelper.java src/java/org/apache/ojb/odmg/oql OQLLexerTokenTypes.java oql-ojb.g OQLLexer.java OQLParser.java src/java/org/apache/ojb/jdo/jdoql QueryTreeResolver.java src/java/org/apache/ojb/broker/metadata RepositoryPersistor.java src/test/org/apache/ojb/broker ExtentAwarePathExpressionsTest.java src/java/org/apache/ojb/broker/util/interceptor InterceptorFactory.java Log: Integrated patch submitted by David Le Strat concerning the ability to set the class loader that is used by OJB Added some javadocs to the ClassHelper class Replaced usages of the context class loader by accesses to ClassHelper Revision Changes Path 1.4 +4 -3 db-ojb/src/java/org/apache/ojb/broker/util/IdentityMapFactory.java Index: IdentityMapFactory.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/IdentityMapFactory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- IdentityMapFactory.java 4 Apr 2004 23:53:37 -0000 1.3 +++ IdentityMapFactory.java 1 Jun 2004 19:15:23 -0000 1.4 @@ -1,7 +1,5 @@ package org.apache.ojb.broker.util; -import java.util.Map; - /* Copyright 2003-2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import java.util.Map; + public final class IdentityMapFactory { private static boolean HAS_JDK_IDENTITY_MAP = true; @@ -26,7 +27,7 @@ { try { - JDK_IDENTITY_MAP = Thread.currentThread().getContextClassLoader().loadClass(CLASS_NAME); + JDK_IDENTITY_MAP = ClassHelper.getClassLoader().loadClass(CLASS_NAME); } catch (ClassNotFoundException e) { 1.10 +206 -93 db-ojb/src/java/org/apache/ojb/broker/util/ClassHelper.java Index: ClassHelper.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/ClassHelper.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- ClassHelper.java 22 May 2004 09:51:25 -0000 1.9 +++ ClassHelper.java 1 Jun 2004 19:15:23 -0000 1.10 @@ -20,6 +20,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; +import java.net.URL; import org.apache.ojb.broker.OJBRuntimeException; import org.apache.ojb.broker.PersistenceBrokerException; @@ -27,56 +28,115 @@ import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; /** - * Helper class with static methods for class, method, field handling. + * Helper class with static methods for java class, method, and field handling. * * @version $Id$ */ public class ClassHelper { + /** Arguments for invoking a default or no-arg constructor */ private static final Object[] NO_ARGS = {}; + /** Parameter types of a default/no-arg constructor */ private static final Class[] NO_ARGS_CLASS = {}; + /** The class loader currently used by OJB */ + private static ClassLoader _classLoader = null; + /** A mutex for changing the class loader */ + private static Object _mutex = new Object(); + + /** + * Prevents instatiation. + */ private ClassHelper() { } /** - * Returns a class object for the given name or null + * Sets the classloader to be used by OJB. This can be set by external + * application that need to pass a specific classloader to OJB. + * + * @param loader The class loader. If null then OJB will use + * the class loader of the current thread + */ + public static void setClassLoader(ClassLoader loader) + { + synchronized (_mutex) + { + _classLoader = loader; + } + } + + /** + * Returns the class loader currently used by OJB. Defaults to the class loader of + * the current thread (Thread.currentThread().getContextClassLoader()) + * if not set differently. + * + * @return The classloader used by OJB + */ + public static ClassLoader getClassLoader() + { + return _classLoader == null ? Thread.currentThread().getContextClassLoader() : _classLoader; + } + + /** + * Determines the url of the indicated resource using the currently set class loader. + * + * @param name The resource name + * @return The resource's url + */ + public static URL getResource(String name) + { + return getClassLoader().getResource(name); + } + + /** + * Retrieves the class object for the given qualified class name. + * + * @param className The qualified name of the class + * @param initialize Whether the class shall be initialized + * @return The class object */ public static Class getClass(String className, boolean initialize) throws ClassNotFoundException { - return Class.forName(className, initialize, Thread.currentThread().getContextClassLoader()); + return Class.forName(className, initialize, getClassLoader()); } /** - * Returns new instance of the given class, using the default constructor. + * Returns a new instance of the given class, using the default or a no-arg constructor. + * + * @param target The class to instantiate + * @return The instance */ public static Object newInstance(Class target) throws InstantiationException, - IllegalAccessException + IllegalAccessException { return target.newInstance(); } /** - * Returns new instance of the given class, using the default constructor - if argument - * makeAccessible is set 'true' Java language access - * checking will be suppressed.. + * Returns a new instance of the given class, using the default or a no-arg constructor. + * This method can also use private no-arg constructors if makeAccessible + * is set to true (and there are no other security constraints). + * + * @param target The class to instantiate + * @param makeAccessible If the constructor shall be made accessible prior to using it + * @return The instance */ public static Object newInstance(Class target, boolean makeAccessible) throws InstantiationException, - IllegalAccessException + IllegalAccessException { - if(makeAccessible) + if (makeAccessible) { try { return newInstance(target, NO_ARGS_CLASS, NO_ARGS, makeAccessible); } - catch(InvocationTargetException e) + catch (InvocationTargetException e) { throw new OJBRuntimeException("Unexpected exception while instantiate class '" + target + "' with default constructor", e); } - catch(NoSuchMethodException e) + catch (NoSuchMethodException e) { throw new OJBRuntimeException("Unexpected exception while instantiate class '" + target + "' with default constructor", e); @@ -89,31 +149,49 @@ } /** - * Returns new instance of the given class, using specified constructor. - */ - public static Object newInstance(Class target, Class[] types, Object[] args) throws - InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchMethodException, SecurityException + * Returns a new instance of the given class, using the constructor with the specified parameter types. + * + * @param target The class to instantiate + * @param types The parameter types + * @param args The arguments + * @return The instance + */ + public static Object newInstance(Class target, Class[] types, Object[] args) throws InstantiationException, + IllegalAccessException, + IllegalArgumentException, + InvocationTargetException, + NoSuchMethodException, + SecurityException { return newInstance(target, types, args, false); } /** - * Returns new instance of the given class, using specified constructor - if argument - * makeAccessible is set 'true' Java language access - * checking will be suppressed. - */ - public static Object newInstance(Class target, Class[] types, Object[] args, boolean makeAccessible) throws - InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchMethodException, SecurityException + * Returns a new instance of the given class, using the constructor with the specified parameter types. + * This method can also use private constructors if makeAccessible is set to + * true (and there are no other security constraints). + * + * @param target The class to instantiate + * @param types The parameter types + * @param args The arguments + * @param makeAccessible If the constructor shall be made accessible prior to using it + * @return The instance + */ + public static Object newInstance(Class target, Class[] types, Object[] args, boolean makeAccessible) throws InstantiationException, + IllegalAccessException, + IllegalArgumentException, + InvocationTargetException, + NoSuchMethodException, + SecurityException { Constructor con; - if(makeAccessible) + + if (makeAccessible) { con = target.getDeclaredConstructor(types); - if(makeAccessible) + if (makeAccessible && !con.isAccessible()) { - if(!con.isAccessible()) con.setAccessible(true); + con.setAccessible(true); } } else @@ -124,46 +202,40 @@ } /** - * Returns a method via reflection look-up of the specific signature. - * @param clazz method's java class - * @param methodName method name - * @param params method signature - * @return method invokable via java.lang.reflect.Method#invoke, - * or null if no matching method can be found + * Determines the method with the specified signature via reflection look-up. + * + * @param clazz The java class to search in + * @param methodName The method's name + * @param params The parameter types + * @return The method object or null if no matching method was found */ public static Method getMethod(Class clazz, String methodName, Class[] params) { - Method method; try { - method = clazz.getMethod(methodName, params); + return clazz.getMethod(methodName, params); } - catch(Exception ignore) - { - method = null; - } - return method; + catch (Exception ignored) + {} + return null; } /** - * Returns a field via reflection look-up. - * @param clazz fields's java class - * @param fieldName field name - * @return field retrievable via java.lang.reflect.Field#getXXX, - * or null if no matching field can be found + * Determines the field via reflection look-up. + * + * @param clazz The java class to search in + * @param fieldName The field's name + * @return The field object or null if no matching field was found */ public static Field getField(Class clazz, String fieldName) { - Field field; try { - field = clazz.getField(fieldName); - } - catch(Exception ignore) - { - field = null; + return clazz.getField(fieldName); } - return field; + catch (Exception ignored) + {} + return null; } @@ -172,8 +244,10 @@ // ******************************************************************* /** - * Convenience for - * {@link #getClass(String, boolean) getClass(name, true)} + * Convenience method for {@link #getClass(String, boolean) getClass(name, true)} + * + * @param name The qualified class name + * @return The class object */ public static Class getClass(String name) throws ClassNotFoundException { @@ -182,45 +256,83 @@ /** - * Returns new instance of the given class name, using the default constructor. + * Returns a new instance of the class with the given qualified name using the default or + * or a no-arg constructor. + * + * @param className The qualified name of the class to instantiate */ public static Object newInstance(String className) throws InstantiationException, - IllegalAccessException, ClassNotFoundException + IllegalAccessException, + ClassNotFoundException { return newInstance(getClass(className)); } /** - * Returns new instance of the given class name, using the default constructor. - */ - public static Object newInstance(String className, Class[] types, Object[] args) throws - InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchMethodException, SecurityException, ClassNotFoundException + * Returns a new instance of the class with the given qualified name using the constructor with + * the specified signature. + * + * @param className The qualified name of the class to instantiate + * @param types The parameter types + * @param args The arguments + * @return The instance + */ + public static Object newInstance(String className, Class[] types, Object[] args) throws InstantiationException, + IllegalAccessException, + IllegalArgumentException, + InvocationTargetException, + NoSuchMethodException, + SecurityException, + ClassNotFoundException { return newInstance(getClass(className), types, args); } - public static Object newInstance(Class target, Class type, Object arg) throws - InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchMethodException, SecurityException - { - return newInstance(target, new Class[]{type}, new Object[]{arg}); + /** + * Returns a new instance of the given class using the constructor with the specified parameter. + * + * @param target The class to instantiate + * @param type The types of the single parameter of the constructor + * @param arg The argument + * @return The instance + */ + public static Object newInstance(Class target, Class type, Object arg) throws InstantiationException, + IllegalAccessException, + IllegalArgumentException, + InvocationTargetException, + NoSuchMethodException, + SecurityException + { + return newInstance(target, new Class[]{ type }, new Object[]{ arg }); } - public static Object newInstance(String className, Class type, Object arg) throws - InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchMethodException, SecurityException, ClassNotFoundException + /** + * Returns a new instance of the class with the given qualified name using the constructor with + * the specified parameter. + * + * @param className The qualified name of the class to instantiate + * @param type The types of the single parameter of the constructor + * @param arg The argument + * @return The instance + */ + public static Object newInstance(String className, Class type, Object arg) throws InstantiationException, + IllegalAccessException, + IllegalArgumentException, + InvocationTargetException, + NoSuchMethodException, + SecurityException, + ClassNotFoundException { return newInstance(className, new Class[]{type}, new Object[]{arg}); } /** - * Returns a method via reflection look-up of the specific signature. - * @param object runtime object instance - * @param methodName method name - * @param params method signature - * @return method invokable via java.lang.reflect.Method#invoke, - * or null if no matching method can be found + * Determines the method with the specified signature via reflection look-up. + * + * @param object The instance whose class is searched for the method + * @param methodName The method's name + * @param params The parameter types + * @return A method object or null if no matching method was found */ public static Method getMethod(Object object, String methodName, Class[] params) { @@ -228,45 +340,46 @@ } /** - * Returns a method via reflection look-up of the specific signature. - * @param className class name - * @param methodName method name - * @param params method signature - * @return method invokable via java.lang.reflect.Method#invoke, - * or null if no matching method can be found + * Determines the method with the specified signature via reflection look-up. + * + * @param className The qualified name of the searched class + * @param methodName The method's name + * @param params The parameter types + * @return A method object or null if no matching method was found */ public static Method getMethod(String className, String methodName, Class[] params) { - Method method = null; try { - Class clazz = getClass(className, false); - method = getMethod(clazz, methodName, params); + return getMethod(getClass(className, false), methodName, params); } - catch(Exception ignore) - { - //ignore it - } - return method; + catch (Exception ignored) + {} + return null; } /** - * Builds a new object instance based on the given {@link org.apache.ojb.broker.metadata.ClassDescriptor}. + * Builds a new instance for the class represented by the given class descriptor. + * + * @param cld The class descriptor + * @return The instance */ public static Object buildNewObjectInstance(ClassDescriptor cld) { Object result = null; + // If either the factory class and/or factory method is null, // just follow the normal code path and create via constructor - if (cld.getFactoryClass() == null || cld.getFactoryMethod() == null) + if ((cld.getFactoryClass() == null) || (cld.getFactoryMethod() == null)) { try { // 1. create an empty Object (persistent classes need a public default constructor) Constructor con = cld.getZeroArgumentConstructor(); + result = ConstructorHelper.instantiate(con); } - catch(InstantiationException e) + catch (InstantiationException e) { throw new ClassNotPersistenceCapableException( "Can't instantiate class '" + cld.getClassNameOfObject()+"'"); 1.14 +1 -0 db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexerTokenTypes.java Index: OQLLexerTokenTypes.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexerTokenTypes.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- OQLLexerTokenTypes.java 9 Apr 2004 13:22:29 -0000 1.13 +++ OQLLexerTokenTypes.java 1 Jun 2004 19:15:23 -0000 1.14 @@ -23,6 +23,7 @@ import org.apache.ojb.broker.query.*; import org.apache.ojb.broker.metadata.*; import org.apache.ojb.broker.*; +import org.apache.ojb.broker.util.ClassHelper; import java.util.*; public interface OQLLexerTokenTypes { 1.23 +2 -2 db-ojb/src/java/org/apache/ojb/odmg/oql/oql-ojb.g Index: oql-ojb.g =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/oql-ojb.g,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- oql-ojb.g 8 Jan 2004 08:16:05 -0000 1.22 +++ oql-ojb.g 1 Jun 2004 19:15:23 -0000 1.23 @@ -81,6 +81,7 @@ import org.apache.ojb.broker.query.*; import org.apache.ojb.broker.metadata.*; import org.apache.ojb.broker.*; +import org.apache.ojb.broker.util.ClassHelper; import java.util.*; } @@ -428,8 +429,7 @@ id:Identifier { try { - clazz = Class.forName(id.getText(), true, - Thread.currentThread().getContextClassLoader()); + clazz = ClassHelper.getClass(id.getText()); } catch (Exception e) { } } 1.19 +1 -0 db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexer.java Index: OQLLexer.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/OQLLexer.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- OQLLexer.java 9 Apr 2004 13:22:29 -0000 1.18 +++ OQLLexer.java 1 Jun 2004 19:15:23 -0000 1.19 @@ -23,6 +23,7 @@ import org.apache.ojb.broker.query.*; import org.apache.ojb.broker.metadata.*; import org.apache.ojb.broker.*; +import org.apache.ojb.broker.util.ClassHelper; import java.util.*; import java.io.InputStream; 1.26 +2 -2 db-ojb/src/java/org/apache/ojb/odmg/oql/OQLParser.java Index: OQLParser.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/oql/OQLParser.java,v retrieving revision 1.25 retrieving revision 1.26 diff -u -r1.25 -r1.26 --- OQLParser.java 9 Apr 2004 13:22:29 -0000 1.25 +++ OQLParser.java 1 Jun 2004 19:15:23 -0000 1.26 @@ -23,6 +23,7 @@ import org.apache.ojb.broker.query.*; import org.apache.ojb.broker.metadata.*; import org.apache.ojb.broker.*; +import org.apache.ojb.broker.util.ClassHelper; import java.util.*; import antlr.TokenBuffer; @@ -295,8 +296,7 @@ match(Identifier); try { - clazz = Class.forName(id.getText(), true, - Thread.currentThread().getContextClassLoader()); + clazz = ClassHelper.getClass(id.getText()); } catch (Exception e) { } 1.3 +2 -1 db-ojb/src/java/org/apache/ojb/jdo/jdoql/QueryTreeResolver.java Index: QueryTreeResolver.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/jdo/jdoql/QueryTreeResolver.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- QueryTreeResolver.java 23 May 2004 12:02:29 -0000 1.2 +++ QueryTreeResolver.java 1 Jun 2004 19:15:23 -0000 1.3 @@ -26,6 +26,7 @@ import javax.jdo.JDOUserException; import org.apache.ojb.broker.metadata.*; +import org.apache.ojb.broker.util.ClassHelper; import org.apache.ojb.jdo.QueryImpl; /** @@ -471,7 +472,7 @@ */ public void visit(Type type) throws JDOUserException { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); + ClassLoader loader = ClassHelper.getClassLoader(); Class result = null; String name = type.getName(); int pos = name.indexOf('.'); 1.24 +3 -2 db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java Index: RepositoryPersistor.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- RepositoryPersistor.java 4 Apr 2004 23:53:34 -0000 1.23 +++ RepositoryPersistor.java 1 Jun 2004 19:15:23 -0000 1.24 @@ -17,6 +17,7 @@ import org.apache.commons.lang.SerializationUtils; import org.apache.commons.lang.SystemUtils; +import org.apache.ojb.broker.util.ClassHelper; import org.apache.ojb.broker.util.configuration.Configurable; import org.apache.ojb.broker.util.configuration.Configuration; import org.apache.ojb.broker.util.configuration.ConfigurationException; @@ -330,7 +331,7 @@ private URL buildURL(String repositoryFileName) throws MalformedURLException { //j2ee compliant lookup of resources - URL url = Thread.currentThread().getContextClassLoader().getResource(repositoryFileName); + URL url = ClassHelper.getResource(repositoryFileName); // don't be too strict: if resource is not on the classpath, try ordinary file lookup if (url == null) 1.7 +3 -2 db-ojb/src/test/org/apache/ojb/broker/ExtentAwarePathExpressionsTest.java Index: ExtentAwarePathExpressionsTest.java =================================================================== RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/ExtentAwarePathExpressionsTest.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- ExtentAwarePathExpressionsTest.java 10 Jul 2003 22:05:18 -0000 1.6 +++ ExtentAwarePathExpressionsTest.java 1 Jun 2004 19:15:24 -0000 1.7 @@ -10,6 +10,7 @@ import org.apache.ojb.broker.query.Criteria; import org.apache.ojb.broker.query.QueryByCriteria; +import org.apache.ojb.broker.util.ClassHelper; /** * Tests for extent aware path expressions @@ -39,7 +40,7 @@ { //OBS: when running this test from eclipse, one must copy the database file to the directory where //u checked out ojb from cvs - URL url = Thread.currentThread().getContextClassLoader().getResource("DB.script"); + URL url = ClassHelper.getResource("DB.script"); System.out.println("Hsql database @ :" + url); String[] arr = {ExtentAwarePathExpressionsTest.class.getName()}; junit.textui.TestRunner.main(arr); 1.10 +1 -1 db-ojb/src/java/org/apache/ojb/broker/util/interceptor/InterceptorFactory.java Index: InterceptorFactory.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/interceptor/InterceptorFactory.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- InterceptorFactory.java 4 Apr 2004 23:53:37 -0000 1.9 +++ InterceptorFactory.java 1 Jun 2004 19:15:24 -0000 1.10 @@ -87,7 +87,7 @@ Class[] interfaces = computeInterfaceArrayFor(instanceToIntercept.getClass()); Object result = Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), + ClassHelper.getClassLoader(), interfaces, handler); return result; --------------------------------------------------------------------- To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org For additional commands, e-mail: ojb-dev-help@db.apache.org