Coverage Report - org.kuali.rice.core.resourceloader.ObjectDefinitionResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectDefinitionResolver
0%
0/49
0%
0/10
2.2
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.resourceloader;
 18  
 
 19  
 import java.lang.reflect.InvocationTargetException;
 20  
 import java.util.Collection;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.commons.beanutils.ConstructorUtils;
 25  
 import org.apache.commons.beanutils.PropertyUtils;
 26  
 import org.apache.log4j.Logger;
 27  
 import org.kuali.rice.core.reflect.DataDefinition;
 28  
 import org.kuali.rice.core.reflect.ObjectDefinition;
 29  
 import org.kuali.rice.core.reflect.PropertyDefinition;
 30  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 31  
 
 32  
 /**
 33  
  * Resolves object definitions into java Objects that are wrapped in a proxy whose classloader is the current
 34  
  * context classloader.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  *
 38  
  */
 39  0
 public class ObjectDefinitionResolver {
 40  
 
 41  0
         private static final Logger LOG = Logger.getLogger(ObjectDefinitionResolver.class);
 42  
 
 43  
         /**
 44  
          * Wraps the given object in a proxy which switches the context classloader appropriately.  The classloader
 45  
          * of this resource loader is used.
 46  
          */
 47  
         public static Object wrap(Object object) {
 48  0
                 return wrap(object, ClassLoaderUtils.getDefaultClassLoader());
 49  
         }
 50  
 
 51  
         /**
 52  
          * Wraps the given object in a proxy which switches the context classloader appropriately.  The given classloader
 53  
          * is used as the context classloader.
 54  
          */
 55  
         public static Object wrap(Object object, ClassLoader classLoader) {
 56  
 //                return ContextClassLoaderProxy.wrap(object, classLoader);
 57  0
                 return object;
 58  
         }
 59  
 
 60  
         public static Object createObject(String className, ClassLoader classLoader) {
 61  0
                 return createObject(new ObjectDefinition(className), classLoader, true);
 62  
         }
 63  
 
 64  
         public static Object createObject(ObjectDefinition definition, ClassLoader classLoader, boolean wrap) {
 65  0
                 Object object = null;
 66  0
                 String className = definition.getClassName();
 67  
                 try {
 68  0
                         ContextClassLoaderBinder.bind(classLoader);
 69  0
                         Class<?>[] constructorParamTypes = buildConstructorParamTypes(definition.getConstructorParameters());
 70  0
                         Object[] constructorParams = buildConstructorParams(definition.getConstructorParameters());
 71  
                         try {
 72  0
                                 Class<?> objectClass = Class.forName(className, true, classLoader);
 73  0
                                 object = loadObject(objectClass, constructorParams, constructorParamTypes);
 74  0
                         } catch (ClassNotFoundException e) {
 75  0
                                 return null;
 76  0
                         }
 77  0
                         invokeProperties(object, definition.getProperties());
 78  0
                         if (wrap) {
 79  0
                                 return wrap(object, classLoader);
 80  
                         }
 81  0
                         return object;
 82  0
                 } catch (Exception e) {
 83  0
                         handleException(className, e);
 84  
                 } finally {
 85  0
                         ContextClassLoaderBinder.unbind();
 86  0
                 }
 87  0
                 return object;
 88  
         }
 89  
 
 90  
         protected static Object loadObject(Class<?> objectClass, Object[] constructorParams, Class<?>[] constructorParamTypes) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
 91  0
                 return ConstructorUtils.invokeConstructor(objectClass, constructorParams, constructorParamTypes);
 92  
         }
 93  
 
 94  
         private static void handleException(String className, Exception e) {
 95  0
                 if (e instanceof RuntimeException) {
 96  0
                         throw (RuntimeException) e;
 97  
                 }
 98  0
                 throw new ResourceLoaderException("Could not materialize object from definition, using classname: " + className, e);
 99  
         }
 100  
 
 101  
         protected static Class<?>[] buildConstructorParamTypes(List<DataDefinition> constructorParameters) {
 102  0
                 Class<?>[] params = new Class[constructorParameters.size()];
 103  0
                 int index = 0;
 104  0
                 for (Iterator iterator = constructorParameters.iterator(); iterator.hasNext();) {
 105  0
                         DataDefinition dataDef = (DataDefinition) iterator.next();
 106  0
                         params[index++] = dataDef.getType();
 107  0
                 }
 108  0
                 return params;
 109  
         }
 110  
 
 111  
         protected static Object[] buildConstructorParams(List<DataDefinition> constructorParameters) {
 112  0
                 Object[] params = new Object[constructorParameters.size()];
 113  0
                 int index = 0;
 114  0
                 for (Iterator iterator = constructorParameters.iterator(); iterator.hasNext();) {
 115  0
                         DataDefinition dataDef = (DataDefinition) iterator.next();
 116  0
                         params[index++] = dataDef.getValue();
 117  0
                 }
 118  0
                 return params;
 119  
         }
 120  
 
 121  
         protected static void invokeProperties(Object object, Collection<PropertyDefinition> properties) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
 122  0
                 for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
 123  0
                         PropertyDefinition propertyDef = (PropertyDefinition) iterator.next();
 124  0
                         invokeProperty(object, propertyDef);
 125  0
                 }
 126  0
         }
 127  
 
 128  
         protected static void invokeProperty(Object object, PropertyDefinition definition) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
 129  0
                 PropertyUtils.setProperty(object, definition.getName(), definition.getData().getValue());
 130  0
         }
 131  
 
 132  
 
 133  
 }