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