Skip to content
Snippets Groups Projects
SshPSUClient.java 17 KiB
Newer Older
tjc's avatar
tjc committed
/* SshPSUClient.java
tjc's avatar
tjc committed
 *
 * created: Aug 2005
 *
 * This file is part of Artemis
 *
 * Copyright(C) 2005  Genome Research Limited
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 **/

package uk.ac.sanger.artemis.j2ssh;

tjc's avatar
tjc committed
import uk.ac.sanger.artemis.Options;
tjc's avatar
tjc committed
import uk.ac.sanger.artemis.components.MessageDialog;
tjc's avatar
tjc committed

tjc's avatar
tjc committed
import javax.swing.JFileChooser;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.IOException;

import java.util.Vector;
import java.util.Properties;

tjc's avatar
tjc committed
import com.sshtools.j2ssh.SshException;
tjc's avatar
tjc committed
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.SftpClient;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;


/**
tjc's avatar
tjc committed
*
* Client to use ssh connection to server to run blast/fasta
* remotely. 
*
*/
tjc's avatar
tjc committed
public class SshPSUClient extends Thread
{
tjc's avatar
tjc committed
  public static org.apache.log4j.Logger logger4j = 
      org.apache.log4j.Logger.getLogger(SshPSUClient.class);
tjc's avatar
tjc committed
  // defaults
tjc's avatar
tjc committed
  private String listfilepath = null;
  private String cmd      = null;
  private String bsub     = null;
  private String logfile  = null;
  private String db       = null;
tjc's avatar
tjc committed
  private String wdir     = null;
tjc's avatar
tjc committed
  private boolean justProg = false;
  
  //
  private SshClient ssh;
  private String user;
tjc's avatar
tjc committed
  private boolean keep = false;
tjc's avatar
tjc committed
  private boolean zipResults = false;
tjc's avatar
tjc committed
  
  //
  StdoutStdErrHandler stdouth;
  StdoutStdErrHandler stderrh;
tjc's avatar
tjc committed

  public SshPSUClient(String args[])
  {
    // process arguments
    if(args != null && args.length > 0)
    {
      for(int i=0; i<args.length; i++)
      {
        if(args[i].equals("-f") && i < args.length-1)
tjc's avatar
tjc committed
          listfilepath = args[i+1];
        else if(args[i].equals("-cmd") && i < args.length-1)
          cmd = args[i+1];
        else if(args[i].equals("-bsub") && i < args.length-1)
          bsub = args[i+1];
        else if(args[i].equals("-l") && i < args.length-1)
          logfile = args[i+1];
        else if(args[i].equals("-d") && i < args.length-1)
          db = args[i+1];
        else if(args[i].equals("-wdir") && i < args.length-1)
          wdir = args[i+1];
tjc's avatar
tjc committed
        else if(args[i].equals("-keep"))
          keep = true;
tjc's avatar
tjc committed
      }
    }

    SshLogin sshLogin = new SshLogin();
    ssh = sshLogin.getSshClient();
    user = sshLogin.getUser();
tjc's avatar
tjc committed
  }
tjc's avatar
tjc committed
  
  public SshPSUClient(final String cmd)
  {
    this.cmd = cmd;
    SshLogin sshLogin = new SshLogin();
    ssh = sshLogin.getSshClient();
    justProg = true;
  }
tjc's avatar
tjc committed

tjc's avatar
tjc committed
  private SshClient rescue()
  {
tjc's avatar
tjc committed
    try
    {
      ssh.disconnect();
      SshLogin sshLogin = new SshLogin();
      ssh = sshLogin.getSshClient();
    }
    catch(Exception exp)
    {
tjc's avatar
tjc committed
      logger4j.warn("SshPSUClient.rescue()");
tjc's avatar
tjc committed
      exp.printStackTrace();
    }
tjc's avatar
tjc committed
    return ssh;
  }

tjc's avatar
tjc committed
  public void run()
  {
    String program = cmd;
    boolean completed = false;
    try
    {
tjc's avatar
tjc committed
      ConfigurationLoader.initialize(false);

tjc's avatar
tjc committed
      if(ssh == null)
        return;
tjc's avatar
tjc committed
      if(justProg)
        runProgram();
      else
        completed = runBlastOrFasta(program);
tjc's avatar
tjc committed

tjc's avatar
tjc committed
      // Quit
      //ssh.disconnect();
tjc's avatar
tjc committed
    }
    catch(IOException ioe){}
tjc's avatar
tjc committed
    finally
    {
tjc's avatar
tjc committed
      if(completed)
tjc's avatar
tjc committed
        new MessageDialog(null,
            "Finished \n" + program, 
tjc's avatar
tjc committed
            "Process Finished",
tjc's avatar
tjc committed
            false);
tjc's avatar
tjc committed
    }
  }

tjc's avatar
tjc committed

