|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ClassLoaderUtils.java | 50% | 71.4% | 75% | 70% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2001-2003 The Apache Software Foundation.
|
|
| 5 |
*
|
|
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 7 |
* you may not use this file except in compliance with the License.
|
|
| 8 |
* You may obtain a copy of the License at
|
|
| 9 |
*
|
|
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 11 |
*
|
|
| 12 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 15 |
* See the License for the specific language governing permissions and
|
|
| 16 |
* limitations under the License.
|
|
| 17 |
*
|
|
| 18 |
* ========================================================================
|
|
| 19 |
*/
|
|
| 20 |
package org.apache.cactus.internal.util;
|
|
| 21 |
|
|
| 22 |
import java.util.Locale;
|
|
| 23 |
import java.util.MissingResourceException;
|
|
| 24 |
import java.util.PropertyResourceBundle;
|
|
| 25 |
import java.util.ResourceBundle;
|
|
| 26 |
|
|
| 27 |
/**
|
|
| 28 |
* Utiliy methods related to class loading in a webapp environment.
|
|
| 29 |
*
|
|
| 30 |
* @version $Id: ClassLoaderUtils.java 238991 2004-05-22 11:34:50Z vmassol $
|
|
| 31 |
*/
|
|
| 32 |
public class ClassLoaderUtils |
|
| 33 |
{
|
|
| 34 |
/**
|
|
| 35 |
* Try loading a class first by using the context class loader or by using
|
|
| 36 |
* the classloader of the referrer class if the context classloader failed
|
|
| 37 |
* to load the class.
|
|
| 38 |
*
|
|
| 39 |
* @param theClassName the name of the test class
|
|
| 40 |
* @param theReferrer the class will be loaded using the classloader which
|
|
| 41 |
* has loaded this referrer class
|
|
| 42 |
* @return the class object the test class to call
|
|
| 43 |
* @exception ClassNotFoundException if the class cannot be loaded through
|
|
| 44 |
* either classloader
|
|
| 45 |
*/
|
|
| 46 | 25 |
public static Class loadClass(String theClassName, Class theReferrer) |
| 47 |
throws ClassNotFoundException
|
|
| 48 |
{
|
|
| 49 |
// Get the class to call and build an instance of it.
|
|
| 50 | 25 |
Class clazz = null;
|
| 51 |
|
|
| 52 | 25 |
try
|
| 53 |
{
|
|
| 54 |
// try loading from webapp classloader first
|
|
| 55 | 25 |
clazz = loadClassFromWebappClassLoader(theClassName, theReferrer); |
| 56 |
} |
|
| 57 |
catch (Throwable internalException)
|
|
| 58 |
{
|
|
| 59 |
// Then try first from Context class loader so that we can put the
|
|
| 60 |
// Cactus jar as an external library.
|
|
| 61 | 0 |
clazz = loadClassFromContextClassLoader(theClassName); |
| 62 |
} |
|
| 63 |
|
|
| 64 | 25 |
return clazz;
|
| 65 |
} |
|
| 66 |
|
|
| 67 |
/**
|
|
| 68 |
* Try loading class using the Context class loader.
|
|
| 69 |
*
|
|
| 70 |
* @param theClassName the class to load
|
|
| 71 |
* @return the <code>Class</code> object for the class to load
|
|
| 72 |
* @exception ClassNotFoundException if the class cannot be loaded through
|
|
| 73 |
* this class loader
|
|
| 74 |
*/
|
|
| 75 | 0 |
public static Class loadClassFromContextClassLoader(String theClassName) |
| 76 |
throws ClassNotFoundException
|
|
| 77 |
{
|
|
| 78 | 0 |
return Class.forName(theClassName, true, |
| 79 |
Thread.currentThread().getContextClassLoader()); |
|
| 80 |
} |
|
| 81 |
|
|
| 82 |
/**
|
|
| 83 |
* Try loading class using the Webapp class loader.
|
|
| 84 |
*
|
|
| 85 |
* @param theClassName the class to load
|
|
| 86 |
* @param theReferrer the class will be loaded using the classloader which
|
|
| 87 |
* has loaded this referrer class
|
|
| 88 |
* @return the <code>Class</code> object for the class to load
|
|
| 89 |
* @exception ClassNotFoundException if the class cannot be loaded through
|
|
| 90 |
* this class loader
|
|
| 91 |
*/
|
|
| 92 | 25 |
public static Class loadClassFromWebappClassLoader(String theClassName, |
| 93 |
Class theReferrer) throws ClassNotFoundException
|
|
| 94 |
{
|
|
| 95 | 25 |
return Class.forName(theClassName, true, theReferrer.getClassLoader()); |
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* Try loading a resource bundle from either the context class loader or
|
|
| 100 |
* the
|
|
| 101 |
*
|
|
| 102 |
* @param theName the resource bundle name
|
|
| 103 |
* @param theReferrer the resource bundle will be loaded using the
|
|
| 104 |
* classloader which has loaded this referrer class
|
|
| 105 |
* @return the loaded resource bundle
|
|
| 106 |
*/
|
|
| 107 | 1 |
public static ResourceBundle loadPropertyResourceBundle(String theName, |
| 108 |
Class theReferrer) |
|
| 109 |
{
|
|
| 110 | 1 |
ResourceBundle bundle; |
| 111 |
|
|
| 112 | 1 |
try
|
| 113 |
{
|
|
| 114 |
// Try to load from the referrer class loader first
|
|
| 115 |
|
|
| 116 |
// Some JDK implementation will return "null" when calling
|
|
| 117 |
// getClassLoader(), signalling that the classloader is the
|
|
| 118 |
// bootstrap class loader. However, getBundle() does not support
|
|
| 119 |
// passing null for the class loader, hence the following test.
|
|
| 120 | 1 |
if (theReferrer.getClassLoader() == null) |
| 121 |
{
|
|
| 122 | 0 |
bundle = PropertyResourceBundle.getBundle(theName, |
| 123 |
Locale.getDefault()); |
|
| 124 |
} |
|
| 125 |
else
|
|
| 126 |
{
|
|
| 127 | 1 |
bundle = PropertyResourceBundle.getBundle(theName, |
| 128 |
Locale.getDefault(), theReferrer.getClassLoader()); |
|
| 129 |
} |
|
| 130 |
} |
|
| 131 |
catch (MissingResourceException e)
|
|
| 132 |
{
|
|
| 133 |
// Then, try to load from context classloader
|
|
| 134 | 1 |
bundle = PropertyResourceBundle.getBundle(theName, |
| 135 |
Locale.getDefault(), |
|
| 136 |
Thread.currentThread().getContextClassLoader()); |
|
| 137 |
} |
|
| 138 |
|
|
| 139 | 0 |
return bundle;
|
| 140 |
} |
|
| 141 |
} |
|
| 142 |
|
|
||||||||||