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.karaf.commands;
018    
019    import java.io.File;
020    
021    import org.apache.felix.gogo.commands.Option;
022    import org.apache.felix.gogo.commands.Command;
023    import org.apache.karaf.shell.console.OsgiCommandSupport;
024    
025    /**
026     * @version $Rev: 960482 $ $Date: 2010-07-05 10:28:33 +0200 (Mon, 05 Jul 2010) $
027     */
028    @Command(scope="activemq", name="destroy-broker", description="Destory a broker instance.")
029    public class DestroyBrokerCommand extends OsgiCommandSupport {
030    
031        @Option(name = "-n", aliases = {"--name"}, description = "The name of the broker (defaults to localhost).")
032        private String name = "localhost";
033    
034        protected Object doExecute() throws Exception {
035    
036            try {
037                String name = getName();
038                File base = new File(System.getProperty("karaf.base"));
039                File deploy = new File(base, "deploy");
040                File configFile = new File(deploy, name + "-broker.xml");
041    
042                configFile.delete();
043    
044                System.out.println("");
045                System.out.println("Default ActiveMQ Broker (" + name + ") configuration file created at: "
046                               + configFile.getPath() + " removed.");
047                System.out.println("");
048    
049            } catch (Exception e) {
050                e.printStackTrace();
051                throw e;
052            }
053    
054            return 0;
055        }
056    
057        public String getName() {
058            if (name == null) {
059                File base = new File(System.getProperty("karaf.base"));
060                name = base.getName();
061            }
062            return name;
063        }
064    
065        public void setName(String name) {
066            this.name = name;
067        }
068    }