KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > impl > SeparateBrokerProcessAgentImpl


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.systest.impl;
19
20 import java.io.File JavaDoc;
21
22 import org.apache.activemq.ActiveMQConnectionFactory;
23 import org.apache.activemq.systest.BrokerAgent;
24
25 import javax.jms.ConnectionFactory JavaDoc;
26
27
28 /**
29  * Runs a broker in a separate process
30  *
31  * @version $Revision: 1.1 $
32  */

33 public class SeparateBrokerProcessAgentImpl extends SeparateProcessAgent implements BrokerAgent {
34
35     private static final String JavaDoc ENV_HOME = "ACTIVEMQ_HOME";
36
37     private static int portCounter = 61616;
38
39     private int port;
40     private String JavaDoc connectionURI;
41     private String JavaDoc brokerScript;
42     private File JavaDoc workingDirectory = new File JavaDoc("target/test-brokers");
43     private String JavaDoc defaultPrefix = "~/activemq";
44     private String JavaDoc coreURI;
45
46     public SeparateBrokerProcessAgentImpl(String JavaDoc host) throws Exception JavaDoc {
47         port = portCounter++;
48         coreURI = "tcp://" + host + ":" + port;
49         connectionURI = "failover:(" + coreURI + ")?useExponentialBackOff=false&initialReconnectDelay=500&&maxReconnectAttempts=20";
50     }
51
52     public void kill() throws Exception JavaDoc {
53         stop();
54     }
55
56     public ConnectionFactory getConnectionFactory() {
57         return new ActiveMQConnectionFactory(getConnectionURI());
58     }
59
60     public String JavaDoc getConnectionURI() {
61         return connectionURI;
62     }
63
64     public void connectTo(BrokerAgent remoteBroker) throws Exception JavaDoc {
65         // lets assume discovery works! :)
66
}
67
68     public String JavaDoc getBrokerScript() {
69         if (brokerScript == null) {
70             brokerScript = createBrokerScript();
71         }
72         return brokerScript;
73     }
74
75     public void setBrokerScript(String JavaDoc activemqScript) {
76         this.brokerScript = activemqScript;
77     }
78
79     public String JavaDoc getDefaultPrefix() {
80         return defaultPrefix;
81     }
82
83     public void setDefaultPrefix(String JavaDoc defaultPrefix) {
84         this.defaultPrefix = defaultPrefix;
85     }
86
87     public File JavaDoc getWorkingDirectory() {
88         return workingDirectory;
89     }
90
91     public void setWorkingDirectory(File JavaDoc workingDirectory) {
92         this.workingDirectory = workingDirectory;
93     }
94
95     // Implementation methods
96
// -------------------------------------------------------------------------
97
protected Process JavaDoc createProcess() throws Exception JavaDoc {
98         String JavaDoc commands[] = getCommands();
99
100         System.out.print("About to execute command:");
101         for (int i=0; i<commands.length; i++) {
102             System.out.print(" ");
103             System.out.print(commands[i]);
104         }
105         System.out.println();
106
107         File JavaDoc workingDir = createBrokerWorkingDirectory();
108
109         System.out.println("In directory: " + workingDir);
110
111         Process JavaDoc answer = Runtime.getRuntime().exec(commands, null, workingDir);
112         return answer;
113     }
114
115     protected File JavaDoc createBrokerWorkingDirectory() {
116         workingDirectory.mkdirs();
117
118         // now lets create a new temporary directory
119
File JavaDoc brokerDir = new File JavaDoc(workingDirectory, "broker_" + port);
120         brokerDir.mkdirs();
121
122         File JavaDoc varDir = new File JavaDoc(brokerDir, "data");
123         varDir.mkdirs();
124
125         File JavaDoc workDir = new File JavaDoc(brokerDir, "work");
126         workDir.mkdirs();
127         return workDir;
128     }
129
130     protected String JavaDoc createBrokerScript() {
131         String JavaDoc version = null;
132         Package JavaDoc p = Package.getPackage("org.apache.activemq");
133         if (p != null) {
134             version = p.getImplementationVersion();
135         }
136         if (version == null) {
137             version = "activemq-4.0-SNAPSHOT";
138         }
139         return "../../../../../assembly/target/" + version + "/bin/" + version + "/bin/activemq";
140     }
141
142     protected String JavaDoc[] createCommand() {
143         // lets try load the broker script from a system property
144
String JavaDoc script = System.getProperty("brokerScript");
145         if (script == null) {
146             String JavaDoc home = System.getenv(ENV_HOME);
147             if (home == null) {
148                 script = getBrokerScript();
149             }
150             else {
151                 script = home + "/bin/" + brokerScript;
152             }
153         }
154
155         String JavaDoc[] answer = { "/bin/bash", script, "broker:" + coreURI };
156         return answer;
157     }
158 }
159
Popular Tags