| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | public class ObjectDefinitionResolver { |
| 45 | |
|
| 46 | 0 | private static final Logger LOG = Logger.getLogger(ObjectDefinitionResolver.class); |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public static Object wrap(Object object) { |
| 53 | 0 | return wrap(object, ClassLoaderUtils.getDefaultClassLoader()); |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static Object wrap(Object object, ClassLoader classLoader) { |
| 61 | |
|
| 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 | |
} |