Coverage Report - org.kuali.rice.core.framework.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.framework.resourceloader;
 18  
 
 19  
 import org.apache.commons.beanutils.ConstructorUtils;
 20  
 import org.apache.commons.beanutils.PropertyUtils;
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.rice.core.api.reflect.DataDefinition;
 23  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 24  
 import org.kuali.rice.core.api.reflect.PropertyDefinition;
 25  
 import org.kuali.rice.core.api.resourceloader.ResourceLoaderException;
 26  
 import org.kuali.rice.core.api.util.ClassLoaderUtils;
 27  
 import org.kuali.rice.core.api.util.ContextClassLoaderBinder;
 28  
 
 29  
 import java.lang.reflect.InvocationTargetException;
 30  
 import java.util.Collection;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * Resolves object definitions into java Objects that are wrapped in a proxy whose classloader is the current
 36  
  * context classloader.
 37  
  *
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  *
 40  
  */
 41  0
 public class ObjectDefinitionResolver {
 42  
 
 43  0
         private static final Logger LOG = Logger.getLogger(ObjectDefinitionResolver.class);
 44  
 
 45  
         /**
 46  
          * Wraps the given object in a proxy which switches the context classloader appropriately.  The classloader
 47  
          * of this resource loader is used.
 48  
          */
 49  
         public static Object wrap(Object object) {
 50  0
                 return wrap(object, ClassLoaderUtils.getDefaultClassLoader());
 51  
         }
 52  
 
 53  
         /**
 54  
          * Wraps the given object in a proxy which switches the context classloader appropriately.  The given classloader
 55  
          * is used as the context classloader.
 56  
          */
 57  
         public static Object wrap(Object object, ClassLoader classLoader) {
 58  
 //                return ContextClassLoaderProxy.wrap(object, classLoader);
 59  0
                 return object;
 60  
         }
 61  
 
 62  
         public static Object createObject(String className, ClassLoader classLoader) {
 63  0
                 return createObject(new ObjectDefinition(className), classLoader, true);
 64  
         }
 65  
 
 66  
         public static Object createObject(ObjectDefinition definition, ClassLoader classLoader, boolean wrap) {
 67  0
                 Object object = null;
 68  0
                 String className = definition.getClassName();
 69  
                 try {
 70  0
                         ContextClassLoaderBinder.bind(classLoader);
 71  0
                         Class<?>[] constructorParamTypes = buildConstructorParamTypes(definition.getConstructorParameters());
 72  0
                         Object[] constructorParams = buildConstructorParams(definition.getConstructorParameters());
 73  
                         try {
 74  0
                                 Class<?> objectClass = Class.forName(className, true, classLoader);
 75  0
                                 object = loadObject(objectClass, constructorParams, constructorParamTypes);
 76  0
                         } catch (ClassNotFoundException e) {
 77  0
                                 return null;
 78  0
                         }
 79  0
                         invokeProperties(object, definition.getProperties());
 80  0
                         if (wrap) {
 81  0
                                 return wrap(object, classLoader);
 82  
                         }
 83  0
                         return object;
 84  0
                 } catch (Exception e) {
 85  0
                         handleException(className, e);
 86  
                 } finally {
 87  0
                         ContextClassLoaderBinder.unbind();
 88  0
                 }
 89  0
                 return object;
 90  
         }
 91  
 
 92  
         protected static Object loadObject(Class<?> objectClass, Object[] constructorParams, Class<?>[] constructorParamTypes) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
 93  0
                 return ConstructorUtils.invokeConstructor(objectClass, constructorParams, constructorParamTypes);
 94  
         }
 95  
 
 96  
         private static void handleException(String className, Exception e) {
 97  0
                 if (e instanceof RuntimeException) {
 98  0
                         throw (RuntimeException) e;
 99  
                 }
 100  0
                 throw new ResourceLoaderException("Could not materialize object from definition, using classname: " + className, e);
 101  
         }
 102  
 
 103  
         protected static Class<?>[] buildConstructorParamTypes(List<DataDefinition> constructorParameters) {
 104  0
                 Class<?>[] params = new Class[constructorParameters.size()];
 105  0
                 int index = 0;
 106  0
                 for (Iterator iterator = constructorParameters.iterator(); iterator.hasNext();) {
 107  0
                         DataDefinition dataDef = (DataDefinition) iterator.next();
 108  0
                         params[index++] = dataDef.getType();
 109  0
                 }
 110  0
                 return params;
 111  
         }
 112  
 
 113  
         protected static Object[] buildConstructorParams(List<DataDefinition> constructorParameters) {
 114  0
                 Object[] params = new Object[constructorParameters.size()];
 115  0
                 int index = 0;
 116  0
                 for (Iterator iterator = constructorParameters.iterator(); iterator.hasNext();) {
 117  0
                         DataDefinition dataDef = (DataDefinition) iterator.next();
 118  0
                         params[index++] = dataDef.getValue();
 119  0
                 }
 120  0
                 return params;
 121  
         }
 122  
 
 123  
         protected static void invokeProperties(Object object, Collection<PropertyDefinition> properties) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
 124  0
                 for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
 125  0
                         PropertyDefinition propertyDef = (PropertyDefinition) iterator.next();
 126  0
                         invokeProperty(object, propertyDef);
 127  0
                 }
 128  0
         }
 129  
 
 130  
         protected static void invokeProperty(Object object, PropertyDefinition definition) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
 131  0
                 PropertyUtils.setProperty(object, definition.getName(), definition.getData().getValue());
 132  0
         }
 133  
 
 134  
 
 135  
 }