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    
018    package org.apache.activemq.console.command;
019    
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    import java.util.ArrayList;
023    import java.util.Iterator;
024    import java.util.List;
025    
026    import org.apache.activemq.broker.BrokerFactory;
027    import org.apache.activemq.broker.BrokerService;
028    
029    public class StartCommand extends AbstractCommand {
030    
031        public static final String DEFAULT_CONFIG_URI = "xbean:activemq.xml";
032    
033        protected String[] helpFile = new String[] {
034            "Task Usage: Main start [start-options] [uri]",
035            "Description: Creates and starts a broker using a configuration file, or a broker URI.",
036            "",
037            "Start Options:",
038            "    -D<name>=<value>      Define a system property.",
039            "    --version             Display the version information.", 
040            "    -h,-?,--help          Display the start broker help information.",
041            "",
042            "URI:",
043            "",
044            "    XBean based broker configuration:",
045            "",
046            "        Example: Main xbean:file:activemq.xml",
047            "            Loads the xbean configuration file from the current working directory",
048            "        Example: Main xbean:activemq.xml",
049            "            Loads the xbean configuration file from the classpath",
050            "",
051            "    URI Parameter based broker configuration:",
052            "",
053            "        Example: Main broker:(tcp://localhost:61616, tcp://localhost:5000)?useJmx=true",
054            "            Configures the broker with 2 transport connectors and jmx enabled",
055            "        Example: Main broker:(tcp://localhost:61616, network:tcp://localhost:5000)?persistent=false",
056            "            Configures the broker with 1 transport connector, and 1 network connector and persistence disabled",
057            ""
058        };
059    
060        private URI configURI;
061        private List<BrokerService> brokers = new ArrayList<BrokerService>(5);
062    
063        /**
064         * The default task to start a broker or a group of brokers
065         * 
066         * @param brokerURIs
067         */
068        protected void runTask(List<String> brokerURIs) throws Exception {
069            try {
070                // If no config uri, use default setting
071                if (brokerURIs.isEmpty()) {
072                    setConfigUri(new URI(DEFAULT_CONFIG_URI));
073                    startBroker(getConfigUri());
074    
075                    // Set configuration data, if available, which in this case
076                    // would be the config URI
077                } else {
078                    String strConfigURI;
079    
080                    while (!brokerURIs.isEmpty()) {
081                        strConfigURI = (String)brokerURIs.remove(0);
082    
083                        try {
084                            setConfigUri(new URI(strConfigURI));
085                        } catch (URISyntaxException e) {
086                            context.printException(e);
087                            return;
088                        }
089    
090                        startBroker(getConfigUri());
091                    }
092                }
093    
094                // Prevent the main thread from exiting unless it is terminated
095                // elsewhere
096                waitForShutdown();
097            } catch (Exception e) {
098                context.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e));
099                throw new Exception(e);
100            }
101        }
102    
103        /**
104         * Create and run a broker specified by the given configuration URI
105         * 
106         * @param configURI
107         * @throws Exception
108         */
109        public void startBroker(URI configURI) throws Exception {
110            System.out.println("Loading message broker from: " + configURI);
111            BrokerService broker = BrokerFactory.createBroker(configURI);
112            brokers.add(broker);
113            broker.start();
114        }
115    
116        /**
117         * Wait for a shutdown invocation elsewhere
118         * 
119         * @throws Exception
120         */
121        protected void waitForShutdown() throws Exception {
122            final boolean[] shutdown = new boolean[] {
123                false
124            };
125            Runtime.getRuntime().addShutdownHook(new Thread() {
126                public void run() {
127                    synchronized (shutdown) {
128                        shutdown[0] = true;
129                        shutdown.notify();
130                    }
131                }
132            });
133    
134            // Wait for any shutdown event
135            synchronized (shutdown) {
136                while (!shutdown[0]) {
137                    try {
138                        shutdown.wait();
139                    } catch (InterruptedException e) {
140                    }
141                }
142            }
143    
144            // Stop each broker
145            for (Iterator<BrokerService> i = brokers.iterator(); i.hasNext();) {
146                BrokerService broker = i.next();
147                broker.stop();
148            }
149        }
150    
151        /**
152         * Sets the current configuration URI used by the start task
153         * 
154         * @param uri
155         */
156        public void setConfigUri(URI uri) {
157            configURI = uri;
158        }
159    
160        /**
161         * Gets the current configuration URI used by the start task
162         * 
163         * @return current configuration URI
164         */
165        public URI getConfigUri() {
166            return configURI;
167        }
168    
169        /**
170         * Print the help messages for the browse command
171         */
172        protected void printHelp() {
173            context.printHelp(helpFile);
174        }
175    
176    }