1 16 17 package org.apache.axis.utils ; 18 19 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 45 48 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 62 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 } 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 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 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 value = args[loop].substring(i+1) ; 152 args[loop] = args[loop].substring(0, i); 153 } 154 else { 155 args[loop] = args[loop].substring(0, i); 157 158 if ( loop+1 < args.length && args[loop+1] != null ) { 160 if ( args[loop+1].charAt(0) != '-' ) { 162 value = args[loop+1]; 163 args[loop+1] = null ; 164 } 165 } 166 else { 167 } 170 } 171 if ( args[loop].length() == 1 ) 172 args[loop] = null ; 173 } 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 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 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 public String getURL() throws MalformedURLException { 225 String tmp ; 226 String host = null ; String port = null ; String servlet = null ; String protocol = null ; 230 231 URL url = null ; 232 233 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 } 302 | Popular Tags |