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.directory.server.tools; 021 022 023 import java.util.ArrayList; 024 import java.util.HashMap; 025 import java.util.Iterator; 026 import java.util.List; 027 import java.util.Map; 028 029 import org.apache.commons.cli.AlreadySelectedException; 030 import org.apache.commons.cli.CommandLine; 031 import org.apache.commons.cli.CommandLineParser; 032 import org.apache.commons.cli.HelpFormatter; 033 import org.apache.commons.cli.MissingArgumentException; 034 import org.apache.commons.cli.MissingOptionException; 035 import org.apache.commons.cli.Option; 036 import org.apache.commons.cli.Options; 037 import org.apache.commons.cli.ParseException; 038 import org.apache.commons.cli.PosixParser; 039 import org.apache.commons.cli.UnrecognizedOptionException; 040 import org.apache.directory.server.i18n.I18n; 041 042 043 /** 044 * The primary command base class. 045 * 046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 047 * @version $Rev: 901657 $ 048 */ 049 public class BaseCommand 050 { 051 private Map commands = new HashMap(); 052 053 private List commandsOrdered = new ArrayList(); 054 055 private Options global = new Options(); 056 057 private String productCommand; 058 059 private String productVersion; 060 061 private String productDisplayName; 062 063 private String productUrl; 064 065 private String productBanner; 066 067 068 public BaseCommand() 069 { 070 init(); 071 } 072 073 074 protected void init() 075 { 076 ToolCommand command; 077 078 command = new DiagnosticCommand(); 079 commands.put( command.getName(), command ); 080 commandsOrdered.add( command.getName() ); 081 082 command = new DisconnectNotificationCommand(); 083 commands.put( command.getName(), command ); 084 commandsOrdered.add( command.getName() ); 085 086 command = new DumpCommand(); 087 commands.put( command.getName(), command ); 088 commandsOrdered.add( command.getName() ); 089 090 command = new CapacityTestCommand(); 091 commands.put( command.getName(), command ); 092 commandsOrdered.add( command.getName() ); 093 094 command = new GracefulShutdownCommand(); 095 commands.put( command.getName(), command ); 096 commandsOrdered.add( command.getName() ); 097 098 command = new ImportCommand(); 099 commands.put( command.getName(), command ); 100 commandsOrdered.add( command.getName() ); 101 102 command = new IndexCommand(); 103 commands.put( command.getName(), command ); 104 commandsOrdered.add( command.getName() ); 105 106 Option op = new Option( "i", "install-path", true, "path to installation directory" ); 107 getGlobal().addOption( op ); 108 op = new Option( "z", "instance-path", true, "path to ADS instance directory" ); 109 getGlobal().addOption( op ); 110 op = new Option( "b", "banner", false, "suppress banner print outs" ); 111 getGlobal().addOption( op ); 112 op = new Option( "d", "debug", false, "toggle debug mode" ); 113 getGlobal().addOption( op ); 114 op = new Option( "v", "verbose", false, "toggle verbose debugging" ); 115 getGlobal().addOption( op ); 116 op = new Option( "q", "quiet", false, "keep the noise down to a minimum" ); 117 getGlobal().addOption( op ); 118 op = new Option( "c", "configuration", false, "force loading the server.xml (requires -i)" ); 119 getGlobal().addOption( op ); 120 op = new Option( "version", false, "print the version information and exit" ); 121 getGlobal().addOption( op ); 122 } 123 124 125 public static boolean hasBannerOption( String[] args ) 126 { 127 for ( int ii = 0; ii < args.length; ii++ ) 128 { 129 if ( args[ii].equals( "-b" ) || args[ii].equals( "-banner" ) ) 130 { 131 return true; 132 } 133 } 134 return false; 135 } 136 137 138 public CommandLine getCommandLine( String command, String[] args ) 139 { 140 Options all = allOptions( command ); 141 CommandLineParser parser = new PosixParser(); 142 CommandLine cmdline = null; 143 try 144 { 145 cmdline = parser.parse( all, args ); 146 } 147 catch ( AlreadySelectedException ase ) 148 { 149 System.err.println( I18n.err( I18n.ERR_187, command, ase.getLocalizedMessage() ) ); 150 System.exit( 1 ); 151 } 152 catch ( MissingArgumentException mae ) 153 { 154 System.err.println( I18n.err( I18n.ERR_188, command, mae.getLocalizedMessage() ) ); 155 System.exit( 1 ); 156 } 157 catch ( MissingOptionException moe ) 158 { 159 System.err.println( I18n.err( I18n.ERR_189, command, moe.getLocalizedMessage() ) ); 160 System.exit( 1 ); 161 } 162 catch ( UnrecognizedOptionException uoe ) 163 { 164 System.err.println( I18n.err( I18n.ERR_190, command, uoe.getLocalizedMessage() ) ); 165 System.exit( 1 ); 166 } 167 catch ( ParseException pe ) 168 { 169 System.err.println( I18n.err( I18n.ERR_191, command, pe.getClass() ) ); 170 System.exit( 1 ); 171 } 172 173 return cmdline; 174 } 175 176 177 public Options allOptions( String command ) 178 { 179 if ( command.equals( "help" ) ) 180 { 181 return getGlobal(); 182 } 183 184 Options all = new Options(); 185 ToolCommand cmd = ( ToolCommand ) getCommands().get( command ); 186 187 for ( Iterator ii = getGlobal().getOptions().iterator(); ii.hasNext(); ) 188 { 189 all.addOption( ( Option ) ii.next() ); 190 } 191 192 for ( Iterator ii = cmd.getOptions().getOptions().iterator(); ii.hasNext(); ) 193 { 194 all.addOption( ( Option ) ii.next() ); 195 } 196 return all; 197 } 198 199 200 public static void dumpArgs( String msg, String[] args ) 201 { 202 if ( args.length == 0 ) 203 { 204 System.out.println( msg ); 205 System.out.println( "\t NONE" ); 206 return; 207 } 208 209 StringBuffer buf = new StringBuffer(); 210 buf.append( msg ).append( "\n" ); 211 212 for ( int ii = 0; ii < args.length; ii++ ) 213 { 214 buf.append( "\targs[" + ii + "] = " ).append( args[ii] ).append( "\n" ); 215 } 216 System.out.println( buf ); 217 } 218 219 220 public void helpOnCommand( String command ) 221 { 222 if ( command.equals( "help" ) ) 223 { 224 printUsage(); 225 System.exit( 0 ); 226 } 227 228 if ( getCommands().containsKey( command ) ) 229 { 230 ToolCommand cmd = ( ToolCommand ) getCommands().get( command ); 231 HelpFormatter formatter = new HelpFormatter(); 232 formatter.printHelp( getProductCommand() + " " + cmd + " [options]", cmd.getOptions() ); 233 } 234 else 235 { 236 System.err.println( I18n.err( I18n.ERR_192, command ) ); 237 System.exit( 1 ); 238 } 239 } 240 241 242 public void printUsage() 243 { 244 HelpFormatter formatter = new HelpFormatter(); 245 formatter.printHelp( getProductCommand() + " <command> [options]", "\nGlobal options:", getGlobal(), 246 "\nType \"" + getProductCommand() + " help <command>\" for help on a command." ); 247 System.out.println( "\nAvailable commands:" ); 248 249 Iterator it = commandsOrdered.iterator(); 250 System.out.println( "\thelp" ); 251 252 while ( it.hasNext() ) 253 { 254 System.out.println( "\t" + it.next() ); 255 } 256 257 System.out.println( "\nThese tools are used to manage " + getProductDisplayName() + "." ); 258 System.out.println( "For additional information, see " + getProductUrl() ); 259 } 260 261 static final String BANNER = " _ _ ____ ____ _____ _ \n" 262 + " / \\ _ __ __ _ ___| |__ ___| _ \\/ ___| |_ _|__ ___ | |___ \n" 263 + " / _ \\ | '_ \\ / _` |/ __| '_ \\ / _ \\ | | \\___ \\ | |/ _ \\ / _ \\| / __| \n" 264 + " / ___ \\| |_) | (_| | (__| | | | __/ |_| |___) | | | (_) | (_) | \\__ \\ \n" 265 + " /_/ \\_\\ .__/ \\__,_|\\___|_| |_|\\___|____/|____/ |_|\\___/ \\___/|_|___/ \n" 266 + " |_| \n"; 267 268 269 public void printBanner() 270 { 271 System.out.println( getProductBanner() ); 272 } 273 274 275 public void setProductCommand( String productCommand ) 276 { 277 this.productCommand = productCommand; 278 } 279 280 281 public String getProductCommand() 282 { 283 return productCommand; 284 } 285 286 287 public void setProductVersion( String productVersion ) 288 { 289 this.productVersion = productVersion; 290 } 291 292 293 public String getProductVersion() 294 { 295 return productVersion; 296 } 297 298 299 public void setProductDisplayName( String productDisplayName ) 300 { 301 this.productDisplayName = productDisplayName; 302 } 303 304 305 public String getProductDisplayName() 306 { 307 return productDisplayName; 308 } 309 310 311 public void setProductUrl( String productUrl ) 312 { 313 this.productUrl = productUrl; 314 } 315 316 317 public String getProductUrl() 318 { 319 return productUrl; 320 } 321 322 323 public void setProductBanner( String productBanner ) 324 { 325 this.productBanner = productBanner; 326 } 327 328 329 public String getProductBanner() 330 { 331 return productBanner; 332 } 333 334 335 public void setCommands( Map commands ) 336 { 337 this.commands = commands; 338 } 339 340 341 public Map getCommands() 342 { 343 return commands; 344 } 345 346 347 public void setGlobal( Options global ) 348 { 349 this.global = global; 350 } 351 352 353 public Options getGlobal() 354 { 355 return global; 356 } 357 }