Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.activemq.systest.impl;
   18   
   19   import java.io.File;
   20   
   21   import org.apache.activemq.ActiveMQConnectionFactory;
   22   import org.apache.activemq.systest.BrokerAgent;
   23   
   24   import javax.jms.ConnectionFactory;
   25   
   26   
   27   /**
   28    * Runs a broker in a separate process
   29    * 
   30    * @version $Revision: 1.1 $
   31    */
   32   public class SeparateBrokerProcessAgentImpl extends SeparateProcessAgent implements BrokerAgent {
   33   
   34       private static final String ENV_HOME = "ACTIVEMQ_HOME";
   35   
   36       private static int portCounter = 61616;
   37   
   38       private int port;
   39       private String connectionURI;
   40       private String brokerScript;
   41       private File workingDirectory = new File("target/test-brokers");
   42       private String defaultPrefix = "~/activemq";
   43       private String coreURI;
   44   
   45       public SeparateBrokerProcessAgentImpl(String host) throws Exception {
   46           port = portCounter++;
   47           coreURI = "tcp://" + host + ":" + port;
   48           connectionURI = "failover:(" + coreURI + ")?useExponentialBackOff=false&initialReconnectDelay=500&&maxReconnectAttempts=20";
   49       }
   50   
   51       public void kill() throws Exception {
   52           stop();
   53       }
   54   
   55       public ConnectionFactory getConnectionFactory() {
   56           return new ActiveMQConnectionFactory(getConnectionURI());
   57       }
   58   
   59       public String getConnectionURI() {
   60           return connectionURI;
   61       }
   62   
   63       public void connectTo(BrokerAgent remoteBroker) throws Exception {
   64           // lets assume discovery works! :)
   65       }
   66   
   67       public String getBrokerScript() {
   68           if (brokerScript == null) {
   69               brokerScript = createBrokerScript();
   70           }
   71           return brokerScript;
   72       }
   73   
   74       public void setBrokerScript(String activemqScript) {
   75           this.brokerScript = activemqScript;
   76       }
   77   
   78       public String getDefaultPrefix() {
   79           return defaultPrefix;
   80       }
   81   
   82       public void setDefaultPrefix(String defaultPrefix) {
   83           this.defaultPrefix = defaultPrefix;
   84       }
   85   
   86       public File getWorkingDirectory() {
   87           return workingDirectory;
   88       }
   89   
   90       public void setWorkingDirectory(File workingDirectory) {
   91           this.workingDirectory = workingDirectory;
   92       }
   93   
   94       // Implementation methods
   95       // -------------------------------------------------------------------------
   96       protected Process createProcess() throws Exception {
   97           String commands[] = getCommands();
   98   
   99           System.out.print("About to execute command:");
  100           for (int i=0; i<commands.length; i++) {
  101               System.out.print(" ");
  102               System.out.print(commands[i]);
  103           }
  104           System.out.println();
  105   
  106           File workingDir = createBrokerWorkingDirectory();
  107   
  108           System.out.println("In directory: " + workingDir);
  109   
  110           Process answer = Runtime.getRuntime().exec(commands, null, workingDir);
  111           return answer;
  112       }
  113   
  114       protected File createBrokerWorkingDirectory() {
  115           workingDirectory.mkdirs();
  116   
  117           // now lets create a new temporary directory
  118           File brokerDir = new File(workingDirectory, "broker_" + port);
  119           brokerDir.mkdirs();
  120   
  121           File varDir = new File(brokerDir, "data");
  122           varDir.mkdirs();
  123   
  124           File workDir = new File(brokerDir, "work");
  125           workDir.mkdirs();
  126           return workDir;
  127       }
  128   
  129       protected String createBrokerScript() {
  130           String version = null;
  131           Package p = Package.getPackage("org.apache.activemq");
  132           if (p != null) {
  133               version = p.getImplementationVersion();
  134           }
  135           if (version == null) {
  136               version = "activemq-4.0-SNAPSHOT";
  137           }
  138           return "../../../../../assembly/target/" + version + "/bin/" + version + "/bin/activemq";
  139       }
  140   
  141       protected String[] createCommand() {
  142           // lets try load the broker script from a system property
  143           String script = System.getProperty("brokerScript");
  144           if (script == null) {
  145               String home = System.getenv(ENV_HOME);
  146               if (home == null) {
  147                   script = getBrokerScript();
  148               }
  149               else {
  150                   script = home + "/bin/" + brokerScript;
  151               }
  152           }
  153   
  154           String[] answer = { "/bin/bash", script, "broker:" + coreURI };
  155           return answer;
  156       }
  157   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]