Author: jdcasey Date: Fri May 27 15:34:21 2011 New Revision: 1128363 URL: http://svn.apache.org/viewvc?rev=1128363&view=rev Log: move MAEPrompter out to a separate module, to allow the use of mae-api without the need to depend on jline unless you have a use case that requires prompting from the CLI.
Added: maven/sandbox/trunk/mae/mae-prompter-cli/ maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml (with props) maven/sandbox/trunk/mae/mae-prompter-cli/src/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java (with props) Removed: maven/sandbox/trunk/mae/mae-api/src/main/java/org/apache/maven/mae/io/ Modified: maven/sandbox/trunk/mae/mae-api/pom.xml maven/sandbox/trunk/mae/pom.xml Modified: maven/sandbox/trunk/mae/mae-api/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-api/pom.xml?rev=1128363&r1=1128362&r2=1128363&view=diff ============================================================================== --- maven/sandbox/trunk/mae/mae-api/pom.xml (original) +++ maven/sandbox/trunk/mae/mae-api/pom.xml Fri May 27 15:34:21 2011 @@ -80,11 +80,6 @@ </exclusions> </dependency> <dependency> - <groupId>jline</groupId> - <artifactId>jline</artifactId> - <version>0.9.94</version> - </dependency> - <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-component-annotations</artifactId> </dependency> Added: maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml?rev=1128363&view=auto ============================================================================== --- maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml (added) +++ maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml Fri May 27 15:34:21 2011 @@ -0,0 +1,26 @@ +<?xml version="1.0"?> +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + <parent> + <artifactId>mae</artifactId> + <groupId>org.apache.maven.mae</groupId> + <version>1.0-alpha-1-SNAPSHOT</version> + </parent> + + <artifactId>mae-prompter-cli</artifactId> + <name>Maven App Engine: CLI Prompter</name> + + <dependencies> + <dependency> + <groupId>jline</groupId> + <artifactId>jline</artifactId> + <version>0.9.94</version> + </dependency> + <dependency> + <groupId>org.apache.maven.mae</groupId> + <artifactId>mae-api</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> +</project> Propchange: maven/sandbox/trunk/mae/mae-prompter-cli/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java?rev=1128363&view=auto ============================================================================== --- maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java (added) +++ maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java Fri May 27 15:34:21 2011 @@ -0,0 +1,245 @@ +/* + * 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.maven.mae.prompt; + +import org.apache.maven.mae.conf.MAEConfiguration; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.components.interactivity.Prompter; +import org.codehaus.plexus.components.interactivity.PrompterException; +import org.codehaus.plexus.util.StringUtils; + +import javax.inject.Inject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.Iterator; +import java.util.List; + +import jline.ConsoleReader; + +@Component( role = Prompter.class, hint = MAEPrompter.NAME ) +public class MAEPrompter + implements Prompter +{ + + public static final String NAME = "mae"; + + @Requirement + private final MAEConfiguration config; + + @Inject + public MAEPrompter( final MAEConfiguration config ) + { + this.config = config; + } + + public String prompt( final String message ) + throws PrompterException + { + try + { + writePrompt( message ); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + return readLine(); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + } + + private String readLine() + throws IOException + { + return new BufferedReader( new InputStreamReader( config.getStandardIn() ) ).readLine(); + } + + public String prompt( final String message, final String defaultReply ) + throws PrompterException + { + try + { + writePrompt( formatMessage( message, null, defaultReply ) ); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + String line = readLine(); + + if ( StringUtils.isEmpty( line ) ) + { + line = defaultReply; + } + + return line; + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + } + + @SuppressWarnings( "rawtypes" ) + public String prompt( final String message, final List possibleValues, final String defaultReply ) + throws PrompterException + { + final String formattedMessage = formatMessage( message, possibleValues, defaultReply ); + + String line; + + do + { + try + { + writePrompt( formattedMessage ); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + line = readLine(); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + + if ( StringUtils.isEmpty( line ) ) + { + line = defaultReply; + } + + if ( line != null && !possibleValues.contains( line ) ) + { + writeLine( "Invalid selection." ); + } + } + while ( line == null || !possibleValues.contains( line ) ); + + return line; + } + + private void writeLine( final String message ) + { + config.getStandardOut().println( message ); + } + + @SuppressWarnings( "rawtypes" ) + public String prompt( final String message, final List possibleValues ) + throws PrompterException + { + return prompt( message, possibleValues, null ); + } + + public String promptForPassword( final String message ) + throws PrompterException + { + try + { + writePrompt( message ); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + try + { + return new ConsoleReader( config.getStandardIn(), new OutputStreamWriter( config.getStandardOut() ) ).readLine( new Character( + '*' ) ); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to read user response", e ); + } + } + + @SuppressWarnings( "rawtypes" ) + private String formatMessage( final String message, final List possibleValues, final String defaultReply ) + { + final StringBuffer formatted = new StringBuffer( message.length() * 2 ); + + formatted.append( message ); + + if ( possibleValues != null && !possibleValues.isEmpty() ) + { + formatted.append( " (" ); + + for ( final Iterator it = possibleValues.iterator(); it.hasNext(); ) + { + final 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( final String message ) + throws IOException + { + config.getStandardOut().print( message + ": " ); + } + + public void showMessage( final String message ) + throws PrompterException + { + try + { + writePrompt( message ); + } + catch ( final IOException e ) + { + throw new PrompterException( "Failed to present prompt", e ); + } + + } + +} Propchange: maven/sandbox/trunk/mae/mae-prompter-cli/src/main/java/org/apache/maven/mae/prompt/MAEPrompter.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: maven/sandbox/trunk/mae/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/mae/pom.xml?rev=1128363&r1=1128362&r2=1128363&view=diff ============================================================================== --- maven/sandbox/trunk/mae/pom.xml (original) +++ maven/sandbox/trunk/mae/pom.xml Fri May 27 15:34:21 2011 @@ -16,8 +16,7 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -57,6 +56,7 @@ <module>mae-container</module> <module>mae-booter</module> <module>mae-app</module> + <module>mae-prompter-cli</module> </modules> <scm> @@ -147,4 +147,4 @@ </plugins> </pluginManagement> </build> -</project> +</project> \ No newline at end of file