001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.config.core;
020:
021: import java.io.BufferedReader;
022: import java.io.InputStreamReader;
023: import java.util.Vector;
024:
025: import org.apache.lenya.config.core.Configuration;
026: import org.apache.lenya.config.core.FileConfiguration;
027: import org.apache.lenya.config.core.Parameter;
028: import org.apache.lenya.config.impl.BuildPropertiesConfiguration;
029:
030: /**
031: * A command line tool to configure Lenya build
032: */
033: abstract public class ConfigureCommandLine {
034:
035: /**
036: *
037: */
038: static public void changeConfigurations(Vector configs) {
039:
040: BufferedReader br = new BufferedReader(new InputStreamReader(
041: System.in));
042: for (int i = 0; i < configs.size(); i++) {
043: Configuration config = (Configuration) configs.elementAt(i);
044: config.read();
045:
046: Parameter[] params = config.getConfigurableParameters();
047: readParameters(params, config);
048:
049: if (config.localConfigExists()) {
050: System.out
051: .println("\nWARNING: Local configuration already exists!");
052: System.out.print("Do you want to overwrite (y/N)? ");
053: try {
054: String value = br.readLine();
055: if (value.equals("y")) {
056: config.writeLocal();
057: } else {
058: System.out
059: .println("Local configuration has NOT been overwritten.");
060: }
061: } catch (Exception e) {
062: System.err.println(e.getMessage());
063: }
064: } else {
065: config.writeLocal();
066: }
067: }
068: }
069:
070: /**
071: *
072: */
073: abstract public Vector setConfigurations(String rootDir);
074:
075: /**
076: *
077: */
078: static public void readParameters(Parameter[] params,
079: Configuration config) {
080: BufferedReader br = new BufferedReader(new InputStreamReader(
081: System.in));
082: for (int k = 0; k < params.length; k++) {
083: try {
084: boolean notOK = true;
085: while (notOK) {
086: System.out.println("\nParameter "
087: + params[k].getName() + ":");
088: System.out.println(" Default Value : "
089: + params[k].getDefaultValue());
090: System.out.println(" Local Value : "
091: + params[k].getLocalValue());
092: System.out.print(" New Local Value (d/L): ");
093:
094: // Read new value
095: String newValue = br.readLine();
096: if (newValue.equals("d")) {
097: newValue = params[k].getDefaultValue();
098: } else if (newValue.equals("L")
099: || newValue.equals("")) {
100: newValue = params[k].getLocalValue();
101: }
102:
103: // Test entered value
104: if (params[k].test(newValue)) {
105: params[k].setLocalValue(newValue);
106: notOK = false;
107: } else {
108: System.err
109: .println(" WARNING: No such value available!");
110: String[] aValues = params[k]
111: .getAvailableValues();
112: System.err
113: .print(" Available values: ");
114: for (int j = 0; j < aValues.length; j++) {
115: System.err.print(aValues[j] + " ");
116: }
117: System.err.println("");
118: System.err
119: .println(" Re-enter value ...");
120: }
121: }
122:
123: System.out.println(" Value entered : "
124: + params[k].getLocalValue());
125: Parameter[] subParams = params[k]
126: .getSubsequentParameters(params[k]
127: .getLocalValue(), config);
128: if (subParams != null) {
129: String sp = "";
130: for (int j = 0; j < subParams.length; j++) {
131: sp = sp + subParams[j].getName();
132: if (j != subParams.length - 1)
133: sp = sp + ", ";
134: }
135: System.out.println(" " + subParams.length
136: + " Subsequent Params : " + sp);
137: readParameters(subParams, config);
138: }
139: } catch (Exception e) {
140: System.err.println(e.getMessage());
141: }
142: config.setParameter(params[k]);
143: }
144: }
145: }
|