001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.impl;
016:
017: import java.net.URL;
018:
019: import org.apache.hivemind.ApplicationRuntimeException;
020: import org.apache.hivemind.ClassResolver;
021:
022: /**
023: * Default implementation of {@link org.apache.hivemind.ClassResolver} based around
024: * {@link Thread#getContextClassLoader()} (which is set by the servlet container).
025: *
026: * @author Howard Lewis Ship
027: */
028: public class DefaultClassResolver implements ClassResolver {
029: private ClassLoader _loader;
030:
031: /**
032: * Constructs a new instance using {@link Thread#getContextClassLoader()}.
033: */
034:
035: public DefaultClassResolver() {
036: this (Thread.currentThread().getContextClassLoader());
037: }
038:
039: public DefaultClassResolver(ClassLoader loader) {
040: _loader = loader;
041: }
042:
043: public URL getResource(String name) {
044: String stripped = removeLeadingSlash(name);
045:
046: URL result = _loader.getResource(stripped);
047:
048: return result;
049: }
050:
051: private String removeLeadingSlash(String name) {
052: if (name.startsWith("/"))
053: return name.substring(1);
054:
055: return name;
056: }
057:
058: /**
059: * Invokes {@link Class#forName(java.lang.String, boolean, java.lang.ClassLoader)}.
060: *
061: * @param type
062: * the complete class name to locate and load; alternately, may be a primitive name
063: * or an array type (primitive or object)
064: * @return The loaded class
065: * @throws ApplicationRuntimeException
066: * if loading the class throws an exception (typically
067: * {@link ClassNotFoundException} or a security exception)
068: * @see JavaTypeUtils
069: */
070:
071: public Class findClass(String type) {
072: try {
073: return lookupClass(type);
074: } catch (Throwable t) {
075: throw new ApplicationRuntimeException(ImplMessages
076: .unableToLoadClass(type, _loader, t), t);
077: }
078: }
079:
080: private Class lookupClass(String type)
081: throws ClassNotFoundException {
082: Class result = JavaTypeUtils.getPrimtiveClass(type);
083:
084: if (result != null)
085: return result;
086:
087: // This does some magic to handle arrays of primitives or objects in the
088: // format needed by Class.forName().
089:
090: String jvmName = JavaTypeUtils.getJVMClassName(type);
091:
092: return Class.forName(jvmName, true, _loader);
093: }
094:
095: public Class checkForClass(String type) {
096: try {
097: return lookupClass(type);
098: } catch (ClassNotFoundException ex) {
099: return null;
100: } catch (Throwable t) {
101: throw new ApplicationRuntimeException(ImplMessages
102: .unableToLoadClass(type, _loader, t), t);
103: }
104:
105: }
106:
107: public ClassLoader getClassLoader() {
108: return _loader;
109: }
110:
111: }
|