1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.cli; 18 19 import java.io.PrintStream; 20 21 import org.apache.geronimo.cli.CLParserException; 22 import org.apache.geronimo.kernel.util.MainConfigurationBootstrapper; 23 24 /** 25 * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $ 26 */ 27 public abstract class AbstractCLI { 28 29 private final String[] args; 30 private final PrintStream errStream; 31 32 protected AbstractCLI(String[] args, PrintStream errStream) { 33 if (null == args) { 34 throw new IllegalArgumentException("args is required"); 35 } else if (null == errStream) { 36 throw new IllegalArgumentException("errStream is required"); 37 } 38 this.args = args; 39 this.errStream = errStream; 40 } 41 42 public int executeMain() { 43 CLParser parser = getCLParser(); 44 try { 45 parser.parse(args); 46 } catch (CLParserException e) { 47 errStream.println(e.getMessage()); 48 parser.displayHelp(); 49 return 1; 50 } 51 52 if (parser.isHelp()) { 53 parser.displayHelp(); 54 return 0; 55 } 56 57 boolean executed = executeCommand(parser); 58 if (executed) { 59 return 0; 60 } 61 62 initializeLogging(parser); 63 64 MainConfigurationBootstrapper mainConfigurationBootstrapper = newMainConfigurationBootstrapper(); 65 return MainConfigurationBootstrapper.main(mainConfigurationBootstrapper, parser); 66 } 67 68 protected boolean executeCommand(CLParser parser) { 69 return false; 70 } 71 72 protected void initializeLogging(final CLParser parser) { 73 assert parser != null; 74 75 String level = "WARN"; 76 77 if (parser.isVerboseInfo()) { 78 level = "INFO"; 79 } else if (parser.isVerboseDebug()) { 80 level = "DEBUG"; 81 } else if (parser.isVerboseTrace()) { 82 level = "TRACE"; 83 } 84 85 System.setProperty("org.apache.geronimo.log.ConsoleLogLevel", level); 86 } 87 88 protected abstract MainConfigurationBootstrapper newMainConfigurationBootstrapper(); 89 90 protected abstract CLParser getCLParser(); 91 92 }