Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[geclipse-dev] Patch eu.geclipse.batch

Hi all,

for the pbs implementation to work I need to access the standard input of the ssh connection.

You'll find the patch for the eu.geclipse.batch bundle attached to this message.
Can it be merged in  the trunk ?

Cheers,
Romain.
Index: src/eu/geclipse/batch/SSHConnection.java
===================================================================
--- src/eu/geclipse/batch/SSHConnection.java	(revision 20533)
+++ src/eu/geclipse/batch/SSHConnection.java	(working copy)
@@ -143,161 +143,175 @@
    * <code>null</code> is returned.
    * @throws ProblemException If the command is not successfully executed.
    */
+  /**
+   * Execute a command on the remote host and returns the output.
+   * @param command The command to be executed on the remote host.
+   * @return Returns the output of the executed command if executed 
+   * successfully, if no output of the successfully executed command 
+   * <code>null</code> is returned.
+   * @throws ProblemException If the command is not successfully executed.
+   */
   public String execCommand( final String command ) throws ProblemException {
-    String line;
-    Channel channel = null;
-    InputStream stdout = null;
-    InputStream stderr = null;
-    String result = ""; //$NON-NLS-1$
-    String errResult = ""; //$NON-NLS-1$
-    int exitStatus = -1;
-    
-    BufferedReader stdoutReader = null;
-    BufferedReader stderrReader = null;
-    
-    // We throw exception if we don't have a session 
-    if ( ! this.isSessionActive() ) {
-      IProblem problem;
-      problem = ReportingPlugin.getReportingService()
-                  .getProblem( ICoreProblems.NET_CONNECTION_FAILED,
-                               null,
-                               null,
-                               Activator.PLUGIN_ID );
-      ISolution solution;
-      solution = ReportingPlugin.getReportingService()
-                   .getSolution( ICoreSolutions.NET_CHECK_INTERNET_CONNECTION, null );
-      problem.addSolution( solution );
-      
-      throw new ProblemException( problem );
-    }
-    
-    try {
-      channel = this.session.openChannel( "exec" );  //$NON-NLS-1$
+	  return execCommand(command, null);
+  }
+  
+  
+  public String execCommand( final String command, InputStream stdin) throws ProblemException {
+	    String line;
+	    Channel channel = null;
+	    InputStream stdout = null;
+	    InputStream stderr = null;
+	    String result = ""; //$NON-NLS-1$
+	    String errResult = ""; //$NON-NLS-1$
+	    int exitStatus = -1;
+	    
+	    BufferedReader stdoutReader = null;
+	    BufferedReader stderrReader = null;
+	    
+	    // We throw exception if we don't have a session 
+	    if ( ! this.isSessionActive() ) {
+	      IProblem problem;
+	      problem = ReportingPlugin.getReportingService()
+	                  .getProblem( ICoreProblems.NET_CONNECTION_FAILED,
+	                               null,
+	                               null,
+	                               Activator.PLUGIN_ID );
+	      ISolution solution;
+	      solution = ReportingPlugin.getReportingService()
+	                   .getSolution( ICoreSolutions.NET_CHECK_INTERNET_CONNECTION, null );
+	      problem.addSolution( solution );
+	      
+	      throw new ProblemException( problem );
+	    }
+	    
+	    try {
+	      channel = this.session.openChannel( "exec" );  //$NON-NLS-1$
 
-      ( ( ChannelExec )channel ).setCommand( command );
+	      ( ( ChannelExec )channel ).setCommand( command );
 
-      channel.setInputStream( null );
-      stdout = channel.getInputStream();
-      stderr = channel.getExtInputStream();
+	      channel.setInputStream( stdin );
+	      
+	      stdout = channel.getInputStream();
+	      stderr = channel.getExtInputStream();
 
-      channel.connect();
+	      channel.connect();
 
-      stdoutReader = new BufferedReader( new InputStreamReader( stdout ) );
-      stderrReader = new BufferedReader( new InputStreamReader( stderr ) );
+	      stdoutReader = new BufferedReader( new InputStreamReader( stdout ) );
+	      stderrReader = new BufferedReader( new InputStreamReader( stderr ) );
 
-      // Make sure that the command is executed before we exit
-      while ( !channel.isClosed() ) {
-        line = stdoutReader.readLine();
-        while ( null != line ) {
-          result = result + line + '\n';
-          line = stdoutReader.readLine();
-        }
+	      // Make sure that the command is executed before we exit
+	      while ( !channel.isClosed() ) {
+	        line = stdoutReader.readLine();
+	        while ( null != line ) {
+	          result = result + line + '\n';
+	          line = stdoutReader.readLine();
+	        }
 
-        line = stderrReader.readLine();
-        while ( null != line ) {
-          errResult = errResult + line + '\n'; 
-          line = stderrReader.readLine();
-        }
-         
-        try { Thread.sleep( 1000 ); }catch( Exception ee ) {
-          // Nothing to do here
-        }
-      }        
+	        line = stderrReader.readLine();
+	        while ( null != line ) {
+	          errResult = errResult + line + '\n'; 
+	          line = stderrReader.readLine();
+	        }
+	         
+	        try { Thread.sleep( 1000 ); }catch( Exception ee ) {
+	          // Nothing to do here
+	        }
+	      }        
 
-      // Read what is left after the channel is closed
-      line = stdoutReader.readLine();
-      while ( null != line ) {
-        result = result + line + '\n'; 
-        line = stdoutReader.readLine();
-      }
+	      // Read what is left after the channel is closed
+	      line = stdoutReader.readLine();
+	      while ( null != line ) {
+	        result = result + line + '\n'; 
+	        line = stdoutReader.readLine();
+	      }
 
-      line = stderrReader.readLine();
-      while ( null != line ) {
-        errResult = errResult + line + '\n'; 
-        line = stderrReader.readLine();
-      }
-        
-      exitStatus = channel.getExitStatus();
+	      line = stderrReader.readLine();
+	      while ( null != line ) {
+	        errResult = errResult + line + '\n'; 
+	        line = stderrReader.readLine();
+	      }
+	        
+	      exitStatus = channel.getExitStatus();
 
-      channel.disconnect();
-    } catch( JSchException jschExc ) {
-      IProblem problem;
-      problem = ReportingPlugin.getReportingService()
-                  .getProblem( ICoreProblems.NET_CONNECTION_FAILED,
-                               null,
-                               jschExc,
-                               Activator.PLUGIN_ID );
-      ISolution solution;
-      solution = ReportingPlugin.getReportingService()
-                          .getSolution( ICoreSolutions.NET_CHECK_INTERNET_CONNECTION, null );
-      problem.addSolution( solution );
+	      channel.disconnect();
+	    } catch( JSchException jschExc ) {
+	      IProblem problem;
+	      problem = ReportingPlugin.getReportingService()
+	                  .getProblem( ICoreProblems.NET_CONNECTION_FAILED,
+	                               null,
+	                               jschExc,
+	                               Activator.PLUGIN_ID );
+	      ISolution solution;
+	      solution = ReportingPlugin.getReportingService()
+	                          .getSolution( ICoreSolutions.NET_CHECK_INTERNET_CONNECTION, null );
+	      problem.addSolution( solution );
 
-      solution = ReportingPlugin.getReportingService().getSolution( 
-                                                       IBatchSolutions.CHECK_USERNAME_AND_PASSWORD, null );
-      problem.addSolution( solution );
-      
-      solution = ReportingPlugin.getReportingService()
-                     .getSolution( ICoreSolutions.NET_CHECK_FIREWALL, null );
-      problem.addSolution( solution );
+	      solution = ReportingPlugin.getReportingService().getSolution( 
+	                                                       IBatchSolutions.CHECK_USERNAME_AND_PASSWORD, null );
+	      problem.addSolution( solution );
+	      
+	      solution = ReportingPlugin.getReportingService()
+	                     .getSolution( ICoreSolutions.NET_CHECK_FIREWALL, null );
+	      problem.addSolution( solution );
 
-      throw new ProblemException( problem );
-    } catch ( IOException ioExc ) {
-      IProblem problem;
-      problem = ReportingPlugin.getReportingService()
-                  .getProblem( IBatchProblems.CONNECTION_IO_ERROR,
-                               null,
-                               ioExc,
-                               Activator.PLUGIN_ID );
-      ISolution solution = ReportingPlugin.getReportingService()
-                             .getSolution( ICoreSolutions.NET_CHECK_INTERNET_CONNECTION, null );
-      problem.addSolution( solution );
+	      throw new ProblemException( problem );
+	    } catch ( IOException ioExc ) {
+	      IProblem problem;
+	      problem = ReportingPlugin.getReportingService()
+	                  .getProblem( IBatchProblems.CONNECTION_IO_ERROR,
+	                               null,
+	                               ioExc,
+	                               Activator.PLUGIN_ID );
+	      ISolution solution = ReportingPlugin.getReportingService()
+	                             .getSolution( ICoreSolutions.NET_CHECK_INTERNET_CONNECTION, null );
+	      problem.addSolution( solution );
 
-      throw new ProblemException( problem );
-    } finally {
-      if ( null != stdoutReader ) {
-        try {
-          stdoutReader.close();
-        } catch( IOException e ) {
-          // Ignore this exception
-        }
-      }
-      
-      if ( null != stderrReader ) {
-        try {
-          stderrReader.close();
-        } catch( IOException e ) {
-          // Ignore this exception
-        }
-      }
-    }
+	      throw new ProblemException( problem );
+	    } finally {
+	      if ( null != stdoutReader ) {
+	        try {
+	          stdoutReader.close();
+	        } catch( IOException e ) {
+	          // Ignore this exception
+	        }
+	      }
+	      
+	      if ( null != stderrReader ) {
+	        try {
+	          stderrReader.close();
+	        } catch( IOException e ) {
+	          // Ignore this exception
+	        }
+	      }
+	    }
 
-    // Was the command executed successfully 
-    if ( ( 0 != exitStatus || 0 < errResult.length() ) && 0 == result.length() ) {
-      if ( 0 < errResult.length() ) {
-        IProblem problem;
-        problem = ReportingPlugin.getReportingService()
-                    .getProblem( IBatchProblems.COMMAND_FAILED,
-                                 errResult,
-                                 null,
-                                 Activator.PLUGIN_ID );
+	    // Was the command executed successfully 
+	    if ( ( 0 != exitStatus || 0 < errResult.length() ) && 0 == result.length() ) {
+	      if ( 0 < errResult.length() ) {
+	        IProblem problem;
+	        problem = ReportingPlugin.getReportingService()
+	                    .getProblem( IBatchProblems.COMMAND_FAILED,
+	                                 errResult,
+	                                 null,
+	                                 Activator.PLUGIN_ID );
 
-        throw new ProblemException( problem );
-      }
+	        throw new ProblemException( problem );
+	      }
 
-      IProblem problem;
-      problem = ReportingPlugin.getReportingService()
-                  .getProblem( IBatchProblems.COMMAND_FAILED,
-                               null,
-                               null,
-                               Activator.PLUGIN_ID );
+	      IProblem problem;
+	      problem = ReportingPlugin.getReportingService()
+	                  .getProblem( IBatchProblems.COMMAND_FAILED,
+	                               null,
+	                               null,
+	                               Activator.PLUGIN_ID );
 
-      throw new ProblemException( problem );
-    }
-    
-    //  If no output 
-    if ( 0 == result.length() )
-      result = null;
-    
-    return result;
+	      throw new ProblemException( problem );
+	    }
+	    
+	    //  If no output 
+	    if ( 0 == result.length() )
+	      result = null;
+	    
+	    return result;
   }
 }

Back to the top