001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *       http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.activemq.console;
021    
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    
026    import org.apache.activemq.console.command.Command;
027    import org.apache.activemq.console.command.ShutdownCommand;
028    import org.apache.activemq.console.command.StartCommand;
029    import org.apache.activemq.console.CommandContext;
030    import org.apache.activemq.console.Main;
031    import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
032    import org.apache.commons.daemon.Daemon;
033    import org.apache.commons.daemon.DaemonContext;
034    
035    /**
036     * This class launches activemq under Apache JSVC {@link http://commons.apache.org/daemon/jsvc.html}
037     * 
038     * @author areese
039     * 
040     */
041    public class ActiveMQLauncher implements Daemon {
042        private List<String> args;
043    
044        /**
045         * 
046         */
047        public ActiveMQLauncher() {
048        }
049    
050        /*
051         * (non-Javadoc)
052         * 
053         * @see org.apache.commons.daemon.Daemon#destroy()
054         */
055        public void destroy() {
056        }
057    
058        /*
059         * (non-Javadoc)
060         * 
061         * @see
062         * org.apache.commons.daemon.Daemon#init(org.apache.commons.daemon.DaemonContext
063         * )
064         */
065        public void init(DaemonContext arg0) throws Exception {
066            // we need to save the args we started with.
067            args = Arrays.asList(arg0.getArguments());
068        }
069    
070        /*
071         * (non-Javadoc)
072         * 
073         * @see org.apache.commons.daemon.Daemon#start()
074         */
075        public void start() throws Exception {
076            CommandContext context = new CommandContext();
077            context.setFormatter(new CommandShellOutputFormatter(System.out));
078    
079            Command command = new StartCommand();
080            command.setCommandContext(context);
081    
082            command.execute(args);
083        }
084    
085        /*
086         * (non-Javadoc)
087         * 
088         * @see org.apache.commons.daemon.Daemon#stop()
089         */
090        public void stop() throws Exception {
091            CommandContext context = new CommandContext();
092            context.setFormatter(new CommandShellOutputFormatter(System.out));
093    
094            Command command = new ShutdownCommand();
095            command.setCommandContext(context);
096    
097            List<String> tokens = new ArrayList<String>(Arrays.asList(new String[] {
098                    "--jmxlocal", "--all", }));
099    
100            command.execute(tokens);
101        }
102    
103    }