Last commit for OptionsParser.java: a87d8e28d0332a216a5d6e0104908fc9e5ff58b9

added javadoc comments

dp2041 [2002-10-26 00:38:20]
added javadoc comments
  1. /*
  2.  * @(#)OptionsParser.java
  3.  *
  4.  * Copyright (c) 2002: The Trustees of Columbia University in the City of New York. All Rights Reserved
  5.  *
  6.  * Copyright (c) 2002: @author Dan Phung
  7.  *
  8.  * CVS version control block - do not edit manually
  9.  * $RCSfile$
  10.  * $Revision$
  11.  * $Date$
  12.  * $Source$
  13.  */
  14.  
  15. package psl.worklets;
  16. import java.io.*;
  17.  
  18. /** Parses the options from the command line for the {@link WVM} */
  19. public class OptionsParser {
  20.  
  21. /** Name of the program running the parser */
  22. private String _program;
  23. /** Output print stream */
  24. private PrintStream out;
  25. /** Error print stream */
  26. private PrintStream err;
  27.  
  28. /** RMI name to bind to (default "WVM_HOST") */
  29. public String name = "WVM_Host";
  30. /** Port on which to create the RMI registry (default = <code>WVM_Host.PORT</code>) */
  31. public int port = WVM_Host.PORT;
  32. /** File holding the public/private keys */
  33. public String keysfile = null;
  34. /** Password into the keysfile */
  35. public String password = null;
  36. /** <code>SSLContext</code> under which to create the {@link WVM} */
  37. public String ctx = null;
  38. /** <code>KeyManagerFactory</code> type of use */
  39. public String kmf = null;
  40. /** <code>KeyStore</code> type to use */
  41. public String ks = null;
  42. /** <code>SecureRandom</code> (random number generator algorithm) to use (default = SHA1PRNG) */
  43. public String rng = "SHA1PRNG";
  44. /** File holding all the {@link WVM} properties */
  45. public String WVMfile = null;
  46. /**
  47.   * Level of security of the created {@link WVM}
  48.   */
  49. public int securityLevel = WVM.NO_SECURITY; // the default security level of the WVM.
  50.  
  51. /** Creates an OptionsParser */
  52. public OptionsParser(){
  53. this("");
  54. }
  55.  
  56. /**
  57.   * Creates an OptionsParser to be used by the specified program
  58.   *
  59.   * @param program: Parent process that will the OptionsParser
  60.   */
  61. public OptionsParser(String program){
  62. _program = program;
  63. out = WVM.out;
  64. err = WVM.err;
  65. }
  66.  
  67. /**
  68.   * Parses the options from the environment variables, the WVM file, and the
  69.   * command line
  70.   *
  71.   * @param args[]: Arguments, usually from the command prompt as received by main
  72.   * @return error value
  73.   */
  74. public int ParseOptions(String args[]) {
  75.  
  76. // first we load the system environment variables.
  77. loadSystemProps();
  78.  
  79. // then we see if the -wvmfile option was given.
  80. // the WVMfile could also have been set by a system property at startup
  81. if (args.length != 0) {
  82. int i=-1;
  83. String opt = null;
  84. while(++i < args.length) {
  85. opt = args[i];
  86. if (opt.equals("--wvmfile") || opt.equals("-wvmfile") ||
  87. opt.equals("--file") || opt.equals("-file") || opt.equals("-f")) {
  88. WVMfile = args[++i];
  89. break;
  90. }
  91. }
  92. }
  93.  
  94. // next we load the WVM file properties
  95. if (WVMfile != null) loadWVMFile(WVMfile);
  96.  
  97. // then we load the arguements from the command line.
  98. int error= 0;
  99. if (args.length != 0) error = loadCommandLine(args);
  100.  
  101. // set the system properties to the parameters we have so far.
  102. setWVMProperties();
  103.  
  104. return error;
  105. }
  106.  
  107. /**
  108.   * Loads the {@link WVM} properties related environment variables
  109.   * and accordingly sets the OptionsParser's internal public fields
  110.   */
  111. public void loadSystemProps() {
  112. String prop = "";
  113.  
  114. prop = System.getProperty("WVM_RMI_NAME");
  115. if (prop != null) name = prop;
  116.  
  117. prop = System.getProperty("WVM_RMI_PORT");
  118. if (prop != null) port = Integer.parseInt(prop);
  119.  
  120. prop = System.getProperty("WVM_KEYSFILE");
  121. if (prop != null) keysfile = prop;
  122.  
  123. prop = System.getProperty("WVM_PASSWORD");
  124. System.setProperty("WVM_PASSWORD", "");
  125. if (prop != null) password = prop;
  126.  
  127. prop = System.getProperty("WVM_SSLCONTEXT");
  128. if (prop != null) ctx = prop;
  129.  
  130. prop = System.getProperty("WVM_KEYMANAGER");
  131. if (prop != null) kmf = prop;
  132.  
  133. prop = System.getProperty("WVM_KEYSTORE");
  134. if (prop != null) ks = prop;
  135.  
  136. prop = System.getProperty("WVM_RNG");
  137. if (prop != null) rng = prop;
  138.  
  139. prop = System.getProperty("WVM_FILE");
  140. if (prop != null) WVMfile = prop;
  141.  
  142. prop = System.getProperty("WVM_SECURITY_LEVEL");
  143. if (prop != null) securityLevel = Integer.parseInt(prop);
  144. }
  145.  
  146. /**
  147.   * Loads and parses the given WVM file
  148.   *
  149.   * @param wvmfile: file holding all the WVM related properties
  150.   */
  151. public void loadWVMFile(String wvmfile) {
  152.  
  153. BufferedReader reader = null;
  154. try {
  155. reader = new BufferedReader(new FileReader(wvmfile));
  156. } catch (FileNotFoundException e){
  157. err.println("Error with file " + wvmfile + ": " + e);
  158. }
  159.  
  160. String[] buffer;
  161. try {
  162. while(reader.ready()){
  163. String nextline = reader.readLine().trim();
  164.  
  165. // add the ability to put commments in the wvm file
  166. if (nextline.matches(".*[#].*")) {
  167. buffer = nextline.split("#");
  168. nextline = buffer[0].trim();
  169.  
  170. }
  171.  
  172. if (nextline.matches("\\s*"))
  173. continue;
  174.  
  175. buffer = nextline.split("=");
  176. if (buffer.length == 1)
  177. continue;
  178.  
  179. if (buffer[0].equals("WVM_RMI_PORT"))
  180. port = Integer.parseInt(buffer[1]);
  181. else if (buffer[0].equals("WVM_RMI_NAME"))
  182. name = buffer[1];
  183. else if (buffer[0].equals("WVM_KEYSFILE"))
  184. keysfile = buffer[1];
  185. else if (buffer[0].equals("WVM_PASSWORD"))
  186. password = buffer[1];
  187. else if (buffer[0].equals("WVM_SSLCONTEXT"))
  188. ctx = buffer[1];
  189. else if (buffer[0].equals("WVM_KEYMANAGER"))
  190. kmf = buffer[1];
  191. else if (buffer[0].equals("WVM_KEYSTORE"))
  192. ks = buffer[1];
  193. else if (buffer[0].equals("WVM_RNG"))
  194. rng = buffer[1];
  195. else if (buffer[0].equals("WVM_SECURITY_LEVEL"))
  196. securityLevel = Integer.parseInt(buffer[1]);
  197. else if (buffer[0].equals("WVM_FILE"))
  198. WVMfile = buffer[1];
  199. else
  200. System.setProperty(buffer[0], buffer[1]);
  201. }
  202. reader.close();
  203.  
  204. } catch (IOException e) {
  205. err.println("Error with file " + wvmfile + ": " + e);
  206. }
  207. }
  208.  
  209. /**
  210.   * Parses the options from the command line
  211.   *
  212.   * @param args[]: Arguments, usually from the command prompt as received by main
  213.   * @return error code
  214.   */
  215. private int loadCommandLine(String[] args) {
  216. int i=-1;
  217. while(++i < args.length) {
  218. String opt = args[i];
  219. if (opt.equals("--help") || opt.equals("-help") || opt.equals("-h"))
  220. {
  221. printHelp();
  222. return 1;
  223. }
  224. else if (opt.equals("--name") || opt.equals("-name") || opt.equals("-n"))
  225. name = args[++i];
  226. else if (opt.equals("--port") || opt.equals("-port") || opt.equals("-p"))
  227. port = Integer.parseInt(args[++i]);
  228. else if (opt.equals("--keysfile") || opt.equals("-keysfile") || opt.equals("-k"))
  229. keysfile = args[++i];
  230. else if (opt.equals("--password") || opt.equals("-password") || opt.equals("-P"))
  231. password = args[++i];
  232. else if (opt.equals("--context") || opt.equals("-context") || opt.equals("-c"))
  233. ctx = args[++i];
  234. else if (opt.equals("--keymanager") || opt.equals("-keymanager") || opt.equals("-m"))
  235. kmf = args[++i];
  236. else if (opt.equals("--keystore") || opt.equals("-keystore") || opt.equals("-s"))
  237. ks = args[++i];
  238. else if (opt.equals("--rng") || opt.equals("-rng") || opt.equals("-r"))
  239. rng = args[++i];
  240. else if (opt.equals("--securityLevel") || opt.equals("-securityLevel") || opt.equals("-S"))
  241. securityLevel = Integer.parseInt(args[++i]);
  242. else if (opt.equals("--hostsAllow") || opt.equals("-hostsAllow") || opt.equals("-a"))
  243. {
  244. // append these to the list already in the system
  245. String allowed = System.getProperty("WVM_HOSTS_ALLOW");
  246. if (allowed != null) System.setProperty("WVM_HOSTS_ALLOW", allowed + "," + args[++i]);
  247. else System.setProperty("WVM_HOSTS_ALLOW", args[++i]);
  248. }
  249. else if (opt.equals("--hostsDeny") || opt.equals("-hostDeny") || opt.equals("-d"))
  250. {
  251. // append these to the list already in the system
  252. String denied = System.getProperty("WVM_HOSTS_DENY");
  253. if (denied != null) System.setProperty("WVM_HOSTS_DENY", denied + "," + args[++i]);
  254. else System.setProperty("WVM_HOSTS_ALLOW", args[++i]);
  255. }
  256. else if (opt.equals("--wvmfile") || opt.equals("-wvmfile") ||
  257. opt.equals("--file") || opt.equals("-file") || opt.equals("-f"))
  258. WVMfile = args[++i];
  259. else
  260. err.println("WVM Error -- unknown arguements: " + opt);
  261. }
  262. return 0;
  263. }
  264.  
  265. /**
  266.   * Sets certain parsed values into the environment. The environment
  267.   * variables are: WVM_RMI_NAME, WVM_RMI_PORT, WVM_KEYSFILE,
  268.   * WVM_SSLCONTEXT, WVM_KEYMANAGER, WVM_KEYSTORE, WVM_RNG,
  269.   * WVM_SECURITY_LEVEL, and WVM_FILE.
  270.   */
  271. public void setWVMProperties() {
  272. if (name != null) System.setProperty("WVM_RMI_NAME", name);
  273. if (port != -1) System.setProperty("WVM_RMI_PORT", "" + port);
  274. if (keysfile != null) System.setProperty("WVM_KEYSFILE", keysfile);
  275. if (ctx != null) System.setProperty("WVM_SSLCONTEXT", ctx);
  276. if (kmf != null) System.setProperty("WVM_KEYMANAGER", kmf);
  277. if (ks != null) System.setProperty("WVM_KEYSTORE", ks);
  278. if (rng != null) System.setProperty("WVM_RNG", rng);
  279. System.setProperty("WVM_SECURITY_LEVEL", ""+securityLevel);
  280. if (WVMfile != null) System.setProperty("WVM_FILE", WVMfile);
  281. }
  282.  
  283. /** Prints out the usage and help info */
  284. private void printHelp() {
  285. out.println("Usage: java " + _program + " <options>");
  286. out.println("");
  287. out.println("<Options>: ");
  288. out.println("-help, -h:\tPrint this message.");
  289. out.println("-name, -n:\tThe name to be bound to in the rmi registry (default=WVM_Host).");
  290. out.println("-port, -p:\tThe port used to for the RMI service (default=9100).");
  291. out.println("-keysfile, -k:\tLocation and name of keys file holding the");
  292. out.println("\t\t public/private keys (required for secure transporter).");
  293. out.println("-password, -P:\tPassword for keysfile (req'd for secure transporter).");
  294. out.println("-context, -c:\tThe instance of the context to be implemented");
  295. out.println("\t\t (optional, default = \"TLS\").");
  296. out.println("-keymanager, -m: The algorithm of the Key Manager Factory to be ");
  297. out.println("\t\t implemented (optional, default = \"SunX509\").");
  298. out.println("-keystore, -s:\tThe algorithm used in the keystore of the keys file");
  299. out.println("\t\t (optional, default = \"JKS\").");
  300. out.println("-rng, -R:\tThe random number generator algorithm to use");
  301. out.println("\t\t (optional, default = \"SHA1PRNG\").");
  302. out.println("-securityLevel, -S:Set the security level (see below.)");
  303. out.println("-hostsAllow, -a: Specify the allowed hosts.");
  304. out.println("-hostsDeny, -d:\tSpecify the hosts that should be denied access.");
  305. out.println("-wvmfile, -f:\tThe filename containing the WVM properties (see below).");
  306. out.println("");
  307. out.println(" DESCRIPTION");
  308. out.println(" To create a plain WVM host you do not need to specify any parameters");
  309. out.println(" and the RMI server name/port will default to WVM_Host/9100. To create");
  310. out.println(" a secure WVM host you will need to specify at least the keysfile, ");
  311. out.println(" password, and the WVM Properties file. This can be done either by setting");
  312. out.println(" the environment variables with -D or by using the switches as described");
  313. out.println(" above. The WVM parameters in the WVM file are the same as you would set");
  314. out.println(" them using these options facility described above. The file contains ");
  315. out.println(" property=value pairs separated by white space. Note that the security of");
  316. out.println(" this file is at the operating systems level, so you must be aware of the");
  317. out.println(" permissions set for the file (as there might be passwords in the file.)");
  318. out.println(" The possible WVM systems properties are: WVM_RMI_NAME, WVM_RMI_PORT, ");
  319. out.println(" WVM_KEYSFILE, WVM_PASSWORD, WVM_SSLCONTEXT, WVM_KEYMANAGER, WVM_KEYSTORE,");
  320. out.println(" WVM_RNG, WVM_SECURITY_LEVEL, WVM_HOSTS_ALLOW, WVM_HOSTS_DENY and WVM_FILE");
  321. out.println(" which all correspond to the settings as described above. Note that the");
  322. out.println(" WVM_PASSWORD setting is never explicitly set in the Systems.Property.");
  323. out.println(" Also, if you set the WVM_PASSWORD with the -D facility the property will");
  324. out.println(" be flushed from the system properties after it is read by the program.");
  325. out.println(" IMPORTANT: There is also an internally needed parameter that needs to be");
  326. out.println(" set in the WVM file, namely: ");
  327. out.println(" java.rmi.server.RMIClassLoaderSpi=psl.worklets.WVM_RMIClassLoaderSpi");
  328. out.println(" ");
  329. out.println(" The precedence of loaded settings (from lowest to highest) are:");
  330. out.println(" 1) ENVIRONMENT/SYSTEM settings");
  331. out.println(" 2) WVM file settings");
  332. out.println(" 3) command-line settings");
  333. out.println(" Only the WVM_HOST_ALLOW and DENY properties are concantenated.");
  334. out.println(" ");
  335. out.println("Examples:");
  336. out.println("Plain WVM: java psl.worklets.WVM -name target");
  337. out.println("Secure WVM: java -DWVM_FILE=wvm_properties psl.worklets.WVM");
  338. out.println("");
  339. out.println("For more information please see the Worklets documentation.");
  340. }
  341. }