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.lifecycle.InitialisationException; |
15 | |
|
16 | |
import java.util.Map; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
public class SingletonObjectFactory extends AbstractObjectFactory |
22 | |
{ |
23 | |
private Object instance; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public SingletonObjectFactory() |
29 | |
{ |
30 | 0 | super(); |
31 | 0 | } |
32 | |
|
33 | |
public SingletonObjectFactory(String objectClassName) |
34 | |
{ |
35 | 0 | super(objectClassName); |
36 | 0 | } |
37 | |
|
38 | |
public SingletonObjectFactory(String objectClassName, Map properties) |
39 | |
{ |
40 | 0 | super(objectClassName, properties); |
41 | 0 | } |
42 | |
|
43 | |
public SingletonObjectFactory(Class objectClass) |
44 | |
{ |
45 | 0 | super(objectClass); |
46 | 0 | } |
47 | |
|
48 | |
public SingletonObjectFactory(Class<?> objectClass, Map properties) |
49 | |
{ |
50 | 0 | super(objectClass, properties); |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public SingletonObjectFactory(Object instance) |
57 | |
{ |
58 | 0 | super(instance.getClass()); |
59 | 0 | this.instance = instance; |
60 | 0 | } |
61 | |
|
62 | |
@Override |
63 | |
public void dispose() |
64 | |
{ |
65 | 0 | instance = null; |
66 | 0 | super.dispose(); |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Override |
75 | |
public Object getInstance(MuleContext muleContext) throws Exception |
76 | |
{ |
77 | 0 | if (instance == null) |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | instance = super.getInstance(muleContext); |
82 | |
} |
83 | 0 | catch (Exception e) |
84 | |
{ |
85 | 0 | throw new InitialisationException(e, this); |
86 | 0 | } |
87 | |
} |
88 | 0 | return instance; |
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
public Class<?> getObjectClass() |
93 | |
{ |
94 | 0 | if (instance != null) |
95 | |
{ |
96 | 0 | return instance.getClass(); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | return super.getObjectClass(); |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public boolean isSingleton() |
106 | |
{ |
107 | 0 | return true; |
108 | |
} |
109 | |
} |