01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.tools.ant.input;
20:
21: import java.io.FileInputStream;
22: import java.io.IOException;
23: import java.util.Properties;
24: import org.apache.tools.ant.BuildException;
25:
26: /**
27: * Reads input from a property file, the file name is read from the
28: * system property ant.input.properties, the prompt is the key for input.
29: *
30: * @since Ant 1.5
31: */
32: public class PropertyFileInputHandler implements InputHandler {
33: private Properties props = null;
34:
35: /**
36: * Name of the system property we expect to hold the file name.
37: */
38: public static final String FILE_NAME_KEY = "ant.input.properties";
39:
40: /**
41: * Empty no-arg constructor.
42: */
43: public PropertyFileInputHandler() {
44: }
45:
46: /**
47: * Picks up the input from a property, using the prompt as the
48: * name of the property.
49: * @param request an input request.
50: *
51: * @exception BuildException if no property of that name can be found.
52: */
53: public void handleInput(InputRequest request) throws BuildException {
54: readProps();
55:
56: Object o = props.get(request.getPrompt());
57: if (o == null) {
58: throw new BuildException("Unable to find input for \'"
59: + request.getPrompt() + "\'");
60: }
61: request.setInput(o.toString());
62: if (!request.isInputValid()) {
63: throw new BuildException("Found invalid input " + o
64: + " for \'" + request.getPrompt() + "\'");
65: }
66: }
67:
68: /**
69: * Reads the properties file if it hasn't already been read.
70: */
71: private synchronized void readProps() throws BuildException {
72: if (props == null) {
73: String propsFile = System.getProperty(FILE_NAME_KEY);
74: if (propsFile == null) {
75: throw new BuildException("System property "
76: + FILE_NAME_KEY
77: + " for PropertyFileInputHandler not" + " set");
78: }
79:
80: props = new Properties();
81:
82: try {
83: props.load(new FileInputStream(propsFile));
84: } catch (IOException e) {
85: throw new BuildException("Couldn't load " + propsFile,
86: e);
87: }
88: }
89: }
90:
91: }
|