Source code: org/apache/axis/utils/Options.java
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.axis.utils ;
18
19 /**
20 * General purpose command line options parser.
21 * If this is used outside of Axis just remove the Axis specific sections.
22 *
23 * @author Doug Davis (dug@us.ibm.com)
24 */
25
26 import org.apache.axis.components.logger.LogFactory;
27 import org.apache.commons.logging.Log;
28
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.Vector;
33
34 public class Options {
35 protected static Log log =
36 LogFactory.getLog(Options.class.getName());
37
38 String args[] = null ;
39 Vector usedArgs = null ;
40 URL defaultURL = null ;
41
42 //////////////////////////////////////////////////////////////////////////
43 // SOASS (Start of Axis Specific Stuff)
44
45 // EOASS
46 //////////////////////////////////////////////////////////////////////////
47
48 /**
49 * Constructor - just pass in the <b>args</b> from the command line.
50 */
51 public Options(String _args[]) throws MalformedURLException {
52 if (_args == null) {
53 _args = new String [] {};
54 }
55 args = _args ;
56 usedArgs = null ;
57 defaultURL = new URL("http://localhost:8080/axis/servlet/AxisServlet");
58
59 ///////////////////////////////////////////////////////////////////////
60 // SOASS
61
62 /* Process these well known options first */
63 /******************************************/
64 try {
65 getURL();
66 } catch( MalformedURLException e ) {
67 log.error( Messages.getMessage("cantDoURL00") );
68 throw e ;
69 }
70 getUser();
71 getPassword();
72
73 // EOASS
74 ///////////////////////////////////////////////////////////////////////
75 }
76
77 public void setDefaultURL(String url) throws MalformedURLException {
78 defaultURL = new URL(url);
79 }
80
81 public void setDefaultURL(URL url) {
82 defaultURL = url ;
83 }
84
85 /**
86 * Returns an int specifying the number of times that the flag was
87 * specified on the command line. Once this flag is looked for you
88 * must save the result because if you call it again for the same
89 * flag you'll get zero.
90 */
91 public int isFlagSet(char optChar) {
92 int value = 0 ;
93 int loop ;
94 int i ;
95
96 for ( loop = 0 ; usedArgs != null && loop < usedArgs.size() ; loop++ ) {
97 String arg = (String) usedArgs.elementAt(loop);
98 if ( arg.charAt(0) != '-' ) continue ;
99 for ( i = 0 ; i < arg.length() ; i++ )
100 if ( arg.charAt(i) == optChar ) value++ ;
101 }
102
103 for ( loop = 0 ; loop < args.length ; loop++ ) {
104 if ( args[loop] == null || args[loop].length() == 0 ) continue ;
105 if ( args[loop].charAt(0) != '-' ) continue ;
106 while (args[loop] != null &&
107 (i = args[loop].indexOf(optChar)) != -1) {
108 args[loop] = args[loop].substring(0, i) + args[loop].substring(i+1) ;
109 if ( args[loop].length() == 1 )
110 args[loop] = null ;
111 value++ ;
112 if ( usedArgs == null ) usedArgs = new Vector();
113 usedArgs.add( "-" + optChar );
114 }
115 }
116 return( value );
117 }
118
119 /**
120 * Returns a string (or null) specifying the value for the passed
121 * option. If the option isn't there then null is returned. The
122 * option's value can be specified one of two ways:
123 * -x value
124 * -xvalue
125 * Note that: -ax value
126 * is not value (meaning flag 'a' followed by option 'x'.
127 * Options with values must be the first char after the '-'.
128 * If the option is specified more than once then the last one wins.
129 */
130 public String isValueSet(char optChar) {
131 String value = null ;
132 int loop ;
133 int i ;
134
135 for ( loop = 0 ; usedArgs != null && loop < usedArgs.size() ; loop++ ) {
136 String arg = (String) usedArgs.elementAt(loop);
137 if ( arg.charAt(0) != '-' || arg.charAt(1) != optChar )
138 continue ;
139 value = arg.substring(2);
140 if ( loop+1 < usedArgs.size() )
141 value = (String) usedArgs.elementAt(++loop);
142 }
143
144 for ( loop = 0 ; loop < args.length ; loop++ ) {
145 if ( args[loop] == null || args[loop].length() == 0 ) continue ;
146 if ( args[loop].charAt(0) != '-' ) continue ;
147 i = args[loop].indexOf( optChar );
148 if ( i != 1 ) continue ;
149 if ( i != args[loop].length()-1 ) {
150 // Not at end of arg, so use rest of arg as value
151 value = args[loop].substring(i+1) ;
152 args[loop] = args[loop].substring(0, i);
153 }
154 else {
155 // Remove the char from the current arg
156 args[loop] = args[loop].substring(0, i);
157
158 // Nothing after char so use next arg
159 if ( loop+1 < args.length && args[loop+1] != null ) {
160 // Next arg is there and non-null
161 if ( args[loop+1].charAt(0) != '-' ) {
162 value = args[loop+1];
163 args[loop+1] = null ;
164 }
165 }
166 else {
167 // Next is null or not there - do nothing
168 // value = null ;
169 }
170 }
171 if ( args[loop].length() == 1 )
172 args[loop] = null ;
173 // For now, keep looping to get that last on there
174 // break ;
175 }
176 if ( value != null ) {
177 if ( usedArgs == null ) usedArgs = new Vector();
178 usedArgs.add( "-" + optChar );
179 if ( value.length() > 0 ) usedArgs.add( value );
180 }
181 return( value );
182 }
183
184 /**
185 * This just returns a string with the unprocessed flags - mainly
186 * for error reporting - so you can report the unknown flags.
187 */
188 public String getRemainingFlags() {
189 StringBuffer sb = null ;
190 int loop ;
191
192 for ( loop = 0 ; loop < args.length ; loop++ ) {
193 if ( args[loop] == null || args[loop].length() == 0 ) continue ;
194 if ( args[loop].charAt(0) != '-' ) continue ;
195 if ( sb == null ) sb = new StringBuffer();
196 sb.append( args[loop].substring(1) );
197 }
198 return( sb == null ? null : sb.toString() );
199 }
200
201 /**
202 * This returns an array of unused args - these are the non-option
203 * args from the command line.
204 */
205 public String[] getRemainingArgs() {
206 ArrayList al = null ;
207 int loop ;
208
209 for ( loop = 0 ; loop < args.length ; loop++ ) {
210 if ( args[loop] == null || args[loop].length() == 0 ) continue ;
211 if ( args[loop].charAt(0) == '-' ) continue ;
212 if ( al == null ) al = new ArrayList();
213 al.add( (String) args[loop] );
214 }
215 if ( al == null ) return( null );
216 String a[] = new String[ al.size() ];
217 for ( loop = 0 ; loop < al.size() ; loop++ )
218 a[loop] = (String) al.get(loop);
219 return( a );
220 }
221
222 //////////////////////////////////////////////////////////////////////////
223 // SOASS
224 public String getURL() throws MalformedURLException {
225 String tmp ;
226 String host = null ; // -h also -l (url)
227 String port = null ; // -p
228 String servlet = null ; // -s also -f (file)
229 String protocol = null ;
230
231 URL url = null ;
232
233 // Just in case...
234 org.apache.axis.client.Call.initialize();
235
236 if ( (tmp = isValueSet( 'l' )) != null ) {
237 url = new URL( tmp );
238 host = url.getHost();
239 port = "" + url.getPort();
240 servlet = url.getFile();
241 protocol = url.getProtocol();
242 }
243
244 if ( (tmp = isValueSet( 'f' )) != null ) {
245 host = "";
246 port = "-1";
247 servlet = tmp;
248 protocol = "file";
249 }
250
251 tmp = isValueSet( 'h' ); if ( host == null ) host = tmp ;
252 tmp = isValueSet( 'p' ); if ( port == null ) port = tmp ;
253 tmp = isValueSet( 's' ); if ( servlet == null ) servlet = tmp ;
254
255 if ( host == null ) host = defaultURL.getHost();
256 if ( port == null ) port = "" + defaultURL.getPort();
257 if ( servlet == null ) servlet = defaultURL.getFile();
258 else
259 if ( servlet.length()>0 && servlet.charAt(0)!='/' )
260 servlet = "/" + servlet ;
261
262 if (url == null) {
263 if (protocol == null) protocol = defaultURL.getProtocol();
264 tmp = protocol + "://" + host ;
265 if ( port != null && !port.equals("-1")) tmp += ":" + port ;
266 if ( servlet != null ) tmp += servlet ;
267 } else tmp = url.toString();
268 log.debug( Messages.getMessage("return02", "getURL", tmp) );
269 return( tmp );
270 }
271
272 public String getHost() {
273 try {
274 URL url = new URL(getURL());
275 return( url.getHost() );
276 }
277 catch( Exception exp ) {
278 return( "localhost" );
279 }
280 }
281
282 public int getPort() {
283 try {
284 URL url = new URL(getURL());
285 return( url.getPort() );
286 }
287 catch( Exception exp ) {
288 return( -1 );
289 }
290 }
291
292 public String getUser() {
293 return( isValueSet('u') );
294 }
295
296 public String getPassword() {
297 return( isValueSet('w') );
298 }
299 // EOASS
300 //////////////////////////////////////////////////////////////////////////
301 }