KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > ra > JcaComponent


1 /*
2  * $Id: JcaComponent.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.ra;
12
13 import org.mule.MuleException;
14 import org.mule.MuleManager;
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.Messages;
17 import org.mule.impl.MuleDescriptor;
18 import org.mule.impl.RequestContext;
19 import org.mule.impl.container.ContainerKeyPair;
20 import org.mule.impl.internal.notifications.ComponentNotification;
21 import org.mule.management.stats.ComponentStatistics;
22 import org.mule.umo.UMOComponent;
23 import org.mule.umo.UMODescriptor;
24 import org.mule.umo.UMOEvent;
25 import org.mule.umo.UMOException;
26 import org.mule.umo.UMOMessage;
27 import org.mule.umo.lifecycle.InitialisationException;
28 import org.mule.umo.lifecycle.RecoverableException;
29 import org.mule.umo.manager.ObjectNotFoundException;
30 import org.mule.umo.model.ModelException;
31 import org.mule.umo.model.UMOEntryPoint;
32
33 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
34
35 /**
36  * <code>JcaComponent</code> Is the type of component used in Mule when embedded
37  * inside an app server using JCA. In the future we might want to use one of the
38  * existing models.
39  */

40 public class JcaComponent implements UMOComponent
41 {
42     /**
43      * Serial version
44      */

45     private static final long serialVersionUID = -1510441245219710451L;
46
47     private transient final MuleDescriptor descriptor;
48     private transient UMOEntryPoint entryPoint;
49     private Object JavaDoc component;
50     private ComponentStatistics stats;
51
52     /**
53      * Determines if the component has been initialised
54      */

55     private final AtomicBoolean initialised = new AtomicBoolean(false);
56
57     /**
58      * Determines if the component has been started
59      */

60     private final AtomicBoolean started = new AtomicBoolean(false);
61
62     public JcaComponent(MuleDescriptor descriptor)
63     {
64         if (descriptor == null)
65         {
66             throw new IllegalArgumentException JavaDoc("Descriptor cannot be null");
67         }
68
69         this.descriptor = descriptor;
70     }
71
72     public UMODescriptor getDescriptor()
73     {
74         return descriptor;
75     }
76
77     public void dispatchEvent(UMOEvent event) throws UMOException
78     {
79         try
80         {
81             // Invoke method
82
entryPoint.invoke(component, RequestContext.getEventContext());
83         }
84         catch (Exception JavaDoc e)
85         {
86             throw new MuleException(new Message(Messages.FAILED_TO_INVOKE_X, "UMO Component: "
87                                                                              + descriptor.getName()), e);
88         }
89     }
90
91     /**
92      * This is the synchronous call method and not supported by components managed in
93      * a JCA container
94      *
95      * @param event
96      * @return
97      * @throws UMOException
98      */

99     public UMOMessage sendEvent(UMOEvent event) throws UMOException
100     {
101         throw new UnsupportedOperationException JavaDoc("sendEvent()");
102     }
103
104     public void pause() throws UMOException
105     {
106         // nothing to do
107
}
108
109     public void resume() throws UMOException
110     {
111         // nothing to do
112
}
113
114     public boolean isPaused()
115     {
116         return false;
117     }
118
119     public void start() throws UMOException
120     {
121         started.set(true);
122     }
123
124     public void stop() throws UMOException
125     {
126         started.set(false);
127     }
128
129     public void dispose()
130     {
131         ((MuleManager)MuleManager.getInstance()).getStatistics().remove(stats);
132     }
133
134     public synchronized void initialise() throws InitialisationException, RecoverableException
135     {
136         if (initialised.get())
137         {
138             throw new InitialisationException(new Message(Messages.OBJECT_X_ALREADY_INITIALISED,
139                 "Component '" + descriptor.getName() + "'"), this);
140         }
141         descriptor.initialise();
142         try
143         {
144             entryPoint = MuleManager.getInstance().getModel().getEntryPointResolver().resolveEntryPoint(
145                 descriptor);
146         }
147         catch (ModelException e)
148         {
149             throw new InitialisationException(e, this);
150         }
151
152         // initialise statistics
153
stats = new ComponentStatistics(descriptor.getName(), -1, -1);
154
155         stats.setEnabled(((MuleManager)MuleManager.getInstance()).getStatistics().isEnabled());
156         ((MuleManager)MuleManager.getInstance()).getStatistics().add(stats);
157         stats.setOutboundRouterStat(getDescriptor().getOutboundRouter().getStatistics());
158         stats.setInboundRouterStat(getDescriptor().getInboundRouter().getStatistics());
159
160         component = descriptor.getImplementation();
161
162         initialised.set(true);
163         MuleManager.getInstance().fireNotification(
164             new ComponentNotification(descriptor, ComponentNotification.COMPONENT_INITIALISED));
165     }
166
167     protected Object JavaDoc getDelegateComponent() throws InitialisationException
168     {
169         Object JavaDoc impl = descriptor.getImplementation();
170         Object JavaDoc component = null;
171
172         try
173         {
174             if (impl instanceof ContainerKeyPair)
175             {
176                 component = MuleManager.getInstance().getContainerContext().getComponent(impl);
177
178                 if (descriptor.isSingleton())
179                 {
180                     descriptor.setImplementation(component);
181                 }
182             }
183             else
184             {
185                 component = impl;
186             }
187         }
188         catch (ObjectNotFoundException e)
189         {
190             throw new InitialisationException(e, this);
191         }
192
193         // Call any custom initialisers
194
descriptor.fireInitialisationCallbacks(component);
195         return component;
196     }
197
198     public boolean isStarted()
199     {
200         return started.get();
201     }
202
203     /**
204      * Gets the underlying instance form this component Where the Component
205      * implmentation provides pooling this is no 1-2-1 mapping between UMOComponent
206      * and instance, so this method will return the object in initial state. <p/> If
207      * the underlying component is Container managed in Spring or another IoC
208      * container then the object instance in the IoC container will be returned
209      *
210      * @return the underlying instance form this component
211      */

212     public Object JavaDoc getInstance() throws UMOException
213     {
214         return component;
215     }
216 }
217
Popular Tags