diff --git a/uk/ac/sanger/artemis/j2ssh/SshFileManager.java b/uk/ac/sanger/artemis/j2ssh/SshFileManager.java new file mode 100644 index 0000000000000000000000000000000000000000..bd20d0f7a96f08947ebdcbc68876af421fc0aad0 --- /dev/null +++ b/uk/ac/sanger/artemis/j2ssh/SshFileManager.java @@ -0,0 +1,431 @@ +/* ExternalProgram.java + * + * 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; + + +import javax.swing.JOptionPane; +import javax.swing.JFileChooser; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.JPasswordField; +import javax.swing.SwingConstants; + +import java.awt.GridLayout; +import java.io.File; +import java.io.FileReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import java.util.Vector; +import java.util.Properties; +import java.util.logging.FileHandler; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; + +import com.sshtools.j2ssh.SshClient; +import com.sshtools.j2ssh.authentication.AuthenticationProtocolState; +import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient; +import com.sshtools.j2ssh.session.SessionChannelClient; +import com.sshtools.j2ssh.sftp.FileAttributes; +import com.sshtools.j2ssh.sftp.SftpFile; +import com.sshtools.j2ssh.sftp.SftpFileOutputStream; +import com.sshtools.j2ssh.SftpClient; +import com.sshtools.j2ssh.configuration.ConfigurationLoader; + + +/** +* +* Client to use ssh connection to server to run blast/fasta +* remotely. +* +*/ +public class SshFileManager +{ + + private Vector dir_list; + private Vector file_list; + + // defaults + private String logfile = null; + private String wdir = "/nfs/pathscratch1/scratch"; + + // login variables + private String hostname = null; + private String user = null; + private int port = -1; + private static JPasswordField pfield = new JPasswordField(16); + private static JTextField portfield = new JTextField(16); + private static JTextField hostfield = new JTextField(16); + private static JTextField ufield = new JTextField(16); + private SshClient ssh; + + public SshFileManager() + { + try + { + // Setup a logfile + if(logfile != null) + { + Handler fh = new FileHandler(logfile); + fh.setFormatter(new SimpleFormatter()); + Logger.getLogger("com.sshtools").setUseParentHandlers(false); + Logger.getLogger("com.sshtools").addHandler(fh); + Logger.getLogger("com.sshtools").setLevel(Level.ALL); + } + else + Logger.getLogger("com.sshtools").setLevel(Level.OFF); + } + catch(IOException ioe){} + + Properties settings = getProperties(); + try + { + ssh = login(); + } + catch(IOException ioe){} + } + + /** + * + * Log the user in. + * + */ + private SshClient login() + throws IOException + { + SshClient ssh = null; + int result = AuthenticationProtocolState.FAILED; + + while(result != AuthenticationProtocolState.COMPLETE) + { + if(!setLogin()) + return null; + + // Create a password authentication instance + PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); + user = ufield.getText().trim(); + pwd.setUsername(user); + pwd.setPassword(new String(pfield.getPassword())); + + // Make a client connection + ssh = new SshClient(); + hostname = hostfield.getText().trim(); + if(portfield.getText().trim().equals("")) + port = -1; + else + port = Integer.parseInt(portfield.getText().trim()); + + if(port < 0) + ssh.connect(hostname); + else + ssh.connect(hostname,port); + + // Try the authentication + result = ssh.authenticate(pwd); + } + return ssh; + } + + /** + * + * Set the login information. + * + */ + private boolean setLogin() + { + JPanel promptPanel = new JPanel(new GridLayout(4,2)); + + if(hostname != null && hostfield.getText().equals("")) + hostfield.setText(hostname); + + if(port >-1 && portfield.getText().equals("")) + portfield.setText(Integer.toString(port)); + + if(user != null && ufield.getText().equals("")) + ufield.setText(user); + + JLabel hostlab = new JLabel(" Hostname:", SwingConstants.LEFT); + JLabel portlab = new JLabel(" Port:", SwingConstants.LEFT); + + JLabel ulab = new JLabel(" Username:", SwingConstants.LEFT); + JLabel plab = new JLabel(" Password:", SwingConstants.LEFT); + //add labels etc + promptPanel.add(hostlab); + promptPanel.add(hostfield); + + promptPanel.add(portlab); + promptPanel.add(portfield); + + promptPanel.add(ulab); + promptPanel.add(ufield); + + promptPanel.add(plab); + promptPanel.add(pfield); + + Object[] options = { "CANCEL", "LOGIN AND RUN"}; + + int select = JOptionPane.showOptionDialog(null, promptPanel, + "LOGIN", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[1]); + + if(select == 0) + return false; + + return true; + } + + + /** + * + * Get the properties from the j2ssh.properties file. + * + */ + 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) + { + } + + if(hostname == null && settings.getProperty("host") != null) + hostname = settings.getProperty("host"); + if(port < 0 && settings.getProperty("port") != null) + port = Integer.parseInt(settings.getProperty("port")); + if(user == null) + user = System.getProperty("user.name"); + + return settings; + } + + public boolean remoteList(String remoteRootDir) + throws IOException + { + SftpClient sftp = ssh.openSftpClient(); + + try + { + sftp.cd(remoteRootDir); + } + catch(java.io.FileNotFoundException fnf) + { + } + + Object list[] = sftp.ls().toArray(); + + dir_list = new Vector(); + file_list = new Vector(); + + for(int i=0; i < list.length; i++) + { + SftpFile sfile = (SftpFile)list[i]; + if(sfile.isDirectory()) + dir_list.add(sfile.getFilename()); + + file_list.add(sfile.getFilename()); + } + + sftp.quit(); + return true; + } + + public boolean delete(String filename) + { + try + { + SftpClient sftp = ssh.openSftpClient(); + sftp.rm(filename); + sftp.quit(); + } + catch(IOException ioe) + { + ioe.printStackTrace(); + return false; + } + + return true; + } + + public boolean mkdir(String dir) + { + try + { + SftpClient sftp = ssh.openSftpClient(); + sftp.mkdir(dir); + sftp.quit(); + } + catch(IOException ioe) + { + ioe.printStackTrace(); + return false; + } + + return true; + } + + public String pwd() + { + String pwd = null; + try + { + SftpClient sftp = ssh.openSftpClient(); + pwd = sftp.pwd(); + sftp.quit(); + } + catch(IOException ioe) + { + ioe.printStackTrace(); + } + + return pwd; + } + + public boolean rename(String old_file, String new_file) + { + try + { + SftpClient sftp = ssh.openSftpClient(); + sftp.rename(old_file,new_file); + sftp.quit(); + } + catch(IOException ioe) + { + ioe.printStackTrace(); + return false; + } + + return true; + } + + + /** + * + * @param dir name of directory to write the file to + * @param local_file file to copy to the server + * + */ + public boolean put(String dir, File local_file) + { + SftpClient sftp = null; + + try + { + sftp = ssh.openSftpClient(); + FileAttributes attr = sftp.stat(dir+"/"+local_file.getName()); + + if(attr.isDirectory()) + { + JOptionPane.showMessageDialog(null, + "Cannot overwrite the directory\n"+ + dir+"/"+local_file.getName(),"Cannot Overwrite", + JOptionPane.ERROR_MESSAGE); + return false; + } + else + { + int n = JOptionPane.showConfirmDialog(null, + "Overwrite\n"+ + dir+"/"+local_file.getName() + "\n?", + "Confirm the sequence entry", + JOptionPane.YES_NO_OPTION); + if(n == JOptionPane.NO_OPTION) + return false; + } + } + catch(IOException ioe) + { + // remote file doesn't exist + } + + if(sftp == null) + return false; + + try + { + sftp.put(local_file.getCanonicalPath(), + dir+"/"+local_file.getName()); + sftp.quit(); + } + catch(IOException ioe) + { + ioe.printStackTrace(); + return false; + } + + return true; + } + + + + /** + * + * Return the file contents as a byte array + * + */ + public byte[] getFileContents(String file) + { + try + { + SftpClient sftp = ssh.openSftpClient(); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + sftp.get(file, os); + sftp.quit(); + return os.toByteArray(); + } + catch(IOException ioe) + { + ioe.printStackTrace(); + return null; + } + } + + + public Vector getFileList() + { + return file_list; + } + + public Vector getDirList() + { + return dir_list; + } + +}