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