  /**
  *
  * Read the sequence filenames in a list file 
  * @param String file	list filename
  * @return the sequence filename collection
  * 
  */
tjc's avatar
tjc committed
  private Vector readListFile(String file)
  {
    Vector seqfiles = new Vector();
    try
    {
      String line;
      BufferedReader in = new BufferedReader(new FileReader(file));
      while((line = in.readLine()) != null )
      {
        File seq = new File(line);
        if(seq.exists())
        {
          seqfiles.add(seq.getAbsolutePath());
        }
      }
tjc's avatar
tjc committed
    }
    catch (IOException e)
    {
tjc's avatar
tjc committed
      logger4j.warn("Problem reading list file");
tjc's avatar
tjc committed
    }
    return seqfiles;
  }

tjc's avatar
tjc committed
  /**
  *
  *  Wait until a file appears on the server.  
  *
  */
tjc's avatar
tjc committed
  private boolean waitUntilFileAppears(String file)
tjc's avatar
tjc committed
                   throws InterruptedException, IOException
  {
    for(int i=0; i < 500; i++)
tjc's avatar
tjc committed
    {
tjc's avatar
tjc committed
      logger4j.debug("waitUntilFileAppears() "+file);
      Thread.sleep(1000);
tjc's avatar
tjc committed
        if(fileExists(getSftpClient() , file))
tjc's avatar
tjc committed
          return true;
tjc's avatar
tjc committed
      }
      catch(SshException sshe)
tjc's avatar
tjc committed
      {
tjc's avatar
tjc committed
        if(System.getProperty("debug") != null)
        {
tjc's avatar
tjc committed
          logger4j.warn("waitUntilFileAppears()");
tjc's avatar
tjc committed
          sshe.printStackTrace();
        }
        try
        {
          rescue();
          continue;
        } catch(Exception exp) {}
tjc's avatar
tjc committed
      }
    }

    return false;
  }

tjc's avatar
tjc committed
  private boolean fileExists(SftpClient sftp, String file)
             throws SshException, IOException
  {
tjc's avatar
tjc committed
    Object list[] = null;
    try
    {
      list = sftp.ls(wdir).toArray();
    }
    catch(SshException sshe)
    {
      sftp = getSftpClient();
      list = sftp.ls(wdir).toArray();
    }

tjc's avatar
tjc committed
    for(int j=0; j<list.length;j++)
    {
      if( ((SftpFile)list[j]).getFilename().equals(file) )
        return true;
    }
    return false;
  }
tjc's avatar
tjc committed

  /**
  *
  * Get the properties from the j2ssh.properties file.
  *
  */
tjc's avatar
tjc committed
  private Properties getProperties()
  {
    Properties settings = new Properties();
    ClassLoader cl = this.getClass().getClassLoader();
    // try out of the classpath
    try
    {
      settings.load(cl.getResourceAsStream("j2ssh.properties"));
    }
    catch (Exception e)
    {
    }
tjc's avatar
tjc committed

    if(bsub == null && settings.getProperty("bsub") != null)
      bsub = settings.getProperty("bsub");
    if(db == null)
    {
      if(settings.getProperty("default_db") != null)
        db = settings.getProperty("default_db");
      else
        db = "%uniprot";
    } 
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    if(settings.getProperty("zip") != null)
    {
      String zipValue = settings.getProperty("zip");
      zipResults = Boolean.parseBoolean(zipValue);
      
      logger4j.debug("zip results :: "+zipResults);
    }
    
tjc's avatar
tjc committed
    if(wdir == null && settings.getProperty("wdir") != null)
tjc's avatar
tjc committed
      wdir = settings.getProperty("wdir");

tjc's avatar
tjc committed
    if(cmd != null)
    {
      if(cmd.equals("blastp") && settings.getProperty("blastp") != null)
        cmd = settings.getProperty("blastp");
      else if(cmd.equals("blastn") && settings.getProperty("blastn") != null)
        cmd = settings.getProperty("blastn");
      else if(cmd.equals("blastx") && settings.getProperty("blastx") != null)
        cmd = settings.getProperty("blastx");
      else if(cmd.equals("tblastx") && settings.getProperty("tblastx") != null)
        cmd = settings.getProperty("tblastx"); 
      else if(cmd.equals("fasta") && settings.getProperty("fasta") != null) 
        cmd = settings.getProperty("fasta");
      else if(cmd.equals("fastx") && settings.getProperty("fastx") != null)
        cmd = settings.getProperty("fastx");
    }
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    return settings;
  }

