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