001 /**
002 *
003 * Copyright 2004 Hiram Chirino
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.ra;
019
020 import java.lang.reflect.Method;
021
022 import javax.jms.Connection;
023 import javax.jms.ConnectionConsumer;
024 import javax.jms.JMSException;
025 import javax.jms.Message;
026 import javax.jms.MessageListener;
027 import javax.jms.Session;
028 import javax.resource.ResourceException;
029 import javax.resource.spi.endpoint.MessageEndpointFactory;
030 import javax.resource.spi.work.WorkException;
031 import javax.resource.spi.work.WorkManager;
032
033 import org.apache.commons.logging.Log;
034 import org.apache.commons.logging.LogFactory;
035
036 /**
037 * @version $Revision: 1.1.1.1 $ $Date: 2005/03/11 21:15:09 $
038 */
039 public abstract class ActiveMQBaseEndpointWorker {
040
041 private static final Log log = LogFactory.getLog(ActiveMQBaseEndpointWorker.class);
042 public static final Method ON_MESSAGE_METHOD;
043
044 static {
045 try {
046 ON_MESSAGE_METHOD = MessageListener.class.getMethod("onMessage", new Class[]{Message.class});
047 }
048 catch (Exception e) {
049 throw new ExceptionInInitializerError(e);
050 }
051 }
052
053 protected ActiveMQResourceAdapter adapter;
054 protected ActiveMQEndpointActivationKey endpointActivationKey;
055 protected MessageEndpointFactory endpointFactory;
056 protected WorkManager workManager;
057 protected boolean transacted;
058
059 /**
060 * @param s
061 */
062 public static void safeClose(Session s) {
063 try {
064 if (s != null) {
065 s.close();
066 }
067 }
068 catch (JMSException e) {
069 }
070 }
071
072 /**
073 * @param c
074 */
075 public static void safeClose(Connection c) {
076 try {
077 if (c != null) {
078 c.close();
079 }
080 }
081 catch (JMSException e) {
082 }
083 }
084
085 /**
086 * @param cc
087 */
088 public static void safeClose(ConnectionConsumer cc) {
089 try {
090 if (cc != null) {
091 cc.close();
092 }
093 }
094 catch (JMSException e) {
095 }
096 }
097
098 public ActiveMQBaseEndpointWorker(ActiveMQResourceAdapter adapter, ActiveMQEndpointActivationKey key) throws ResourceException {
099 this.endpointActivationKey = key;
100 this.adapter = adapter;
101 this.endpointFactory = endpointActivationKey.getMessageEndpointFactory();
102 this.workManager = adapter.getBootstrapContext().getWorkManager();
103 try {
104 this.transacted = endpointFactory.isDeliveryTransacted(ON_MESSAGE_METHOD);
105 }
106 catch (NoSuchMethodException e) {
107 throw new ResourceException("Endpoint does not implement the onMessage method.");
108 }
109 }
110
111 public abstract void start() throws WorkException, ResourceException;
112
113 public abstract void stop() throws InterruptedException;
114
115
116 }