1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.tools;
21
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.cli.AlreadySelectedException;
30 import org.apache.commons.cli.CommandLine;
31 import org.apache.commons.cli.CommandLineParser;
32 import org.apache.commons.cli.HelpFormatter;
33 import org.apache.commons.cli.MissingArgumentException;
34 import org.apache.commons.cli.MissingOptionException;
35 import org.apache.commons.cli.Option;
36 import org.apache.commons.cli.Options;
37 import org.apache.commons.cli.ParseException;
38 import org.apache.commons.cli.PosixParser;
39 import org.apache.commons.cli.UnrecognizedOptionException;
40
41
42
43
44
45
46
47
48 public class BaseCommand
49 {
50 private Map commands = new HashMap();
51
52 private List commandsOrdered = new ArrayList();
53
54 private Options global = new Options();
55
56 private String productCommand;
57
58 private String productVersion;
59
60 private String productDisplayName;
61
62 private String productUrl;
63
64 private String productBanner;
65
66
67 public BaseCommand()
68 {
69 init();
70 }
71
72
73 protected void init()
74 {
75 ToolCommand command;
76
77 command = new DiagnosticCommand();
78 commands.put( command.getName(), command );
79 commandsOrdered.add( command.getName() );
80
81 command = new DisconnectNotificationCommand();
82 commands.put( command.getName(), command );
83 commandsOrdered.add( command.getName() );
84
85 command = new DumpCommand();
86 commands.put( command.getName(), command );
87 commandsOrdered.add( command.getName() );
88
89 command = new CapacityTestCommand();
90 commands.put( command.getName(), command );
91 commandsOrdered.add( command.getName() );
92
93 command = new GracefulShutdownCommand();
94 commands.put( command.getName(), command );
95 commandsOrdered.add( command.getName() );
96
97 command = new ImportCommand();
98 commands.put( command.getName(), command );
99 commandsOrdered.add( command.getName() );
100
101 command = new IndexCommand();
102 commands.put( command.getName(), command );
103 commandsOrdered.add( command.getName() );
104
105 Option op = new Option( "i", "install-path", true, "path to installation directory" );
106 getGlobal().addOption( op );
107 op = new Option( "b", "banner", false, "suppress banner print outs" );
108 getGlobal().addOption( op );
109 op = new Option( "d", "debug", false, "toggle debug mode" );
110 getGlobal().addOption( op );
111 op = new Option( "v", "verbose", false, "toggle verbose debugging" );
112 getGlobal().addOption( op );
113 op = new Option( "q", "quiet", false, "keep the noise down to a minimum" );
114 getGlobal().addOption( op );
115 op = new Option( "c", "configuration", false, "force loading the server.xml (requires -i)" );
116 getGlobal().addOption( op );
117 op = new Option( "version", false, "print the version information and exit" );
118 getGlobal().addOption( op );
119 }
120
121
122 public static boolean hasBannerOption( String[] args )
123 {
124 for ( int ii = 0; ii < args.length; ii++ )
125 {
126 if ( args[ii].equals( "-b" ) || args[ii].equals( "-banner" ) )
127 {
128 return true;
129 }
130 }
131 return false;
132 }
133
134
135 public CommandLine getCommandLine( String command, String[] args )
136 {
137 Options all = allOptions( command );
138 CommandLineParser parser = new PosixParser();
139 CommandLine cmdline = null;
140 try
141 {
142 cmdline = parser.parse( all, args );
143 }
144 catch ( AlreadySelectedException ase )
145 {
146 System.err.println( "Command line parsing failed for " + command + ". Reason: already selected "
147 + ase.getMessage() );
148 System.exit( 1 );
149 }
150 catch ( MissingArgumentException mae )
151 {
152 System.err.println( "Command line parsing failed for " + command + ". Reason: missing argument "
153 + mae.getMessage() );
154 System.exit( 1 );
155 }
156 catch ( MissingOptionException moe )
157 {
158 System.err.println( "Command line parsing failed for " + command + ". Reason: missing option "
159 + moe.getMessage() );
160 System.exit( 1 );
161 }
162 catch ( UnrecognizedOptionException uoe )
163 {
164 System.err.println( "Command line parsing failed for " + command + ". Reason: unrecognized option"
165 + uoe.getMessage() );
166 System.exit( 1 );
167 }
168 catch ( ParseException pe )
169 {
170 System.err.println( "Command line parsing failed for " + command + ". Reason: " + pe.getClass() );
171 System.exit( 1 );
172 }
173
174 return cmdline;
175 }
176
177
178 public Options allOptions( String command )
179 {
180 if ( command.equals( "help" ) )
181 {
182 return getGlobal();
183 }
184
185 Options all = new Options();
186 ToolCommand cmd = ( ToolCommand ) getCommands().get( command );
187
188 for ( Iterator ii = getGlobal().getOptions().iterator(); ii.hasNext(); )
189 {
190 all.addOption( ( Option ) ii.next() );
191 }
192
193 for ( Iterator ii = cmd.getOptions().getOptions().iterator(); ii.hasNext(); )
194 {
195 all.addOption( ( Option ) ii.next() );
196 }
197 return all;
198 }
199
200
201 public static void dumpArgs( String msg, String[] args )
202 {
203 if ( args.length == 0 )
204 {
205 System.out.println( msg );
206 System.out.println( "\t NONE" );
207 return;
208 }
209
210 StringBuffer buf = new StringBuffer();
211 buf.append( msg ).append( "\n" );
212
213 for ( int ii = 0; ii < args.length; ii++ )
214 {
215 buf.append( "\targs[" + ii + "] = " ).append( args[ii] ).append( "\n" );
216 }
217 System.out.println( buf );
218 }
219
220
221 public void helpOnCommand( String command )
222 {
223 if ( command.equals( "help" ) )
224 {
225 printUsage();
226 System.exit( 0 );
227 }
228
229 if ( getCommands().containsKey( command ) )
230 {
231 ToolCommand cmd = ( ToolCommand ) getCommands().get( command );
232 HelpFormatter formatter = new HelpFormatter();
233 formatter.printHelp( getProductCommand() + " " + cmd + " [options]", cmd.getOptions() );
234 }
235 else
236 {
237 System.err.println( command + ": unknown command" );
238 System.exit( 1 );
239 }
240 }
241
242
243 public void printUsage()
244 {
245 HelpFormatter formatter = new HelpFormatter();
246 formatter.printHelp( getProductCommand() + " <command> [options]", "\nGlobal options:", getGlobal(),
247 "\nType \"" + getProductCommand() + " help <command>\" for help on a command." );
248 System.out.println( "\nAvailable commands:" );
249
250 Iterator it = commandsOrdered.iterator();
251 System.out.println( "\thelp" );
252
253 while ( it.hasNext() )
254 {
255 System.out.println( "\t" + it.next() );
256 }
257
258 System.out.println( "\nThese tools are used to manage " + getProductDisplayName() + "." );
259 System.out.println( "For additional information, see " + getProductUrl() );
260 }
261
262 static final String BANNER = " _ _ ____ ____ _____ _ \n"
263 + " / \\ _ __ __ _ ___| |__ ___| _ \\/ ___| |_ _|__ ___ | |___ \n"
264 + " / _ \\ | '_ \\ / _` |/ __| '_ \\ / _ \\ | | \\___ \\ | |/ _ \\ / _ \\| / __| \n"
265 + " / ___ \\| |_) | (_| | (__| | | | __/ |_| |___) | | | (_) | (_) | \\__ \\ \n"
266 + " /_/ \\_\\ .__/ \\__,_|\\___|_| |_|\\___|____/|____/ |_|\\___/ \\___/|_|___/ \n"
267 + " |_| \n";
268
269
270 public void printBanner()
271 {
272 System.out.println( getProductBanner() );
273 }
274
275
276 public void setProductCommand( String productCommand )
277 {
278 this.productCommand = productCommand;
279 }
280
281
282 public String getProductCommand()
283 {
284 return productCommand;
285 }
286
287
288 public void setProductVersion( String productVersion )
289 {
290 this.productVersion = productVersion;
291 }
292
293
294 public String getProductVersion()
295 {
296 return productVersion;
297 }
298
299
300 public void setProductDisplayName( String productDisplayName )
301 {
302 this.productDisplayName = productDisplayName;
303 }
304
305
306 public String getProductDisplayName()
307 {
308 return productDisplayName;
309 }
310
311
312 public void setProductUrl( String productUrl )
313 {
314 this.productUrl = productUrl;
315 }
316
317
318 public String getProductUrl()
319 {
320 return productUrl;
321 }
322
323
324 public void setProductBanner( String productBanner )
325 {
326 this.productBanner = productBanner;
327 }
328
329
330 public String getProductBanner()
331 {
332 return productBanner;
333 }
334
335
336 public void setCommands( Map commands )
337 {
338 this.commands = commands;
339 }
340
341
342 public Map getCommands()
343 {
344 return commands;
345 }
346
347
348 public void setGlobal( Options global )
349 {
350 this.global = global;
351 }
352
353
354 public Options getGlobal()
355 {
356 return global;
357 }
358 }