1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.component; |
8 | |
|
9 | |
import org.mule.api.MuleException; |
10 | |
import org.mule.api.component.InterfaceBinding; |
11 | |
import org.mule.api.component.JavaComponent; |
12 | |
import org.mule.api.component.LifecycleAdapter; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.model.EntryPointResolverSet; |
15 | |
import org.mule.api.object.ObjectFactory; |
16 | |
import org.mule.api.registry.ServiceException; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.config.i18n.MessageFactory; |
19 | |
|
20 | |
import java.util.List; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class DefaultJavaComponent extends AbstractJavaComponent |
27 | |
{ |
28 | |
|
29 | |
protected LifecycleAdapter singletonComponentLifecycleAdapter; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public DefaultJavaComponent() |
35 | |
{ |
36 | 0 | super(); |
37 | 0 | } |
38 | |
|
39 | |
public DefaultJavaComponent(ObjectFactory objectFactory) |
40 | |
{ |
41 | 0 | super(objectFactory); |
42 | 0 | } |
43 | |
|
44 | |
public DefaultJavaComponent(ObjectFactory objectFactory, |
45 | |
EntryPointResolverSet entryPointResolverSet, |
46 | |
List<InterfaceBinding> bindings) |
47 | |
{ |
48 | 0 | super(objectFactory, entryPointResolverSet, bindings); |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
protected void doStart() throws MuleException |
53 | |
{ |
54 | 0 | super.doStart(); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | if (objectFactory != null && objectFactory.isSingleton()) |
61 | |
{ |
62 | |
|
63 | |
try |
64 | |
{ |
65 | 0 | if (singletonComponentLifecycleAdapter == null) |
66 | |
{ |
67 | 0 | singletonComponentLifecycleAdapter = createLifecycleAdaptor(); |
68 | |
} |
69 | |
} |
70 | 0 | catch (Exception e) |
71 | |
{ |
72 | 0 | throw new InitialisationException( |
73 | |
MessageFactory.createStaticMessage("Unable to create instance of POJO service"), e, this); |
74 | |
|
75 | 0 | } |
76 | |
|
77 | 0 | if (!singletonComponentLifecycleAdapter.isStarted()) |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | singletonComponentLifecycleAdapter.start(); |
82 | |
} |
83 | 0 | catch (Exception e) |
84 | |
{ |
85 | 0 | throw new ServiceException(CoreMessages.failedToStart("Service '" + flowConstruct.getName() + "'"), e); |
86 | 0 | } |
87 | |
} |
88 | |
} |
89 | 0 | } |
90 | |
|
91 | |
@Override |
92 | |
protected void doStop() throws MuleException |
93 | |
{ |
94 | 0 | super.doStop(); |
95 | |
|
96 | |
|
97 | 0 | if (singletonComponentLifecycleAdapter != null && singletonComponentLifecycleAdapter.isStarted()) |
98 | |
{ |
99 | |
try |
100 | |
{ |
101 | 0 | singletonComponentLifecycleAdapter.stop(); |
102 | |
} |
103 | 0 | catch (Exception e) |
104 | |
{ |
105 | 0 | throw new ServiceException(CoreMessages.failedToStop("Service '" + flowConstruct.getName() + "'"), e); |
106 | 0 | } |
107 | |
} |
108 | 0 | } |
109 | |
|
110 | |
@Override |
111 | |
protected void doDispose() |
112 | |
{ |
113 | 0 | super.doDispose(); |
114 | |
|
115 | |
|
116 | 0 | if (singletonComponentLifecycleAdapter != null) |
117 | |
{ |
118 | 0 | singletonComponentLifecycleAdapter.dispose(); |
119 | |
} |
120 | 0 | } |
121 | |
|
122 | |
@Override |
123 | |
protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception |
124 | |
{ |
125 | |
LifecycleAdapter componentLifecycleAdapter; |
126 | 0 | if (singletonComponentLifecycleAdapter != null) |
127 | |
{ |
128 | 0 | componentLifecycleAdapter = singletonComponentLifecycleAdapter; |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | 0 | componentLifecycleAdapter = createLifecycleAdaptor(); |
133 | 0 | componentLifecycleAdapter.start(); |
134 | |
} |
135 | 0 | return componentLifecycleAdapter; |
136 | |
} |
137 | |
|
138 | |
@Override |
139 | |
protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception |
140 | |
{ |
141 | 0 | if (singletonComponentLifecycleAdapter == null && lifecycleAdapter != null) |
142 | |
{ |
143 | 0 | lifecycleAdapter.stop(); |
144 | 0 | lifecycleAdapter.dispose(); |
145 | 0 | lifecycleAdapter = null; |
146 | |
} |
147 | 0 | } |
148 | |
|
149 | |
} |