Parameter.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Nicolas Christin
00022  * Contributor(s): ______________________________________.
00023  *
00024  * --------------------------------------------------------------------------
00025  * $Id: Parameter.java,v 1.6 2003/11/21 01:13:08 skrupanszky Exp $
00026  * --------------------------------------------------------------------------
00027  */
00028 
00029 
00030 package org.objectweb.jonas.newbean;
00031 
00032 
00033 import java.io.BufferedReader;
00034 import java.io.InputStreamReader;
00035 import java.io.IOException;
00036 
00037 import org.apache.velocity.VelocityContext;
00038 
00039 
00056 public abstract class Parameter {
00057 
00058     private static final String PROMPT = "> ";
00059     private static final int INPUT_BUFFER_SIZE = 80;
00060     private static BufferedReader reader =
00061         new BufferedReader(new InputStreamReader(System.in));
00062 
00067     protected VelocityContext vContext = null;
00068 
00072     protected String value = null;
00073 
00074     
00082     public Parameter(VelocityContext context) {
00083         vContext = context;
00084     }
00085 
00086 
00096     public void walkThrough() {
00097         obtainValue();
00098         export();
00099         Parameter nextParameter = getNextParameter();
00100         if (nextParameter != null) {
00101             nextParameter.walkThrough();
00102         }
00103     }
00104 
00105 
00112     public void obtainValue() {
00113 
00114                 // check first if a cmd-line value has been specified
00115                 String  inp = getCmdArg( getArgKeyword() );
00116                 if( inp!=null ) {
00117                         setValue(inp);
00118                         if( isValid() ) { return; }
00119                 }
00120 
00121         for (;;) {
00122             String input = null;
00123             System.out.println(getPrompt());
00124             System.out.print("> ");
00125             try {
00126                 input = reader.readLine();
00127             } catch (IOException e) {
00128                 NewBean.error(e.toString());
00129             }
00130             if (input == null) {
00131                 input = "";
00132             }
00133             setValue(input);
00134             if (isValid())
00135                 break;
00136             System.out.println("Invalid value, please retry");
00137         }
00138         System.out.println();
00139     }
00140 
00141 
00151     public void setValue(String input) {
00152         value = input;
00153     }
00154 
00155 
00160     public abstract String getPrompt();
00161 
00162 
00170     public abstract boolean isValid();
00171 
00172 
00177     public abstract void export();
00178 
00179 
00191     public abstract Parameter getNextParameter();
00192 
00196         public abstract String          getArgKeyword();
00197 
00198         private String                          getCmdArg( String kwd )
00199         {
00200                 return (String)NewBean.commandLine.get( kwd );
00201         }
00202 
00203 }

Generated on Tue Feb 15 15:05:19 2005 for JOnAS by  doxygen 1.3.9.1