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