From e63b88bfd5789a4d2ac07e3f9b3b15308b2416e8 Mon Sep 17 00:00:00 2001 From: brett <brett@30dfa635-7bf8-0310-9608-e41f2d776689> Date: Mon, 10 Oct 2005 17:01:27 +0000 Subject: [PATCH] renamed library --- plexus-interactivity-api/pom.xml | 11 + .../interactivity/AbstractInputHandler.java | 55 +++++ .../interactivity/DefaultInputHandler.java | 77 ++++++ .../interactivity/DefaultOutputHandler.java | 68 +++++ .../interactivity/DefaultPrompter.java | 232 ++++++++++++++++++ .../interactivity/InputHandler.java | 66 +++++ .../interactivity/OutputHandler.java | 52 ++++ .../components/interactivity/Prompter.java | 56 +++++ .../interactivity/PrompterException.java | 45 ++++ .../resources/META-INF/plexus/components.xml | 30 +++ plexus-interactivity-jline/pom.xml | 23 ++ .../jline/JLineInputHandler.java | 70 ++++++ .../resources/META-INF/plexus/components.xml | 10 + pom.xml | 16 ++ 14 files changed, 811 insertions(+) create mode 100644 plexus-interactivity-api/pom.xml create mode 100755 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java create mode 100755 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java create mode 100644 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java create mode 100644 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java create mode 100755 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java create mode 100644 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java create mode 100644 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java create mode 100644 plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java create mode 100644 plexus-interactivity-api/src/main/resources/META-INF/plexus/components.xml create mode 100644 plexus-interactivity-jline/pom.xml create mode 100755 plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java create mode 100644 plexus-interactivity-jline/src/main/resources/META-INF/plexus/components.xml create mode 100644 pom.xml diff --git a/plexus-interactivity-api/pom.xml b/plexus-interactivity-api/pom.xml new file mode 100644 index 0000000..5e3db7a --- /dev/null +++ b/plexus-interactivity-api/pom.xml @@ -0,0 +1,11 @@ +<project> + <parent> + <artifactId>plexus-interactivity</artifactId> + <groupId>org.codehaus.plexus</groupId> + <version>1.0-alpha-4-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>plexus-interactivity-api</artifactId> + <name>Plexus Default Interactivity Handler</name> + <version>1.0-alpha-4-SNAPSHOT</version> +</project> \ No newline at end of file diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java new file mode 100755 index 0000000..35b642a --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java @@ -0,0 +1,55 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.codehaus.plexus.logging.AbstractLogEnabled; + +import java.util.List; +import java.util.ArrayList; +import java.io.IOException; + +/** + * Base input handler, implements a default <code>readMultipleLines</code>. + * + * @author Brett Porter + * @version $Id$ + */ +public abstract class AbstractInputHandler + extends AbstractLogEnabled + implements InputHandler +{ + public List readMultipleLines() + throws IOException + { + List lines = new ArrayList(); + String line = readLine(); + while ( line != null && line.length() > 0 ) + { + lines.add( line ); + line = readLine(); + } + return lines; + } +} diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java new file mode 100755 index 0000000..fb67b36 --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java @@ -0,0 +1,77 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable; +import org.codehaus.plexus.components.interactivity.AbstractInputHandler; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * Default input handler, that uses the console. + * + * @author Brett Porter + * @version $Id$ + */ +public class DefaultInputHandler + extends AbstractInputHandler + implements Initializable, Disposable +{ + private BufferedReader consoleReader; + + public String readLine() + throws IOException + { + return consoleReader.readLine(); + } + + public String readPassword() + throws IOException + { + return consoleReader.readLine(); + } + + public void initialize() + throws InitializationException + { + consoleReader = new BufferedReader( new InputStreamReader( System.in ) ); + } + + public void dispose() + { + try + { + consoleReader.close(); + } + catch ( IOException e ) + { + getLogger().error( "Error closing input stream must be ignored", e ); + } + } +} diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java new file mode 100644 index 0000000..b5f60bc --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java @@ -0,0 +1,68 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; + +import java.io.IOException; +import java.io.PrintWriter; + +/** + * Default output handler, that uses the console. + * + * @author Brett Porter + * @version $Id$ + */ +public class DefaultOutputHandler + implements Initializable, Disposable, OutputHandler +{ + private PrintWriter consoleWriter; + + public void initialize() + throws InitializationException + { + consoleWriter = new PrintWriter( System.out ); + } + + public void dispose() + { + consoleWriter.close(); + } + + public void write( String line ) + throws IOException + { + consoleWriter.print( line ); + consoleWriter.flush(); + } + + public void writeLine( String line ) + throws IOException + { + consoleWriter.println(); + } +} diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java new file mode 100644 index 0000000..6dfe11e --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java @@ -0,0 +1,232 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import org.codehaus.plexus.util.StringUtils; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * Default prompter. + * + * @author Brett Porter + * @version $Id$ + */ +public class DefaultPrompter + implements Prompter +{ + /** + * @requirement + */ + private OutputHandler outputHandler; + + /** + * @requirement + */ + private InputHandler inputHandler; + + public String prompt( String message ) + throws PrompterException + { + try + { + writePrompt( message ); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + return inputHandler.readLine(); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + } + + public String prompt( String message, String defaultReply ) + throws PrompterException + { + try + { + writePrompt( formatMessage( message, null, defaultReply ) ); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + String line = inputHandler.readLine(); + + if ( StringUtils.isEmpty( line ) ) + { + line = defaultReply; + } + + return line; + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + } + + public String prompt( String message, List possibleValues, String defaultReply ) + throws PrompterException + { + String formattedMessage = formatMessage( message, possibleValues, defaultReply ); + + String line; + + do + { + try + { + writePrompt( formattedMessage ); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + line = inputHandler.readLine(); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + + if ( StringUtils.isEmpty( line ) ) + { + line = defaultReply; + } + + if ( line != null && !possibleValues.contains( line ) ) + { + try + { + outputHandler.writeLine( "Invalid selection." ); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to present feedback", e ); + } + } + } + while ( line == null || !possibleValues.contains( line ) ); + + return line; + } + + public String prompt( String message, List possibleValues ) + throws PrompterException + { + return prompt( message, possibleValues, null ); + } + + public String promptForPassword( String message ) + throws PrompterException + { + try + { + writePrompt( message ); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + return inputHandler.readPassword(); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + } + + private String formatMessage( String message, List possibleValues, String defaultReply ) + { + StringBuffer formatted = new StringBuffer( message.length() * 2 ); + + formatted.append( message ); + + if ( possibleValues != null && !possibleValues.isEmpty() ) + { + formatted.append( " (" ); + + for ( Iterator it = possibleValues.iterator(); it.hasNext(); ) + { + String possibleValue = (String) it.next(); + + formatted.append( possibleValue ); + + if ( it.hasNext() ) + { + formatted.append( '/' ); + } + } + + formatted.append( ')' ); + } + + if ( defaultReply != null ) + { + formatted.append( ' ' ).append( defaultReply ).append( ": " ); + } + + return formatted.toString(); + } + + private void writePrompt( String message ) + throws IOException + { + outputHandler.write( message + ": " ); + } + + public void showMessage( String message ) + throws PrompterException + { + try + { + writePrompt( message ); + } + catch ( IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + } +} diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java new file mode 100755 index 0000000..ca0ea24 --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java @@ -0,0 +1,66 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import java.util.List; +import java.io.IOException; + +/** + * Manage user input from different sources. + * + * @todo should this also echo any prompts before the input? + * @todo should this validate the input, reprompt if required? + * @todo readBoolean, readInt, readSingleChar - readLine's that parse the input + * @author <a href="mailto:brett@apache.org">Brett Porter</a> + * @version $Id$ + */ +public interface InputHandler +{ + String ROLE = InputHandler.class.getName(); + + /** + * Read a single line of input, swalling the newline at the end. + * If the input can be echoed, it will be. + * @return the line read + */ + String readLine() + throws IOException; + + /** + * Read a single line of input, swalling the newline at the end. + * This method guarantees input is not echoed. + * @return the line read + */ + String readPassword() + throws IOException; + + /** + * Read a set of lines. Equivalent to multiple calls to {@link #readLine()}. + * Ends when an empty line is encountered. + * @return a list of lines read + */ + List readMultipleLines() + throws IOException; +} diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java new file mode 100644 index 0000000..240a984 --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java @@ -0,0 +1,52 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import java.io.IOException; + +/** + * Manage user output to different sources. + * + * @author <a href="mailto:brett@apache.org">Brett Porter</a> + * @version $Id$ + */ +public interface OutputHandler +{ + String ROLE = OutputHandler.class.getName(); + + /** + * Write a single line of input, excluding the newline at the end. + * @param line the line + */ + void write( String line ) + throws IOException; + + /** + * Write a single line of input, including the newline at the end. + * @param line the line + */ + void writeLine( String line ) + throws IOException; +} diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java new file mode 100644 index 0000000..016597c --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java @@ -0,0 +1,56 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import java.util.List; + +/** + * Prompt the user for input. + * + * @author <a href="mailto:brett@apache.org">Brett Porter</a> + * @version $Id$ + */ +public interface Prompter +{ + String ROLE = Prompter.class.getName(); + + String prompt( String message ) + throws PrompterException; + + String prompt( String message, String defaultReply ) + throws PrompterException; + + String prompt( String message, List possibleValues ) + throws PrompterException; + + String prompt( String message, List possibleValues, String defaultReply ) + throws PrompterException; + + String promptForPassword( String message ) + throws PrompterException; + + void showMessage( String message ) + throws PrompterException; +} diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java new file mode 100644 index 0000000..d6a0b00 --- /dev/null +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java @@ -0,0 +1,45 @@ +package org.codehaus.plexus.components.interactivity; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Error while prompting. + * + * @author <a href="mailto:brett@apache.org">Brett Porter</a> + * @version $Id$ + */ +public class PrompterException + extends Exception +{ + public PrompterException( String message ) + { + super( message ); + } + + public PrompterException( String message, Throwable cause ) + { + super( message, cause ); + } +} diff --git a/plexus-interactivity-api/src/main/resources/META-INF/plexus/components.xml b/plexus-interactivity-api/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000..10ca545 --- /dev/null +++ b/plexus-interactivity-api/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,30 @@ +<component-set> + <components> + <component> + <role>org.codehaus.plexus.components.interactivity.InputHandler</role> + <role-hint>default</role-hint> + <implementation>org.codehaus.plexus.components.interactivity.DefaultInputHandler</implementation> + <instantiation-strategy>per-lookup</instantiation-strategy> + </component> + <component> + <role>org.codehaus.plexus.components.interactivity.OutputHandler</role> + <role-hint>default</role-hint> + <implementation>org.codehaus.plexus.components.interactivity.DefaultOutputHandler</implementation> + <instantiation-strategy>per-lookup</instantiation-strategy> + </component> + <component> + <role>org.codehaus.plexus.components.interactivity.Prompter</role> + <role-hint>default</role-hint> + <implementation>org.codehaus.plexus.components.interactivity.DefaultPrompter</implementation> + <instantiation-strategy>per-lookup</instantiation-strategy> + <requirements> + <requirement> + <role>org.codehaus.plexus.components.interactivity.InputHandler</role> + </requirement> + <requirement> + <role>org.codehaus.plexus.components.interactivity.OutputHandler</role> + </requirement> + </requirements> + </component> + </components> +</component-set> diff --git a/plexus-interactivity-jline/pom.xml b/plexus-interactivity-jline/pom.xml new file mode 100644 index 0000000..04b2983 --- /dev/null +++ b/plexus-interactivity-jline/pom.xml @@ -0,0 +1,23 @@ +<project> + <parent> + <artifactId>plexus-interactivity</artifactId> + <groupId>org.codehaus.plexus</groupId> + <version>1.0-alpha-4-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>plexus-interactivity-jline</artifactId> + <name>Plexus JLine Interactivity Handler</name> + <version>1.0-alpha-4-SNAPSHOT</version> + <dependencies> + <dependency> + <groupId>jline</groupId> + <artifactId>jline</artifactId> + <version>0.9.1</version> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-interactivity-api</artifactId> + <version>1.0-alpha-4-SNAPSHOT</version> + </dependency> + </dependencies> +</project> \ No newline at end of file diff --git a/plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java b/plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java new file mode 100755 index 0000000..fd624d9 --- /dev/null +++ b/plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java @@ -0,0 +1,70 @@ +package org.codehaus.plexus.components.interactivity.jline; + +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import jline.ConsoleReader; +import org.codehaus.plexus.components.interactivity.AbstractInputHandler; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; + +import java.io.IOException; + +/** + * Default input handler, that uses the console. + * + * @author Brett Porter + * @version $Id$ + */ +public class JLineInputHandler + extends AbstractInputHandler + implements Initializable +{ + private ConsoleReader consoleReader; + + public String readLine() + throws IOException + { + return consoleReader.readLine(); + } + + public String readPassword() + throws IOException + { + return consoleReader.readLine( new Character( '*' ) ); + } + + public void initialize() + throws InitializationException + { + try + { + consoleReader = new ConsoleReader(); + } + catch ( IOException e ) + { + throw new InitializationException( "Cannot create console reader: ", e ); + } + } +} diff --git a/plexus-interactivity-jline/src/main/resources/META-INF/plexus/components.xml b/plexus-interactivity-jline/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000..001418c --- /dev/null +++ b/plexus-interactivity-jline/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,10 @@ +<component-set> + <components> + <component> + <role>org.codehaus.plexus.components.inputhandler.InputHandler</role> + <role-hint>jline</role-hint> + <implementation>org.codehaus.plexus.components.interactivity.jline.JLineInputHandler</implementation> + <instantiation-strategy>per-lookup</instantiation-strategy> + </component> + </components> +</component-set> diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4693326 --- /dev/null +++ b/pom.xml @@ -0,0 +1,16 @@ +<project> + <parent> + <artifactId>plexus-components</artifactId> + <groupId>org.codehaus.plexus</groupId> + <version>1.1.3</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>plexus-interactivity</artifactId> + <packaging>pom</packaging> + <name>Plexus Interactivity Handler Component</name> + <version>1.0-alpha-4-SNAPSHOT</version> + <modules> + <module>plexus-interactivity-api</module> + <module>plexus-interactivity-jline</module> + </modules> +</project>