1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc.
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.jencks;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.jms.Message;
24 import javax.jms.MessageListener;
25 import javax.resource.ResourceException;
26 import javax.resource.spi.LocalTransaction;
27 import javax.resource.spi.endpoint.MessageEndpoint;
28 import java.lang.reflect.Method;
29
30 /***
31 * Performs a local transaction while processing the message
32 *
33 * @version $Revision: 1.1.1.1 $
34 */
35 public class LocalTransactionEndpoint implements MessageEndpoint, MessageListener {
36
37 private static final Log log = LogFactory.getLog(LocalTransactionEndpoint.class);
38
39 private MessageListener messageListener;
40 private LocalTransaction localTransaction;
41
42 public LocalTransactionEndpoint(MessageListener messageListener, LocalTransaction localTransaction) {
43 this.messageListener = messageListener;
44 this.localTransaction = localTransaction;
45 }
46
47 public void beforeDelivery(Method method) throws NoSuchMethodException, ResourceException {
48 getLocalTransaction().begin();
49 }
50
51 public void afterDelivery() throws ResourceException {
52 getLocalTransaction().commit();
53 }
54
55 public void release() {
56 if (localTransaction != null) {
57 try {
58 localTransaction.rollback();
59 }
60 catch (ResourceException e) {
61 log.warn("Failed to rollback local transaction: " + e, e);
62 }
63 localTransaction = null;
64 }
65 }
66
67 public void onMessage(Message message) {
68 messageListener.onMessage(message);
69 }
70
71
72 /***
73 * A getter which will return the current local transaction or throw a new exception
74 * if this endpoint has already been released.
75 */
76 protected LocalTransaction getLocalTransaction() throws ResourceException {
77 if (localTransaction == null) {
78 throw new ResourceException("This endpoint has already been released via a call to release() you cannot deliver messages to me");
79 }
80 return localTransaction;
81 }
82
83 }