KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > tools > GenerateWrapperConf


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  *
22  * --------------------------------------------------------------------------
23  * $Id: GenerateWrapperConf.java,v 1.1 2003/12/04 21:26:30 ehardesty Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.tools;
28
29 import java.util.StringTokenizer JavaDoc;
30 /**
31  * This class is called by 'jonas start' command to generate
32  * properties for the Java Service Wrapper configuration file.
33  */

34 public class GenerateWrapperConf
35 {
36
37   /**
38    * Generate property file for Java Service Wrapper.
39    * <p>
40    * GenerateWrapperConf parses CLASSPATH or an array of JAVA options
41    * for the current JONAS_BASEW
42    * into individual property definitions for the Java Service Wrapper
43    * configuration file.
44    * <p>
45    * <pre>
46    * Usage: java org.objectweb.jonas.tools.GenerateWrapperConf
47    * [-h | -?] to display usage
48    * [-d <delimiter>] to define delimiter for StringTokenizer
49    * <delimiter> is a string of one or more
50    * characters to be used by StringTokenizer
51    * [-i <index>] to define initial index for generated property
52    * <index> is the initial value to be appended
53    * to the generated property.name entries
54    * property.name name of property(s) to be generated
55    * args string(s) to be parsed into properties
56    * </pre>
57    * <p>
58    * Refer to Java Service Wrapper documentation
59    * for details on configuration file format.
60    * http://wrapper.tanukisoftware.org
61    */

62   String JavaDoc propertyName = null;
63   int index = 1;
64
65   /**
66    * Display command syntax.
67    */

68   void syntax()
69   {
70     System.err.println("syntax: GenerateWrapperConf [-d <delimiters>] [-i <indx>] property.name args");
71     System.exit(1);
72   }
73
74   /**
75    * Display command syntax with unrecognized command line argument
76    */

77   void syntax(String JavaDoc arg)
78   {
79     System.err.println("GenerateWrapperConf: unrecognized argument '" + arg + "'");
80     syntax();
81   }
82
83   /**
84    * Display command syntax with exception message
85    */

86   void syntax(Exception JavaDoc e)
87   {
88     System.err.println(e.toString());
89     syntax();
90   }
91
92   /**
93    * Generate Wrapper.conf property
94    */

95   void generateKey(String JavaDoc val)
96   {
97     System.out.println (propertyName + "." + index + "=" + val);
98     index += 1;
99   }
100
101   /**
102    * Process command line args and generate requested
103    * wrapper.conf property entries to STDOUT.
104    */

105     void run(String JavaDoc[] args)
106     {
107     String JavaDoc delimiter = null;
108     int i = 0; // index into args[]
109

110     for ( ; i < args.length; i++)
111     {
112       if (args[i].charAt(0) != '-') break;
113       try {
114         switch(args[i].charAt(1))
115         {
116           case 'h':
117           case '?':
118             usage();
119
120           case 'd':
121             delimiter = args[++i];
122             break;
123
124           case 'i':
125             index = Integer.parseInt(args[++i]);
126             break;
127
128           default:
129             syntax(args[i]);
130         }
131       }
132       catch (StringIndexOutOfBoundsException JavaDoc e) {
133         syntax(args[i]);
134       }
135       catch (NumberFormatException JavaDoc e) {
136         syntax(e);
137       }
138     }
139
140     // first positional arg after -d is the property.name
141
if ((args.length - i) < 2) syntax();
142     propertyName = args[i];
143     i += 1;
144
145     if (delimiter == null) // process array of individual args
146
{
147       for ( ; i < args.length; ++i) generateKey(args[i]);
148     }
149
150     else // use StringTokenizer to parse single string
151
{
152       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args[i], delimiter);
153       while (st.hasMoreTokens()) generateKey(st.nextToken());
154       i += 1;
155     }
156
157     if ((args.length - i) > 0) syntax();
158     }
159
160   /**
161    * run a new instance of the class
162    */

163   public static void main(String JavaDoc[] args)
164   {
165     if (args.length == 0)
166       usage();
167     else
168       new GenerateWrapperConf().run(args);
169   }
170
171   /**
172    * Display syntax
173    */

174   private static void usage()
175   {
176     System.out.println(
177       "\nUsage: java org.objectweb.java.tools.GenerateWrapperConf [-h | -?]\n" +
178       " [-d <delimiters>] [-i <index>] property.name args\n" +
179       "\n" +
180       " -h or -? display this help message.\n" +
181       " -d <delimiters> specifies a string of delimiters used to parse\n" +
182       " args into individual tokens.\n" +
183       " -i <index> specifies the initial index for generated property\n" +
184       " values. The default value is 1.\n" +
185       " property.name specifies the property name to be generated.\n" +
186       " Property names for the individual tokens are formed\n" +
187       " as 'property.name.index' where 'index' is incremented\n" +
188       " for each token.\n" +
189       " args Either a single string to be parsed using StringTokenizer\n" +
190       " and the delimiter specified by the -d option.\n" +
191       " This form is used to process the CLASSPATH variable.\n\n" +
192       " Or, an array of strings to be processed as individual tokens.\n" +
193       " This form is used to process JAVA_OPTS style variables.\n"
194     );
195     System.exit(1);
196   }
197 }
Popular Tags