1 /***
2 *
3 * Copyright 2004 Hiram Chirino
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.ra;
19
20 import java.lang.reflect.Method;
21
22 import javax.jms.ConnectionConsumer;
23 import javax.jms.JMSException;
24 import javax.jms.Message;
25 import javax.jms.MessageListener;
26 import javax.jms.Session;
27 import javax.resource.ResourceException;
28 import javax.resource.spi.endpoint.MessageEndpointFactory;
29 import javax.resource.spi.work.WorkException;
30 import javax.resource.spi.work.WorkManager;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /***
36 * @version $Revision: 1.2 $ $Date: 2004/07/30 01:52:52 $
37 */
38 public abstract class ActiveMQBaseEndpointWorker {
39
40 private static final Log log = LogFactory.getLog(ActiveMQBaseEndpointWorker.class);
41 public static final Method ON_MESSAGE_METHOD;
42
43 static {
44 try {
45 ON_MESSAGE_METHOD = MessageListener.class.getMethod("onMessage", new Class[]{Message.class});
46 } catch (Exception e) {
47 throw new ExceptionInInitializerError(e);
48 }
49 }
50
51 protected ActiveMQResourceAdapter adapter;
52 protected ActiveMQEndpointActivationKey endpointActivationKey;
53 protected MessageEndpointFactory endpointFactory;
54 protected WorkManager workManager;
55 protected boolean transacted;
56
57 /***
58 * @param s
59 */
60 public static void safeClose(Session s) {
61 try {
62 if (s != null) {
63 s.close();
64 }
65 } catch (JMSException e) {
66 }
67 }
68
69 /***
70 * @param cc
71 */
72 public static void safeClose(ConnectionConsumer cc) {
73 try {
74 if (cc != null) {
75 cc.close();
76 }
77 } catch (JMSException e) {
78 }
79 }
80
81 public ActiveMQBaseEndpointWorker(ActiveMQResourceAdapter adapter, ActiveMQEndpointActivationKey key) throws ResourceException {
82 this.endpointActivationKey = key;
83 this.adapter = adapter;
84 this.endpointFactory = endpointActivationKey.getMessageEndpointFactory();
85 this.workManager = adapter.getBootstrapContext().getWorkManager();
86 try {
87 this.transacted = endpointFactory.isDeliveryTransacted(ON_MESSAGE_METHOD);
88 } catch (NoSuchMethodException e) {
89 throw new ResourceException("Endpoint does not implement the onMessage method.");
90 }
91 }
92
93 public abstract void start() throws WorkException, ResourceException;
94
95 public abstract void stop() throws InterruptedException;
96
97
98 }