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 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 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class ObjectDefinitionResolver { |
42 | |
|
43 | 0 | private static final Logger LOG = Logger.getLogger(ObjectDefinitionResolver.class); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public static Object wrap(Object object) { |
50 | 0 | return wrap(object, ClassLoaderUtils.getDefaultClassLoader()); |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public static Object wrap(Object object, ClassLoader classLoader) { |
58 | |
|
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 | |
} |