KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > input > PropertyFileInputHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Properties JavaDoc;
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 JavaDoc props = null;
34
35     /**
36      * Name of the system property we expect to hold the file name.
37      */

38     public static final String JavaDoc 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 JavaDoc 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 JavaDoc propsFile = System.getProperty(FILE_NAME_KEY);
74             if (propsFile == null) {
75                 throw new BuildException("System property "
76                                          + FILE_NAME_KEY
77                                          + " for PropertyFileInputHandler not"
78                                          + " set");
79             }
80
81             props = new Properties JavaDoc();
82
83             try {
84                 props.load(new FileInputStream JavaDoc(propsFile));
85             } catch (IOException JavaDoc e) {
86                 throw new BuildException("Couldn't load " + propsFile, e);
87             }
88         }
89     }
90
91 }
92
Popular Tags