2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 127   Methods: 8
NCLOC: 67   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultClassResolver.java 100% 95.2% 100% 97%
coverage coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.hivemind.impl;
 16   
 17    import java.net.URL;
 18   
 19    import org.apache.hivemind.ApplicationRuntimeException;
 20    import org.apache.hivemind.ClassResolver;
 21   
 22    /**
 23    * Default implementation of {@link org.apache.hivemind.ClassResolver} based around
 24    * {@link Thread#getContextClassLoader()} (which is set by the servlet container).
 25    *
 26    * @author Howard Lewis Ship
 27    */
 28    public class DefaultClassResolver implements ClassResolver
 29    {
 30    private ClassLoader _loader;
 31   
 32    /**
 33    * Constructs a new instance using {@link Thread#getContextClassLoader()}.
 34    */
 35   
 36  456 public DefaultClassResolver()
 37    {
 38  456 this(Thread.currentThread().getContextClassLoader());
 39    }
 40   
 41  457 public DefaultClassResolver(ClassLoader loader)
 42    {
 43  457 _loader = loader;
 44    }
 45   
 46  21 public URL getResource(String name)
 47    {
 48  21 String stripped = removeLeadingSlash(name);
 49   
 50  21 URL result = _loader.getResource(stripped);
 51   
 52  21 return result;
 53    }
 54   
 55  21 private String removeLeadingSlash(String name)
 56    {
 57  21 if (name.startsWith("/"))
 58  15 return name.substring(1);
 59   
 60  6 return name;
 61    }
 62   
 63    /**
 64    * Invokes {@link Class#forName(java.lang.String, boolean, java.lang.ClassLoader)}.
 65    *
 66    * @param type
 67    * the complete class name to locate and load; alternately, may be a primitive name
 68    * or an array type (primitive or object)
 69    * @return The loaded class
 70    * @throws ApplicationRuntimeException
 71    * if loading the class throws an exception (typically
 72    * {@link ClassNotFoundException} or a security exception)
 73    * @see JavaTypeUtils
 74    */
 75   
 76  15 public Class findClass(String type)
 77    {
 78  15 try
 79    {
 80  15 return lookupClass(type);
 81    }
 82    catch (Throwable t)
 83    {
 84  4 throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t),
 85    t);
 86    }
 87    }
 88   
 89  8704 private Class lookupClass(String type) throws ClassNotFoundException
 90    {
 91  8704 Class result = JavaTypeUtils.getPrimtiveClass(type);
 92   
 93  8704 if (result != null)
 94  1 return result;
 95   
 96    // This does some magic to handle arrays of primitives or objects in the
 97    // format needed by Class.forName().
 98   
 99  8703 String jvmName = JavaTypeUtils.getJVMClassName(type);
 100   
 101  8703 return Class.forName(jvmName, true, _loader);
 102    }
 103   
 104  8689 public Class checkForClass(String type)
 105    {
 106  8689 try
 107    {
 108  8689 return lookupClass(type);
 109    }
 110    catch (ClassNotFoundException ex)
 111    {
 112  3908 return null;
 113    }
 114    catch (Throwable t)
 115    {
 116  0 throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t),
 117    t);
 118    }
 119   
 120    }
 121   
 122  127 public ClassLoader getClassLoader()
 123    {
 124  127 return _loader;
 125    }
 126   
 127    }