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 private Object instance = null;
24
25
26 public SingletonObjectFactory()
27 {
28 super();
29 }
30
31 public SingletonObjectFactory(String objectClassName)
32 {
33 super(objectClassName);
34 }
35
36 public SingletonObjectFactory(String objectClassName, Map properties)
37 {
38 super(objectClassName, properties);
39 }
40
41 public SingletonObjectFactory(Class objectClass)
42 {
43 super(objectClass);
44 }
45
46 public SingletonObjectFactory(Class objectClass, Map properties)
47 {
48 super(objectClass, properties);
49 }
50
51
52
53
54 public SingletonObjectFactory(Object instance)
55 {
56 super(instance.getClass());
57 this.instance = instance;
58 }
59
60
61 public void initialise() throws InitialisationException
62 {
63 super.initialise();
64 if (instance == null)
65 {
66 try
67 {
68 instance = super.getInstance();
69 }
70 catch (Exception e)
71 {
72 throw new InitialisationException(e, this);
73 }
74 }
75 }
76
77
78 public void dispose()
79 {
80 instance = null;
81 super.dispose();
82 }
83
84
85
86
87
88 public Object getInstance() throws Exception
89 {
90 if (instance != null)
91 {
92 return instance;
93 }
94 else
95 {
96 throw new InitialisationException(
97 MessageFactory.createStaticMessage("Object factory has not been initialized."), this);
98 }
99 }
100
101
102 public Class getObjectClass()
103 {
104 if (instance != null)
105 {
106 return instance.getClass();
107 }
108 else
109 {
110 return super.getObjectClass();
111 }
112 }
113
114 public boolean isSingleton()
115 {
116 return true;
117 }
118
119 }