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.geronimo.cli;
018    
019    import java.io.PrintStream;
020    
021    import org.apache.geronimo.cli.CLParserException;
022    import org.apache.geronimo.kernel.log.GeronimoLogging;
023    import org.apache.geronimo.kernel.util.MainConfigurationBootstrapper;
024    
025    
026    /**
027     * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
028     */
029    public abstract class AbstractCLI {
030    
031        private final String[] args;
032        private final PrintStream errStream;
033        
034        protected AbstractCLI(String[] args, PrintStream errStream) {
035            if (null == args) {
036                throw new IllegalArgumentException("args is required");
037            } else if (null == errStream) {
038                throw new IllegalArgumentException("errStream is required");
039            }
040            this.args = args;
041            this.errStream = errStream;
042        }
043    
044        public int executeMain() {
045            CLParser parser = getCLParser();
046            try {
047                parser.parse(args);
048            } catch (CLParserException e) {
049                errStream.println(e.getMessage());
050                parser.displayHelp();
051                return 1;
052            }
053    
054            if (parser.isHelp()) {
055                parser.displayHelp();
056                return 0;
057            }
058            
059            boolean executed = executeCommand(parser);
060            if (executed) {
061                return 0;
062            }
063            
064            initializeLogging(parser);
065            
066            MainConfigurationBootstrapper mainConfigurationBootstrapper = newMainConfigurationBootstrapper();
067            return MainConfigurationBootstrapper.main(mainConfigurationBootstrapper, parser);
068        }
069    
070        protected boolean executeCommand(CLParser parser) {
071            return false;
072        }
073    
074        protected void initializeLogging(CLParser parser) {
075            GeronimoLogging level = GeronimoLogging.WARN;
076            if (parser.isVerboseInfo()) {
077                level = GeronimoLogging.INFO;
078            } else if (parser.isVerboseDebug()) {
079                level = GeronimoLogging.DEBUG;
080            } else if (parser.isVerboseTrace()) {
081                level = GeronimoLogging.TRACE;
082            }
083            GeronimoLogging.initialize(level);
084        }
085    
086        protected abstract MainConfigurationBootstrapper newMainConfigurationBootstrapper();
087    
088        protected abstract CLParser getCLParser();
089    
090    }