001: /*
002: * $Id: JcaComponent.java 11414 2008-03-18 03:16:19Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.jca;
012:
013: import org.mule.OptimizedRequestContext;
014: import org.mule.RequestContext;
015: import org.mule.api.MessagingException;
016: import org.mule.api.MuleEvent;
017: import org.mule.api.MuleException;
018: import org.mule.api.MuleMessage;
019: import org.mule.api.component.LifecycleAdapter;
020: import org.mule.api.lifecycle.InitialisationException;
021: import org.mule.api.model.EntryPointResolverSet;
022: import org.mule.api.service.Service;
023: import org.mule.component.AbstractJavaComponent;
024: import org.mule.config.i18n.CoreMessages;
025: import org.mule.config.i18n.Message;
026: import org.mule.module.jca.i18n.JcaMessages;
027:
028: import javax.resource.spi.UnavailableException;
029: import javax.resource.spi.endpoint.MessageEndpoint;
030: import javax.resource.spi.endpoint.MessageEndpointFactory;
031: import javax.resource.spi.work.Work;
032: import javax.resource.spi.work.WorkEvent;
033: import javax.resource.spi.work.WorkListener;
034: import javax.resource.spi.work.WorkManager;
035:
036: public class JcaComponent extends AbstractJavaComponent implements
037: WorkListener {
038: protected MessageEndpointFactory messageEndpointFactory;
039: protected WorkManager workManager;
040:
041: public JcaComponent(MessageEndpointFactory messageEndpointFactory,
042: EntryPointResolverSet entryPointResolverSet,
043: Service service, WorkManager workManager) {
044: this .messageEndpointFactory = messageEndpointFactory;
045: this .entryPointResolverSet = entryPointResolverSet;
046: this .service = service;
047: this .workManager = workManager;
048: }
049:
050: /*
051: * The service ins actually managed by the Application Server container,Since the
052: * instance might be pooled by the server, we should use the
053: * MessageEndPointFactory to delegate the request for creation to the container.
054: * The container might create a Proxy object to intercept the actual method call
055: * to implement transaction,security related functionalities
056: */
057: public Object getManagedInstance() throws UnavailableException,
058: MuleException {
059: return messageEndpointFactory.createEndpoint(null);
060: }
061:
062: public MuleMessage doOnCall(MuleEvent event) {
063: try {
064: workManager.scheduleWork(new MuleJcaWorker(event),
065: WorkManager.INDEFINITE, null, this );
066: } catch (Exception e) {
067: logger.error(CoreMessages.failedToInvoke("UMO Service: "
068: + service.getName()));
069: }
070: return null;
071: }
072:
073: public Class getObjectType() {
074: return MessageEndpoint.class;
075: }
076:
077: // @Override
078: protected LifecycleAdapter borrowComponentLifecycleAdaptor()
079: throws Exception {
080: // Template method unused because doOnCall and doOnEvent have been overridden
081: return null;
082: }
083:
084: // @Override
085: protected void returnComponentLifecycleAdaptor(
086: LifecycleAdapter lifecycleAdapter) {
087: // Template method unused because doOnCall and doOnEvent have been overridden
088: }
089:
090: // @Override
091: protected void doInitialise() throws InitialisationException {
092: // no-op no object-factory
093: }
094:
095: public class MuleJcaWorker implements Work {
096:
097: private MuleEvent event;
098:
099: MuleJcaWorker(MuleEvent event) {
100: this .event = event;
101: }
102:
103: public void release() {
104: // TODO Auto-generated method stub
105: }
106:
107: public void run() {
108:
109: if (logger.isTraceEnabled()) {
110: logger
111: .trace("MuleJcaWorker: async MuleEvent for Mule JCA EndPoint "
112: + service.getName());
113: }
114: try {
115: // Invoke method
116: event = OptimizedRequestContext.criticalSetEvent(event);
117: entryPointResolverSet.invoke(getManagedInstance(),
118: RequestContext.getEventContext());
119: } catch (Exception e) {
120: if (e instanceof UnavailableException) {
121: Message message = JcaMessages
122: .cannotAllocateManagedInstance();
123: logger.error(message);
124: handleException(new MessagingException(message, e));
125: } else if (e instanceof MessagingException) {
126: logger.error("Failed to execute JCAEndPoint "
127: + e.getMessage(), e);
128: handleException(e);
129: } else {
130: handleException(new MessagingException(
131: CoreMessages
132: .eventProcessingFailedFor(service
133: .getName()), e));
134: }
135: }
136: }
137: }
138:
139: public void workAccepted(WorkEvent arg0) {
140: // TODO Auto-generated method stub
141: }
142:
143: public void workCompleted(WorkEvent arg0) {
144: // TODO Auto-generated method stub
145: }
146:
147: public void workRejected(WorkEvent arg0) {
148: // TODO Auto-generated method stub
149: }
150:
151: public void workStarted(WorkEvent arg0) {
152: // TODO Auto-generated method stub
153: }
154:
155: }
|