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 super();
41 }
42
43 public DefaultJavaComponent(ObjectFactory objectFactory)
44 {
45 super(objectFactory);
46 }
47
48 public DefaultJavaComponent(ObjectFactory objectFactory,
49 EntryPointResolverSet entryPointResolverSet,
50 List<InterfaceBinding> bindings)
51 {
52 super(objectFactory, entryPointResolverSet, bindings);
53 }
54
55 @Override
56 protected void doStart() throws MuleException
57 {
58 super.doStart();
59
60
61
62
63
64 if (objectFactory != null && objectFactory.isSingleton())
65 {
66
67 try
68 {
69 if (singletonComponentLifecycleAdapter == null)
70 {
71 singletonComponentLifecycleAdapter = createLifecycleAdaptor();
72 }
73 }
74 catch (Exception e)
75 {
76 throw new InitialisationException(
77 MessageFactory.createStaticMessage("Unable to create instance of POJO service"), e, this);
78
79 }
80
81 if (!singletonComponentLifecycleAdapter.isStarted())
82 {
83 try
84 {
85 singletonComponentLifecycleAdapter.start();
86 }
87 catch (Exception e)
88 {
89 throw new ServiceException(CoreMessages.failedToStart("Service '" + flowConstruct.getName() + "'"), e);
90 }
91 }
92 }
93 }
94
95 @Override
96 protected void doStop() throws MuleException
97 {
98 super.doStop();
99
100
101 if (singletonComponentLifecycleAdapter != null && singletonComponentLifecycleAdapter.isStarted())
102 {
103 try
104 {
105 singletonComponentLifecycleAdapter.stop();
106 }
107 catch (Exception e)
108 {
109 throw new ServiceException(CoreMessages.failedToStop("Service '" + flowConstruct.getName() + "'"), e);
110 }
111 }
112 }
113
114 @Override
115 protected void doDispose()
116 {
117 super.doDispose();
118
119
120 if (singletonComponentLifecycleAdapter != null)
121 {
122 singletonComponentLifecycleAdapter.dispose();
123 }
124 }
125
126 @Override
127 protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
128 {
129 LifecycleAdapter componentLifecycleAdapter;
130 if (singletonComponentLifecycleAdapter != null)
131 {
132 componentLifecycleAdapter = singletonComponentLifecycleAdapter;
133 }
134 else
135 {
136 componentLifecycleAdapter = createLifecycleAdaptor();
137 componentLifecycleAdapter.start();
138 }
139 return componentLifecycleAdapter;
140 }
141
142 @Override
143 protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception
144 {
145 if (singletonComponentLifecycleAdapter == null && lifecycleAdapter != null)
146 {
147 lifecycleAdapter.stop();
148 lifecycleAdapter.dispose();
149 lifecycleAdapter = null;
150 }
151 }
152
153 }