001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: *
022: * --------------------------------------------------------------------------
023: * $Id: GenerateWrapperConf.java 3830 2003-12-04 21:26:30Z ehardesty $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.tools;
028:
029: import java.util.StringTokenizer;
030:
031: /**
032: * This class is called by 'jonas start' command to generate
033: * properties for the Java Service Wrapper configuration file.
034: */
035: public class GenerateWrapperConf {
036:
037: /**
038: * Generate property file for Java Service Wrapper.
039: * <p>
040: * GenerateWrapperConf parses CLASSPATH or an array of JAVA options
041: * for the current JONAS_BASEW
042: * into individual property definitions for the Java Service Wrapper
043: * configuration file.
044: * <p>
045: * <pre>
046: * Usage: java org.objectweb.jonas.tools.GenerateWrapperConf
047: * [-h | -?] to display usage
048: * [-d <delimiter>] to define delimiter for StringTokenizer
049: * <delimiter> is a string of one or more
050: * characters to be used by StringTokenizer
051: * [-i <index>] to define initial index for generated property
052: * <index> is the initial value to be appended
053: * to the generated property.name entries
054: * property.name name of property(s) to be generated
055: * args string(s) to be parsed into properties
056: * </pre>
057: * <p>
058: * Refer to Java Service Wrapper documentation
059: * for details on configuration file format.
060: * http://wrapper.tanukisoftware.org
061: */
062: String propertyName = null;
063: int index = 1;
064:
065: /**
066: * Display command syntax.
067: */
068: void syntax() {
069: System.err
070: .println("syntax: GenerateWrapperConf [-d <delimiters>] [-i <indx>] property.name args");
071: System.exit(1);
072: }
073:
074: /**
075: * Display command syntax with unrecognized command line argument
076: */
077: void syntax(String arg) {
078: System.err
079: .println("GenerateWrapperConf: unrecognized argument '"
080: + arg + "'");
081: syntax();
082: }
083:
084: /**
085: * Display command syntax with exception message
086: */
087: void syntax(Exception e) {
088: System.err.println(e.toString());
089: syntax();
090: }
091:
092: /**
093: * Generate Wrapper.conf property
094: */
095: void generateKey(String val) {
096: System.out.println(propertyName + "." + index + "=" + val);
097: index += 1;
098: }
099:
100: /**
101: * Process command line args and generate requested
102: * wrapper.conf property entries to STDOUT.
103: */
104: void run(String[] args) {
105: String delimiter = null;
106: int i = 0; // index into args[]
107:
108: for (; i < args.length; i++) {
109: if (args[i].charAt(0) != '-')
110: break;
111: try {
112: switch (args[i].charAt(1)) {
113: case 'h':
114: case '?':
115: usage();
116:
117: case 'd':
118: delimiter = args[++i];
119: break;
120:
121: case 'i':
122: index = Integer.parseInt(args[++i]);
123: break;
124:
125: default:
126: syntax(args[i]);
127: }
128: } catch (StringIndexOutOfBoundsException e) {
129: syntax(args[i]);
130: } catch (NumberFormatException e) {
131: syntax(e);
132: }
133: }
134:
135: // first positional arg after -d is the property.name
136: if ((args.length - i) < 2)
137: syntax();
138: propertyName = args[i];
139: i += 1;
140:
141: if (delimiter == null) // process array of individual args
142: {
143: for (; i < args.length; ++i)
144: generateKey(args[i]);
145: }
146:
147: else // use StringTokenizer to parse single string
148: {
149: StringTokenizer st = new StringTokenizer(args[i], delimiter);
150: while (st.hasMoreTokens())
151: generateKey(st.nextToken());
152: i += 1;
153: }
154:
155: if ((args.length - i) > 0)
156: syntax();
157: }
158:
159: /**
160: * run a new instance of the class
161: */
162: public static void main(String[] args) {
163: if (args.length == 0)
164: usage();
165: else
166: new GenerateWrapperConf().run(args);
167: }
168:
169: /**
170: * Display syntax
171: */
172: private static void usage() {
173: System.out
174: .println("\nUsage: java org.objectweb.java.tools.GenerateWrapperConf [-h | -?]\n"
175: + " [-d <delimiters>] [-i <index>] property.name args\n"
176: + "\n"
177: + " -h or -? display this help message.\n"
178: + " -d <delimiters> specifies a string of delimiters used to parse\n"
179: + " args into individual tokens.\n"
180: + " -i <index> specifies the initial index for generated property\n"
181: + " values. The default value is 1.\n"
182: + " property.name specifies the property name to be generated.\n"
183: + " Property names for the individual tokens are formed\n"
184: + " as 'property.name.index' where 'index' is incremented\n"
185: + " for each token.\n"
186: + " args Either a single string to be parsed using StringTokenizer\n"
187: + " and the delimiter specified by the -d option.\n"
188: + " This form is used to process the CLASSPATH variable.\n\n"
189: + " Or, an array of strings to be processed as individual tokens.\n"
190: + " This form is used to process JAVA_OPTS style variables.\n");
191: System.exit(1);
192: }
193: }
|