tjc's avatar
tjc committed
 
tjc's avatar
tjc committed
  /**
  *
  * Run fasta or blast on the server ssh'ed into
  *
  */
tjc's avatar
tjc committed
  private boolean runBlastOrFasta(String program)
tjc's avatar
tjc committed
                    throws IOException
  {
tjc's avatar
tjc committed
    Properties settings = getProperties();

tjc's avatar
tjc committed
    // prompt for local listfile
    if(listfilepath == null)
    {
      JFileChooser chooser = new JFileChooser();
      int returnVal = chooser.showOpenDialog(null);
      if(returnVal == JFileChooser.APPROVE_OPTION) 
        listfilepath = chooser.getSelectedFile().getAbsolutePath();
      else
        return false;
    }

tjc's avatar
tjc committed
    SftpClient sftp = getSftpClient();
tjc's avatar
tjc committed

    // loop over sequence files in the listfile
    Vector seqfile = readListFile(listfilepath);
    for(int i=0; i<seqfile.size();i++)
    {
      String filepath = (String)seqfile.get(i);
      int index = filepath.lastIndexOf(System.getProperty("file.separator"));
      String filename = filepath;
      if(index > -1)
        filename = filename.substring(index+1);

tjc's avatar
tjc committed
      if(i == 0)
tjc's avatar
tjc committed
        try
tjc's avatar
tjc committed
        {
tjc's avatar
tjc committed
          if(!keep)
            wdir = wdir + "/" + user;
          sftp.mkdir(wdir);
          wdir = wdir + "/" + program + "/";

          sftp.mkdir(wdir);
          logger4j.debug("mkdir() " + wdir);
          // sftp.put(filepath, wdir+filename);
        }
        catch(SshException sshe)
        {
          logger4j.debug("runBlastOrFasta()");
          if(System.getProperty("debug") != null)
          {
            sshe.printStackTrace();
          }
          rescue();
          sftp = getSftpClient();
          if(!wdir.endsWith(program + "/"))
            wdir = wdir + "/" + program + "/";
        }
        catch(IOException ioe)
        {
          // directory already created
tjc's avatar
tjc committed
        }
tjc's avatar
tjc committed

tjc's avatar
tjc committed
      try
      {
        sftp.put(filepath, wdir+filename);

tjc's avatar
tjc committed
        logger4j.debug("PUT SUCCESS "+wdir+filename);
tjc's avatar
tjc committed
      }
      catch(SshException ioe)
      {
tjc's avatar
tjc committed
        logger4j.debug("runBlastOrFasta() - 2");
tjc's avatar
tjc committed
        if(System.getProperty("debug") != null)
        {
          ioe.printStackTrace();
        }
tjc's avatar
tjc committed
        rescue();
tjc's avatar
tjc committed
        sftp = getSftpClient();
tjc's avatar
tjc committed
        sftp.put(filepath, wdir+filename);
      }

tjc's avatar
tjc committed
      logger4j.debug("STARTING session");
tjc's avatar
tjc committed

      SessionChannelClient session = null;

      try 
      {
        if(!ssh.isConnected())
          rescue();

        session = ssh.openSessionChannel();
      }
      catch(IOException exp)
      {
tjc's avatar
tjc committed
        logger4j.debug("NOT STARTED runBlastOrFasta() ----- 3 "+filename);
tjc's avatar
tjc committed
        if(System.getProperty("debug") != null)
        {
          exp.printStackTrace();
        }
tjc's avatar
tjc committed
        rescue();
      }

tjc's avatar
tjc committed
      String outputfile = wdir+filename+".out";
      final String actualCMD;
tjc's avatar
tjc committed
     
tjc's avatar
tjc committed
      if(bsub == null)
tjc's avatar
tjc committed
      {
tjc's avatar
tjc committed
        if( ((cmd.indexOf("fasta3") > -1) || (cmd.indexOf("fastx3") > -1))
            && settings.getProperty(db) != null)
tjc's avatar
tjc committed
          db = settings.getProperty(db);
tjc's avatar
tjc committed
        else if(db.startsWith("%"))
          db = db.substring(1,db.length());

        if( (cmd.indexOf("fasta3") > -1) ||
            (cmd.indexOf("fastx3") > -1) )
          actualCMD = cmd+" "+wdir+filename+" "+db+" > "+outputfile;
        else
          actualCMD = cmd+" -d "+db+" -i "+wdir+filename+" -o "+outputfile;
tjc's avatar
tjc committed
      }
      else
tjc's avatar
tjc committed
      {
        if( (cmd.indexOf("fasta3") > -1) ||
            (cmd.indexOf("fastx3") > -1) )
        {
          if(settings.getProperty(db) != null)
            db = settings.getProperty(db);
          actualCMD = bsub+" -o "+ outputfile +" -e "+ outputfile + ".err " +
                       cmd+" "+wdir+filename+" "+db;
        }
        else
          actualCMD = bsub+" -o "+ outputfile +" -e "+ outputfile + ".err " +
tjc's avatar
tjc committed
                       cmd+" "+db+" "+wdir+filename;
tjc's avatar
tjc committed
      }
tjc's avatar
tjc committed

      // run the application
tjc's avatar
tjc committed
      logger4j.debug(actualCMD);
tjc's avatar
tjc committed

      try
      {
        session.executeCommand(actualCMD);
      }
      catch(IOException exp)
      {
tjc's avatar
tjc committed
        logger4j.debug("runBlastOrFasta() - 3");
tjc's avatar
tjc committed
        if(System.getProperty("debug") != null)
        {
          exp.printStackTrace();
        }
      }

tjc's avatar
tjc committed
      logger4j.debug("STARTED session "+filename);
tjc's avatar
tjc committed

      // Reading from the session InputStream
      StdoutStdErrHandler stdouth = new StdoutStdErrHandler(session, true);
      StdoutStdErrHandler stderrh = new StdoutStdErrHandler(session, false);
    
      stdouth.start();
      stderrh.start();

      boolean isFile = false;
      try
      {
        // make sure we hang around for stdout
        while(stdouth.isAlive() || stderrh.isAlive())
tjc's avatar
tjc committed
          Thread.sleep(10);
tjc's avatar
tjc committed

tjc's avatar
tjc committed
        isFile = waitUntilFileAppears(filename+".out");
tjc's avatar
tjc committed
      }
      catch(InterruptedException ie)
      {
        ie.printStackTrace();
      }
tjc's avatar
tjc committed
       
tjc's avatar
tjc committed
      // stdout & stderr
      logger4j.debug("STDOUT "+filename+"\n"+stdouth.getOutput());
      logger4j.debug("STDERR "+filename+"\n"+stderrh.getOutput());

tjc's avatar
tjc committed

tjc's avatar
tjc committed
        sftp = getSftpClient();
tjc's avatar
tjc committed
        sftp.get(outputfile, filepath+".out");
      }
      catch(Exception ioe)
      {
tjc's avatar
tjc committed
        logger4j.debug("runBlastOrFasta() - 3");
tjc's avatar
tjc committed
        if(System.getProperty("debug") != null)
        {
          ioe.printStackTrace();
        }
tjc's avatar
tjc committed
        rescue();
tjc's avatar
tjc committed
        sftp = getSftpClient();
tjc's avatar
tjc committed
        sftp.get(outputfile, filepath+".out");
      }

tjc's avatar
tjc committed
      logger4j.debug("GET SUCCESS "+filepath+".out");
tjc's avatar
tjc committed
      sftp.rm(wdir+filename);
      
tjc's avatar
tjc committed
      if(!keep)
      {
        sftp.rm(outputfile);
tjc's avatar
tjc committed
      }
      else if(zipResults)
      {
        cmd = "gzip "+outputfile+"; zip -j "+wdir+program+".zip "+
                                    outputfile+".gz; rm -f "+outputfile+".gz";
        logger4j.debug(cmd);
        SshPSUClient sshClient = new SshPSUClient(cmd);
        sshClient.start();
tjc's avatar
tjc committed
      }
tjc's avatar
tjc committed
      sftp = getSftpClient();
      sftp.rm(outputfile+".err");
tjc's avatar
tjc committed
      session.close();
    }

    return true;
  }

