View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.jca;
8   
9   import org.mule.RequestContext;
10  import org.mule.api.DefaultMuleException;
11  import org.mule.api.MessagingException;
12  import org.mule.api.MuleEvent;
13  import org.mule.api.MuleException;
14  import org.mule.api.component.LifecycleAdapter;
15  import org.mule.api.construct.FlowConstruct;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.model.EntryPointResolverSet;
18  import org.mule.component.AbstractJavaComponent;
19  import org.mule.config.i18n.CoreMessages;
20  import org.mule.config.i18n.Message;
21  import org.mule.module.jca.i18n.JcaMessages;
22  import org.mule.work.AbstractMuleEventWork;
23  
24  import javax.resource.spi.UnavailableException;
25  import javax.resource.spi.endpoint.MessageEndpoint;
26  import javax.resource.spi.endpoint.MessageEndpointFactory;
27  import javax.resource.spi.work.WorkEvent;
28  import javax.resource.spi.work.WorkListener;
29  import javax.resource.spi.work.WorkManager;
30  
31  public class JcaComponent extends AbstractJavaComponent implements WorkListener
32  {
33      protected MessageEndpointFactory messageEndpointFactory;
34      protected WorkManager workManager;
35  
36      public JcaComponent(MessageEndpointFactory messageEndpointFactory,
37                          EntryPointResolverSet entryPointResolverSet,
38                          FlowConstruct flowConstruct,
39                          WorkManager workManager)
40      {
41          this.messageEndpointFactory = messageEndpointFactory;
42          this.entryPointResolverSet = entryPointResolverSet;
43          this.flowConstruct = flowConstruct;
44          this.workManager = workManager;
45      }
46  
47      /*
48       * The service ins actually managed by the Application Server container,Since the
49       * instance might be pooled by the server, we should use the
50       * MessageEndPointFactory to delegate the request for creation to the container.
51       * The container might create a Proxy object to intercept the actual method call
52       * to implement transaction,security related functionalities
53       */
54      public Object getManagedInstance() throws UnavailableException, MuleException
55      {
56          return messageEndpointFactory.createEndpoint(null);
57      }
58  
59      @Override
60      public Object doInvoke(MuleEvent event)
61      {
62          try
63          {
64              workManager.scheduleWork(new MuleJcaWorker(event), WorkManager.INDEFINITE, null, this);
65          }
66          catch (Exception e)
67          {
68              logger.error(CoreMessages.failedToInvoke("Service: " + flowConstruct.getName()));
69          }
70          return null;
71      }
72  
73      public Class getObjectType()
74      {
75          return MessageEndpoint.class;
76      }
77  
78      @Override
79      protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
80      {
81          // Template method unused because doOnCall and doOnEvent have been overridden
82          return null;
83      }
84  
85      @Override
86      protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter)
87      {
88          // Template method unused because doOnCall and doOnEvent have been overridden
89      }
90  
91      @Override
92      protected void doInitialise() throws InitialisationException
93      {
94          // no-op no object-factory
95      }
96  
97      public class MuleJcaWorker extends AbstractMuleEventWork
98      {
99  
100         MuleJcaWorker(MuleEvent event)
101         {
102             super(event);
103         }
104 
105         public void doRun()
106         {
107 
108             if (logger.isTraceEnabled())
109             {
110                 logger.trace("MuleJcaWorker: async MuleEvent for Mule  JCA EndPoint " + flowConstruct.getName());
111             }
112             try
113             {
114                 // Invoke method
115                 entryPointResolverSet.invoke(getManagedInstance(), RequestContext.getEventContext());
116             }
117             catch (Exception e)
118             {
119                 if (e instanceof UnavailableException)
120                 {
121                     Message message = JcaMessages.cannotAllocateManagedInstance();
122                     logger.error(message);
123                     flowConstruct.getExceptionListener().handleException(new DefaultMuleException(message, e), event);
124                 }
125                 else if (e instanceof MessagingException)
126                 {
127                     logger.error("Failed to execute  JCAEndPoint " + e.getMessage(), e);
128                     flowConstruct.getExceptionListener().handleException(e, event);
129                 }
130                 else
131                 {
132                     flowConstruct.getExceptionListener().handleException(
133                         new DefaultMuleException(CoreMessages.eventProcessingFailedFor(flowConstruct.getName()), e), event);
134                 }
135             }
136         }
137     }
138 
139     public void workAccepted(WorkEvent arg0)
140     {
141         // TODO Auto-generated method stub
142     }
143 
144     public void workCompleted(WorkEvent arg0)
145     {
146         // TODO Auto-generated method stub
147     }
148 
149     public void workRejected(WorkEvent arg0)
150     {
151         // TODO Auto-generated method stub
152     }
153 
154     public void workStarted(WorkEvent arg0)
155     {
156         // TODO Auto-generated method stub
157     }
158 
159 }