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