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 property="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 property="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 property="fork" default-value="false"
075         */
076        private boolean fork;
077    
078        /**
079         * System properties to add
080         *
081         * @parameter property="systemProperties"
082         */
083        private Properties systemProperties;
084    
085        /**
086         * Skip execution of the ActiveMQ Broker plugin if set to true
087         *
088         * @parameter property="skip"
089         */
090        private boolean skip;
091    
092        @Override
093        public void execute() throws MojoExecutionException {
094            try {
095                if (skip) {
096                    getLog().info("Skipped execution of ActiveMQ Broker");
097                    return;
098                }
099    
100                setSystemProperties();
101    
102                getLog().info("Loading broker configUri: " + configUri);
103                if (XBeanFileResolver.isXBeanFile(configUri)) {
104                    getLog().debug("configUri before transformation: " + configUri);
105                    configUri = XBeanFileResolver.toUrlCompliantAbsolutePath(configUri);
106                    getLog().debug("configUri after transformation: " + configUri);
107                }
108    
109                final BrokerService broker = BrokerFactory.createBroker(configUri);
110                if (fork) {
111                    new Thread(new Runnable() {
112                        @Override
113                        public void run() {
114                            try {
115                                broker.start();
116                                waitForShutdown(broker);
117                            } catch (Exception e) {
118                                e.printStackTrace();
119                            }
120                        }
121                    }).start();
122                } else {
123                    broker.start();
124                    waitForShutdown(broker);
125                }
126            } catch (Exception e) {
127                throw new MojoExecutionException("Failed to start ActiveMQ Broker", e);
128            }
129        }
130    
131        /**
132         * Wait for a shutdown invocation elsewhere
133         *
134         * @throws Exception
135         */
136        protected void waitForShutdown(BrokerService broker) throws Exception {
137            final boolean[] shutdown = new boolean[] {
138                false
139            };
140            Runtime.getRuntime().addShutdownHook(new Thread() {
141                @Override
142                public void run() {
143                    synchronized (shutdown) {
144                        shutdown[0] = true;
145                        shutdown.notify();
146                    }
147                }
148            });
149    
150            // Wait for any shutdown event
151            synchronized (shutdown) {
152                while (!shutdown[0]) {
153                    try {
154                        shutdown.wait();
155                    } catch (InterruptedException e) {
156                    }
157                }
158            }
159    
160            // Stop broker
161            broker.stop();
162        }
163    
164        /**
165         * Set system properties
166         */
167        protected void setSystemProperties() {
168            // Set the default properties
169            System.setProperty("activemq.base", project.getBuild().getDirectory() + "/");
170            System.setProperty("activemq.home", project.getBuild().getDirectory() + "/");
171            System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "true");
172            System.setProperty("org.apache.activemq.default.directory.prefix", project.getBuild().getDirectory() + "/");
173            System.setProperty("derby.system.home", project.getBuild().getDirectory() + "/");
174            System.setProperty("derby.storage.fileSyncTransactionLog", "true");
175    
176            // Overwrite any custom properties
177            System.getProperties().putAll(systemProperties);
178        }
179    }