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