1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.object; |
12 | |
|
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.config.i18n.MessageFactory; |
15 | |
|
16 | |
import java.util.Map; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
public class SingletonObjectFactory extends AbstractObjectFactory |
22 | |
{ |
23 | 416 | private Object instance = null; |
24 | |
|
25 | |
|
26 | |
public SingletonObjectFactory() |
27 | |
{ |
28 | 12 | super(); |
29 | 12 | } |
30 | |
|
31 | |
public SingletonObjectFactory(String objectClassName) |
32 | |
{ |
33 | 0 | super(objectClassName); |
34 | 0 | } |
35 | |
|
36 | |
public SingletonObjectFactory(String objectClassName, Map properties) |
37 | |
{ |
38 | 0 | super(objectClassName, properties); |
39 | 0 | } |
40 | |
|
41 | |
public SingletonObjectFactory(Class objectClass) |
42 | |
{ |
43 | 14 | super(objectClass); |
44 | 14 | } |
45 | |
|
46 | |
public SingletonObjectFactory(Class objectClass, Map properties) |
47 | |
{ |
48 | 384 | super(objectClass, properties); |
49 | 384 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public SingletonObjectFactory(Object instance) |
55 | |
{ |
56 | 6 | super(instance.getClass()); |
57 | 6 | this.instance = instance; |
58 | 6 | } |
59 | |
|
60 | |
|
61 | |
public void initialise() throws InitialisationException |
62 | |
{ |
63 | 790 | super.initialise(); |
64 | 788 | if (instance == null) |
65 | |
{ |
66 | |
try |
67 | |
{ |
68 | 402 | instance = super.getInstance(); |
69 | |
} |
70 | 0 | catch (Exception e) |
71 | |
{ |
72 | 0 | throw new InitialisationException(e, this); |
73 | 402 | } |
74 | |
} |
75 | 788 | } |
76 | |
|
77 | |
|
78 | |
public void dispose() |
79 | |
{ |
80 | 2 | instance = null; |
81 | 2 | super.dispose(); |
82 | 2 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public Object getInstance() throws Exception |
89 | |
{ |
90 | 30 | if (instance != null) |
91 | |
{ |
92 | 26 | return instance; |
93 | |
} |
94 | |
else |
95 | |
{ |
96 | 4 | throw new InitialisationException( |
97 | |
MessageFactory.createStaticMessage("Object factory has not been initialized."), this); |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
|
102 | |
public Class getObjectClass() |
103 | |
{ |
104 | 16 | if (instance != null) |
105 | |
{ |
106 | 10 | return instance.getClass(); |
107 | |
} |
108 | |
else |
109 | |
{ |
110 | 6 | return super.getObjectClass(); |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
public boolean isSingleton() |
115 | |
{ |
116 | 16 | return true; |
117 | |
} |
118 | |
|
119 | |
} |