git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270696 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -0,0 +1,63 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import com.oroinc.net.telnet.TelnetClient; | |||
| import java.io.InputStream; | |||
| import java.io.OutputStream; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class handles the abstraction of the telnet protocol. Currently it | |||
| * is a wrapper around <a href="www.oroinc.com">ORO</a> 's NetComponents | |||
| */ | |||
| public class AntTelnetClient | |||
| extends TelnetClient | |||
| { | |||
| private TelnetTask m_task; | |||
| public AntTelnetClient( final TelnetTask task ) | |||
| { | |||
| m_task = task; | |||
| } | |||
| /** | |||
| * Write this string to the telnet session. | |||
| */ | |||
| public void sendString( final String string, final boolean echoString ) | |||
| throws TaskException | |||
| { | |||
| final OutputStream output = this.getOutputStream(); | |||
| m_task.doSendString( output, string, echoString ); | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found | |||
| */ | |||
| public void waitForString( final String string ) | |||
| throws TaskException | |||
| { | |||
| waitForString( string, null ); | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found or the timeout has been reached | |||
| * | |||
| * @parm s The string to wait on | |||
| * @parm timeout The maximum number of seconds to wait | |||
| */ | |||
| public void waitForString( final String string, | |||
| final Integer timeout ) | |||
| throws TaskException | |||
| { | |||
| final InputStream input = this.getInputStream(); | |||
| m_task.doWaitForString( input, string, timeout ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,45 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class reads the output from the connected server until the required | |||
| * string is found. | |||
| */ | |||
| public class TelnetRead | |||
| extends TelnetSubTask | |||
| { | |||
| private Integer m_timeout; | |||
| /** | |||
| * Sets the default timeout if none has been set already | |||
| */ | |||
| public void setDefaultTimeout( final Integer defaultTimeout ) | |||
| { | |||
| if( m_timeout == null ) | |||
| { | |||
| m_timeout = defaultTimeout; | |||
| } | |||
| } | |||
| /** | |||
| * Override any default timeouts | |||
| */ | |||
| public void setTimeout( final Integer timeout ) | |||
| { | |||
| m_timeout = timeout; | |||
| } | |||
| public void execute( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| telnet.waitForString( getTaskString(), m_timeout ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,40 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class is the parent of the Read and Write tasks. It handles the | |||
| * common attributes for both. | |||
| */ | |||
| public abstract class TelnetSubTask | |||
| { | |||
| private String m_taskString = ""; | |||
| public void setString( final String string ) | |||
| { | |||
| m_taskString += string; | |||
| } | |||
| public void addContent( final String string ) | |||
| { | |||
| setString( string ); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| throw new TaskException( "Shouldn't be able instantiate a SubTask directly" ); | |||
| } | |||
| protected final String getTaskString() | |||
| { | |||
| return m_taskString; | |||
| } | |||
| } | |||
| @@ -7,398 +7,277 @@ | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import com.oroinc.net.telnet.TelnetClient; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.OutputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.Calendar; | |||
| import java.util.Iterator; | |||
| import org.apache.myrmidon.api.AbstractTask; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.tools.ant.Task; | |||
| /** | |||
| * Class to provide automated telnet protocol support for the Ant build tool | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @author <a href="mailto:ScottCarlson@email.com">ScottCarlson@email.com</a> | |||
| * @version $Revision$ | |||
| */ | |||
| public class TelnetTask extends Task | |||
| public class TelnetTask | |||
| extends AbstractTask | |||
| { | |||
| /** | |||
| * The userid to login with, if automated login is used | |||
| */ | |||
| private String userid = null; | |||
| private String m_userid; | |||
| /** | |||
| * The password to login with, if automated login is used | |||
| */ | |||
| private String password = null; | |||
| private String m_password; | |||
| /** | |||
| * The server to connect to. | |||
| */ | |||
| private String server = null; | |||
| private String m_server; | |||
| /** | |||
| * The tcp port to connect to. | |||
| */ | |||
| private int port = 23; | |||
| /** | |||
| * The Object which handles the telnet session. | |||
| */ | |||
| private AntTelnetClient telnet = null; | |||
| private int m_port = 23; | |||
| /** | |||
| * The list of read/write commands for this session | |||
| */ | |||
| private ArrayList telnetTasks = new ArrayList(); | |||
| private ArrayList m_telnetTasks = new ArrayList(); | |||
| /** | |||
| * If true, adds a CR to beginning of login script | |||
| */ | |||
| private boolean addCarriageReturn = false; | |||
| private boolean m_addCarriageReturn; | |||
| /** | |||
| * Default time allowed for waiting for a valid response for all child | |||
| * reads. A value of 0 means no limit. | |||
| */ | |||
| private Integer defaultTimeout = null; | |||
| private Integer m_defaultTimeout; | |||
| /** | |||
| * Set the tcp port to connect to attribute | |||
| * | |||
| * @param b The new InitialCR value | |||
| */ | |||
| public void setInitialCR( boolean b ) | |||
| public void setInitialCR( final boolean addCarriageReturn ) | |||
| { | |||
| this.addCarriageReturn = b; | |||
| m_addCarriageReturn = addCarriageReturn; | |||
| } | |||
| /** | |||
| * Set the password attribute | |||
| * | |||
| * @param p The new Password value | |||
| */ | |||
| public void setPassword( String p ) | |||
| public void setPassword( final String password ) | |||
| { | |||
| this.password = p; | |||
| m_password = password; | |||
| } | |||
| /** | |||
| * Set the tcp port to connect to attribute | |||
| * | |||
| * @param p The new Port value | |||
| */ | |||
| public void setPort( int p ) | |||
| public void setPort( final int port ) | |||
| { | |||
| this.port = p; | |||
| m_port = port; | |||
| } | |||
| /** | |||
| * Set the server address attribute | |||
| * | |||
| * @param m The new Server value | |||
| */ | |||
| public void setServer( String m ) | |||
| public void setServer( final String server ) | |||
| { | |||
| this.server = m; | |||
| m_server = server; | |||
| } | |||
| /** | |||
| * Change the default timeout to wait for valid responses | |||
| * | |||
| * @param i The new Timeout value | |||
| */ | |||
| public void setTimeout( Integer i ) | |||
| public void setTimeout( final Integer defaultTimeout ) | |||
| { | |||
| this.defaultTimeout = i; | |||
| m_defaultTimeout = defaultTimeout; | |||
| } | |||
| /** | |||
| * Set the userid attribute | |||
| * | |||
| * @param u The new Userid value | |||
| */ | |||
| public void setUserid( String u ) | |||
| public void setUserid( final String userid ) | |||
| { | |||
| this.userid = u; | |||
| m_userid = userid; | |||
| } | |||
| /** | |||
| * A subTask <read> tag was found. Create the object, Save it in our | |||
| * list, and return it. | |||
| * | |||
| * @return Description of the Returned Value | |||
| */ | |||
| public TelnetSubTask createRead() | |||
| public void addRead( final TelnetRead read ) | |||
| { | |||
| TelnetSubTask task = (TelnetSubTask)new TelnetRead(); | |||
| telnetTasks.add( task ); | |||
| return task; | |||
| m_telnetTasks.add( read ); | |||
| } | |||
| /** | |||
| * A subTask <write> tag was found. Create the object, Save it in our | |||
| * list, and return it. | |||
| * | |||
| * @return Description of the Returned Value | |||
| */ | |||
| public TelnetSubTask createWrite() | |||
| public void addWrite( final TelnetWrite write ) | |||
| { | |||
| TelnetSubTask task = (TelnetSubTask)new TelnetWrite(); | |||
| telnetTasks.add( task ); | |||
| return task; | |||
| m_telnetTasks.add( write ); | |||
| } | |||
| /** | |||
| * Verify that all parameters are included. Connect and possibly login | |||
| * Iterate through the list of Reads and writes | |||
| * | |||
| * @exception TaskException Description of Exception | |||
| */ | |||
| public void execute() | |||
| throws TaskException | |||
| { | |||
| /** | |||
| * A server name is required to continue | |||
| */ | |||
| if( server == null ) | |||
| throw new TaskException( "No Server Specified" ); | |||
| /** | |||
| * A userid and password must appear together if they appear. They are | |||
| * not required. | |||
| */ | |||
| if( userid == null && password != null ) | |||
| throw new TaskException( "No Userid Specified" ); | |||
| if( password == null && userid != null ) | |||
| throw new TaskException( "No Password Specified" ); | |||
| validate(); | |||
| /** | |||
| * Create the telnet client object | |||
| */ | |||
| telnet = new AntTelnetClient(); | |||
| final AntTelnetClient telnet = new AntTelnetClient( this ); | |||
| try | |||
| { | |||
| telnet.connect( server, port ); | |||
| telnet.connect( m_server, m_port ); | |||
| } | |||
| catch( IOException e ) | |||
| catch( final IOException ioe ) | |||
| { | |||
| throw new TaskException( "Can't connect to " + server ); | |||
| throw new TaskException( "Can't connect to " + m_server, ioe ); | |||
| } | |||
| /** | |||
| * Login if userid and password were specified | |||
| */ | |||
| if( userid != null && password != null ) | |||
| login(); | |||
| /** | |||
| * Process each sub command | |||
| */ | |||
| Iterator tasksToRun = telnetTasks.iterator(); | |||
| while( tasksToRun != null && tasksToRun.hasNext() ) | |||
| if( m_userid != null && m_password != null ) | |||
| { | |||
| TelnetSubTask task = (TelnetSubTask)tasksToRun.next(); | |||
| if( task instanceof TelnetRead && defaultTimeout != null ) | |||
| ( (TelnetRead)task ).setDefaultTimeout( defaultTimeout ); | |||
| task.execute( telnet ); | |||
| login( telnet ); | |||
| } | |||
| } | |||
| /** | |||
| * Process a 'typical' login. If it differs, use the read and write tasks | |||
| * explicitely | |||
| */ | |||
| private void login() | |||
| { | |||
| if( addCarriageReturn ) | |||
| telnet.sendString( "\n", true ); | |||
| telnet.waitForString( "ogin:" ); | |||
| telnet.sendString( userid, true ); | |||
| telnet.waitForString( "assword:" ); | |||
| telnet.sendString( password, false ); | |||
| processTasks( telnet ); | |||
| } | |||
| /** | |||
| * This class handles the abstraction of the telnet protocol. Currently it | |||
| * is a wrapper around <a href="www.oroinc.com">ORO</a> 's NetComponents | |||
| * | |||
| * @author RT | |||
| * Process each sub command | |||
| */ | |||
| public class AntTelnetClient extends TelnetClient | |||
| private void processTasks( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| /** | |||
| * Write this string to the telnet session. | |||
| * | |||
| * @param s Description of Parameter | |||
| * @param echoString Description of Parameter | |||
| * @parm echoString Logs string sent | |||
| */ | |||
| public void sendString( String s, boolean echoString ) | |||
| final Iterator tasks = m_telnetTasks.iterator(); | |||
| while( tasks != null && tasks.hasNext() ) | |||
| { | |||
| OutputStream os = this.getOutputStream(); | |||
| try | |||
| { | |||
| os.write( ( s + "\n" ).getBytes() ); | |||
| if( echoString ) | |||
| getLogger().info( s ); | |||
| os.flush(); | |||
| } | |||
| catch( Exception e ) | |||
| final TelnetSubTask task = (TelnetSubTask)tasks.next(); | |||
| if( task instanceof TelnetRead && m_defaultTimeout != null ) | |||
| { | |||
| throw new TaskException( "Error", e ); | |||
| } | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found | |||
| * | |||
| * @param s Description of Parameter | |||
| * @parm s The string to wait on | |||
| */ | |||
| public void waitForString( String s ) | |||
| { | |||
| waitForString( s, null ); | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found or the timeout has been reached | |||
| * | |||
| * @param s Description of Parameter | |||
| * @param timeout Description of Parameter | |||
| * @parm s The string to wait on | |||
| * @parm timeout The maximum number of seconds to wait | |||
| */ | |||
| public void waitForString( String s, Integer timeout ) | |||
| { | |||
| InputStream is = this.getInputStream(); | |||
| try | |||
| { | |||
| StringBuffer sb = new StringBuffer(); | |||
| if( timeout == null || timeout.intValue() == 0 ) | |||
| { | |||
| while( sb.toString().indexOf( s ) == -1 ) | |||
| { | |||
| sb.append( (char)is.read() ); | |||
| } | |||
| } | |||
| else | |||
| { | |||
| Calendar endTime = Calendar.getInstance(); | |||
| endTime.add( Calendar.SECOND, timeout.intValue() ); | |||
| while( sb.toString().indexOf( s ) == -1 ) | |||
| { | |||
| while( Calendar.getInstance().before( endTime ) && | |||
| is.available() == 0 ) | |||
| { | |||
| Thread.sleep( 250 ); | |||
| } | |||
| if( is.available() == 0 ) | |||
| throw new TaskException( "Response Timed-Out" ); | |||
| sb.append( (char)is.read() ); | |||
| } | |||
| } | |||
| getLogger().info( sb.toString() ); | |||
| } | |||
| catch( TaskException be ) | |||
| { | |||
| throw be; | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| throw new TaskException( "Error", e ); | |||
| ( (TelnetRead)task ).setDefaultTimeout( m_defaultTimeout ); | |||
| } | |||
| task.execute( telnet ); | |||
| } | |||
| } | |||
| /** | |||
| * This class reads the output from the connected server until the required | |||
| * string is found. | |||
| * | |||
| * @author RT | |||
| */ | |||
| public class TelnetRead extends TelnetSubTask | |||
| private void validate() | |||
| throws TaskException | |||
| { | |||
| private Integer timeout = null; | |||
| /** | |||
| * Sets the default timeout if none has been set already | |||
| * | |||
| * @param defaultTimeout The new DefaultTimeout value | |||
| */ | |||
| public void setDefaultTimeout( Integer defaultTimeout ) | |||
| //A server name is required to continue | |||
| if( m_server == null ) | |||
| { | |||
| if( timeout == null ) | |||
| timeout = defaultTimeout; | |||
| throw new TaskException( "No Server Specified" ); | |||
| } | |||
| /** | |||
| * Override any default timeouts | |||
| * | |||
| * @param i The new Timeout value | |||
| */ | |||
| public void setTimeout( Integer i ) | |||
| //A userid and password must appear together if they appear. They are | |||
| //not required. | |||
| if( m_userid == null && m_password != null ) | |||
| { | |||
| this.timeout = i; | |||
| throw new TaskException( "No Userid Specified" ); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| if( m_password == null && m_userid != null ) | |||
| { | |||
| telnet.waitForString( taskString, timeout ); | |||
| throw new TaskException( "No Password Specified" ); | |||
| } | |||
| } | |||
| /** | |||
| * This class is the parent of the Read and Write tasks. It handles the | |||
| * common attributes for both. | |||
| * | |||
| * @author RT | |||
| * Process a 'typical' login. If it differs, use the read and write tasks | |||
| * explicitely | |||
| */ | |||
| public class TelnetSubTask | |||
| private void login( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| protected String taskString = ""; | |||
| public void setString( String s ) | |||
| if( m_addCarriageReturn ) | |||
| { | |||
| taskString += s; | |||
| telnet.sendString( "\n", true ); | |||
| } | |||
| telnet.waitForString( "ogin:" ); | |||
| telnet.sendString( m_userid, true ); | |||
| telnet.waitForString( "assword:" ); | |||
| telnet.sendString( m_password, false ); | |||
| } | |||
| public void addContent( String s ) | |||
| protected void doSendString( final OutputStream output, | |||
| final String string, | |||
| final boolean echoString ) | |||
| throws TaskException | |||
| { | |||
| try | |||
| { | |||
| setString( s ); | |||
| output.write( ( string + "\n" ).getBytes() ); | |||
| if( echoString ) | |||
| { | |||
| getLogger().info( string ); | |||
| } | |||
| output.flush(); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| catch( final Exception e ) | |||
| { | |||
| throw new TaskException( "Shouldn't be able instantiate a SubTask directly" ); | |||
| throw new TaskException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| /** | |||
| * This class sends text to the connected server | |||
| * | |||
| * @author RT | |||
| */ | |||
| public class TelnetWrite extends TelnetSubTask | |||
| protected void doWaitForString( final InputStream input, | |||
| final String string, | |||
| final Integer timeout ) | |||
| throws TaskException | |||
| { | |||
| private boolean echoString = true; | |||
| public void setEcho( boolean b ) | |||
| try | |||
| { | |||
| echoString = b; | |||
| final StringBuffer sb = new StringBuffer(); | |||
| if( timeout == null || timeout.intValue() == 0 ) | |||
| { | |||
| while( sb.toString().indexOf( string ) == -1 ) | |||
| { | |||
| sb.append( (char)input.read() ); | |||
| } | |||
| } | |||
| else | |||
| { | |||
| final Calendar endTime = Calendar.getInstance(); | |||
| endTime.add( Calendar.SECOND, timeout.intValue() ); | |||
| while( sb.toString().indexOf( string ) == -1 ) | |||
| { | |||
| while( Calendar.getInstance().before( endTime ) && | |||
| input.available() == 0 ) | |||
| { | |||
| Thread.sleep( 250 ); | |||
| } | |||
| if( input.available() == 0 ) | |||
| { | |||
| throw new TaskException( "Response Timed-Out" ); | |||
| } | |||
| sb.append( (char)input.read() ); | |||
| } | |||
| } | |||
| getLogger().info( sb.toString() ); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| catch( final TaskException te ) | |||
| { | |||
| throw te; | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| telnet.sendString( taskString, echoString ); | |||
| throw new TaskException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,30 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class sends text to the connected server | |||
| */ | |||
| public class TelnetWrite | |||
| extends TelnetSubTask | |||
| { | |||
| private boolean m_echo = true; | |||
| public void setEcho( final boolean echo ) | |||
| { | |||
| m_echo = echo; | |||
| } | |||
| public void execute( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| telnet.sendString( getTaskString(), m_echo ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,63 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import com.oroinc.net.telnet.TelnetClient; | |||
| import java.io.InputStream; | |||
| import java.io.OutputStream; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class handles the abstraction of the telnet protocol. Currently it | |||
| * is a wrapper around <a href="www.oroinc.com">ORO</a> 's NetComponents | |||
| */ | |||
| public class AntTelnetClient | |||
| extends TelnetClient | |||
| { | |||
| private TelnetTask m_task; | |||
| public AntTelnetClient( final TelnetTask task ) | |||
| { | |||
| m_task = task; | |||
| } | |||
| /** | |||
| * Write this string to the telnet session. | |||
| */ | |||
| public void sendString( final String string, final boolean echoString ) | |||
| throws TaskException | |||
| { | |||
| final OutputStream output = this.getOutputStream(); | |||
| m_task.doSendString( output, string, echoString ); | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found | |||
| */ | |||
| public void waitForString( final String string ) | |||
| throws TaskException | |||
| { | |||
| waitForString( string, null ); | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found or the timeout has been reached | |||
| * | |||
| * @parm s The string to wait on | |||
| * @parm timeout The maximum number of seconds to wait | |||
| */ | |||
| public void waitForString( final String string, | |||
| final Integer timeout ) | |||
| throws TaskException | |||
| { | |||
| final InputStream input = this.getInputStream(); | |||
| m_task.doWaitForString( input, string, timeout ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,45 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class reads the output from the connected server until the required | |||
| * string is found. | |||
| */ | |||
| public class TelnetRead | |||
| extends TelnetSubTask | |||
| { | |||
| private Integer m_timeout; | |||
| /** | |||
| * Sets the default timeout if none has been set already | |||
| */ | |||
| public void setDefaultTimeout( final Integer defaultTimeout ) | |||
| { | |||
| if( m_timeout == null ) | |||
| { | |||
| m_timeout = defaultTimeout; | |||
| } | |||
| } | |||
| /** | |||
| * Override any default timeouts | |||
| */ | |||
| public void setTimeout( final Integer timeout ) | |||
| { | |||
| m_timeout = timeout; | |||
| } | |||
| public void execute( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| telnet.waitForString( getTaskString(), m_timeout ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,40 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class is the parent of the Read and Write tasks. It handles the | |||
| * common attributes for both. | |||
| */ | |||
| public abstract class TelnetSubTask | |||
| { | |||
| private String m_taskString = ""; | |||
| public void setString( final String string ) | |||
| { | |||
| m_taskString += string; | |||
| } | |||
| public void addContent( final String string ) | |||
| { | |||
| setString( string ); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| throw new TaskException( "Shouldn't be able instantiate a SubTask directly" ); | |||
| } | |||
| protected final String getTaskString() | |||
| { | |||
| return m_taskString; | |||
| } | |||
| } | |||
| @@ -7,398 +7,277 @@ | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import com.oroinc.net.telnet.TelnetClient; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.OutputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.Calendar; | |||
| import java.util.Iterator; | |||
| import org.apache.myrmidon.api.AbstractTask; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.tools.ant.Task; | |||
| /** | |||
| * Class to provide automated telnet protocol support for the Ant build tool | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @author <a href="mailto:ScottCarlson@email.com">ScottCarlson@email.com</a> | |||
| * @version $Revision$ | |||
| */ | |||
| public class TelnetTask extends Task | |||
| public class TelnetTask | |||
| extends AbstractTask | |||
| { | |||
| /** | |||
| * The userid to login with, if automated login is used | |||
| */ | |||
| private String userid = null; | |||
| private String m_userid; | |||
| /** | |||
| * The password to login with, if automated login is used | |||
| */ | |||
| private String password = null; | |||
| private String m_password; | |||
| /** | |||
| * The server to connect to. | |||
| */ | |||
| private String server = null; | |||
| private String m_server; | |||
| /** | |||
| * The tcp port to connect to. | |||
| */ | |||
| private int port = 23; | |||
| /** | |||
| * The Object which handles the telnet session. | |||
| */ | |||
| private AntTelnetClient telnet = null; | |||
| private int m_port = 23; | |||
| /** | |||
| * The list of read/write commands for this session | |||
| */ | |||
| private ArrayList telnetTasks = new ArrayList(); | |||
| private ArrayList m_telnetTasks = new ArrayList(); | |||
| /** | |||
| * If true, adds a CR to beginning of login script | |||
| */ | |||
| private boolean addCarriageReturn = false; | |||
| private boolean m_addCarriageReturn; | |||
| /** | |||
| * Default time allowed for waiting for a valid response for all child | |||
| * reads. A value of 0 means no limit. | |||
| */ | |||
| private Integer defaultTimeout = null; | |||
| private Integer m_defaultTimeout; | |||
| /** | |||
| * Set the tcp port to connect to attribute | |||
| * | |||
| * @param b The new InitialCR value | |||
| */ | |||
| public void setInitialCR( boolean b ) | |||
| public void setInitialCR( final boolean addCarriageReturn ) | |||
| { | |||
| this.addCarriageReturn = b; | |||
| m_addCarriageReturn = addCarriageReturn; | |||
| } | |||
| /** | |||
| * Set the password attribute | |||
| * | |||
| * @param p The new Password value | |||
| */ | |||
| public void setPassword( String p ) | |||
| public void setPassword( final String password ) | |||
| { | |||
| this.password = p; | |||
| m_password = password; | |||
| } | |||
| /** | |||
| * Set the tcp port to connect to attribute | |||
| * | |||
| * @param p The new Port value | |||
| */ | |||
| public void setPort( int p ) | |||
| public void setPort( final int port ) | |||
| { | |||
| this.port = p; | |||
| m_port = port; | |||
| } | |||
| /** | |||
| * Set the server address attribute | |||
| * | |||
| * @param m The new Server value | |||
| */ | |||
| public void setServer( String m ) | |||
| public void setServer( final String server ) | |||
| { | |||
| this.server = m; | |||
| m_server = server; | |||
| } | |||
| /** | |||
| * Change the default timeout to wait for valid responses | |||
| * | |||
| * @param i The new Timeout value | |||
| */ | |||
| public void setTimeout( Integer i ) | |||
| public void setTimeout( final Integer defaultTimeout ) | |||
| { | |||
| this.defaultTimeout = i; | |||
| m_defaultTimeout = defaultTimeout; | |||
| } | |||
| /** | |||
| * Set the userid attribute | |||
| * | |||
| * @param u The new Userid value | |||
| */ | |||
| public void setUserid( String u ) | |||
| public void setUserid( final String userid ) | |||
| { | |||
| this.userid = u; | |||
| m_userid = userid; | |||
| } | |||
| /** | |||
| * A subTask <read> tag was found. Create the object, Save it in our | |||
| * list, and return it. | |||
| * | |||
| * @return Description of the Returned Value | |||
| */ | |||
| public TelnetSubTask createRead() | |||
| public void addRead( final TelnetRead read ) | |||
| { | |||
| TelnetSubTask task = (TelnetSubTask)new TelnetRead(); | |||
| telnetTasks.add( task ); | |||
| return task; | |||
| m_telnetTasks.add( read ); | |||
| } | |||
| /** | |||
| * A subTask <write> tag was found. Create the object, Save it in our | |||
| * list, and return it. | |||
| * | |||
| * @return Description of the Returned Value | |||
| */ | |||
| public TelnetSubTask createWrite() | |||
| public void addWrite( final TelnetWrite write ) | |||
| { | |||
| TelnetSubTask task = (TelnetSubTask)new TelnetWrite(); | |||
| telnetTasks.add( task ); | |||
| return task; | |||
| m_telnetTasks.add( write ); | |||
| } | |||
| /** | |||
| * Verify that all parameters are included. Connect and possibly login | |||
| * Iterate through the list of Reads and writes | |||
| * | |||
| * @exception TaskException Description of Exception | |||
| */ | |||
| public void execute() | |||
| throws TaskException | |||
| { | |||
| /** | |||
| * A server name is required to continue | |||
| */ | |||
| if( server == null ) | |||
| throw new TaskException( "No Server Specified" ); | |||
| /** | |||
| * A userid and password must appear together if they appear. They are | |||
| * not required. | |||
| */ | |||
| if( userid == null && password != null ) | |||
| throw new TaskException( "No Userid Specified" ); | |||
| if( password == null && userid != null ) | |||
| throw new TaskException( "No Password Specified" ); | |||
| validate(); | |||
| /** | |||
| * Create the telnet client object | |||
| */ | |||
| telnet = new AntTelnetClient(); | |||
| final AntTelnetClient telnet = new AntTelnetClient( this ); | |||
| try | |||
| { | |||
| telnet.connect( server, port ); | |||
| telnet.connect( m_server, m_port ); | |||
| } | |||
| catch( IOException e ) | |||
| catch( final IOException ioe ) | |||
| { | |||
| throw new TaskException( "Can't connect to " + server ); | |||
| throw new TaskException( "Can't connect to " + m_server, ioe ); | |||
| } | |||
| /** | |||
| * Login if userid and password were specified | |||
| */ | |||
| if( userid != null && password != null ) | |||
| login(); | |||
| /** | |||
| * Process each sub command | |||
| */ | |||
| Iterator tasksToRun = telnetTasks.iterator(); | |||
| while( tasksToRun != null && tasksToRun.hasNext() ) | |||
| if( m_userid != null && m_password != null ) | |||
| { | |||
| TelnetSubTask task = (TelnetSubTask)tasksToRun.next(); | |||
| if( task instanceof TelnetRead && defaultTimeout != null ) | |||
| ( (TelnetRead)task ).setDefaultTimeout( defaultTimeout ); | |||
| task.execute( telnet ); | |||
| login( telnet ); | |||
| } | |||
| } | |||
| /** | |||
| * Process a 'typical' login. If it differs, use the read and write tasks | |||
| * explicitely | |||
| */ | |||
| private void login() | |||
| { | |||
| if( addCarriageReturn ) | |||
| telnet.sendString( "\n", true ); | |||
| telnet.waitForString( "ogin:" ); | |||
| telnet.sendString( userid, true ); | |||
| telnet.waitForString( "assword:" ); | |||
| telnet.sendString( password, false ); | |||
| processTasks( telnet ); | |||
| } | |||
| /** | |||
| * This class handles the abstraction of the telnet protocol. Currently it | |||
| * is a wrapper around <a href="www.oroinc.com">ORO</a> 's NetComponents | |||
| * | |||
| * @author RT | |||
| * Process each sub command | |||
| */ | |||
| public class AntTelnetClient extends TelnetClient | |||
| private void processTasks( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| /** | |||
| * Write this string to the telnet session. | |||
| * | |||
| * @param s Description of Parameter | |||
| * @param echoString Description of Parameter | |||
| * @parm echoString Logs string sent | |||
| */ | |||
| public void sendString( String s, boolean echoString ) | |||
| final Iterator tasks = m_telnetTasks.iterator(); | |||
| while( tasks != null && tasks.hasNext() ) | |||
| { | |||
| OutputStream os = this.getOutputStream(); | |||
| try | |||
| { | |||
| os.write( ( s + "\n" ).getBytes() ); | |||
| if( echoString ) | |||
| getLogger().info( s ); | |||
| os.flush(); | |||
| } | |||
| catch( Exception e ) | |||
| final TelnetSubTask task = (TelnetSubTask)tasks.next(); | |||
| if( task instanceof TelnetRead && m_defaultTimeout != null ) | |||
| { | |||
| throw new TaskException( "Error", e ); | |||
| } | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found | |||
| * | |||
| * @param s Description of Parameter | |||
| * @parm s The string to wait on | |||
| */ | |||
| public void waitForString( String s ) | |||
| { | |||
| waitForString( s, null ); | |||
| } | |||
| /** | |||
| * Read from the telnet session until the string we are waiting for is | |||
| * found or the timeout has been reached | |||
| * | |||
| * @param s Description of Parameter | |||
| * @param timeout Description of Parameter | |||
| * @parm s The string to wait on | |||
| * @parm timeout The maximum number of seconds to wait | |||
| */ | |||
| public void waitForString( String s, Integer timeout ) | |||
| { | |||
| InputStream is = this.getInputStream(); | |||
| try | |||
| { | |||
| StringBuffer sb = new StringBuffer(); | |||
| if( timeout == null || timeout.intValue() == 0 ) | |||
| { | |||
| while( sb.toString().indexOf( s ) == -1 ) | |||
| { | |||
| sb.append( (char)is.read() ); | |||
| } | |||
| } | |||
| else | |||
| { | |||
| Calendar endTime = Calendar.getInstance(); | |||
| endTime.add( Calendar.SECOND, timeout.intValue() ); | |||
| while( sb.toString().indexOf( s ) == -1 ) | |||
| { | |||
| while( Calendar.getInstance().before( endTime ) && | |||
| is.available() == 0 ) | |||
| { | |||
| Thread.sleep( 250 ); | |||
| } | |||
| if( is.available() == 0 ) | |||
| throw new TaskException( "Response Timed-Out" ); | |||
| sb.append( (char)is.read() ); | |||
| } | |||
| } | |||
| getLogger().info( sb.toString() ); | |||
| } | |||
| catch( TaskException be ) | |||
| { | |||
| throw be; | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| throw new TaskException( "Error", e ); | |||
| ( (TelnetRead)task ).setDefaultTimeout( m_defaultTimeout ); | |||
| } | |||
| task.execute( telnet ); | |||
| } | |||
| } | |||
| /** | |||
| * This class reads the output from the connected server until the required | |||
| * string is found. | |||
| * | |||
| * @author RT | |||
| */ | |||
| public class TelnetRead extends TelnetSubTask | |||
| private void validate() | |||
| throws TaskException | |||
| { | |||
| private Integer timeout = null; | |||
| /** | |||
| * Sets the default timeout if none has been set already | |||
| * | |||
| * @param defaultTimeout The new DefaultTimeout value | |||
| */ | |||
| public void setDefaultTimeout( Integer defaultTimeout ) | |||
| //A server name is required to continue | |||
| if( m_server == null ) | |||
| { | |||
| if( timeout == null ) | |||
| timeout = defaultTimeout; | |||
| throw new TaskException( "No Server Specified" ); | |||
| } | |||
| /** | |||
| * Override any default timeouts | |||
| * | |||
| * @param i The new Timeout value | |||
| */ | |||
| public void setTimeout( Integer i ) | |||
| //A userid and password must appear together if they appear. They are | |||
| //not required. | |||
| if( m_userid == null && m_password != null ) | |||
| { | |||
| this.timeout = i; | |||
| throw new TaskException( "No Userid Specified" ); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| if( m_password == null && m_userid != null ) | |||
| { | |||
| telnet.waitForString( taskString, timeout ); | |||
| throw new TaskException( "No Password Specified" ); | |||
| } | |||
| } | |||
| /** | |||
| * This class is the parent of the Read and Write tasks. It handles the | |||
| * common attributes for both. | |||
| * | |||
| * @author RT | |||
| * Process a 'typical' login. If it differs, use the read and write tasks | |||
| * explicitely | |||
| */ | |||
| public class TelnetSubTask | |||
| private void login( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| protected String taskString = ""; | |||
| public void setString( String s ) | |||
| if( m_addCarriageReturn ) | |||
| { | |||
| taskString += s; | |||
| telnet.sendString( "\n", true ); | |||
| } | |||
| telnet.waitForString( "ogin:" ); | |||
| telnet.sendString( m_userid, true ); | |||
| telnet.waitForString( "assword:" ); | |||
| telnet.sendString( m_password, false ); | |||
| } | |||
| public void addContent( String s ) | |||
| protected void doSendString( final OutputStream output, | |||
| final String string, | |||
| final boolean echoString ) | |||
| throws TaskException | |||
| { | |||
| try | |||
| { | |||
| setString( s ); | |||
| output.write( ( string + "\n" ).getBytes() ); | |||
| if( echoString ) | |||
| { | |||
| getLogger().info( string ); | |||
| } | |||
| output.flush(); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| catch( final Exception e ) | |||
| { | |||
| throw new TaskException( "Shouldn't be able instantiate a SubTask directly" ); | |||
| throw new TaskException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| /** | |||
| * This class sends text to the connected server | |||
| * | |||
| * @author RT | |||
| */ | |||
| public class TelnetWrite extends TelnetSubTask | |||
| protected void doWaitForString( final InputStream input, | |||
| final String string, | |||
| final Integer timeout ) | |||
| throws TaskException | |||
| { | |||
| private boolean echoString = true; | |||
| public void setEcho( boolean b ) | |||
| try | |||
| { | |||
| echoString = b; | |||
| final StringBuffer sb = new StringBuffer(); | |||
| if( timeout == null || timeout.intValue() == 0 ) | |||
| { | |||
| while( sb.toString().indexOf( string ) == -1 ) | |||
| { | |||
| sb.append( (char)input.read() ); | |||
| } | |||
| } | |||
| else | |||
| { | |||
| final Calendar endTime = Calendar.getInstance(); | |||
| endTime.add( Calendar.SECOND, timeout.intValue() ); | |||
| while( sb.toString().indexOf( string ) == -1 ) | |||
| { | |||
| while( Calendar.getInstance().before( endTime ) && | |||
| input.available() == 0 ) | |||
| { | |||
| Thread.sleep( 250 ); | |||
| } | |||
| if( input.available() == 0 ) | |||
| { | |||
| throw new TaskException( "Response Timed-Out" ); | |||
| } | |||
| sb.append( (char)input.read() ); | |||
| } | |||
| } | |||
| getLogger().info( sb.toString() ); | |||
| } | |||
| public void execute( AntTelnetClient telnet ) | |||
| throws TaskException | |||
| catch( final TaskException te ) | |||
| { | |||
| throw te; | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| telnet.sendString( taskString, echoString ); | |||
| throw new TaskException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,30 @@ | |||
| /* | |||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||
| * | |||
| * This software is published under the terms of the Apache Software License | |||
| * version 1.1, a copy of which has been included with this distribution in | |||
| * the LICENSE.txt file. | |||
| */ | |||
| package org.apache.tools.ant.taskdefs.optional.net; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This class sends text to the connected server | |||
| */ | |||
| public class TelnetWrite | |||
| extends TelnetSubTask | |||
| { | |||
| private boolean m_echo = true; | |||
| public void setEcho( final boolean echo ) | |||
| { | |||
| m_echo = echo; | |||
| } | |||
| public void execute( final AntTelnetClient telnet ) | |||
| throws TaskException | |||
| { | |||
| telnet.sendString( getTaskString(), m_echo ); | |||
| } | |||
| } | |||