1
2
3
4
5
6
7
8
9
10
11 package org.mule.ra;
12
13 import org.mule.MuleException;
14 import org.mule.MuleManager;
15 import org.mule.config.i18n.CoreMessages;
16 import org.mule.impl.MuleDescriptor;
17 import org.mule.impl.RequestContext;
18 import org.mule.impl.container.ContainerKeyPair;
19 import org.mule.impl.internal.notifications.ComponentNotification;
20 import org.mule.management.stats.ComponentStatistics;
21 import org.mule.umo.UMOComponent;
22 import org.mule.umo.UMODescriptor;
23 import org.mule.umo.UMOEvent;
24 import org.mule.umo.UMOException;
25 import org.mule.umo.UMOMessage;
26 import org.mule.umo.lifecycle.InitialisationException;
27 import org.mule.umo.manager.ObjectNotFoundException;
28 import org.mule.umo.model.UMOEntryPoint;
29
30 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
31
32
33
34
35
36
37 public class JcaComponent implements UMOComponent
38 {
39
40
41
42 private static final long serialVersionUID = -1510441245219710451L;
43
44 private transient final MuleDescriptor descriptor;
45 private transient UMOEntryPoint entryPoint;
46 private Object component;
47 private ComponentStatistics stats;
48
49
50
51
52 private final AtomicBoolean initialised = new AtomicBoolean(false);
53
54
55
56
57 private final AtomicBoolean started = new AtomicBoolean(false);
58
59 public JcaComponent(MuleDescriptor descriptor)
60 {
61 if (descriptor == null)
62 {
63 throw new IllegalArgumentException("Descriptor cannot be null");
64 }
65
66 this.descriptor = descriptor;
67 }
68
69 public UMODescriptor getDescriptor()
70 {
71 return descriptor;
72 }
73
74 public void dispatchEvent(UMOEvent event) throws UMOException
75 {
76 try
77 {
78
79 entryPoint.invoke(component, RequestContext.getEventContext());
80 }
81 catch (Exception e)
82 {
83 throw new MuleException(
84 CoreMessages.failedToInvoke("UMO Component: " + descriptor.getName()), e);
85 }
86 }
87
88
89
90
91
92
93
94
95
96 public UMOMessage sendEvent(UMOEvent event) throws UMOException
97 {
98 throw new UnsupportedOperationException("sendEvent()");
99 }
100
101 public void pause() throws UMOException
102 {
103
104 }
105
106 public void resume() throws UMOException
107 {
108
109 }
110
111 public boolean isPaused()
112 {
113 return false;
114 }
115
116 public void start() throws UMOException
117 {
118 started.set(true);
119 }
120
121 public void stop() throws UMOException
122 {
123 started.set(false);
124 }
125
126 public void dispose()
127 {
128 ((MuleManager)MuleManager.getInstance()).getStatistics().remove(stats);
129 }
130
131 public synchronized void initialise() throws InitialisationException
132 {
133 if (initialised.get())
134 {
135 throw new InitialisationException(
136 CoreMessages.objectAlreadyInitialised("Component '" + descriptor.getName() + "'"), this);
137 }
138 descriptor.initialise();
139 try
140 {
141 entryPoint = MuleManager.getInstance().lookupModel(JcaModel.JCA_MODEL_TYPE).getEntryPointResolver().resolveEntryPoint(
142 descriptor);
143 }
144 catch (UMOException e)
145 {
146 throw new InitialisationException(e, this);
147 }
148
149
150 stats = new ComponentStatistics(descriptor.getName(), -1);
151
152 stats.setEnabled(((MuleManager)MuleManager.getInstance()).getStatistics().isEnabled());
153 ((MuleManager)MuleManager.getInstance()).getStatistics().add(stats);
154 stats.setOutboundRouterStat(getDescriptor().getOutboundRouter().getStatistics());
155 stats.setInboundRouterStat(getDescriptor().getInboundRouter().getStatistics());
156
157 component = descriptor.getImplementation();
158
159 initialised.set(true);
160 MuleManager.getInstance().fireNotification(
161 new ComponentNotification(descriptor, ComponentNotification.COMPONENT_INITIALISED));
162 }
163
164 protected Object getDelegateComponent() throws InitialisationException
165 {
166 Object impl = descriptor.getImplementation();
167 Object component = null;
168
169 try
170 {
171 if (impl instanceof ContainerKeyPair)
172 {
173 component = MuleManager.getInstance().getContainerContext().getComponent(impl);
174
175 if (descriptor.isSingleton())
176 {
177 descriptor.setImplementation(component);
178 }
179 }
180 else
181 {
182 component = impl;
183 }
184 }
185 catch (ObjectNotFoundException e)
186 {
187 throw new InitialisationException(e, this);
188 }
189
190
191 descriptor.fireInitialisationCallbacks(component);
192 return component;
193 }
194
195 public boolean isStarted()
196 {
197 return started.get();
198 }
199
200
201
202
203
204
205
206
207
208
209 public Object getInstance() throws UMOException
210 {
211 return component;
212 }
213 }