1
2
3
4
5
6
7 package org.mule.transport.jms.integration.activemq;
8
9 import org.mule.transport.jms.test.TestReconnectionConnectionFactoryWrapper;
10 import org.mule.transport.jms.xa.TargetInvocationHandler;
11
12 import java.lang.reflect.Method;
13 import java.lang.reflect.Proxy;
14 import java.net.URI;
15 import java.util.Date;
16 import java.util.List;
17
18 import javax.jms.Connection;
19 import javax.jms.JMSException;
20 import javax.jms.QueueConnection;
21 import javax.jms.TopicConnection;
22
23 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
24
25 import org.apache.activemq.ActiveMQConnectionFactory;
26 import org.apache.activemq.Closeable;
27 import org.apache.activemq.StreamConnection;
28 import org.apache.activemq.management.StatsCapable;
29 import org.apache.activemq.transport.TransportListener;
30
31 public class ActiveMQTestReconnectionConnectionFactoryWrapper extends ActiveMQConnectionFactory
32 implements TargetInvocationHandler, TestReconnectionConnectionFactoryWrapper
33 {
34 private static List calledMethods;
35 private static volatile boolean enabled = true;
36 private static Connection connection;
37
38 public void init()
39 {
40 enabled = true;
41 calledMethods = new CopyOnWriteArrayList();
42 }
43
44 public ActiveMQTestReconnectionConnectionFactoryWrapper()
45 {
46 init();
47 }
48
49 public ActiveMQTestReconnectionConnectionFactoryWrapper(String brokerURL)
50 {
51 super(brokerURL);
52 init();
53 }
54
55 public ActiveMQTestReconnectionConnectionFactoryWrapper(URI brokerURL)
56 {
57 super(brokerURL);
58 init();
59 }
60
61 public ActiveMQTestReconnectionConnectionFactoryWrapper(String userName, String password, URI brokerURL)
62 {
63 super(userName, password, brokerURL);
64 init();
65 }
66
67 public ActiveMQTestReconnectionConnectionFactoryWrapper(String userName, String password, String brokerURL)
68 {
69 super(userName, password, brokerURL);
70 init();
71 }
72
73 @Override
74 public QueueConnection createQueueConnection() throws JMSException
75 {
76 registration();
77 connection = super.createQueueConnection();
78 return (QueueConnection) Proxy.newProxyInstance(
79 ActiveMQTestReconnectionConnectionFactoryWrapper.class.getClassLoader(), new Class[]{Connection.class,
80 TopicConnection.class, QueueConnection.class, StatsCapable.class, Closeable.class,
81 StreamConnection.class, TransportListener.class}, this);
82 }
83
84 @Override
85 public QueueConnection createQueueConnection(String userName, String password) throws JMSException
86 {
87 registration();
88 connection = super.createQueueConnection(userName, password);
89 return (QueueConnection) Proxy.newProxyInstance(
90 ActiveMQTestReconnectionConnectionFactoryWrapper.class.getClassLoader(), new Class[]{Connection.class,
91 TopicConnection.class, QueueConnection.class, StatsCapable.class, Closeable.class,
92 StreamConnection.class, TransportListener.class}, this);
93 }
94
95 @Override
96 public TopicConnection createTopicConnection() throws JMSException
97 {
98 registration();
99 connection = super.createTopicConnection();
100 return (TopicConnection) Proxy.newProxyInstance(
101 ActiveMQTestReconnectionConnectionFactoryWrapper.class.getClassLoader(), new Class[]{Connection.class,
102 TopicConnection.class, QueueConnection.class, StatsCapable.class, Closeable.class,
103 StreamConnection.class, TransportListener.class}, this);
104 }
105
106 @Override
107 public TopicConnection createTopicConnection(String userName, String password) throws JMSException
108 {
109 registration();
110 connection = super.createTopicConnection(userName, password);
111 return (TopicConnection) Proxy.newProxyInstance(
112 ActiveMQTestReconnectionConnectionFactoryWrapper.class.getClassLoader(), new Class[]{Connection.class,
113 TopicConnection.class, QueueConnection.class, StatsCapable.class, Closeable.class,
114 StreamConnection.class, TransportListener.class}, this);
115 }
116
117
118 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
119 {
120 registration();
121 return method.invoke(connection, args);
122 }
123
124 public Object getTargetObject()
125 {
126 return connection;
127 }
128
129
130
131
132
133
134
135 private void registration() throws JMSException
136 {
137
138
139 calledMethods.add(new Date());
140 if (!isEnabled())
141 {
142 if (connection.getExceptionListener() != null)
143 {
144 try
145 {
146 connection.getExceptionListener().onException(new JMSException("Disabled"));
147 }
148 catch (Exception e)
149 {
150 throw new JMSException("Disabled");
151 }
152 }
153 else
154 {
155 throw new JMSException("Disabled");
156 }
157 }
158
159 }
160
161 public List getCalledMethods()
162 {
163 return calledMethods;
164 }
165
166 public boolean isEnabled()
167 {
168 return enabled;
169 }
170
171 public void setEnabled(boolean enabled)
172 {
173 ActiveMQTestReconnectionConnectionFactoryWrapper.enabled = enabled;
174 }
175
176 public void closeConnection()
177 {
178 try
179 {
180 this.connection.close();
181 }
182 catch (Exception e)
183 {
184 System.out.println("Error while closing connection: " + e.getMessage());
185 }
186 }
187 }