KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > frontends > CLISetup


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.frontends;
9
10 import java.util.List JavaDoc;
11 import org.apache.avalon.excalibur.cli.CLArgsParser;
12 import org.apache.avalon.excalibur.cli.CLOption;
13 import org.apache.avalon.excalibur.cli.CLOptionDescriptor;
14 import org.apache.avalon.excalibur.cli.CLUtil;
15 import org.apache.avalon.excalibur.i18n.ResourceManager;
16 import org.apache.avalon.excalibur.i18n.Resources;
17 import org.apache.avalon.framework.parameters.Parameters;
18 import org.apache.avalon.phoenix.interfaces.SystemManager;
19
20 /**
21  * The class prepare parameters based on input options.
22  *
23  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
24  * @author <a HREF="mail@leosimons.com">Leo Simons</a>
25  */

26 class CLISetup
27 {
28     private static final Resources REZ =
29         ResourceManager.getPackageResources( CLISetup.class );
30
31     private static final String JavaDoc MANAGER_IMPL =
32         "org.apache.avalon.phoenix.components.manager.DefaultManager";
33
34     private static final int DEBUG_LOG_OPT = 'd';
35
36     private static final int HELP_OPT = 'h';
37
38     private static final int LOG_FILE_OPT = 'l';
39
40     private static final int APPS_PATH_OPT = 'a';
41
42     private static final int PERSISTENT_OPT = 'p';
43
44     private static final int CONFIGFILE_OPT = 'f';
45
46     private static final int REMOTE_MANAGER_OPT = 1;
47
48     private static final int DISABLE_HOOK_OPT = 2;
49
50     private static final int APPLICATION_OPT = 3;
51
52     ///Parameters created by parsing CLI options
53
private Parameters m_parameters = new Parameters();
54
55     ///Command used to execute program
56
private String JavaDoc m_command;
57
58     public CLISetup( final String JavaDoc command )
59     {
60         m_command = command;
61     }
62
63     /**
64      * Display usage report.
65      */

66     private void usage( final CLOptionDescriptor[] options )
67     {
68         System.err.println( m_command );
69         System.err.println( "\t" + REZ.getString( "cli.desc.available.header" ) );
70         System.err.println( CLUtil.describeOptions( options ) );
71     }
72
73     /**
74      * Initialise the options for command line parser.
75      */

76     private CLOptionDescriptor[] createCLOptions()
77     {
78         final CLOptionDescriptor options[] = new CLOptionDescriptor[ 9 ];
79         options[ 0 ] =
80             new CLOptionDescriptor( "help",
81                                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
82                                     HELP_OPT,
83                                     REZ.getString( "cli.opt.help.desc" ) );
84         options[ 1 ] =
85             new CLOptionDescriptor( "log-file",
86                                     CLOptionDescriptor.ARGUMENT_REQUIRED,
87                                     LOG_FILE_OPT,
88                                     REZ.getString( "cli.opt.log-file.desc" ) );
89
90         options[ 2 ] =
91             new CLOptionDescriptor( "apps-path",
92                                     CLOptionDescriptor.ARGUMENT_REQUIRED,
93                                     APPS_PATH_OPT,
94                                     REZ.getString( "cli.opt.apps-path.desc" ) );
95
96         options[ 3 ] =
97             new CLOptionDescriptor( "debug-init",
98                                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
99                                     DEBUG_LOG_OPT,
100                                     REZ.getString( "cli.opt.debug-init.desc" ) );
101
102         options[ 4 ] =
103             new CLOptionDescriptor( "remote-manager",
104                                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
105                                     REMOTE_MANAGER_OPT,
106                                     REZ.getString( "cli.opt.remote-manager.desc" ) );
107
108         options[ 5 ] =
109             new CLOptionDescriptor( "disable-hook",
110                                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
111                                     DISABLE_HOOK_OPT,
112                                     REZ.getString( "cli.opt.disable-hook.desc" ) );
113
114         options[ 6 ] =
115             new CLOptionDescriptor( "application",
116                                     CLOptionDescriptor.ARGUMENT_REQUIRED,
117                                     APPLICATION_OPT,
118                                     REZ.getString( "cli.opt.application.desc" ) );
119
120         options[ 7 ] =
121             new CLOptionDescriptor( "persistent",
122                                     CLOptionDescriptor.ARGUMENT_DISALLOWED,
123                                     PERSISTENT_OPT,
124                                     REZ.getString( "cli.opt.persistent.desc" ) );
125
126         options[ 8 ] =
127             new CLOptionDescriptor( "configfile",
128                                     CLOptionDescriptor.ARGUMENT_REQUIRED,
129                                     CONFIGFILE_OPT,
130                                     REZ.getString( "cli.opt.configfile.desc" ) );
131
132
133         return options;
134     }
135
136     public Parameters getParameters()
137     {
138         return m_parameters;
139     }
140
141     public boolean parseCommandLineOptions( final String JavaDoc[] args )
142     {
143         final CLOptionDescriptor[] options = createCLOptions();
144         final CLArgsParser parser = new CLArgsParser( args, options );
145
146         if( null != parser.getErrorString() )
147         {
148             final String JavaDoc message = REZ.getString( "cli.error.parser", parser.getErrorString() );
149             System.err.println( message );
150             return false;
151         }
152
153         final List JavaDoc clOptions = parser.getArguments();
154         final int size = clOptions.size();
155
156         for( int i = 0; i < size; i++ )
157         {
158             final CLOption option = (CLOption)clOptions.get( i );
159
160             switch( option.getId() )
161             {
162                 case 0:
163                     {
164                         final String JavaDoc message =
165                             REZ.getString( "cli.error.unknown.arg", option.getArgument() );
166                         System.err.println( message );
167                     }
168                     return false;
169
170                 case HELP_OPT:
171                     usage( options );
172                     return false;
173
174                 case DEBUG_LOG_OPT:
175                     m_parameters.setParameter( "log-priority", "DEBUG" );
176                     break;
177
178                 case LOG_FILE_OPT:
179                     m_parameters.setParameter( "log-destination", option.getArgument() );
180                     break;
181
182                 case APPS_PATH_OPT:
183                     m_parameters.setParameter( "phoenix.apps.dir", option.getArgument() );
184                     break;
185
186                 case REMOTE_MANAGER_OPT:
187                     m_parameters.setParameter( SystemManager.ROLE, MANAGER_IMPL );
188                     break;
189
190                 case APPLICATION_OPT:
191                     m_parameters.setParameter( "application-location", option.getArgument() );
192                     break;
193
194                 case PERSISTENT_OPT:
195                     m_parameters.setParameter( "persistent", "true" );
196                     break;
197
198                 case DISABLE_HOOK_OPT:
199                     m_parameters.setParameter( "disable-hook", "true" );
200                     break;
201
202                 case CONFIGFILE_OPT:
203                     m_parameters.setParameter( "phoenix.configfile", option.getArgument() );
204                     break;
205             }
206         }
207
208         return true;
209     }
210 }
211
Popular Tags