01: /*
02: * $Id: ConnectionInvocationHandler.java 10590 2008-01-29 01:39:47Z tcarlson $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10: package org.mule.transport.jms.xa;
11:
12: import org.mule.transaction.XaTransaction;
13:
14: import java.lang.reflect.InvocationHandler;
15: import java.lang.reflect.Method;
16: import java.lang.reflect.Proxy;
17:
18: import javax.jms.QueueSession;
19: import javax.jms.Session;
20: import javax.jms.TopicSession;
21: import javax.jms.XAConnection;
22: import javax.jms.XAQueueConnection;
23: import javax.jms.XAQueueSession;
24: import javax.jms.XASession;
25: import javax.jms.XATopicConnection;
26: import javax.jms.XATopicSession;
27:
28: public class ConnectionInvocationHandler implements InvocationHandler {
29:
30: private Object xaConnection;
31:
32: public ConnectionInvocationHandler(Object xac) {
33: this .xaConnection = xac;
34: }
35:
36: /**
37: * Can be one of 3 types.
38: * TODO check if we can portably cast it (JMS 1.1 vs 1.0.2b), see Jms102bSupport why
39: *
40: * @return underlying XAConnection instance
41: */
42: public Object getTargetObject() {
43: return xaConnection;
44: }
45:
46: /*
47: * (non-Javadoc)
48: *
49: * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
50: * java.lang.reflect.Method, java.lang.Object[])
51: */
52: public Object invoke(Object proxy, Method method, Object[] args)
53: throws Throwable {
54: if (ConnectionFactoryWrapper.logger.isDebugEnabled()) {
55: ConnectionFactoryWrapper.logger.debug("Invoking " + method);
56: }
57: if (method.getName().equals("createSession")) {
58: XASession xas = ((XAConnection) xaConnection)
59: .createXASession();
60: return Proxy.newProxyInstance(Session.class
61: .getClassLoader(), new Class[] { Session.class,
62: XaTransaction.MuleXaObject.class },
63: new SessionInvocationHandler(xas));
64: } else if (method.getName().equals("createQueueSession")) {
65: XAQueueSession xaqs = ((XAQueueConnection) xaConnection)
66: .createXAQueueSession();
67: return Proxy.newProxyInstance(Session.class
68: .getClassLoader(), new Class[] {
69: QueueSession.class,
70: XaTransaction.MuleXaObject.class },
71: new SessionInvocationHandler(xaqs));
72: } else if (method.getName().equals("createTopicSession")) {
73: XATopicSession xats = ((XATopicConnection) xaConnection)
74: .createXATopicSession();
75: return Proxy.newProxyInstance(Session.class
76: .getClassLoader(), new Class[] {
77: TopicSession.class,
78: XaTransaction.MuleXaObject.class },
79: new SessionInvocationHandler(xats));
80: } else {
81: return method.invoke(xaConnection, args);
82: }
83: }
84:
85: }
|