From acbd43742d0f9551ad39a7a610ac9980364cb6a1 Mon Sep 17 00:00:00 2001 From: Greg Watson Date: Mon, 27 Jan 2014 18:15:25 -0500 Subject: [PATCH] Cleanup Signed-off-by: Greg Watson --- .../internal/jsch/core/JSchConnection.java | 106 ++++++++++-------- .../core/commands/AbstractRemoteCommand.java | 18 ++- .../jsch/core/commands/ChildInfosCommand.java | 2 - .../jsch/core/commands/FetchInfoCommand.java | 2 - .../jsch/core/commands/MkdirCommand.java | 2 - .../jsch/core/commands/PutInfoCommand.java | 3 - .../internal/jsch/core/messages/Messages.java | 27 +---- .../jsch/core/messages/messages.properties | 24 +--- 8 files changed, 81 insertions(+), 103 deletions(-) diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java index dfafb3e37f7..86bdeeb6a4f 100644 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/JSchConnection.java @@ -23,6 +23,7 @@ import org.eclipse.core.runtime.ListenerList; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jsch.core.IJSchService; +import org.eclipse.osgi.util.NLS; import org.eclipse.remote.core.IRemoteConnection; import org.eclipse.remote.core.IRemoteConnectionChangeEvent; import org.eclipse.remote.core.IRemoteConnectionChangeListener; @@ -49,8 +50,6 @@ import com.jcraft.jsch.UserInfo; * @since 5.0 */ public class JSchConnection implements IRemoteConnection { - private final boolean logging = false; - /** * Class to supply credentials from connection attributes without user interaction. */ @@ -62,6 +61,22 @@ public class JSchConnection implements IRemoteConnection { fAuthenticator = authenticator; } + @Override + public String getPassphrase() { + if (logging) { + System.out.println("getPassphrase"); //$NON-NLS-1$ + } + return JSchConnection.this.getPassphrase(); + } + + @Override + public String getPassword() { + if (logging) { + System.out.println("getPassword"); //$NON-NLS-1$ + } + return JSchConnection.this.getPassword(); + } + @Override public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo) { @@ -81,19 +96,22 @@ public class JSchConnection implements IRemoteConnection { } @Override - public String getPassphrase() { + public boolean promptPassphrase(String message) { if (logging) { - System.out.println("getPassphrase"); //$NON-NLS-1$ + System.out.println("promptPassphrase:" + message); //$NON-NLS-1$ } - return JSchConnection.this.getPassphrase(); - } - - @Override - public String getPassword() { - if (logging) { - System.out.println("getPassword"); //$NON-NLS-1$ + if (firstTry && !getPassphrase().equals("")) { //$NON-NLS-1$ + firstTry = false; + return true; } - return JSchConnection.this.getPassword(); + if (fAuthenticator != null) { + PasswordAuthentication auth = fAuthenticator.prompt(null, message); + if (auth == null) { + return false; + } + fAttributes.setSecureAttribute(JSchConnectionAttributes.PASSPHRASE_ATTR, new String(auth.getPassword())); + } + return true; } @Override @@ -116,25 +134,6 @@ public class JSchConnection implements IRemoteConnection { return true; } - @Override - public boolean promptPassphrase(String message) { - if (logging) { - System.out.println("promptPassphrase:" + message); //$NON-NLS-1$ - } - if (firstTry && !getPassphrase().equals("")) { //$NON-NLS-1$ - firstTry = false; - return true; - } - if (fAuthenticator != null) { - PasswordAuthentication auth = fAuthenticator.prompt(null, message); - if (auth == null) { - return false; - } - fAttributes.setSecureAttribute(JSchConnectionAttributes.PASSPHRASE_ATTR, new String(auth.getPassword())); - } - return true; - } - @Override public boolean promptYesNo(String message) { if (logging) { @@ -160,6 +159,8 @@ public class JSchConnection implements IRemoteConnection { } } + private final boolean logging = false; + public static final int DEFAULT_PORT = 22; public static final int DEFAULT_TIMEOUT = 5; public static final boolean DEFAULT_IS_PASSWORD = true; @@ -273,6 +274,22 @@ public class JSchConnection implements IRemoteConnection { return getName().compareTo(o.getName()); } + /** + * Execute the command and return the result as a string. + * + * @param cmd + * command to execute + * @param monitor + * progress monitor + * @return result of command + * @throws RemoteConnectionException + */ + private String executeCommand(String cmd, IProgressMonitor monitor) throws RemoteConnectionException { + ExecCommand exec = new ExecCommand(this); + monitor.subTask(NLS.bind(Messages.JSchConnection_Executing_command, cmd)); + return exec.setCommand(cmd).getResult(monitor).trim(); + } + /** * Notify all fListeners when this connection's status changes. * @@ -439,9 +456,8 @@ public class JSchConnection implements IRemoteConnection { */ private String getCwd(IProgressMonitor monitor) { SubMonitor subMon = SubMonitor.convert(monitor, 10); - ExecCommand exec = new ExecCommand(this); try { - return exec.setCommand("pwd").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + return executeCommand("pwd", subMon.newChild(10)); //$NON-NLS-1$ } catch (RemoteConnectionException e) { // Ignore } @@ -660,8 +676,7 @@ public class JSchConnection implements IRemoteConnection { private void loadEnv(IProgressMonitor monitor) throws RemoteConnectionException { SubMonitor subMon = SubMonitor.convert(monitor, 10); - ExecCommand exec = new ExecCommand(this); - String env = exec.setCommand("printenv").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + String env = executeCommand("printenv", subMon.newChild(10)); //$NON-NLS-1$ String[] vars = env.split("\n"); //$NON-NLS-1$ for (String var : vars) { String[] kv = var.split("="); //$NON-NLS-1$ @@ -728,31 +743,30 @@ public class JSchConnection implements IRemoteConnection { fProperties.put(LINE_SEPARATOR_PROPERTY, "\n"); //$NON-NLS-1$ fProperties.put(USER_HOME_PROPERTY, getWorkingDirectory()); - ExecCommand exec = new ExecCommand(this); String osVersion; String osArch; - String osName = exec.setCommand("uname").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + String osName = executeCommand("uname", subMon.newChild(10)); //$NON-NLS-1$ if (osName.equalsIgnoreCase("Linux")) { //$NON-NLS-1$ - osArch = exec.setCommand("uname -m").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ - osVersion = exec.setCommand("uname -r").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + osArch = executeCommand("uname -m", subMon.newChild(10)); //$NON-NLS-1$ + osVersion = executeCommand("uname -r", subMon.newChild(10)); //$NON-NLS-1$ } else if (osName.equalsIgnoreCase("Darwin")) { //$NON-NLS-1$ - osName = exec.setCommand("sw_vers -productName").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ - osVersion = exec.setCommand("sw_vers -productVersion").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ - osArch = exec.setCommand("uname -m").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + osName = executeCommand("sw_vers -productName", subMon.newChild(10)); //$NON-NLS-1$ + osVersion = executeCommand("sw_vers -productVersion", subMon.newChild(10)); //$NON-NLS-1$ + osArch = executeCommand("uname -m", subMon.newChild(10)); //$NON-NLS-1$ if (osArch.equalsIgnoreCase("i386")) { //$NON-NLS-1$ - String opt = exec.setCommand("sysctl -n hw.optional.x86_64").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + String opt = executeCommand("sysctl -n hw.optional.x86_64", subMon.newChild(10)); //$NON-NLS-1$ if (opt.equals("1")) { //$NON-NLS-1$ osArch = "x86_64"; //$NON-NLS-1$ } } } else if (osName.equalsIgnoreCase("AIX")) { //$NON-NLS-1$ - osArch = exec.setCommand("uname -p").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ - osVersion = exec.setCommand("oslevel").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + osArch = executeCommand("uname -p", subMon.newChild(10)); //$NON-NLS-1$ + osVersion = executeCommand("oslevel", subMon.newChild(10)); //$NON-NLS-1$ if (osArch.equalsIgnoreCase("powerpc")) { //$NON-NLS-1$ /* Make the architecture match what Linux produces: either ppc or ppc64 */ osArch = "ppc"; //$NON-NLS-1$ /* Get Kernel type either 32-bit or 64-bit */ - String opt = exec.setCommand("prtconf -k").getResult(subMon.newChild(10)).trim(); //$NON-NLS-1$ + String opt = executeCommand("prtconf -k", subMon.newChild(10)); //$NON-NLS-1$ if (opt.indexOf("64-bit") > 0) { //$NON-NLS-1$ osArch += "64"; //$NON-NLS-1$ } diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java index b4da5dd9e47..e704712fad2 100755 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/AbstractRemoteCommand.java @@ -12,6 +12,7 @@ package org.eclipse.remote.internal.jsch.core.commands; import java.io.IOException; +import java.text.MessageFormat; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -45,6 +46,9 @@ import com.jcraft.jsch.SftpProgressMonitor; public abstract class AbstractRemoteCommand { protected static class CommandProgressMonitor implements SftpProgressMonitor { private final IProgressMonitor fMonitor; + private double fWorkPercentFactor; + private Long fMaxWorkKB; + private long fWorkToDate; public CommandProgressMonitor(IProgressMonitor monitor) { fMonitor = monitor; @@ -52,6 +56,13 @@ public abstract class AbstractRemoteCommand { @Override public boolean count(long count) { + fWorkToDate += count; + Long workToDateKB = new Long(fWorkToDate / 1024L); + Double workPercent = new Double(fWorkPercentFactor * fWorkToDate); + String subDesc = MessageFormat.format(Messages.AbstractRemoteCommand_format, + new Object[] { workToDateKB, fMaxWorkKB, workPercent }); + + fMonitor.subTask(subDesc); fMonitor.worked((int) count); return !(fMonitor.isCanceled()); } @@ -63,9 +74,10 @@ public abstract class AbstractRemoteCommand { @Override public void init(int op, String src, String dest, long max) { - String srcFile = new Path(src).lastSegment(); - String desc = srcFile; - fMonitor.beginTask(desc, (int) max); + fWorkPercentFactor = 1.0 / max; + fMaxWorkKB = new Long(max / 1024L); + fWorkToDate = 0; + fMonitor.beginTask(new Path(src).lastSegment(), (int) max); } } diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ChildInfosCommand.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ChildInfosCommand.java index 6e42d7c2d75..08af2d649ff 100644 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ChildInfosCommand.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/ChildInfosCommand.java @@ -11,7 +11,6 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.remote.core.exception.RemoteConnectionException; import org.eclipse.remote.internal.jsch.core.JSchConnection; -import org.eclipse.remote.internal.jsch.core.messages.Messages; import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.JSchException; @@ -60,7 +59,6 @@ public class ChildInfosCommand extends AbstractRemoteCommand { } }; try { - subMon.subTask(Messages.ChildInfosCommand_Get_file_attributes); return c.getResult(subMon.newChild(10)); } catch (SftpException e) { throw new RemoteConnectionException(e.getMessage()); diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/FetchInfoCommand.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/FetchInfoCommand.java index 2110075ce6b..039159bc8fd 100644 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/FetchInfoCommand.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/FetchInfoCommand.java @@ -7,7 +7,6 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.remote.core.exception.RemoteConnectionException; import org.eclipse.remote.internal.jsch.core.JSchConnection; -import org.eclipse.remote.internal.jsch.core.messages.Messages; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSchException; @@ -34,7 +33,6 @@ public class FetchInfoCommand extends AbstractRemoteCommand { }; SftpATTRS attrs; try { - subMon.subTask(Messages.FetchInfoCommand_Fetch_info); attrs = c.getResult(subMon.newChild(10)); return convertToFileInfo(fRemotePath, attrs, subMon.newChild(10)); } catch (SftpException e) { diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/MkdirCommand.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/MkdirCommand.java index 02b8fbebd35..2f2b35f93a2 100644 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/MkdirCommand.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/MkdirCommand.java @@ -6,7 +6,6 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.remote.core.exception.RemoteConnectionException; import org.eclipse.remote.internal.jsch.core.JSchConnection; -import org.eclipse.remote.internal.jsch.core.messages.Messages; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.SftpException; @@ -45,7 +44,6 @@ public class MkdirCommand extends AbstractRemoteCommand { } }; try { - subMon.subTask(Messages.MkdirCommand_Create_directory); c.getResult(subMon.newChild(10)); } catch (SftpException e) { throw new RemoteConnectionException(e.getMessage()); diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/PutInfoCommand.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/PutInfoCommand.java index 17592f2a25c..80092035b78 100644 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/PutInfoCommand.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/commands/PutInfoCommand.java @@ -7,7 +7,6 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.remote.core.exception.RemoteConnectionException; import org.eclipse.remote.internal.jsch.core.JSchConnection; -import org.eclipse.remote.internal.jsch.core.messages.Messages; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.SftpException; @@ -63,7 +62,6 @@ public class PutInfoCommand extends AbstractRemoteCommand { } }; try { - subMon.subTask(Messages.PutInfoCommand_Change_permissions); c.getResult(subMon.newChild(10)); } catch (SftpException e) { throw new RemoteConnectionException(e.getMessage()); @@ -80,7 +78,6 @@ public class PutInfoCommand extends AbstractRemoteCommand { } }; try { - subMon.subTask(Messages.PutInfoCommand_Set_modified_time); c.getResult(subMon.newChild(10)); } catch (SftpException e) { throw new RemoteConnectionException(e.getMessage()); diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/Messages.java b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/Messages.java index ed12bffd289..614e0e37a46 100755 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/Messages.java +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/Messages.java @@ -16,21 +16,18 @@ import org.eclipse.osgi.util.NLS; public class Messages extends NLS { private static final String BUNDLE_ID = "org.eclipse.remote.internal.jsch.core.messages.messages"; //$NON-NLS-1$ - public static String AbstractRemoteCommand_Execution_exception; + public static String AbstractRemoteCommand_format; + public static String AbstractRemoteCommand_Get_symlink_target; public static String AbstractRemoteCommand_Operation_cancelled_by_user; public static String AuthInfo_Authentication_message; - public static String ChildInfosCommand_Get_file_attributes; - public static String DeleteCommand_Remove_file; public static String ExecCommand_Exec_command; - public static String FetchInfoCommand_Fetch_info; - public static String GetInputStreamCommand_Get_input_stream; public static String JSchConnection_Connection_was_cancelled; - public static String JSchConnection_connectionNotOpen; + + public static String JSchConnection_Executing_command; public static String JSchConnection_remote_address_must_be_set; public static String JSchConnection_remotePort; - public static String RemoteToolsConnection_open; public static String JSchConnection_forwarding; public static String JSchConnection_Remote_host_does_not_support_sftp; public static String JSchConnection_Unable_to_open_sftp_channel; @@ -46,22 +43,6 @@ public class Messages extends NLS { public static String JschFileStore_No_remote_services_found_for_URI; public static String JschFileStore_The_file_of_name_already_exists; public static String JschFileStore_The_parent_of_directory_does_not_exist; - public static String MkdirCommand_Create_directory; - public static String PutInfoCommand_Change_permissions; - public static String PutInfoCommand_Set_modified_time; - public static String RemoteToolsFileStore_0; - public static String RemoteToolsFileStore_1; - public static String RemoteToolsFileStore_2; - public static String RemoteToolsFileStore_3; - public static String RemoteToolsFileStore_4; - public static String RemoteToolsFileStore_5; - public static String RemoteToolsFileStore_6; - public static String RemoteToolsFileStore_7; - public static String RemoteToolsFileStore_8; - public static String RemoteToolsFileStore_10; - public static String RemoteToolsFileStore_12; - public static String RemoteToolsFileStore_13; - public static String RemoteToolsFileStore_14; static { // load message values from bundle file diff --git a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/messages.properties b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/messages.properties index ce1779fbd8a..1c148bc251c 100755 --- a/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/messages.properties +++ b/bundles/org.eclipse.remote.jsch.core/src/org/eclipse/remote/internal/jsch/core/messages/messages.properties @@ -8,20 +8,16 @@ # Contributors: # IBM Corporation - initial implementation ############################################################################### +AbstractRemoteCommand_format={0,number,integer} KB of {1,number,integer} KB complete ({2,number,percent}) AbstractRemoteCommand_Get_symlink_target=Get symlink target AbstractRemoteCommand_Operation_cancelled_by_user=Operation cancelled by user -AbstractRemoteCommand_Execution_exception=Execution exception AuthInfo_Authentication_message=Authentication Message -ChildInfosCommand_Get_file_attributes=Get file attributes -DeleteCommand_Remove_file=Remove file ExecCommand_Exec_command=Executing command "{0}" -FetchInfoCommand_Fetch_info=Fetch info -GetInputStreamCommand_Get_input_stream=Get input stream JSchConnection_Connection_was_cancelled=Connection was cancelled JSchConnection_connectionNotOpen=Connection is not open +JSchConnection_Executing_command=Executing command "{0}" JSchConnection_remote_address_must_be_set=Remote address must be set before opening connection JSchConnection_remotePort=Could not allocate remote port -RemoteToolsConnection_open=Opening connection... JSchConnection_forwarding=Setting up remote forwarding JSchConnection_Remote_host_does_not_support_sftp=Remote host does not support sftp. Remote functionality requires sftp to be enabled JSchConnection_Unable_to_open_sftp_channel=Unable to open sftp channel: check sftp is enabled on remote host @@ -37,19 +33,3 @@ JschFileStore_Is_a_directory={0} is a directory JschFileStore_No_remote_services_found_for_URI=No remote services found for URI: "{0}" JschFileStore_The_file_of_name_already_exists=A file of name {0} already exists JschFileStore_The_parent_of_directory_does_not_exist=The parent of directory {0} does not exist -MkdirCommand_Create_directory=Create directory -PutInfoCommand_Change_permissions=Change permissions -PutInfoCommand_Set_modified_time=Set modified time -RemoteToolsFileStore_0=Could not delete file {0} -RemoteToolsFileStore_1=The parent of directory {0} does not exist -RemoteToolsFileStore_2=The directory {0} could not be created -RemoteToolsFileStore_3={0} is a directory -RemoteToolsFileStore_4=Could not get input stream for {0} -RemoteToolsFileStore_5=Service failed to initialize -RemoteToolsFileStore_6=Could not open output stream -RemoteToolsFileStore_7=Invalid URI format -RemoteToolsFileStore_8=Invalid connection name: {0} -RemoteToolsFileStore_10=Unable to open connection: {0} -RemoteToolsFileStore_12=Operation was cancelled by user -RemoteToolsFileStore_13=A file of name {0} already exists -RemoteToolsFileStore_14=File doesn't exist: {0}