tjc's avatar
tjc committed
  
  
  /**
  *
  * Run fasta or blast on the server ssh'ed into
  *
  */
  private boolean runProgram()
                    throws IOException
  {
    SessionChannelClient session = null;

    try 
    {
      if(!ssh.isConnected())
        rescue();

      session = ssh.openSessionChannel();
    }
    catch(IOException exp)
    {
tjc's avatar
tjc committed
      logger4j.warn("NOT STARTED runProgram()");
tjc's avatar
tjc committed
      if(System.getProperty("debug") != null)
      {
        exp.printStackTrace();
      }
      rescue();
    }
    
    // run the application

    try
    {
      session.executeCommand(cmd);
    }
    catch(IOException exp)
    {
tjc's avatar
tjc committed
      logger4j.warn("session   : "+cmd);
      logger4j.warn("runProgram: "+exp.getMessage());
tjc's avatar
tjc committed
      if(System.getProperty("debug") != null)
      {
        exp.printStackTrace();
      }
    }

    logger4j.debug("STARTED session "+cmd);

    // Reading from the session InputStream
    stdouth = new StdoutStdErrHandler(session, true);
    stderrh = new StdoutStdErrHandler(session, false);
    
    stdouth.start();
    stderrh.start();

    try
    {
      // make sure we hang around for stdout
      while(stdouth.isAlive() || stderrh.isAlive())
tjc's avatar
tjc committed
        Thread.sleep(2);
tjc's avatar
tjc committed
    }
    catch(InterruptedException ie)
    {
      ie.printStackTrace();
    }
tjc's avatar
tjc committed
    
tjc's avatar
tjc committed
    // stdout & stderr
tjc's avatar
tjc committed
    if(stderrh.getBufferSize() > 0)
tjc's avatar
tjc committed
      logger4j.debug("STDERR :"+stderrh.getOutput());
tjc's avatar
tjc committed

    session.close();
    return true;
  }
