1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.object; |
12 | |
|
13 | |
import org.mule.api.lifecycle.InitialisationCallback; |
14 | |
import org.mule.api.lifecycle.InitialisationException; |
15 | |
import org.mule.api.object.ObjectFactory; |
16 | |
import org.mule.config.i18n.MessageFactory; |
17 | |
import org.mule.util.BeanUtils; |
18 | |
import org.mule.util.ClassUtils; |
19 | |
|
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public abstract class AbstractObjectFactory implements ObjectFactory |
32 | |
{ |
33 | |
public static final String ATTRIBUTE_OBJECT_CLASS_NAME = "objectClassName"; |
34 | |
public static final String ATTRIBUTE_OBJECT_CLASS = "objectClass"; |
35 | |
|
36 | |
protected String objectClassName; |
37 | 466 | protected Class objectClass = null; |
38 | 466 | protected Map properties = null; |
39 | 466 | protected List initialisationCallbacks = new ArrayList(); |
40 | |
|
41 | 466 | protected transient Log logger = LogFactory.getLog(getClass()); |
42 | |
|
43 | |
|
44 | |
public AbstractObjectFactory() |
45 | 24 | { |
46 | |
|
47 | 24 | } |
48 | |
|
49 | |
public AbstractObjectFactory(String objectClassName) |
50 | |
{ |
51 | 0 | this(objectClassName, null); |
52 | 0 | } |
53 | |
|
54 | |
public AbstractObjectFactory(String objectClassName, Map properties) |
55 | 0 | { |
56 | 0 | this.objectClassName = objectClassName; |
57 | 0 | this.properties = properties; |
58 | 0 | } |
59 | |
|
60 | |
public AbstractObjectFactory(Class objectClass) |
61 | |
{ |
62 | 58 | this(objectClass, null); |
63 | 58 | } |
64 | |
|
65 | |
public AbstractObjectFactory(Class objectClass, Map properties) |
66 | 442 | { |
67 | 442 | this.objectClass = objectClass; |
68 | 442 | this.properties = properties; |
69 | 442 | } |
70 | |
|
71 | |
public void initialise() throws InitialisationException |
72 | |
{ |
73 | 838 | if (objectClass == null && objectClassName == null) |
74 | |
{ |
75 | 4 | throw new InitialisationException( |
76 | |
MessageFactory.createStaticMessage("Object factory has not been initialized."), this); |
77 | |
} |
78 | |
|
79 | 834 | if (objectClass == null && objectClassName != null) |
80 | |
{ |
81 | |
try |
82 | |
{ |
83 | 4 | objectClass = ClassUtils.getClass(objectClassName); |
84 | |
} |
85 | 0 | catch (ClassNotFoundException e) |
86 | |
{ |
87 | 0 | throw new InitialisationException(e, this); |
88 | 4 | } |
89 | |
} |
90 | 834 | } |
91 | |
|
92 | |
public void dispose() |
93 | |
{ |
94 | 4 | this.objectClass = null; |
95 | 4 | this.objectClassName = null; |
96 | 4 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public Object getInstance() throws Exception |
102 | |
{ |
103 | 476 | if (objectClass == null) |
104 | |
{ |
105 | 4 | throw new InitialisationException( |
106 | |
MessageFactory.createStaticMessage("Object factory has not been initialized."), this); |
107 | |
} |
108 | |
|
109 | 472 | Object object = ClassUtils.instanciateClass(objectClass, ClassUtils.NO_ARGS); |
110 | |
|
111 | 472 | if (properties != null) |
112 | |
{ |
113 | 0 | BeanUtils.populate(object, properties); |
114 | |
} |
115 | |
|
116 | 472 | fireInitialisationCallbacks(object); |
117 | |
|
118 | 472 | return object; |
119 | |
} |
120 | |
|
121 | |
protected void fireInitialisationCallbacks(Object component) throws InitialisationException |
122 | |
{ |
123 | |
InitialisationCallback callback; |
124 | 472 | for (Iterator iterator = initialisationCallbacks.iterator(); iterator.hasNext();) |
125 | |
{ |
126 | 0 | callback = (InitialisationCallback) iterator.next(); |
127 | 0 | callback.initialise(component); |
128 | |
} |
129 | 472 | } |
130 | |
|
131 | |
public Class getObjectClass() |
132 | |
{ |
133 | 26 | return objectClass; |
134 | |
} |
135 | |
|
136 | |
public void setObjectClass(Class objectClass) |
137 | |
{ |
138 | 24 | this.objectClass = objectClass; |
139 | 24 | } |
140 | |
|
141 | |
protected String getObjectClassName() |
142 | |
{ |
143 | 0 | return objectClassName; |
144 | |
} |
145 | |
|
146 | |
public void setObjectClassName(String objectClassName) |
147 | |
{ |
148 | 4 | this.objectClassName = objectClassName; |
149 | 4 | } |
150 | |
|
151 | |
protected Map getProperties() |
152 | |
{ |
153 | 0 | return properties; |
154 | |
} |
155 | |
|
156 | |
public void setProperties(Map properties) |
157 | |
{ |
158 | 0 | this.properties = properties; |
159 | 0 | } |
160 | |
|
161 | |
public void addObjectInitialisationCallback(InitialisationCallback callback) |
162 | |
{ |
163 | 0 | initialisationCallbacks.add(callback); |
164 | 0 | } |
165 | |
|
166 | |
public boolean isSingleton() |
167 | |
{ |
168 | 6 | return false; |
169 | |
} |
170 | |
|
171 | |
} |