Debian Codegraph @

manual/inputhandler.html

raw
1 <!DOCTYPE html>
2 <!--
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
9
10 https://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 -->
18 <html lang="en">
19 <head>
20 <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
21 <title>InputHandler</title>
22 </head>
23
24 <body>
25 <h1>InputHandler</h1>
26
27 <h2>Overview</h2>
28
29 <p>When a task wants to prompt a user for input, it doesn't simply read the input from the console
30 as this would make it impossible to embed Apache Ant in an IDE. Instead it asks an implementation
31 of the <code class="code">org.apache.tools.ant.input.InputHandler</code> interface to prompt the
32 user and hand the user input back to the task.</p>
33
34 <p>To do this, the task creates an <code class="code">InputRequest</code> object and passes it to
35 the <code class="code">InputHandler</code>. Such an <code class="code">InputRequest</code> may know
36 whether a given user input is valid and the <code class="code">InputHandler</code> is supposed to
37 reject all invalid input.</p>
38
39 <p>Exactly one <code class="code">InputHandler</code> instance is associated with every Ant process,
40 users can specify the implementation using the <kbd>-inputhandler</kbd> command line switch.</p>
41
42 <h2>InputHandler</h2>
43
44 <p>The <code class="code">InputHandler</code> interface contains exactly one method</p>
45
46 <pre>
47 void handleInput(InputRequest request)
48 throws org.apache.tools.ant.BuildException;</pre>
49
50 <p>with some pre- and postconditions. The main postcondition is that this method must not return
51 unless the <code>request</code> considers the user input valid; it is allowed to throw an exception
52 in this situation.</p>
53
54 <p>Ant comes with three built-in implementations of this interface:</p>
55
56 <h3 id="defaulthandler">DefaultInputHandler</h3>
57
58 <p>This is the implementation you get, when you don't use the <kbd>-inputhandler</kbd> command line
59 switch at all. This implementation will print the prompt encapsulated in the <code>request</code>
60 object to Ant's logging system and re-prompt for input until the user enters something that is
61 considered valid input by the <code>request</code> object. Input will be read from the console and
62 the user will need to press the Return key.</p>
63
64 <h3>PropertyFileInputHandler</h3>
65
66 <p>This implementation is useful if you want to run unattended build processes. It reads all input
67 from a properties file and makes the build fail if it cannot find valid input in this file. The
68 name of the properties file must be specified in the Java system
69 property <code>ant.input.properties</code>.</p>
70
71 <p>The prompt encapsulated in a <code>request</code> will be used as the key when looking up the
72 input inside the properties file. If no input can be found, the input is considered invalid and an
73 exception will be thrown.</p>
74
75 <p><strong>Note</strong> that <code>ant.input.properties</code> must be a Java system property, not
76 an Ant property. I.e. you cannot define it as a simple parameter to <kbd>ant</kbd>, but you can
77 define it inside the <code>ANT_OPTS</code> environment variable.</p>
78
79 <h3>GreedyInputHandler</h3>
80 <p><em>Since Ant 1.7</em></p>
81 <p>Like the default implementation, this InputHandler reads from standard input. However, it
82 consumes <em>all</em> available input. This behavior is useful for sending Ant input via an OS
83 pipe.</p>
84
85 <h3>SecureInputHandler</h3>
86 <p><em>Since Ant 1.7.1</em></p>
87 <p>This InputHandler calls <code class="code">System.console().readPassword()</code>, available
88 since Java 6. On earlier platforms it falls back to the behavior
89 of <code class="code">DefaultInputHandler</code>.</p>
90
91 <h2>InputRequest</h2>
92
93 <p>Instances of <code class="code">org.apache.tools.ant.input.InputRequest</code> encapsulate the
94 information necessary to ask a user for input and validate this input.</p>
95
96 <p>The instances of <code class="code">InputRequest</code> itself will accept any input, but
97 subclasses may use stricter
98 validations. <code class="code">org.apache.tools.ant.input.MultipleChoiceInputRequest</code> should
99 be used if the user input must be part of a predefined set of choices.</p>
100
101 </body>
102 </html>