View Javadoc

1   /*
2    * $Id: JcaComponent.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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.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   * <code>JcaComponent</code> Is the type of component used in Mule when embedded
34   * inside an app server using JCA. In the future we might want to use one of the
35   * existing models.
36   */
37  public class JcaComponent implements UMOComponent
38  {
39      /**
40       * Serial version
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       * Determines if the component has been initialised
51       */
52      private final AtomicBoolean initialised = new AtomicBoolean(false);
53  
54      /**
55       * Determines if the component has been started
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              // Invoke method
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       * This is the synchronous call method and not supported by components managed in
90       * a JCA container
91       * 
92       * @param event
93       * @return
94       * @throws UMOException
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         // nothing to do
104     }
105 
106     public void resume() throws UMOException
107     {
108         // nothing to do
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         // initialise statistics
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         // Call any custom initialisers
191         descriptor.fireInitialisationCallbacks(component);
192         return component;
193     }
194 
195     public boolean isStarted()
196     {
197         return started.get();
198     }
199 
200     /**
201      * Gets the underlying instance form this component Where the Component
202      * implmentation provides pooling this is no 1-2-1 mapping between UMOComponent
203      * and instance, so this method will return the object in initial state. <p/> If
204      * the underlying component is Container managed in Spring or another IoC
205      * container then the object instance in the IoC container will be returned
206      * 
207      * @return the underlying instance form this component
208      */
209     public Object getInstance() throws UMOException
210     {
211         return component;
212     }
213 }