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: * Initial developer(s): Nicolas Christin
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: Parameter.java 3736 2003-11-21 01:13:25Z skrupanszky $
026: * --------------------------------------------------------------------------
027: */
028:
029: package org.objectweb.jonas.newbean;
030:
031: import java.io.BufferedReader;
032: import java.io.InputStreamReader;
033: import java.io.IOException;
034:
035: import org.apache.velocity.VelocityContext;
036:
037: /**
038: * This class represents a parameter the user may be prompted for. It
039: * is responsible for asking the user for a value, validating his
040: * input, exporting relevant datas into Velocity's context, and
041: * deciding what should be the next parameter to ask.<p>
042: *
043: * A parameter is created by subclassing this class and overriding the
044: * following abstract methods:
045: *
046: * <ul>
047: * <li>{@link #getPrompt()}</li>
048: * <li>{@link #isValid()}</li>
049: * <li>{@link #export()}</li>
050: * <li>{@link #getNextParameter()}</li>
051: * </ul>
052: */
053: public abstract class Parameter {
054:
055: private static final String PROMPT = "> ";
056: private static final int INPUT_BUFFER_SIZE = 80;
057: private static BufferedReader reader = new BufferedReader(
058: new InputStreamReader(System.in));
059:
060: /**
061: * The Velocity context into which this parameter's variables will
062: * be exported.
063: */
064: protected VelocityContext vContext = null;
065:
066: /**
067: * This parameter's value, as entered by the user.
068: */
069: protected String value = null;
070:
071: /**
072: * Creates a new parameter that will export its variables into the
073: * specified Velocity context.
074: *
075: * @param context the Velocity context into which variables will
076: * be exported
077: */
078: public Parameter(VelocityContext context) {
079: vContext = context;
080: }
081:
082: /**
083: * Recusively walk through the parameters graph, obtaining
084: * parameter ({@link #obtainValue()}) values and exporting ({@link
085: * #export()}) them.<p>
086: *
087: * The path taken through the parameter graph may change depending
088: * on the values entered by the user. It is determined after each
089: * valid input by a call to {@link #getNextParameter()}.
090: */
091: public void walkThrough() {
092: obtainValue();
093: export();
094: Parameter nextParameter = getNextParameter();
095: if (nextParameter != null) {
096: nextParameter.walkThrough();
097: }
098: }
099:
100: /**
101: * Obtains the value of this parameter. This method prompts the
102: * user for a value, stores it into {@link #value} through the
103: * {@link #isValid()}. This process is repeated until the value is
104: * valid.
105: */
106: public void obtainValue() {
107:
108: // check first if a cmd-line value has been specified
109: String inp = getCmdArg(getArgKeyword());
110: if (inp != null) {
111: setValue(inp);
112: if (isValid()) {
113: return;
114: }
115: }
116:
117: for (;;) {
118: String input = null;
119: System.out.println(getPrompt());
120: System.out.print("> ");
121: try {
122: input = reader.readLine();
123: } catch (IOException e) {
124: NewBean.error(e.toString());
125: }
126: if (input == null) {
127: input = "";
128: }
129: setValue(input);
130: if (isValid())
131: break;
132: System.out.println("Invalid value, please retry");
133: }
134: System.out.println();
135: }
136:
137: /**
138: * Sets the {@link #value} of this parameter. This method is
139: * called each time a value is entered is entered by the user,
140: * before its validity has been checked.<p>
141: *
142: * This implementation simply sets {@link #value} to
143: * <code>input</code>. You can override this method if you need to
144: * format this input, for example to convert it to uppercase.
145: */
146: public void setValue(String input) {
147: value = input;
148: }
149:
150: /**
151: * Returns the string used to prompt the user for a value.
152: * @return the string used to prompt the user for a value.
153: */
154: public abstract String getPrompt();
155:
156: /**
157: * Indicates whether this parameter as a valid value. This method
158: * can safely assume {@link #value} is not <code>null</code>.
159: *
160: * @return <code>true</code> if {@link #value} is valid,
161: * <code>false</code> otherwise
162: */
163: public abstract boolean isValid();
164:
165: /**
166: * Exports the variables managed by this parameter into the
167: * associated Velocity context (<i>ie</i> {@link #vContext}).
168: */
169: public abstract void export();
170:
171: /**
172: * Returns the parameter the user will be asked for after this
173: * one. This method is not invoked before a valid value has been
174: * entered for this parameter; therefor it is possible to decide
175: * which object to return based on the value of {@link #value}.<p>
176: *
177: * This method returns <code>null</code> if their is no more
178: * parameters.
179: *
180: * @return the next parameter the user should be prompted for
181: */
182: public abstract Parameter getNextParameter();
183:
184: /**
185: * @return the command line keyword string for this parameter
186: */
187: public abstract String getArgKeyword();
188:
189: private String getCmdArg(String kwd) {
190: return (String) NewBean.commandLine.get(kwd);
191: }
192:
193: }
|