Return-Path: Delivered-To: apmail-incubator-abdera-commits-archive@locus.apache.org Received: (qmail 18627 invoked from network); 27 May 2008 22:03:29 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 27 May 2008 22:03:29 -0000 Received: (qmail 47228 invoked by uid 500); 27 May 2008 22:03:31 -0000 Delivered-To: apmail-incubator-abdera-commits-archive@incubator.apache.org Received: (qmail 47213 invoked by uid 500); 27 May 2008 22:03:31 -0000 Mailing-List: contact abdera-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: abdera-dev@incubator.apache.org Delivered-To: mailing list abdera-commits@incubator.apache.org Received: (qmail 47204 invoked by uid 99); 27 May 2008 22:03:31 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 May 2008 15:03:31 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 May 2008 22:02:44 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 6BF6C238898E; Tue, 27 May 2008 15:03:05 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r660741 - in /incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util: AbderaConfiguration.java ServiceUtil.java Date: Tue, 27 May 2008 22:03:05 -0000 To: abdera-commits@incubator.apache.org From: jmsnell@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080527220305.6BF6C238898E@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jmsnell Date: Tue May 27 15:03:04 2008 New Revision: 660741 URL: http://svn.apache.org/viewvc?rev=660741&view=rev Log: Adopt the classloader handling code from CFX as suggested by Dan. Since the CFX code just works, I adopted it outright with a couple of minor modifications. Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java?rev=660741&r1=660740&r2=660741&view=diff ============================================================================== --- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java (original) +++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java Tue May 27 15:03:04 2008 @@ -68,7 +68,6 @@ } private static ResourceBundle getBundle( - ClassLoader loader, Locale locale) { ResourceBundle bundle = null; try { @@ -76,7 +75,7 @@ ResourceBundle.getBundle( "abdera", locale, - loader); + Thread.currentThread().getContextClassLoader()); } catch (Exception e) { // Do nothing } @@ -95,8 +94,7 @@ protected AbderaConfiguration(ResourceBundle bundle) { this.bundle = (bundle != null) ? bundle : - AbderaConfiguration.getBundle( - ServiceUtil.getClassLoader(), + AbderaConfiguration.getBundle( Locale.getDefault()); factories = ServiceUtil.loadExtensionFactories(); writers = initNamedWriters(); Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java?rev=660741&r1=660740&r2=660741&view=diff ============================================================================== --- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java (original) +++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java Tue May 27 15:03:04 2008 @@ -44,8 +44,6 @@ public final class ServiceUtil implements Constants { - private static ClassLoader classLoader = null; - ServiceUtil() {} /** @@ -130,26 +128,13 @@ abdera); } - /** - * Get the context class loader for this thread - */ - public static ClassLoader getClassLoader() { - if (classLoader == null) - classLoader = Thread.currentThread().getContextClassLoader(); - return classLoader; - } - - public static void setClassLoader(ClassLoader classLoader) { - ServiceUtil.classLoader = classLoader; - } - public static Object locate( String id, String _default, Abdera abdera) { Object object = locate(id, abdera); if (object == null && _default != null) { - object = locateInstance(getClassLoader(), _default, abdera); + object = locateInstance(_default, abdera); } return object; } @@ -161,7 +146,7 @@ Object... args) { Object object = locate(id, abdera); if (object == null && _default != null) { - object = locateInstance(getClassLoader(), _default, abdera, args); + object = locateInstance(_default, abdera, args); } return object; } @@ -226,14 +211,14 @@ return null; } - public static Object locateInstance(ClassLoader loader, String id, Abdera abdera) { - return locateInstance(loader,id,abdera,false); + public static Object locateInstance(String id, Abdera abdera) { + return locateInstance(id,abdera,false); } @SuppressWarnings("unchecked") - public static Object locateInstance(ClassLoader loader, String id, Abdera abdera, boolean classesonly) { + public static Object locateInstance(String id, Abdera abdera, boolean classesonly) { try { - Class _class = loader.loadClass(id); + Class _class = loadClass(id, ServiceUtil.class); return classesonly ? _class : _create(_class, abdera); } catch (Exception e) { // Nothing @@ -248,9 +233,9 @@ } @SuppressWarnings("unchecked") - public static Object locateInstance(ClassLoader loader, String id, Abdera abdera, Object... args) { + public static Object locateInstance(String id, Abdera abdera, Object... args) { try { - Class _class = loader.loadClass(id); + Class _class = loadClass(id, ServiceUtil.class); return _create(_class, abdera, args); } catch (Exception e) { // Nothing @@ -264,14 +249,14 @@ return null; } - public static InputStream locateStream(ClassLoader loader, String id) { - InputStream in = loader.getResourceAsStream(id); + public static InputStream locateStream(String id) { + InputStream in = getResourceAsStream(id, ServiceUtil.class); return (in != null) ? in : ClassLoader.getSystemResourceAsStream(id); } - public static Enumeration locateResources(ClassLoader loader, String id) { + public static Enumeration locateResources(String id) { try { - return loader.getResources(id); + return getResources(id,ServiceUtil.class); } catch (Exception e) { // Nothing } @@ -286,22 +271,21 @@ @SuppressWarnings("unchecked") private static Object checkAbderaConfiguration(String id, Abdera abdera) { String s = abdera.getConfiguration().getConfigurationOption(id); - return (s != null) ? locateInstance(getClassLoader(), id, abdera) : null; + return (s != null) ? locateInstance(id, abdera) : null; } private static Object checkMetaInfServices(String id, Abdera abdera) { Object object = null; String sid = "META-INF/services/" + id; - ClassLoader loader = getClassLoader(); BufferedReader buf = null; try { - InputStream is = locateStream(loader,sid); + InputStream is = locateStream(sid); if (is != null) { buf = new BufferedReader(new InputStreamReader(is)); String line = buf.readLine(); if (line != null) { String s = line.split("#",2)[0].trim(); - object = locateInstance(loader,s, abdera); + object = locateInstance(s, abdera); } } } catch (Exception e) { @@ -336,9 +320,8 @@ @SuppressWarnings("unchecked") protected static List _loadimpls(String sid, boolean classesonly) { List impls = Collections.synchronizedList(new ArrayList()); - ClassLoader loader = getClassLoader(); try { - Enumeration e = locateResources(loader,sid); + Enumeration e = locateResources(sid); for (;e.hasMoreElements();) { BufferedReader buf = null; try { @@ -350,7 +333,7 @@ while ((line = buf.readLine()) != null) { String s = line.split("#",2)[0].trim(); if (!"".equals(s)) { - T impl = (T) locateInstance(loader,s, null); + T impl = (T) locateInstance(s, null); if (impl != null) impls.add(impl); } @@ -379,4 +362,166 @@ protected static List _loadimpls(String sid) { return _loadimpls(sid,false); } + + + // The following class loader functions were adopted from + // http://svn.apache.org/repos/asf/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/classloader/ClassLoaderUtils.java + // CXF is an Apache project licensed under the ASF License 2.0 + // License statement from the file: + /** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + + /** + * Load a given resource.