tjc's avatar
tjc committed

  
  public StringBuffer getStdOutBuffer()
  {
    return stdouth.getStdOutBuffer();
  }
tjc's avatar
tjc committed
  
  public String getStdOut()
  {
    return stdouth.getOutput();
  }

  public String getStdErr()
  {
    return stderrh.getOutput();
  }
  
tjc's avatar
tjc committed
  /**
  *
  * Return an active SftpClient object
  *
  */
tjc's avatar
tjc committed
  private synchronized SftpClient getSftpClient()
tjc's avatar
tjc committed
             throws IOException
  {
    SftpClient sftp;
tjc's avatar
tjc committed
      
tjc's avatar
tjc committed
    try
    {
tjc's avatar
tjc committed
      if(!ssh.hasActiveSftpClient())
        return ssh.openSftpClient();

tjc's avatar
tjc committed
      sftp = ssh.getActiveSftpClient();
      return sftp;
    }
tjc's avatar
tjc committed
    catch(SshException sshe)
    {
      try
      {
        sftp = ssh.openSftpClient();
        return sftp;
      }
      catch(IOException ioe)
      {
tjc's avatar
tjc committed
        logger4j.debug("getSftpClient() -- 2");
tjc's avatar
tjc committed
        if(System.getProperty("debug") != null)
        {
          ioe.printStackTrace();
        }
        rescue();
      }
    }
    catch(IOException ioe2)
tjc's avatar
tjc committed
      logger4j.debug("getSftpClient()");
tjc's avatar
tjc committed
      if(System.getProperty("debug") != null)
      {
tjc's avatar
tjc committed
        ioe2.printStackTrace();
tjc's avatar
tjc committed
      }
tjc's avatar
tjc committed
      rescue();
    }
tjc's avatar
tjc committed
    return ssh.openSftpClient();
  }

tjc's avatar
tjc committed
  /**
  *
  * Thread to handle stdout/stderr reading without blocking.
  *
  */
tjc's avatar
tjc committed
  class StdoutStdErrHandler extends Thread
  {
    private SessionChannelClient session;
    private boolean isStdout;
    private StringBuffer buff = new StringBuffer();

tjc's avatar
tjc committed
    protected StdoutStdErrHandler(final SessionChannelClient session, 
                                  final boolean isStdout)
tjc's avatar
tjc committed
    {
      this.session  = session;
      this.isStdout = isStdout;
    }

    public void run()
    {
      try
      {
        final InputStream in;
        if(isStdout)
          in = session.getInputStream();
        else
          in = session.getStderrInputStream();

tjc's avatar
tjc committed
        final byte buffer[] = new byte[400];
tjc's avatar
tjc committed
        int read;
        while((read = in.read(buffer)) > 0)
          buff.append(new String(buffer, 0, read));
tjc's avatar
tjc committed

        in.close();
      }
      catch(Exception ioe)
      {
        ioe.printStackTrace();
tjc's avatar
tjc committed
      }
    }
tjc's avatar
tjc committed
    
    public synchronized StringBuffer getStdOutBuffer()
    {
      return buff;
    }
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    public synchronized String getOutput()
tjc's avatar
tjc committed
    {
      return buff.toString();
    }
tjc's avatar
tjc committed
    
    public int getBufferSize()
    {
      return buff.length();
    }
tjc's avatar
tjc committed
  }


  /**
   * The main program for the PasswordConnect class
   *
   * @param args The command line arguments
   */
  public static void main(String args[]) 
  {
    new SshPSUClient(args);
  }
}