001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.maven;
018    
019    /**
020     * Licensed to the Apache Software Foundation (ASF) under one or more
021     * contributor license agreements.  See the NOTICE file distributed with
022     * this work for additional information regarding copyright ownership.
023     * The ASF licenses this file to You under the Apache License, Version 2.0
024     * (the "License"); you may not use this file except in compliance with
025     * the License.  You may obtain a copy of the License at
026     *
027     *      http://www.apache.org/licenses/LICENSE-2.0
028     *
029     * Unless required by applicable law or agreed to in writing, software
030     * distributed under the License is distributed on an "AS IS" BASIS,
031     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
032     * See the License for the specific language governing permissions and
033     * limitations under the License.
034     */
035    
036    import java.util.Properties;
037    
038    import org.apache.activemq.broker.BrokerFactory;
039    import org.apache.activemq.broker.BrokerService;
040    import org.apache.maven.plugin.AbstractMojo;
041    import org.apache.maven.plugin.MojoExecutionException;
042    import org.apache.maven.project.MavenProject;
043    
044    /**
045     * Goal which starts an activemq broker.
046     * 
047     * @goal run
048     * @phase process-sources
049     */
050    public class BrokerMojo extends AbstractMojo {
051        /**
052         * The maven project.
053         * 
054         * @parameter expression="${project}"
055         * @required
056         * @readonly
057         */
058        protected MavenProject project;
059    
060        /**
061         * The broker configuration uri The list of currently supported URI syntaxes
062         * is described <a
063         * href="http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html">here</a>
064         * 
065         * @parameter expression="${configUri}"
066         *            default-value="broker:(tcp://localhost:61616)?useJmx=false&persistent=false"
067         * @required
068         */
069        private String configUri;
070    
071        /**
072         * Indicates whether to fork the broker, useful for integration tests.
073         * 
074         * @parameter expression="${fork}" default-value="false"
075         */
076        private boolean fork;
077    
078        /**
079         * System properties to add
080         * 
081         * @parameter expression="${systemProperties}"
082         */
083        private Properties systemProperties;
084    
085        public void execute() throws MojoExecutionException {
086            try {
087                setSystemProperties();
088                getLog().info("Loading broker configUri: " + configUri);
089    
090                final BrokerService broker = BrokerFactory.createBroker(configUri);
091                if (fork) {
092                    new Thread(new Runnable() {
093                        public void run() {
094                            try {
095                                broker.start();
096                                waitForShutdown(broker);
097                            } catch (Exception e) {
098                                e.printStackTrace();
099                            }
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    }