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