1. /*
  2. * Copyright 2002,2004 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. package org.apache.tools.ant.input;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.util.Properties;
  21. import org.apache.tools.ant.BuildException;
  22. /**
  23. * Reads input from a property file, the file name is read from the
  24. * system property ant.input.properties, the prompt is the key for input.
  25. *
  26. * @version $Revision: 1.6.2.4 $
  27. * @since Ant 1.5
  28. */
  29. public class PropertyFileInputHandler implements InputHandler {
  30. private Properties props = null;
  31. /**
  32. * Name of the system property we expect to hold the file name.
  33. */
  34. public static final String FILE_NAME_KEY = "ant.input.properties";
  35. /**
  36. * Empty no-arg constructor.
  37. */
  38. public PropertyFileInputHandler() {
  39. }
  40. /**
  41. * Picks up the input from a property, using the prompt as the
  42. * name of the property.
  43. *
  44. * @exception BuildException if no property of that name can be found.
  45. */
  46. public void handleInput(InputRequest request) throws BuildException {
  47. readProps();
  48. Object o = props.get(request.getPrompt());
  49. if (o == null) {
  50. throw new BuildException("Unable to find input for \'"
  51. + request.getPrompt() + "\'");
  52. }
  53. request.setInput(o.toString());
  54. if (!request.isInputValid()) {
  55. throw new BuildException("Found invalid input " + o
  56. + " for \'" + request.getPrompt() + "\'");
  57. }
  58. }
  59. /**
  60. * Reads the properties file if it hasn't already been read.
  61. */
  62. private synchronized void readProps() throws BuildException {
  63. if (props == null) {
  64. String propsFile = System.getProperty(FILE_NAME_KEY);
  65. if (propsFile == null) {
  66. throw new BuildException("System property "
  67. + FILE_NAME_KEY
  68. + " for PropertyFileInputHandler not"
  69. + " set");
  70. }
  71. props = new Properties();
  72. try {
  73. props.load(new FileInputStream(propsFile));
  74. } catch (IOException e) {
  75. throw new BuildException("Couldn't load " + propsFile, e);
  76. }
  77. }
  78. }
  79. }