This method will try to load the resource + * using the following methods (in order): + *

    + *
  • From Thread.currentThread().getContextClassLoader() + *
  • From ClassLoaderUtil.class.getClassLoader() + *
  • callingClass.getClassLoader() + *
+ * + * @param resourceName The name of the resource to load + * @param callingClass The Class object of the calling object + */ + public static URL getResource( + String resourceName, + Class callingClass) { + URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); + if (url == null && resourceName.startsWith("/")) { + //certain classloaders need it without the leading / + url = Thread.currentThread().getContextClassLoader() + .getResource(resourceName.substring(1)); + } + if (url == null) { + url = ServiceUtil.class.getClassLoader().getResource(resourceName); + } + if (url == null && resourceName.startsWith("/")) { + //certain classloaders need it without the leading / + url = ServiceUtil.class.getClassLoader() + .getResource(resourceName.substring(1)); + } + if (url == null) { + ClassLoader cl = callingClass.getClassLoader(); + if (cl != null) { + url = cl.getResource(resourceName); + } + } + if (url == null) { + url = callingClass.getResource(resourceName); + } + if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) { + return getResource('/' + resourceName, callingClass); + } + return url; + } + + public static Enumeration getResources( + String resourceName, + Class callingClass) + throws IOException { + Enumeration url = Thread.currentThread().getContextClassLoader().getResources(resourceName); + if (url == null && resourceName.startsWith("/")) { + //certain classloaders need it without the leading / + url = Thread.currentThread().getContextClassLoader() + .getResources(resourceName.substring(1)); + } + if (url == null) { + url = ServiceUtil.class.getClassLoader().getResources(resourceName); + } + if (url == null && resourceName.startsWith("/")) { + //certain classloaders need it without the leading / + url = ServiceUtil.class.getClassLoader() + .getResources(resourceName.substring(1)); + } + if (url == null) { + ClassLoader cl = callingClass.getClassLoader(); + if (cl != null) { + url = cl.getResources(resourceName); + } + } + if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) { + return getResources('/' + resourceName, callingClass); + } + return url; + } + + /** + * This is a convenience method to load a resource as a stream.

The + * algorithm used to find the resource is given in getResource() + * + * @param resourceName The name of the resource to load + * @param callingClass The Class object of the calling object + */ + public static InputStream getResourceAsStream( + String resourceName, + Class callingClass) { + URL url = getResource(resourceName, callingClass); + try { + return (url != null) ? url.openStream() : null; + } catch (IOException e) { + return null; + } + } + + /** + * Load a class with a given name.

It will try to load the class in the + * following order: + *

    + *
  • From Thread.currentThread().getContextClassLoader() + *
  • Using the basic Class.forName() + *
  • From ClassLoaderUtil.class.getClassLoader() + *
  • From the callingClass.getClassLoader() + *
+ * + * @param className The name of the class to load + * @param callingClass The Class object of the calling object + * @throws ClassNotFoundException If the class cannot be found anywhere. + */ + public static Class loadClass( + String className, + Class callingClass) + throws ClassNotFoundException { + try { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + if (cl != null) { + return cl.loadClass(className); + } + return loadClass2(className, callingClass); + } catch (ClassNotFoundException e) { + return loadClass2(className, callingClass); + } + } + + private static Class loadClass2( + String className, + Class callingClass) + throws ClassNotFoundException { + try { + return Class.forName(className); + } catch (ClassNotFoundException ex) { + try { + return ServiceUtil.class.getClassLoader().loadClass(className); + } catch (ClassNotFoundException exc) { + return callingClass.getClassLoader().loadClass(className); + } + } + } + }