View Javadoc

1   /*
2    * $Id: JcaComponent.java 10130 2007-12-22 17:07:56Z dfeist $
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.OptimizedRequestContext;
18  import org.mule.impl.RequestContext;
19  import org.mule.impl.model.AbstractComponent;
20  import org.mule.ra.i18n.JcaMessages;
21  import org.mule.umo.ComponentException;
22  import org.mule.umo.MessagingException;
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.manager.ObjectNotFoundException;
29  import org.mule.umo.manager.UMOWorkManager;
30  import org.mule.umo.model.UMOEntryPoint;
31  
32  import javax.resource.spi.UnavailableException;
33  import javax.resource.spi.endpoint.MessageEndpointFactory;
34  import javax.resource.spi.work.Work;
35  import javax.resource.spi.work.WorkEvent;
36  import javax.resource.spi.work.WorkListener;
37  import javax.resource.spi.work.WorkManager;
38  
39  /**
40   * <code>JcaComponent</code> Is the type of component used in Mule when embedded
41   * inside an app server using JCA. In the future we might want to use one of the
42   * existing models.
43   */
44  public class JcaComponent extends AbstractComponent implements WorkListener
45  {
46  
47      /**
48       * Serial version
49       */
50      private static final long serialVersionUID = -1510441245219710451L;
51  
52      private transient UMOEntryPoint entryPoint;
53      protected UMOWorkManager workManager;
54  
55      public JcaComponent(MuleDescriptor descriptor, UMOWorkManager workManager)
56      {
57  
58          super(descriptor, MuleManager.getInstance().lookupModel(descriptor.getModelName()));
59          this.workManager = workManager;
60      }
61  
62      public UMODescriptor getDescriptor()
63      {
64          return descriptor;
65      }
66  
67      /**
68       * This is the synchronous call method and not supported by components managed in
69       * a JCA container
70       * 
71       * @param event
72       * @return
73       * @throws UMOException
74       */
75      public UMOMessage sendEvent(UMOEvent event) throws UMOException
76      {
77          throw new UnsupportedOperationException("sendEvent()");
78      }
79  
80      public boolean isPaused()
81      {
82          // JcaComponent is a wrapper for a hosted component implementation and
83          // therefore cannot be paused by mule
84          return false;
85      }
86  
87      protected void waitIfPaused(UMOEvent event) throws InterruptedException
88      {
89          // JcaComponent is a wrapper for a hosted component implementation and
90          // therefore cannot be paused by mule
91      }
92  
93      protected void doPause() throws UMOException
94      {
95          throw new ComponentException(JcaMessages.cannotPauseResumeJcaComponent(), null, this);
96      }
97  
98      protected void doResume() throws UMOException
99      {
100         throw new ComponentException(JcaMessages.cannotPauseResumeJcaComponent(), null, this);
101     }
102 
103     public synchronized void doInitialise() throws InitialisationException
104     {
105         try
106         {
107             entryPoint = MuleManager.getInstance()
108                 .lookupModel(this.getDescriptor().getModelName())
109                 .getEntryPointResolver()
110                 .resolveEntryPoint(descriptor);
111         }
112         catch (UMOException e)
113         {
114             throw new InitialisationException(e, this);
115         }
116     }
117 
118     protected void doDispatch(UMOEvent event) throws UMOException
119     {
120         try
121         {
122             workManager.scheduleWork(new MuleJcaWorker(event), WorkManager.INDEFINITE, null, this);
123         }
124         catch (Exception e)
125         {
126             throw new MuleException(CoreMessages.failedToInvoke("UMO Component: " + descriptor.getName()), e);
127         }
128     }
129 
130     /**
131      * Implementation of template method which is never call because send() is
132      * overwritten
133      */
134     protected UMOMessage doSend(UMOEvent event) throws UMOException
135     {
136         return null;
137     }
138 
139     /*
140      * The component ins actually managed by the Application Server container,Since
141      * the instance might be pooled by the server, we should use the
142      * MessageEndPointFactory to delegate the request for creation to the container.
143      * The container might create a Proxy object to intercept the actual method call
144      * to implement transaction,security related functionalities
145      */
146     public Object getManagedInstance() throws UMOException
147     {
148         Object managedInstance = null;
149         try
150         {
151 
152             MessageEndpointFactory messageEndpointFactory = (MessageEndpointFactory) descriptor.getImplementation();
153             managedInstance = messageEndpointFactory.createEndpoint(null);
154 
155         }
156         catch (UnavailableException e)
157         {
158 
159             logger.error("Request Failed to allocate Managed Instance" + e.getMessage(), e);
160             throw new ObjectNotFoundException(this.getName(), e);
161         }
162 
163         return managedInstance;
164     }
165 
166     public class MuleJcaWorker implements Work
167     {
168 
169         private UMOEvent event;
170 
171         MuleJcaWorker(UMOEvent event)
172         {
173             this.event = event;
174         }
175 
176         public void release()
177         {
178             // TODO Auto-generated method stub
179         }
180 
181         public void run()
182         {
183 
184             if (logger.isTraceEnabled())
185             {
186                 logger.trace("MuleJcaWorker: async Event for Mule  JCA EndPoint " + descriptor.getName());
187             }
188             try
189             {
190                 // Invoke method
191                 event = OptimizedRequestContext.criticalSetEvent(event);
192                 entryPoint.invoke(getManagedInstance(), RequestContext.getEventContext());
193             }
194             catch (Exception e)
195             {
196                 if (e instanceof MessagingException)
197                 {
198                     logger.error("Failed to execute  JCAEndPoint " + e.getMessage(), e);
199                     handleException(e);
200                 }
201                 else
202                 {
203                     handleException(new MessagingException(CoreMessages.eventProcessingFailedFor(descriptor.getName()),
204                         e));
205                 }
206             }
207         }
208     }
209 
210     public void workAccepted(WorkEvent arg0)
211     {
212         // TODO Auto-generated method stub
213     }
214 
215     public void workCompleted(WorkEvent arg0)
216     {
217         // TODO Auto-generated method stub
218     }
219 
220     public void workRejected(WorkEvent arg0)
221     {
222         // TODO Auto-generated method stub
223     }
224 
225     public void workStarted(WorkEvent arg0)
226     {
227         // TODO Auto-generated method stub
228     }
229 }