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