diff options
Diffstat (limited to 'framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input')
7 files changed, 543 insertions, 0 deletions
diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/DefaultInputHandler.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/DefaultInputHandler.java new file mode 100644 index 00000000..8268d5e7 --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/DefaultInputHandler.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.input; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.KeepAliveInputStream; + +/** + * Prompts on System.err, reads input from System.in + * + * @since Ant 1.5 + */ +public class DefaultInputHandler implements InputHandler { + + /** + * Empty no-arg constructor + */ + public DefaultInputHandler() { + } + + /** + * Prompts and requests input. May loop until a valid input has + * been entered. + * @param request the request to handle + * @throws BuildException if not possible to read from console + */ + public void handleInput(InputRequest request) throws BuildException { + String prompt = getPrompt(request); + BufferedReader r = null; + try { + r = new BufferedReader(new InputStreamReader(getInputStream())); + do { + System.err.println(prompt); + System.err.flush(); + try { + String input = r.readLine(); + request.setInput(input); + } catch (IOException e) { + throw new BuildException("Failed to read input from" + + " Console.", e); + } + } while (!request.isInputValid()); + } finally { + if (r != null) { + try { + r.close(); + } catch (IOException e) { + throw new BuildException("Failed to close input.", e); + } + } + } + } + + /** + * Constructs user prompt from a request. + * + * <p>This implementation adds (choice1,choice2,choice3,...) to the + * prompt for <code>MultipleChoiceInputRequest</code>s.</p> + * + * @param request the request to construct the prompt for. + * Must not be <code>null</code>. + * @return the prompt to ask the user + */ + protected String getPrompt(InputRequest request) { + String prompt = request.getPrompt(); + String def = request.getDefaultValue(); + if (request instanceof MultipleChoiceInputRequest) { + StringBuilder sb = new StringBuilder(prompt).append(" ("); + boolean first = true; + for (String next : ((MultipleChoiceInputRequest) request).getChoices()) { + if (!first) { + sb.append(", "); + } + if (next.equals(def)) { + sb.append('['); + } + sb.append(next); + if (next.equals(def)) { + sb.append(']'); + } + first = false; + } + sb.append(")"); + return sb.toString(); + } else if (def != null) { + return prompt + " [" + def + "]"; + } else { + return prompt; + } + } + + /** + * Returns the input stream from which the user input should be read. + * @return the input stream from which the user input should be read. + */ + protected InputStream getInputStream() { + return KeepAliveInputStream.wrapSystemIn(); + } +} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/GreedyInputHandler.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/GreedyInputHandler.java new file mode 100644 index 00000000..cb52f4f0 --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/GreedyInputHandler.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.input; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.StreamPumper; +import org.apache.tools.ant.util.FileUtils; + +/** + * Prompts on System.err, reads input from System.in until EOF + * + * @since Ant 1.7 + */ +public class GreedyInputHandler extends DefaultInputHandler { + + /** + * Empty no-arg constructor + */ + public GreedyInputHandler() { + } + + /** + * Prompts and requests input. + * @param request the request to handle + * @throws BuildException if not possible to read from console, + * or if input is invalid. + */ + public void handleInput(InputRequest request) throws BuildException { + String prompt = getPrompt(request); + InputStream in = null; + try { + in = getInputStream(); + System.err.println(prompt); + System.err.flush(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + StreamPumper p = new StreamPumper(in, baos); + Thread t = new Thread(p); + t.start(); + try { + t.join(); + } catch (InterruptedException e) { + try { + t.join(); + } catch (InterruptedException e2) { + // Ignore + } + } + request.setInput(new String(baos.toByteArray())); + if (!(request.isInputValid())) { + throw new BuildException( + "Received invalid console input"); + } + if (p.getException() != null) { + throw new BuildException( + "Failed to read input from console", p.getException()); + } + } finally { + FileUtils.close(in); + } + } +} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/InputHandler.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/InputHandler.java new file mode 100644 index 00000000..aea60fcc --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/InputHandler.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.input; + +/** + * Plugin to Ant to handle requests for user input. + * + * @since Ant 1.5 + */ +public interface InputHandler { + + /** + * Handle the request encapsulated in the argument. + * + * <p>Precondition: the request.getPrompt will return a non-null + * value.</p> + * + * <p>Postcondition: request.getInput will return a non-null + * value, request.isInputValid will return true.</p> + * @param request the request to be processed + * @throws org.apache.tools.ant.BuildException if the input cannot be read from the console + */ + void handleInput(InputRequest request) + throws org.apache.tools.ant.BuildException; +} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/InputRequest.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/InputRequest.java new file mode 100644 index 00000000..57a77da2 --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/InputRequest.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.input; + +/** + * Encapsulates an input request. + * + * @since Ant 1.5 + */ +public class InputRequest { + private final String prompt; + private String input; + private String defaultValue; + + /** + * Construct an InputRequest. + * @param prompt The prompt to show to the user. Must not be null. + */ + public InputRequest(String prompt) { + if (prompt == null) { + throw new IllegalArgumentException("prompt must not be null"); + } + + this.prompt = prompt; + } + + /** + * Retrieves the prompt text. + * @return the prompt. + */ + public String getPrompt() { + return prompt; + } + + /** + * Sets the user provided input. + * @param input the string to be used for input. + */ + public void setInput(String input) { + this.input = input; + } + + /** + * Is the user input valid? + * @return true if it is. + */ + public boolean isInputValid() { + return true; + } + + /** + * Retrieves the user input. + * @return the user input. + */ + public String getInput() { + return input; + } + + /** + * Gets a configured default value. + * @return the default value. + * @since Ant 1.7.0 + */ + public String getDefaultValue() { + return defaultValue; + } + + /** + * Configures a default value. + * @param d the value to set. + * @since Ant 1.7.0 + */ + public void setDefaultValue(String d) { + defaultValue = d; + } + +} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java new file mode 100644 index 00000000..4baab8f5 --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.input; + +import java.util.LinkedHashSet; +import java.util.Vector; + +/** + * Encapsulates an input request. + * + * @since Ant 1.5 + */ +public class MultipleChoiceInputRequest extends InputRequest { + private final LinkedHashSet<String> choices; + + /** + * @param prompt The prompt to show to the user. Must not be null. + * @param choices holds all input values that are allowed. + * Must not be null. + */ + public MultipleChoiceInputRequest(String prompt, Vector<String> choices) { + super(prompt); + if (choices == null) { + throw new IllegalArgumentException("choices must not be null"); + } + this.choices = new LinkedHashSet<String>(choices); + } + + /** + * @return The possible values. + */ + public Vector<String> getChoices() { + return new Vector<String>(choices); + } + + /** + * @return true if the input is one of the allowed values. + */ + public boolean isInputValid() { + return choices.contains(getInput()) || ("".equals(getInput()) && getDefaultValue() != null); + } +} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java new file mode 100644 index 00000000..e1e3cf11 --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.input; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +import org.apache.tools.ant.BuildException; + +/** + * Reads input from a property file, the file name is read from the + * system property ant.input.properties, the prompt is the key for input. + * + * @since Ant 1.5 + */ +public class PropertyFileInputHandler implements InputHandler { + private Properties props = null; + + /** + * Name of the system property we expect to hold the file name. + */ + public static final String FILE_NAME_KEY = "ant.input.properties"; + + /** + * Empty no-arg constructor. + */ + public PropertyFileInputHandler() { + } + + /** + * Picks up the input from a property, using the prompt as the + * name of the property. + * @param request an input request. + * + * @exception BuildException if no property of that name can be found. + */ + public void handleInput(InputRequest request) throws BuildException { + readProps(); + + Object o = props.get(request.getPrompt()); + if (o == null) { + throw new BuildException("Unable to find input for \'" + + request.getPrompt() + "\'"); + } + request.setInput(o.toString()); + if (!request.isInputValid()) { + throw new BuildException("Found invalid input " + o + + " for \'" + request.getPrompt() + "\'"); + } + } + + /** + * Reads the properties file if it hasn't already been read. + */ + private synchronized void readProps() throws BuildException { + if (props == null) { + String propsFile = System.getProperty(FILE_NAME_KEY); + if (propsFile == null) { + throw new BuildException("System property " + + FILE_NAME_KEY + + " for PropertyFileInputHandler not" + + " set"); + } + + props = new Properties(); + + try { + props.load(new FileInputStream(propsFile)); + } catch (IOException e) { + throw new BuildException("Couldn't load " + propsFile, e); + } + } + } + +} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/SecureInputHandler.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/SecureInputHandler.java new file mode 100644 index 00000000..d5aecff7 --- /dev/null +++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/input/SecureInputHandler.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.input; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.ReflectUtil; + +/** + * Prompts and requests input. May loop until a valid input has + * been entered. Doesn't echo input (requires Java6). If Java6 is not + * available, falls back to the DefaultHandler (insecure). + * @since Ant 1.7.1 + */ +public class SecureInputHandler extends DefaultInputHandler { + + /** + * Default no-args constructor + */ + public SecureInputHandler() { + } + + /** + * Handle the input + * @param request the request to handle + * @throws BuildException if not possible to read from console + */ + public void handleInput(InputRequest request) throws BuildException { + String prompt = getPrompt(request); + try { + Object console = ReflectUtil.invokeStatic(System.class, "console"); + do { + char[] input = (char[]) ReflectUtil.invoke( + console, "readPassword", String.class, prompt, + Object[].class, (Object[]) null); + request.setInput(new String(input)); + /* for security zero char array after retrieving value */ + java.util.Arrays.fill(input, ' '); + } while (!request.isInputValid()); + } catch (Exception e) { + /* Java6 not present use default handler */ + super.handleInput(request); + } + } +} |