1 28 29 30 package org.objectweb.jonas.newbean; 31 32 33 import java.io.BufferedReader ; 34 import java.io.InputStreamReader ; 35 import java.io.IOException ; 36 37 import org.apache.velocity.VelocityContext; 38 39 40 56 public abstract class Parameter { 57 58 private static final String PROMPT = "> "; 59 private static final int INPUT_BUFFER_SIZE = 80; 60 private static BufferedReader reader = 61 new BufferedReader (new InputStreamReader (System.in)); 62 63 67 protected VelocityContext vContext = null; 68 69 72 protected String value = null; 73 74 75 82 public Parameter(VelocityContext context) { 83 vContext = context; 84 } 85 86 87 96 public void walkThrough() { 97 obtainValue(); 98 export(); 99 Parameter nextParameter = getNextParameter(); 100 if (nextParameter != null) { 101 nextParameter.walkThrough(); 102 } 103 } 104 105 106 112 public void obtainValue() { 113 114 String inp = getCmdArg( getArgKeyword() ); 116 if( inp!=null ) { 117 setValue(inp); 118 if( isValid() ) { return; } 119 } 120 121 for (;;) { 122 String input = null; 123 System.out.println(getPrompt()); 124 System.out.print("> "); 125 try { 126 input = reader.readLine(); 127 } catch (IOException 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 151 public void setValue(String input) { 152 value = input; 153 } 154 155 156 160 public abstract String getPrompt(); 161 162 163 170 public abstract boolean isValid(); 171 172 173 177 public abstract void export(); 178 179 180 191 public abstract Parameter getNextParameter(); 192 193 196 public abstract String getArgKeyword(); 197 198 private String getCmdArg( String kwd ) 199 { 200 return (String )NewBean.commandLine.get( kwd ); 201 } 202 203 } 204 | Popular Tags |