Source code: org/activemq/systest/impl/OlderBrokerAgentImpl.java
1 /**
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
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.activemq.systest.impl;
19
20 import org.activemq.ActiveMQConnectionFactory;
21 import org.activemq.broker.BrokerContainer;
22 import org.activemq.broker.impl.BrokerContainerImpl;
23 import org.activemq.store.vm.VMPersistenceAdapter;
24 import org.activemq.systest.AgentStopper;
25 import org.activemq.systest.AgentSupport;
26 import org.activemq.systest.BrokerAgent;
27 import org.activemq.transport.NetworkConnector;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import javax.jms.ConnectionFactory;
32
33 /**
34 *
35 * @version $Revision: 1.1 $
36 */
37 public class OlderBrokerAgentImpl extends AgentSupport implements BrokerAgent {
38 private static final Log log = LogFactory.getLog(OlderBrokerAgentImpl.class);
39
40 private static int port = 61616;
41
42 private String connectionURI;
43 private BrokerContainer broker;
44 private boolean peristent;
45 private boolean started;
46
47
48 public OlderBrokerAgentImpl() {
49 connectionURI = "tcp://localhost:" + (port++);
50 log.info("Creating a broker with transport connector: " + connectionURI);
51
52 }
53
54 public void kill() throws Exception {
55 stop();
56 }
57
58 public ConnectionFactory getConnectionFactory() {
59 return new ActiveMQConnectionFactory(connectionURI);
60 }
61
62 public void connectTo(BrokerAgent remoteBroker) throws Exception {
63 String remoteURI = remoteBroker.getConnectionURI();
64 log.info("Adding a network connector: " + remoteURI);
65 NetworkConnector connector = getBroker().addNetworkConnector(remoteURI);
66 if (started) {
67 connector.start();
68 }
69 }
70
71 public void start() throws Exception {
72 started = true;
73 getBroker().start();
74 }
75
76 public void stop(AgentStopper stopper) {
77 if (broker != null) {
78 try {
79 broker.stop();
80 }
81 catch (Exception e) {
82 stopper.onException(this, e);
83 }
84 finally {
85 broker = null;
86 started = false;
87 }
88 }
89 }
90
91 public void setBroker(BrokerContainer broker) {
92 this.broker = broker;
93 }
94
95 public String getConnectionURI() {
96 return connectionURI;
97 }
98
99 public void setConnectionURI(String brokerURL) {
100 this.connectionURI = brokerURL;
101 }
102
103 public boolean isPeristent() {
104 return peristent;
105 }
106
107 public void setPeristent(boolean peristent) {
108 this.peristent = peristent;
109 }
110
111 public BrokerContainer getBroker() throws Exception {
112 if (broker == null) {
113 broker = createBroker();
114 }
115 return broker;
116 }
117
118 protected BrokerContainer createBroker() throws Exception {
119 BrokerContainerImpl answer = new BrokerContainerImpl();
120 if (!isPeristent()) {
121 answer.setPersistenceAdapter(new VMPersistenceAdapter());
122 }
123 answer.addConnector(getConnectionURI());
124 return answer;
125 }
126 }