View Javadoc

1   /*
2    * $Id: JcaComponent.java 11488 2008-03-24 14:38:49Z 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.module.jca;
12  
13  import org.mule.OptimizedRequestContext;
14  import org.mule.RequestContext;
15  import org.mule.api.MessagingException;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleException;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.component.LifecycleAdapter;
20  import org.mule.api.lifecycle.InitialisationException;
21  import org.mule.api.model.EntryPointResolverSet;
22  import org.mule.api.service.Service;
23  import org.mule.component.AbstractJavaComponent;
24  import org.mule.config.i18n.CoreMessages;
25  import org.mule.config.i18n.Message;
26  import org.mule.module.jca.i18n.JcaMessages;
27  
28  import javax.resource.spi.UnavailableException;
29  import javax.resource.spi.endpoint.MessageEndpoint;
30  import javax.resource.spi.endpoint.MessageEndpointFactory;
31  import javax.resource.spi.work.Work;
32  import javax.resource.spi.work.WorkEvent;
33  import javax.resource.spi.work.WorkListener;
34  import javax.resource.spi.work.WorkManager;
35  
36  public class JcaComponent extends AbstractJavaComponent implements WorkListener
37  {
38      protected MessageEndpointFactory messageEndpointFactory;
39      protected WorkManager workManager;
40  
41      public JcaComponent(MessageEndpointFactory messageEndpointFactory,
42                          EntryPointResolverSet entryPointResolverSet,
43                          Service service,
44                          WorkManager workManager)
45      {
46          this.messageEndpointFactory = messageEndpointFactory;
47          this.entryPointResolverSet = entryPointResolverSet;
48          this.service = service;
49          this.workManager = workManager;
50      }
51  
52      /*
53       * The service ins actually managed by the Application Server container,Since the
54       * instance might be pooled by the server, we should use the
55       * MessageEndPointFactory to delegate the request for creation to the container.
56       * The container might create a Proxy object to intercept the actual method call
57       * to implement transaction,security related functionalities
58       */
59      public Object getManagedInstance() throws UnavailableException, MuleException
60      {
61          return messageEndpointFactory.createEndpoint(null);
62      }
63  
64      public MuleMessage doOnCall(MuleEvent event)
65      {
66          try
67          {
68              workManager.scheduleWork(new MuleJcaWorker(event), WorkManager.INDEFINITE, null, this);
69          }
70          catch (Exception e)
71          {
72              logger.error(CoreMessages.failedToInvoke("UMO Service: " + service.getName()));
73          }
74          return null;
75      }
76  
77      public Class getObjectType()
78      {
79          return MessageEndpoint.class;
80      }
81  
82      // @Override
83      protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
84      {
85          // Template method unused because doOnCall and doOnEvent have been overridden
86          return null;
87      }
88  
89      // @Override
90      protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter)
91      {
92          // Template method unused because doOnCall and doOnEvent have been overridden
93      }
94  
95      // @Override
96      protected void doInitialise() throws InitialisationException
97      {
98          // no-op no object-factory
99      }
100 
101     public class MuleJcaWorker implements Work
102     {
103 
104         private MuleEvent event;
105 
106         MuleJcaWorker(MuleEvent event)
107         {
108             this.event = event;
109         }
110 
111         public void release()
112         {
113             // TODO Auto-generated method stub
114         }
115 
116         public void run()
117         {
118 
119             if (logger.isTraceEnabled())
120             {
121                 logger.trace("MuleJcaWorker: async MuleEvent for Mule  JCA EndPoint " + service.getName());
122             }
123             try
124             {
125                 // Invoke method
126                 event = OptimizedRequestContext.criticalSetEvent(event);
127                 entryPointResolverSet.invoke(getManagedInstance(), RequestContext.getEventContext());
128             }
129             catch (Exception e)
130             {
131                 if (e instanceof UnavailableException)
132                 {
133                     Message message = JcaMessages.cannotAllocateManagedInstance();
134                     logger.error(message);
135                     service.getExceptionListener().exceptionThrown(new MessagingException(message, e));
136                 }
137                 else if (e instanceof MessagingException)
138                 {
139                     logger.error("Failed to execute  JCAEndPoint " + e.getMessage(), e);
140                     service.getExceptionListener().exceptionThrown(e);
141                 }
142                 else
143                 {
144                     service.getExceptionListener().exceptionThrown(
145                         new MessagingException(CoreMessages.eventProcessingFailedFor(service.getName()), e));
146                 }
147             }
148         }
149     }
150 
151     public void workAccepted(WorkEvent arg0)
152     {
153         // TODO Auto-generated method stub
154     }
155 
156     public void workCompleted(WorkEvent arg0)
157     {
158         // TODO Auto-generated method stub
159     }
160 
161     public void workRejected(WorkEvent arg0)
162     {
163         // TODO Auto-generated method stub
164     }
165 
166     public void workStarted(WorkEvent arg0)
167     {
168         // TODO Auto-generated method stub
169     }
170 
171 }