1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.object; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.construct.FlowConstruct; |
15 | |
import org.mule.api.construct.FlowConstructAware; |
16 | |
import org.mule.api.lifecycle.InitialisationCallback; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.api.object.ObjectFactory; |
19 | |
import org.mule.api.service.Service; |
20 | |
import org.mule.api.service.ServiceAware; |
21 | |
import org.mule.config.i18n.MessageFactory; |
22 | |
import org.mule.util.BeanUtils; |
23 | |
import org.mule.util.ClassUtils; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import org.apache.commons.logging.Log; |
30 | |
import org.apache.commons.logging.LogFactory; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public abstract class AbstractObjectFactory implements ObjectFactory, FlowConstructAware |
37 | |
{ |
38 | |
public static final String ATTRIBUTE_OBJECT_CLASS_NAME = "objectClassName"; |
39 | |
public static final String ATTRIBUTE_OBJECT_CLASS = "objectClass"; |
40 | |
|
41 | |
protected String objectClassName; |
42 | 0 | protected Class<?> objectClass = null; |
43 | 0 | protected Map properties = null; |
44 | 0 | protected List<InitialisationCallback> initialisationCallbacks = new ArrayList<InitialisationCallback>(); |
45 | |
protected FlowConstruct flowConstruct; |
46 | 0 | protected boolean disposed = false; |
47 | |
|
48 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
49 | |
|
50 | |
|
51 | |
public AbstractObjectFactory() |
52 | 0 | { |
53 | |
|
54 | 0 | } |
55 | |
|
56 | |
public AbstractObjectFactory(String objectClassName) |
57 | |
{ |
58 | 0 | this(objectClassName, null); |
59 | 0 | } |
60 | |
|
61 | |
public AbstractObjectFactory(String objectClassName, Map properties) |
62 | |
{ |
63 | 0 | super(); |
64 | 0 | this.objectClassName = objectClassName; |
65 | 0 | this.properties = properties; |
66 | 0 | setupObjectClassFromObjectClassName(); |
67 | 0 | } |
68 | |
|
69 | |
public AbstractObjectFactory(Class<?> objectClass) |
70 | |
{ |
71 | 0 | this(objectClass, null); |
72 | 0 | } |
73 | |
|
74 | |
public AbstractObjectFactory(Class<?> objectClass, Map properties) |
75 | |
{ |
76 | 0 | super(); |
77 | 0 | this.objectClassName = objectClass.getName(); |
78 | 0 | this.objectClass = objectClass; |
79 | 0 | this.properties = properties; |
80 | 0 | } |
81 | |
|
82 | |
protected Class<?> setupObjectClassFromObjectClassName() |
83 | |
{ |
84 | |
try |
85 | |
{ |
86 | 0 | Class<?> klass = ClassUtils.getClass(objectClassName); |
87 | 0 | objectClass = klass; |
88 | 0 | return klass; |
89 | |
} |
90 | 0 | catch (ClassNotFoundException e) |
91 | |
{ |
92 | 0 | throw new IllegalArgumentException(e); |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
97 | |
{ |
98 | 0 | this.flowConstruct = flowConstruct; |
99 | 0 | } |
100 | |
|
101 | |
public void initialise() throws InitialisationException |
102 | |
{ |
103 | 0 | if ((objectClassName == null) || (objectClass == null)) |
104 | |
{ |
105 | 0 | throw new InitialisationException( |
106 | |
MessageFactory.createStaticMessage("Object factory has not been initialized."), this); |
107 | |
} |
108 | 0 | disposed=false; |
109 | 0 | } |
110 | |
|
111 | |
public void dispose() |
112 | |
{ |
113 | 0 | disposed=true; |
114 | |
|
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public Object getInstance(MuleContext muleContext) throws Exception |
125 | |
{ |
126 | 0 | if (objectClass == null || disposed) |
127 | |
{ |
128 | 0 | throw new InitialisationException( |
129 | |
MessageFactory.createStaticMessage("Object factory has not been initialized."), this); |
130 | |
} |
131 | |
|
132 | 0 | Class<?> klass = objectClass; |
133 | 0 | if (klass == null) |
134 | |
{ |
135 | 0 | klass = setupObjectClassFromObjectClassName(); |
136 | |
} |
137 | |
|
138 | 0 | Object object = ClassUtils.instanciateClass(klass); |
139 | |
|
140 | 0 | if (properties != null) |
141 | |
{ |
142 | 0 | BeanUtils.populateWithoutFail(object, properties, true); |
143 | |
} |
144 | |
|
145 | 0 | if(object instanceof FlowConstructAware) |
146 | |
{ |
147 | 0 | ((FlowConstructAware)object).setFlowConstruct(flowConstruct); |
148 | |
} |
149 | |
|
150 | 0 | if(object instanceof ServiceAware && flowConstruct instanceof Service) |
151 | |
{ |
152 | 0 | ((ServiceAware)object).setService((Service) flowConstruct); |
153 | |
} |
154 | |
|
155 | 0 | if(isAutoWireObject()) |
156 | |
{ |
157 | 0 | muleContext.getRegistry().applyProcessors(object); |
158 | |
} |
159 | 0 | fireInitialisationCallbacks(object); |
160 | |
|
161 | 0 | return object; |
162 | |
} |
163 | |
|
164 | |
protected void fireInitialisationCallbacks(Object component) throws InitialisationException |
165 | |
{ |
166 | 0 | for (InitialisationCallback callback : initialisationCallbacks) |
167 | |
{ |
168 | 0 | callback.initialise(component); |
169 | |
} |
170 | 0 | } |
171 | |
|
172 | |
public Class<?> getObjectClass() |
173 | |
{ |
174 | 0 | if (objectClass != null) |
175 | |
{ |
176 | 0 | Class<?> klass = objectClass; |
177 | 0 | if (klass == null) |
178 | |
{ |
179 | 0 | klass = setupObjectClassFromObjectClassName(); |
180 | |
} |
181 | 0 | return klass; |
182 | |
} |
183 | |
|
184 | |
|
185 | 0 | return null; |
186 | |
} |
187 | |
|
188 | |
public void setObjectClass(Class<?> objectClass) |
189 | |
{ |
190 | 0 | this.objectClass = objectClass; |
191 | 0 | this.objectClassName = objectClass.getName(); |
192 | 0 | } |
193 | |
|
194 | |
protected String getObjectClassName() |
195 | |
{ |
196 | 0 | return objectClassName; |
197 | |
} |
198 | |
|
199 | |
public void setObjectClassName(String objectClassName) |
200 | |
{ |
201 | 0 | this.objectClassName = objectClassName; |
202 | 0 | setupObjectClassFromObjectClassName(); |
203 | 0 | } |
204 | |
|
205 | |
protected Map getProperties() |
206 | |
{ |
207 | 0 | return properties; |
208 | |
} |
209 | |
|
210 | |
public void setProperties(Map properties) |
211 | |
{ |
212 | 0 | this.properties = properties; |
213 | 0 | } |
214 | |
|
215 | |
public void addObjectInitialisationCallback(InitialisationCallback callback) |
216 | |
{ |
217 | 0 | initialisationCallbacks.add(callback); |
218 | 0 | } |
219 | |
|
220 | |
public boolean isSingleton() |
221 | |
{ |
222 | 0 | return false; |
223 | |
} |
224 | |
|
225 | |
public boolean isExternallyManagedLifecycle() |
226 | |
{ |
227 | 0 | return false; |
228 | |
} |
229 | |
|
230 | |
public boolean isAutoWireObject() |
231 | |
{ |
232 | 0 | return true; |
233 | |
} |
234 | |
} |