KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > newbean > Parameter


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  * Initial developer(s): Nicolas Christin
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: Parameter.java,v 1.6 2003/11/21 01:13:08 skrupanszky Exp $
26  * --------------------------------------------------------------------------
27  */

28
29
30 package org.objectweb.jonas.newbean;
31
32
33 import java.io.BufferedReader JavaDoc;
34 import java.io.InputStreamReader JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import org.apache.velocity.VelocityContext;
38
39
40 /**
41  * This class represents a parameter the user may be prompted for. It
42  * is responsible for asking the user for a value, validating his
43  * input, exporting relevant datas into Velocity's context, and
44  * deciding what should be the next parameter to ask.<p>
45  *
46  * A parameter is created by subclassing this class and overriding the
47  * following abstract methods:
48  *
49  * <ul>
50  * <li>{@link #getPrompt()}</li>
51  * <li>{@link #isValid()}</li>
52  * <li>{@link #export()}</li>
53  * <li>{@link #getNextParameter()}</li>
54  * </ul>
55  */

56 public abstract class Parameter {
57
58     private static final String JavaDoc PROMPT = "> ";
59     private static final int INPUT_BUFFER_SIZE = 80;
60     private static BufferedReader JavaDoc reader =
61         new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
62
63     /**
64      * The Velocity context into which this parameter's variables will
65      * be exported.
66      */

67     protected VelocityContext vContext = null;
68
69     /**
70      * This parameter's value, as entered by the user.
71      */

72     protected String JavaDoc value = null;
73
74     
75     /**
76      * Creates a new parameter that will export its variables into the
77      * specified Velocity context.
78      *
79      * @param context the Velocity context into which variables will
80      * be exported
81      */

82     public Parameter(VelocityContext context) {
83         vContext = context;
84     }
85
86
87     /**
88      * Recusively walk through the parameters graph, obtaining
89      * parameter ({@link #obtainValue()}) values and exporting ({@link
90      * #export()}) them.<p>
91      *
92      * The path taken through the parameter graph may change depending
93      * on the values entered by the user. It is determined after each
94      * valid input by a call to {@link #getNextParameter()}.
95      */

96     public void walkThrough() {
97         obtainValue();
98         export();
99         Parameter nextParameter = getNextParameter();
100         if (nextParameter != null) {
101             nextParameter.walkThrough();
102         }
103     }
104
105
106     /**
107      * Obtains the value of this parameter. This method prompts the
108      * user for a value, stores it into {@link #value} through the
109      * {@link #isValid()}. This process is repeated until the value is
110      * valid.
111      */

112     public void obtainValue() {
113
114         // check first if a cmd-line value has been specified
115
String JavaDoc inp = getCmdArg( getArgKeyword() );
116         if( inp!=null ) {
117             setValue(inp);
118             if( isValid() ) { return; }
119         }
120
121         for (;;) {
122             String JavaDoc input = null;
123             System.out.println(getPrompt());
124             System.out.print("> ");
125             try {
126                 input = reader.readLine();
127             } catch (IOException JavaDoc e) {
128                 NewBean.error(e.toString());
129             }
130             if (input == null) {
131                 input = "";
132             }
133             setValue(input);
134             if (isValid())
135                 break;
136             System.out.println("Invalid value, please retry");
137         }
138         System.out.println();
139     }
140
141
142     /**
143      * Sets the {@link #value} of this parameter. This method is
144      * called each time a value is entered is entered by the user,
145      * before its validity has been checked.<p>
146      *
147      * This implementation simply sets {@link #value} to
148      * <code>input</code>. You can override this method if you need to
149      * format this input, for example to convert it to uppercase.
150      */

151     public void setValue(String JavaDoc input) {
152         value = input;
153     }
154
155
156     /**
157      * Returns the string used to prompt the user for a value.
158      * @return the string used to prompt the user for a value.
159      */

160     public abstract String JavaDoc getPrompt();
161
162
163     /**
164      * Indicates whether this parameter as a valid value. This method
165      * can safely assume {@link #value} is not <code>null</code>.
166      *
167      * @return <code>true</code> if {@link #value} is valid,
168      * <code>false</code> otherwise
169      */

170     public abstract boolean isValid();
171
172
173     /**
174      * Exports the variables managed by this parameter into the
175      * associated Velocity context (<i>ie</i> {@link #vContext}).
176      */

177     public abstract void export();
178
179
180     /**
181      * Returns the parameter the user will be asked for after this
182      * one. This method is not invoked before a valid value has been
183      * entered for this parameter; therefor it is possible to decide
184      * which object to return based on the value of {@link #value}.<p>
185      *
186      * This method returns <code>null</code> if their is no more
187      * parameters.
188      *
189      * @return the next parameter the user should be prompted for
190      */

191     public abstract Parameter getNextParameter();
192
193     /**
194      * @return the command line keyword string for this parameter
195      */

196     public abstract String JavaDoc getArgKeyword();
197
198     private String JavaDoc getCmdArg( String JavaDoc kwd )
199     {
200         return (String JavaDoc)NewBean.commandLine.get( kwd );
201     }
202
203 }
204
Popular Tags