You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ivy-commits@incubator.apache.org by gs...@apache.org on 2007/06/23 22:42:53 UTC

svn commit: r550132 - in /incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh: AbstractSshBasedRepository.java Scp.java SshCache.java SshResource.java

Author: gscokart
Date: Sat Jun 23 15:42:52 2007
New Revision: 550132

URL: http://svn.apache.org/viewvc?view=rev&rev=550132
Log:
clean code

Modified:
    incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/AbstractSshBasedRepository.java
    incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/Scp.java
    incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshCache.java
    incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshResource.java

Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/AbstractSshBasedRepository.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/AbstractSshBasedRepository.java?view=diff&rev=550132&r1=550131&r2=550132
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/AbstractSshBasedRepository.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/AbstractSshBasedRepository.java Sat Jun 23 15:42:52 2007
@@ -63,8 +63,9 @@
         String user = getUser();
         String userPassword = getUserPassword();
         if (uri != null && uri.getScheme() != null) {
-            if (uri.getHost() != null)
+            if (uri.getHost() != null) {
                 host = uri.getHost();
+            }
             if (uri.getPort() != -1) {
                 port = uri.getPort();
             }
@@ -92,15 +93,20 @@
     private URI parseURI(String source) {
         try {
             URI uri = new URI(source);
-            if (uri.getScheme() != null && !uri.getScheme().toLowerCase(Locale.US).equals(getRepositoryScheme().toLowerCase(Locale.US)))
+            if (uri.getScheme() != null && !uri.getScheme().toLowerCase(Locale.US).equals(
+                    getRepositoryScheme().toLowerCase(Locale.US))) {
                 throw new URISyntaxException(source, "Wrong scheme in URI. Expected "
                         + getRepositoryScheme() + " as scheme!");
-            if (uri.getHost() == null && getHost() == null)
+            }
+            if (uri.getHost() == null && getHost() == null) {
                 throw new URISyntaxException(source, "Missing host in URI or in resolver");
-            if (uri.getPath() == null)
+            }
+            if (uri.getPath() == null) {
                 throw new URISyntaxException(source, "Missing path in URI");
-            if (uri.getUserInfo() == null && getUser() == null)
+            }
+            if (uri.getUserInfo() == null && getUser() == null) {
                 throw new URISyntaxException(source, "Missing username in URI or in resolver");
+            }
             return uri;
         } catch (URISyntaxException e) {
             Message.error(e.getMessage());

Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/Scp.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/Scp.java?view=diff&rev=550132&r1=550131&r2=550132
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/Scp.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/Scp.java Sat Jun 23 15:42:52 2007
@@ -40,7 +40,7 @@
  */
 
 public class Scp {
-    Session session;
+    private Session session;
 
     public class FileInfo {
         private String filename;
@@ -96,25 +96,30 @@
     }
 
     public Scp(Session session) {
-        if (session == null)
+        if (session == null) {
             throw new IllegalArgumentException("Cannot accept null argument!");
+        }
         this.session = session;
     }
 
     private void readResponse(InputStream is) throws IOException, RemoteScpException {
         int c = is.read();
 
-        if (c == 0)
+        if (c == 0) {
             return;
+        }
 
-        if (c == -1)
+        if (c == -1) {
             throw new RemoteScpException("Remote scp terminated unexpectedly.");
+        }
 
-        if ((c != 1) && (c != 2))
+        if ((c != 1) && (c != 2)) {
             throw new RemoteScpException("Remote scp sent illegal error code.");
+        }
 
-        if (c == 2)
+        if (c == 2) {
             throw new RemoteScpException("Remote scp terminated with error.");
+        }
 
         String err = receiveLine(is);
         throw new RemoteScpException("Remote scp terminated with error (" + err + ").");
@@ -128,16 +133,19 @@
              * This is a random limit - if your path names are longer, then adjust it
              */
 
-            if (sb.length() > 8192)
+            if (sb.length() > 8192) {
                 throw new RemoteScpException("Remote scp sent a too long line");
+            }
 
             int c = is.read();
 
-            if (c < 0)
+            if (c < 0) {
                 throw new RemoteScpException("Remote scp terminated unexpectedly.");
+            }
 
-            if (c == '\n')
+            if (c == '\n') {
                 break;
+            }
 
             sb.append((char) c);
 
@@ -150,82 +158,93 @@
 
         long len;
 
-        if (line.length() < 8)
+        if (line.length() < 8) {
             throw new RemoteScpException(
                     "Malformed C line sent by remote SCP binary, line too short.");
+        }
 
-        if ((line.charAt(4) != ' ') || (line.charAt(5) == ' '))
+        if ((line.charAt(4) != ' ') || (line.charAt(5) == ' ')) {
             throw new RemoteScpException("Malformed C line sent by remote SCP binary.");
+        }
 
-        int length_name_sep = line.indexOf(' ', 5);
+        int lengthNameSep = line.indexOf(' ', 5);
 
-        if (length_name_sep == -1)
+        if (lengthNameSep == -1) {
             throw new RemoteScpException("Malformed C line sent by remote SCP binary.");
+        }
 
-        String length_substring = line.substring(5, length_name_sep);
-        String name_substring = line.substring(length_name_sep + 1);
+        String lengthSubstring = line.substring(5, lengthNameSep);
+        String nameSubstring = line.substring(lengthNameSep + 1);
 
-        if ((length_substring.length() <= 0) || (name_substring.length() <= 0))
+        if ((lengthSubstring.length() <= 0) || (nameSubstring.length() <= 0)) {
             throw new RemoteScpException("Malformed C line sent by remote SCP binary.");
+        }
 
-        if ((6 + length_substring.length() + name_substring.length()) != line.length())
+        if ((6 + lengthSubstring.length() + nameSubstring.length()) != line.length()) {
             throw new RemoteScpException("Malformed C line sent by remote SCP binary.");
+        }
 
         try {
-            len = Long.parseLong(length_substring);
+            len = Long.parseLong(lengthSubstring);
         } catch (NumberFormatException e) {
             throw new RemoteScpException(
                     "Malformed C line sent by remote SCP binary, cannot parse file length.");
         }
 
-        if (len < 0)
+        if (len < 0) {
             throw new RemoteScpException(
                     "Malformed C line sent by remote SCP binary, illegal file length.");
+        }
 
         fileInfo.setLength(len);
-        fileInfo.setFilename(name_substring);
+        fileInfo.setFilename(nameSubstring);
     }
 
     private void parseTLine(String line, FileInfo fileInfo) throws RemoteScpException {
         /* Minimum line: "0 0 0 0" ---> 8 chars */
 
         long modtime;
-        long first_msec;
+        long firstMsec;
         long atime;
-        long second_msec;
+        long secondMsec;
 
-        if (line.length() < 8)
+        if (line.length() < 8) {
             throw new RemoteScpException(
                     "Malformed T line sent by remote SCP binary, line too short.");
+        }
 
-        int first_msec_begin = line.indexOf(" ") + 1;
-        if (first_msec_begin == 0 || first_msec_begin >= line.length())
+        int firstMsecBegin = line.indexOf(" ") + 1;
+        if (firstMsecBegin == 0 || firstMsecBegin >= line.length()) {
             throw new RemoteScpException(
                     "Malformed T line sent by remote SCP binary, line not enough data.");
+        }
 
-        int atime_begin = line.indexOf(" ", first_msec_begin + 1) + 1;
-        if (atime_begin == 0 || atime_begin >= line.length())
+        int atimeBegin = line.indexOf(" ", firstMsecBegin + 1) + 1;
+        if (atimeBegin == 0 || atimeBegin >= line.length()) {
             throw new RemoteScpException(
                     "Malformed T line sent by remote SCP binary, line not enough data.");
+        }
 
-        int second_msec_begin = line.indexOf(" ", atime_begin + 1) + 1;
-        if (second_msec_begin == 0 || second_msec_begin >= line.length())
+        int secondMsecBegin = line.indexOf(" ", atimeBegin + 1) + 1;
+        if (secondMsecBegin == 0 || secondMsecBegin >= line.length()) {
             throw new RemoteScpException(
                     "Malformed T line sent by remote SCP binary, line not enough data.");
+        }
 
         try {
-            modtime = Long.parseLong(line.substring(0, first_msec_begin - 1));
-            first_msec = Long.parseLong(line.substring(first_msec_begin, atime_begin - 1));
-            atime = Long.parseLong(line.substring(atime_begin, second_msec_begin - 1));
-            second_msec = Long.parseLong(line.substring(second_msec_begin));
+            modtime = Long.parseLong(line.substring(0, firstMsecBegin - 1));
+            firstMsec = Long.parseLong(line.substring(firstMsecBegin, atimeBegin - 1));
+            atime = Long.parseLong(line.substring(atimeBegin, secondMsecBegin - 1));
+            secondMsec = Long.parseLong(line.substring(secondMsecBegin));
         } catch (NumberFormatException e) {
             throw new RemoteScpException(
                     "Malformed C line sent by remote SCP binary, cannot parse file length.");
         }
 
-        if (modtime < 0 || first_msec < 0 || atime < 0 || second_msec < 0)
+        if (modtime < 0 || firstMsec < 0 || atime < 0 || secondMsec < 0) {
             throw new RemoteScpException(
                     "Malformed C line sent by remote SCP binary, illegal file length.");
+        }
 
         fileInfo.setLastModified(modtime);
     }
@@ -236,10 +255,11 @@
         InputStream is = new BufferedInputStream(channel.getInputStream(), 512);
 
         try {
-            if (channel.isConnected())
+            if (channel.isConnected()) {
                 channel.start();
-            else
+            } else {
                 channel.connect();
+            }
         } catch (JSchException e1) {
             throw (IOException) new IOException("Channel connection problems").initCause(e1);
         }
@@ -271,10 +291,11 @@
         InputStream is = new BufferedInputStream(channel.getInputStream(), 512);
 
         try {
-            if (channel.isConnected())
+            if (channel.isConnected()) {
                 channel.start();
-            else
+            } else {
                 channel.connect();
+            }
         } catch (JSchException e1) {
             throw (IOException) new IOException("Channel connection problems").initCause(e1);
         }
@@ -298,13 +319,14 @@
 
             while (remain > 0) {
                 int trans;
-                if (remain > buffer.length)
+                if (remain > buffer.length) {
                     trans = buffer.length;
-                else
+                } else {
                     trans = (int) remain;
-
-                if (fis.read(buffer, 0, trans) != trans)
+                }
+                if (fis.read(buffer, 0, trans) != trans) {
                     throw new IOException("Cannot read enough from local file " + localFile);
+                }
 
                 os.write(buffer, 0, trans);
 
@@ -350,10 +372,11 @@
         OutputStream os = channel.getOutputStream();
         InputStream is = channel.getInputStream();
         try {
-            if (channel.isConnected())
+            if (channel.isConnected()) {
                 channel.start();
-            else
+            } else {
                 channel.connect();
+            }
         } catch (JSchException e1) {
             throw (IOException) new IOException("Channel connection problems").initCause(e1);
         }
@@ -364,8 +387,9 @@
 
         while (true) {
             int c = is.read();
-            if (c < 0)
+            if (c < 0) {
                 throw new RemoteScpException("Remote scp terminated unexpectedly.");
+            }
 
             String line = receiveLine(is);
 
@@ -375,8 +399,9 @@
                 os.flush();
                 continue;
             }
-            if ((c == 1) || (c == 2))
+            if ((c == 1) || (c == 2)) {
                 throw new RemoteScpException("Remote SCP error: " + line);
+            }
 
             if (c == 'C') {
                 parseCLine(line, fileInfo);
@@ -394,27 +419,28 @@
 
                 while (remain > 0) {
                     int trans;
-                    if (remain > buffer.length)
+                    if (remain > buffer.length) {
                         trans = buffer.length;
-                    else
+                    } else {
                         trans = (int) remain;
+                    }
 
-                    int this_time_received = is.read(buffer, 0, trans);
+                    int thisTimeReceived = is.read(buffer, 0, trans);
 
-                    if (this_time_received < 0) {
+                    if (thisTimeReceived < 0) {
                         throw new IOException("Remote scp terminated connection unexpectedly");
                     }
 
-                    targetStream.write(buffer, 0, this_time_received);
+                    targetStream.write(buffer, 0, thisTimeReceived);
 
-                    remain -= this_time_received;
+                    remain -= thisTimeReceived;
                 }
 
                 targetStream.close();
             } catch (IOException e) {
-                if (targetStream != null)
+                if (targetStream != null) {
                     targetStream.close();
-
+                }
                 throw (e);
             }
 
@@ -488,15 +514,19 @@
             throws IOException, RemoteScpException {
         ChannelExec channel = null;
 
-        if ((remoteFileName == null) || (mode == null))
+        if ((remoteFileName == null) || (mode == null)) {
             throw new IllegalArgumentException("Null argument.");
+        }
 
-        if (mode.length() != 4)
+        if (mode.length() != 4) {
             throw new IllegalArgumentException("Invalid mode.");
+        }
 
-        for (int i = 0; i < mode.length(); i++)
-            if (Character.isDigit(mode.charAt(i)) == false)
+        for (int i = 0; i < mode.length(); i++) {
+            if (!Character.isDigit(mode.charAt(i))) {
                 throw new IllegalArgumentException("Invalid mode.");
+            }
+        }
 
         String cmd = "scp -t ";
         if (remoteTargetDirectory != null && remoteTargetDirectory.length() > 0) {
@@ -509,8 +539,9 @@
             sendBytes(channel, data, remoteFileName, mode);
             // channel.disconnect();
         } catch (JSchException e) {
-            if (channel != null)
+            if (channel != null) {
                 channel.disconnect();
+            }
             throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
                     .initCause(e);
         }
@@ -547,15 +578,19 @@
             throws IOException, RemoteScpException {
         ChannelExec channel = null;
 
-        if ((localFile == null) || (remoteTargetName == null) || (mode == null))
+        if ((localFile == null) || (remoteTargetName == null) || (mode == null)) {
             throw new IllegalArgumentException("Null argument.");
+        }
 
-        if (mode.length() != 4)
+        if (mode.length() != 4) {
             throw new IllegalArgumentException("Invalid mode.");
+        }
 
-        for (int i = 0; i < mode.length(); i++)
-            if (Character.isDigit(mode.charAt(i)) == false)
+        for (int i = 0; i < mode.length(); i++) {
+            if (!Character.isDigit(mode.charAt(i))) {
                 throw new IllegalArgumentException("Invalid mode.");
+            }
+        }
 
         String cmd = "scp -t ";
         if (remoteTargetDir != null && remoteTargetDir.length() > 0) {
@@ -568,8 +603,9 @@
             sendFile(channel, localFile, remoteTargetName, mode);
             channel.disconnect();
         } catch (JSchException e) {
-            if (channel != null)
+            if (channel != null) {
                 channel.disconnect();
+            }
             throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
                     .initCause(e);
         }
@@ -609,8 +645,9 @@
             RemoteScpException {
         ChannelExec channel = null;
 
-        if ((remoteFile == null) || (localTarget == null))
+        if ((remoteFile == null) || (localTarget == null)) {
             throw new IllegalArgumentException("Null argument.");
+        }
 
         String cmd = "scp -p -f " + remoteFile;
 
@@ -620,8 +657,9 @@
             receiveStream(channel, remoteFile, localTarget);
             channel.disconnect();
         } catch (JSchException e) {
-            if (channel != null)
+            if (channel != null) {
                 channel.disconnect();
+            }
             throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
                     .initCause(e);
         }
@@ -642,8 +680,9 @@
         ChannelExec channel = null;
         FileInfo fileInfo = null;
 
-        if (remoteFile == null)
+        if (remoteFile == null) {
             throw new IllegalArgumentException("Null argument.");
+        }
 
         String cmd = "scp -p -f \"" + remoteFile + "\"";
 
@@ -656,8 +695,9 @@
             throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
                     .initCause(e);
         } finally {
-            if (channel != null)
+            if (channel != null) {
                 channel.disconnect();
+            }
         }
         return fileInfo;
     }

Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshCache.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshCache.java?view=diff&rev=550132&r1=550131&r2=550132
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshCache.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshCache.java Sat Jun 23 15:42:52 2007
@@ -42,8 +42,10 @@
  * connecting user / host / port two maps are used to find cache entries one map is using the above
  * keys, the other uses the session itself
  */
-public class SshCache {
+public final class SshCache {
 
+    private static final int SSH_DEFAULT_PORT = 22;
+    
     private SshCache() {
     };
 
@@ -62,7 +64,7 @@
 
         private String user = null;
 
-        private int port = 22;
+        private int port = SSH_DEFAULT_PORT;
 
         /**
          * @return the host
@@ -105,8 +107,9 @@
          *            to attach
          */
         public void setChannelSftp(ChannelSftp newChannel) {
-            if (channelSftp != null && newChannel != null)
+            if (channelSftp != null && newChannel != null) {
                 throw new IllegalStateException("Only one sftp channelSftp per session allowed");
+            }
             this.channelSftp = newChannel;
         }
 
@@ -175,9 +178,11 @@
      */
     private static String createCacheKey(String user, String host, int port) {
         String portToUse = "22";
-        if (port != -1 && port != 22)
+        if (port != -1 && port != SSH_DEFAULT_PORT) {
             portToUse = Integer.toString(port);
-        return user.toLowerCase(Locale.US).trim() + "@" + host.toLowerCase(Locale.US).trim() + ":" + portToUse;
+        }
+        return user.toLowerCase(Locale.US).trim() + "@" 
+                + host.toLowerCase(Locale.US).trim() + ":" + portToUse;
     }
 
     /**
@@ -201,14 +206,15 @@
      *            of the session
      * @param port
      *            of the session
-     * @param session
+     * @param newSession
      *            Session to save
      */
     private void setSession(String user, String host, int port, Session newSession) {
         Entry entry = (Entry) uriCacheMap.get(createCacheKey(user, host, port));
         Session oldSession = null;
-        if (entry != null)
+        if (entry != null) {
             oldSession = entry.getSession();
+        }
         if (oldSession != null && !oldSession.equals(newSession) && oldSession.isConnected()) {
             entry.releaseChannelSftp();
             String oldhost = oldSession.getHost();
@@ -218,8 +224,9 @@
         }
         if ((newSession == null) && (entry != null)) {
             uriCacheMap.remove(createCacheKey(user, host, port));
-            if (entry.getSession() != null)
+            if (entry.getSession() != null) {
                 sessionCacheMap.remove(entry.getSession());
+            }
         } else {
             Entry newEntry = new Entry(newSession, user, host, port);
             uriCacheMap.put(createCacheKey(user, host, port), newEntry);
@@ -235,8 +242,9 @@
      */
     public void clearSession(Session session) {
         Entry entry = (Entry) sessionCacheMap.get(session);
-        if (entry != null)
+        if (entry != null) {
             setSession(entry.getUser(), entry.getHost(), entry.getPort(), null);
+        }
     }
 
     /**
@@ -269,8 +277,9 @@
      */
     public void attachChannelSftp(Session session, ChannelSftp channel) {
         Entry entry = getCacheEntry(session);
-        if (entry == null)
+        if (entry == null) {
             throw new IllegalArgumentException("No entry for " + session + " in the cache");
+        }
         entry.setChannelSftp(channel);
     }
 
@@ -297,20 +306,22 @@
             File pemFile, String pemPassword, File passFile) throws IOException {
         Entry entry = getCacheEntry(username, host, port);
         Session session = null;
-        if (entry != null)
+        if (entry != null) {
             session = entry.getSession();
+        }
         if (session == null || !session.isConnected()) {
             Message.verbose(":: SSH :: connecting to " + host + "...");
             try {
                 JSch jsch = new JSch();
-                if (port != -1)
+                if (port != -1) {
                     session = jsch.getSession(username, host, port);
-                else
+                } else {
                     session = jsch.getSession(username, host);
+                }
                 if (pemFile != null) {
                     jsch.addIdentity(pemFile.getAbsolutePath(), pemPassword);
                 }
-                session.setUserInfo(new cfUserInfo(host, username, userPassword, pemFile,
+                session.setUserInfo(new CfUserInfo(host, username, userPassword, pemFile,
                         pemPassword, passFile));
                 session.connect();
                 Message.verbose(":: SSH :: connected to " + host + "!");
@@ -330,7 +341,7 @@
     /**
      * feeds in password silently into JSch
      */
-    private static class cfUserInfo implements UserInfo {
+    private static class CfUserInfo implements UserInfo {
 
         private String userPassword;
 
@@ -344,7 +355,7 @@
 
         private final File passfile;
 
-        public cfUserInfo(String host, String userName, String userPassword, File pemFile,
+        public CfUserInfo(String host, String userName, String userPassword, File pemFile,
                 String pemPassword, File passfile) {
             this.userPassword = userPassword;
             this.pemPassword = pemPassword;

Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshResource.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshResource.java?view=diff&rev=550132&r1=550131&r2=550132
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshResource.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshResource.java Sat Jun 23 15:42:52 2007
@@ -66,8 +66,9 @@
      * @see org.apache.ivy.repository.Resource#exists()
      */
     public boolean exists() {
-        if (!resolved)
+        if (!resolved) {
             resolve();
+        }
         return bExists;
     }
 
@@ -77,8 +78,9 @@
      * @see org.apache.ivy.repository.Resource#getContentLength()
      */
     public long getContentLength() {
-        if (!resolved)
+        if (!resolved) {
             resolve();
+        }
         return len;
     }
 
@@ -88,8 +90,9 @@
      * @see org.apache.ivy.repository.Resource#getLastModified()
      */
     public long getLastModified() {
-        if (!resolved)
+        if (!resolved) {
             resolve();
+        }
         return lastModified;
     }