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.maven; 18 19 /** 20 * Licensed to the Apache Software Foundation (ASF) under one or more 21 * contributor license agreements. See the NOTICE file distributed with 22 * this work for additional information regarding copyright ownership. 23 * The ASF licenses this file to You under the Apache License, Version 2.0 24 * (the "License"); you may not use this file except in compliance with 25 * the License. You may obtain a copy of the License at 26 * 27 * http://www.apache.org/licenses/LICENSE-2.0 28 * 29 * Unless required by applicable law or agreed to in writing, software 30 * distributed under the License is distributed on an "AS IS" BASIS, 31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 * See the License for the specific language governing permissions and 33 * limitations under the License. 34 */ 35 36 import java.util.Properties; 37 38 import org.apache.activemq.broker.BrokerFactory; 39 import org.apache.activemq.broker.BrokerService; 40 import org.apache.maven.plugin.AbstractMojo; 41 import org.apache.maven.plugin.MojoExecutionException; 42 import org.apache.maven.project.MavenProject; 43 44 /** 45 * Goal which starts an activemq broker. 46 * 47 * @goal run 48 * @phase process-sources 49 */ 50 public class BrokerMojo extends AbstractMojo { 51 /** 52 * The maven project. 53 * 54 * @parameter expression="${project}" 55 * @required 56 * @readonly 57 */ 58 protected MavenProject project; 59 60 /** 61 * The broker configuration uri The list of currently supported URI syntaxes 62 * is described <a 63 * href="http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html">here</a> 64 * 65 * @parameter expression="${configUri}" 66 * default-value="broker:(tcp://localhost:61616)?useJmx=false&persistent=false" 67 * @required 68 */ 69 private String configUri; 70 71 /** 72 * Indicates whether to fork the broker, useful for integration tests. 73 * 74 * @parameter expression="${fork}" default-value="false" 75 */ 76 private boolean fork; 77 78 /** 79 * System properties to add 80 * 81 * @parameter expression="${systemProperties}" 82 */ 83 private Properties systemProperties; 84 85 public void execute() throws MojoExecutionException { 86 try { 87 setSystemProperties(); 88 getLog().info("Loading broker configUri: " + configUri); 89 90 final BrokerService broker = BrokerFactory.createBroker(configUri); 91 if (fork) { 92 new Thread(new Runnable() { 93 public void run() { 94 try { 95 broker.start(); 96 waitForShutdown(broker); 97 } catch (Exception e) { 98 e.printStackTrace(); 99 } 100 } 101 }).start(); 102 } else { 103 broker.start(); 104 waitForShutdown(broker); 105 } 106 } catch (Exception e) { 107 throw new MojoExecutionException("Failed to start ActiveMQ Broker", e); 108 } 109 } 110 111 /** 112 * Wait for a shutdown invocation elsewhere 113 * 114 * @throws Exception 115 */ 116 protected void waitForShutdown(BrokerService broker) throws Exception { 117 final boolean[] shutdown = new boolean[] { 118 false 119 }; 120 Runtime.getRuntime().addShutdownHook(new Thread() { 121 public void run() { 122 synchronized (shutdown) { 123 shutdown[0] = true; 124 shutdown.notify(); 125 } 126 } 127 }); 128 129 // Wait for any shutdown event 130 synchronized (shutdown) { 131 while (!shutdown[0]) { 132 try { 133 shutdown.wait(); 134 } catch (InterruptedException e) { 135 } 136 } 137 } 138 139 // Stop broker 140 broker.stop(); 141 } 142 143 /** 144 * Set system properties 145 */ 146 protected void setSystemProperties() { 147 // Set the default properties 148 System.setProperty("activemq.base", project.getBuild().getDirectory() + "/"); 149 System.setProperty("activemq.home", project.getBuild().getDirectory() + "/"); 150 System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "true"); 151 System.setProperty("org.apache.activemq.default.directory.prefix", project.getBuild().getDirectory() + "/"); 152 System.setProperty("derby.system.home", project.getBuild().getDirectory() + "/"); 153 System.setProperty("derby.storage.fileSyncTransactionLog", "true"); 154 155 // Overwrite any custom properties 156 System.getProperties().putAll(systemProperties); 157 } 158 }