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 xa...@apache.org on 2007/06/05 12:02:43 UTC
svn commit: r544459 [19/36] - in /incubator/ivy/core/trunk:
src/java/org/apache/ivy/ src/java/org/apache/ivy/ant/
src/java/org/apache/ivy/core/ src/java/org/apache/ivy/core/cache/
src/java/org/apache/ivy/core/check/ src/java/org/apache/ivy/core/deliver...
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=544459&r1=544458&r2=544459
==============================================================================
--- 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 Tue Jun 5 05:02:27 2007
@@ -27,15 +27,20 @@
import com.jcraft.jsch.Session;
-
public abstract class AbstractSshBasedRepository extends AbstractRepository {
private File keyFile = null;
+
private File passFile = null;
+
private String userPassword = null;
+
private String keyFilePassword = null;
+
private String user = "guest";
+
private String host = null;
+
private int port = -1;
public AbstractSshBasedRepository() {
@@ -43,10 +48,11 @@
}
/**
- * get a new session using the default attributes
- * if the given String is a full uri, use the data from the uri
- * instead
- * @param pathOrUri might be just a path or a full ssh or sftp uri
+ * get a new session using the default attributes if the given String is a full uri, use the
+ * data from the uri instead
+ *
+ * @param pathOrUri
+ * might be just a path or a full ssh or sftp uri
* @return matching Session
*/
protected Session getSession(String pathOrUri) throws IOException {
@@ -55,47 +61,45 @@
int port = getPort();
String user = getUser();
String userPassword = getUserPassword();
- if(uri != null && uri.getScheme() != null) {
- if(uri.getHost() != null )
+ if (uri != null && uri.getScheme() != null) {
+ if (uri.getHost() != null)
host = uri.getHost();
- if(uri.getPort() != -1) {
+ if (uri.getPort() != -1) {
port = uri.getPort();
}
- if(uri.getUserInfo() != null) {
+ if (uri.getUserInfo() != null) {
String userInfo = uri.getUserInfo();
- if(userInfo.indexOf(":") == -1) {
+ if (userInfo.indexOf(":") == -1) {
user = userInfo;
} else {
user = userInfo.substring(0, userInfo.indexOf(":"));
- userPassword = userInfo.substring(userInfo.indexOf(":")+1);
+ userPassword = userInfo.substring(userInfo.indexOf(":") + 1);
}
}
}
- return SshCache.getInstance().getSession(host,
- port,
- user,
- userPassword,
- getKeyFile(),
- getKeyFilePassword(),
- getPassFile());
+ return SshCache.getInstance().getSession(host, port, user, userPassword, getKeyFile(),
+ getKeyFilePassword(), getPassFile());
}
-
+
/**
* Just check the uri for sanity
- * @param source String of the uri
+ *
+ * @param source
+ * String of the uri
* @return URI object of the String or null
*/
private URI parseURI(String source) {
try {
URI uri = new URI(source);
- if(uri.getScheme() != null && !uri.getScheme().equalsIgnoreCase(getRepositoryScheme()))
- throw new URISyntaxException(source,"Wrong scheme in URI. Expected "+getRepositoryScheme()+" as scheme!");
- if(uri.getHost() == null && getHost() == null)
- throw new URISyntaxException(source,"Missing host in URI or in resolver");
- if(uri.getPath() == null)
- throw new URISyntaxException(source,"Missing path in URI");
- if(uri.getUserInfo() == null && getUser() == null)
- throw new URISyntaxException(source,"Missing username in URI or in resolver");
+ if (uri.getScheme() != null && !uri.getScheme().equalsIgnoreCase(getRepositoryScheme()))
+ throw new URISyntaxException(source, "Wrong scheme in URI. Expected "
+ + getRepositoryScheme() + " as scheme!");
+ if (uri.getHost() == null && getHost() == null)
+ throw new URISyntaxException(source, "Missing host in URI or in resolver");
+ if (uri.getPath() == null)
+ throw new URISyntaxException(source, "Missing path in URI");
+ 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());
@@ -107,17 +111,22 @@
/**
* closes the session and remove it from the cache (eg. on case of errors)
- * @param uri key for the cache
- * @param conn to release
+ *
+ * @param uri
+ * key for the cache
+ * @param conn
+ * to release
*/
- protected void releaseSession(Session session,String pathOrUri) {
+ protected void releaseSession(Session session, String pathOrUri) {
session.disconnect();
SshCache.getInstance().clearSession(session);
}
/**
* set the default user to use for the connection if no user is given or a PEM file is used
- * @param user to use
+ *
+ * @param user
+ * to use
*/
public void setUser(String user) {
this.user = user;
@@ -131,19 +140,21 @@
}
/**
- * Sets the full file path to use for accessing a PEM key file
- * @param filePath fully qualified name
+ * Sets the full file path to use for accessing a PEM key file
+ *
+ * @param filePath
+ * fully qualified name
*/
public void setKeyFile(File filePath) {
this.keyFile = filePath;
- if(!keyFile.exists()) {
- Message.warn("Pemfile "+keyFile.getAbsolutePath()+" doesn't exist.");
+ if (!keyFile.exists()) {
+ Message.warn("Pemfile " + keyFile.getAbsolutePath() + " doesn't exist.");
keyFile = null;
- } else if(!keyFile.canRead()) {
- Message.warn("Pemfile "+keyFile.getAbsolutePath()+" not readable.");
+ } else if (!keyFile.canRead()) {
+ Message.warn("Pemfile " + keyFile.getAbsolutePath() + " not readable.");
keyFile = null;
} else {
- Message.debug("Using "+keyFile.getAbsolutePath()+" as keyfile.");
+ Message.debug("Using " + keyFile.getAbsolutePath() + " as keyfile.");
}
}
@@ -155,7 +166,8 @@
}
/**
- * @param user password to use for user/password authentication
+ * @param user
+ * password to use for user/password authentication
*/
public void setUserPassword(String password) {
this.userPassword = password;
@@ -167,13 +179,15 @@
public String getKeyFilePassword() {
return keyFilePassword;
}
-
+
/**
- * @param keyFilePassword sets password for public key based authentication
+ * @param keyFilePassword
+ * sets password for public key based authentication
*/
public void setKeyFilePassword(String keyFilePassword) {
this.keyFilePassword = keyFilePassword;
}
+
/**
* @return the user password
*/
@@ -187,8 +201,10 @@
public String getHost() {
return host;
}
+
/**
- * @param host the host to set
+ * @param host
+ * the host to set
*/
public void setHost(String host) {
this.host = host;
@@ -202,21 +218,23 @@
}
/**
- * @param port the port to set
+ * @param port
+ * the port to set
*/
public void setPort(int port) {
this.port = port;
}
-
+
/**
- * @param passFile the passfile to set
+ * @param passFile
+ * the passfile to set
*/
public void setPassFile(File passFile) {
this.passFile = passFile;
}
-
+
/**
- * @return the passFile
+ * @return the passFile
*/
public File getPassFile() {
return passFile;
Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/RemoteScpException.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/RemoteScpException.java?view=diff&rev=544459&r1=544458&r2=544459
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/RemoteScpException.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/RemoteScpException.java Tue Jun 5 05:02:27 2007
@@ -18,13 +18,15 @@
package org.apache.ivy.plugins.repository.ssh;
/**
- * This exception will be used for Remote SCP Exceptions (failures on the target system, no connetion probs)
+ * This exception will be used for Remote SCP Exceptions (failures on the target system, no
+ * connetion probs)
*/
public class RemoteScpException extends Exception {
private static final long serialVersionUID = 3107198655563736600L;
- public RemoteScpException() {}
+ public RemoteScpException() {
+ }
/**
* @param message
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=544459&r1=544458&r2=544459
==============================================================================
--- 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 Tue Jun 5 05:02:27 2007
@@ -32,13 +32,11 @@
import com.jcraft.jsch.Session;
/**
- * This class is using the scp client to transfer data and information for the
- * repository. It is based on the SCPClient from the ganymed ssh library from
- * Christian Plattner. To minimize the dependency to the ssh library and because
- * I needed some additional functionality, I decided to copy'n'paste the single
- * class rather than to inherit or delegate it somehow. Nevertheless credit
+ * This class is using the scp client to transfer data and information for the repository. It is
+ * based on the SCPClient from the ganymed ssh library from Christian Plattner. To minimize the
+ * dependency to the ssh library and because I needed some additional functionality, I decided to
+ * copy'n'paste the single class rather than to inherit or delegate it somehow. Nevertheless credit
* should go to the original author.
- *
*/
public class Scp {
@@ -46,38 +44,49 @@
public class FileInfo {
private String filename;
+
private long length;
+
private long lastModified;
+
/**
- * @param filename The filename to set.
+ * @param filename
+ * The filename to set.
*/
public void setFilename(String filename) {
this.filename = filename;
}
+
/**
* @return Returns the filename.
*/
public String getFilename() {
return filename;
}
+
/**
- * @param length The length to set.
+ * @param length
+ * The length to set.
*/
public void setLength(long length) {
this.length = length;
}
+
/**
* @return Returns the length.
*/
public long getLength() {
return length;
}
+
/**
- * @param lastModified The lastModified to set.
+ * @param lastModified
+ * The lastModified to set.
*/
public void setLastModified(long lastModified) {
this.lastModified = lastModified;
}
+
/**
* @return Returns the lastModified.
*/
@@ -111,13 +120,12 @@
throw new RemoteScpException("Remote scp terminated with error (" + err + ").");
}
- private String receiveLine(InputStream is) throws IOException,RemoteScpException {
+ private String receiveLine(InputStream is) throws IOException, RemoteScpException {
StringBuffer sb = new StringBuffer(30);
while (true) {
/*
- * This is a random limit - if your path names are longer, then
- * adjust it
+ * This is a random limit - if your path names are longer, then adjust it
*/
if (sb.length() > 8192)
@@ -160,8 +168,7 @@
if ((length_substring.length() <= 0) || (name_substring.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 + length_substring.length() + name_substring.length()) != line.length())
throw new RemoteScpException("Malformed C line sent by remote SCP binary.");
try {
@@ -178,7 +185,7 @@
fileInfo.setLength(len);
fileInfo.setFilename(name_substring);
}
-
+
private void parseTLine(String line, FileInfo fileInfo) throws RemoteScpException {
/* Minimum line: "0 0 0 0" ---> 8 chars */
@@ -191,26 +198,26 @@
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 first_msec_begin = line.indexOf(" ") + 1;
+ if (first_msec_begin == 0 || first_msec_begin >= 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())
+ "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())
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())
+ "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())
throw new RemoteScpException(
- "Malformed T line sent by remote SCP binary, line not enough data.");
-
+ "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, 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));
} catch (NumberFormatException e) {
throw new RemoteScpException(
"Malformed C line sent by remote SCP binary, cannot parse file length.");
@@ -222,21 +229,21 @@
fileInfo.setLastModified(modtime);
}
-
- private void sendBytes(Channel channel, byte[] data, String fileName,
- String mode) throws IOException,RemoteScpException {
+
+ private void sendBytes(Channel channel, byte[] data, String fileName, String mode)
+ throws IOException, RemoteScpException {
OutputStream os = channel.getOutputStream();
InputStream is = new BufferedInputStream(channel.getInputStream(), 512);
try {
- if(channel.isConnected())
+ if (channel.isConnected())
channel.start();
else
channel.connect();
} catch (JSchException e1) {
throw (IOException) new IOException("Channel connection problems").initCause(e1);
}
-
+
readResponse(is);
String cline = "C" + mode + " " + data.length + " " + fileName + "\n";
@@ -264,7 +271,7 @@
InputStream is = new BufferedInputStream(channel.getInputStream(), 512);
try {
- if(channel.isConnected())
+ if (channel.isConnected())
channel.start();
else
channel.connect();
@@ -297,9 +304,7 @@
trans = (int) remain;
if (fis.read(buffer, 0, trans) != trans)
- throw new IOException(
- "Cannot read enough from local file "
- + localFile);
+ throw new IOException("Cannot read enough from local file " + localFile);
os.write(buffer, 0, trans);
@@ -325,21 +330,27 @@
/**
* Receive a file via scp and store it in a stream
- * @param channel ssh channel to use
- * @param file to receive from remote
- * @param target to store file into (if null, get only file info)
+ *
+ * @param channel
+ * ssh channel to use
+ * @param file
+ * to receive from remote
+ * @param target
+ * to store file into (if null, get only file info)
* @return file information of the file we received
- * @throws IOException in case of network or protocol trouble
- * @throws RemoteScpException in case of problems on the target system (connection is fine)
+ * @throws IOException
+ * in case of network or protocol trouble
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection is fine)
*/
- private FileInfo receiveStream(Channel channel, String file, OutputStream targetStream)
- throws IOException,RemoteScpException {
+ private FileInfo receiveStream(Channel channel, String file, OutputStream targetStream)
+ throws IOException, RemoteScpException {
byte[] buffer = new byte[8192];
OutputStream os = channel.getOutputStream();
InputStream is = channel.getInputStream();
try {
- if(channel.isConnected())
+ if (channel.isConnected())
channel.start();
else
channel.connect();
@@ -359,7 +370,7 @@
String line = receiveLine(is);
if (c == 'T') {
- parseTLine(line,fileInfo);
+ parseTLine(line, fileInfo);
os.write(0x0);
os.flush();
continue;
@@ -368,48 +379,47 @@
throw new RemoteScpException("Remote SCP error: " + line);
if (c == 'C') {
- parseCLine(line,fileInfo);
+ parseCLine(line, fileInfo);
break;
}
throw new RemoteScpException("Remote SCP error: " + ((char) c) + line);
}
- if(targetStream != null) {
-
+ if (targetStream != null) {
+
os.write(0x0);
os.flush();
-
+
try {
long remain = fileInfo.getLength();
-
+
while (remain > 0) {
int trans;
if (remain > buffer.length)
trans = buffer.length;
else
trans = (int) remain;
-
+
int this_time_received = is.read(buffer, 0, trans);
-
+
if (this_time_received < 0) {
- throw new IOException(
- "Remote scp terminated connection unexpectedly");
+ throw new IOException("Remote scp terminated connection unexpectedly");
}
-
+
targetStream.write(buffer, 0, this_time_received);
-
+
remain -= this_time_received;
}
-
+
targetStream.close();
} catch (IOException e) {
if (targetStream != null)
targetStream.close();
-
+
throw (e);
}
readResponse(is);
-
+
os.write(0x0);
os.flush();
}
@@ -417,50 +427,65 @@
}
/**
- * Copy a local file to a remote directory, uses mode 0600 when creating the
- * file on the remote side.
+ * Copy a local file to a remote directory, uses mode 0600 when creating the file on the remote
+ * side.
*
- * @param localFile Path and name of local file.
- * @param remoteTargetDirectory Remote target directory where the file has to end up (optional)
- * @param remoteName target filename to use
- * @throws IOException in case of network problems
- * @throws RemoteScpException in case of problems on the target system (connection ok)
+ * @param localFile
+ * Path and name of local file.
+ * @param remoteTargetDirectory
+ * Remote target directory where the file has to end up (optional)
+ * @param remoteName
+ * target filename to use
+ * @throws IOException
+ * in case of network problems
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection ok)
*/
public void put(String localFile, String remoteTargetDirectory, String remoteName)
- throws IOException, RemoteScpException {
+ throws IOException, RemoteScpException {
put(localFile, remoteTargetDirectory, remoteName, "0600");
}
/**
- * Create a remote file and copy the contents of the passed byte array into
- * it. Uses mode 0600 for creating the remote file.
+ * Create a remote file and copy the contents of the passed byte array into it. Uses mode 0600
+ * for creating the remote file.
*
- * @param data the data to be copied into the remote file.
- * @param remoteFileName The name of the file which will be created in the remote target directory.
- * @param remoteTargetDirectory Remote target directory where the file has to end up (optional)
- * @throws IOException in case of network problems
- * @throws RemoteScpException in case of problems on the target system (connection ok)
+ * @param data
+ * the data to be copied into the remote file.
+ * @param remoteFileName
+ * The name of the file which will be created in the remote target directory.
+ * @param remoteTargetDirectory
+ * Remote target directory where the file has to end up (optional)
+ * @throws IOException
+ * in case of network problems
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection ok)
*/
- public void put(byte[] data, String remoteFileName, String remoteTargetDirectory)
- throws IOException,RemoteScpException {
+ public void put(byte[] data, String remoteFileName, String remoteTargetDirectory)
+ throws IOException, RemoteScpException {
put(data, remoteFileName, remoteTargetDirectory, "0600");
}
/**
- * Create a remote file and copy the contents of the passed byte array into
- * it. The method use the specified mode when creating the file on the
- * remote side.
+ * Create a remote file and copy the contents of the passed byte array into it. The method use
+ * the specified mode when creating the file on the remote side.
*
- * @param data the data to be copied into the remote file.
- * @param remoteFileName The name of the file which will be created in the remote target directory.
- * @param remoteTargetDirectory Remote target directory where the file has to end up (optional)
- * @param mode a four digit string (e.g., 0644, see "man chmod", "man open")
- * @throws IOException in case of network problems
- * @throws RemoteScpException in case of problems on the target system (connection ok)
+ * @param data
+ * the data to be copied into the remote file.
+ * @param remoteFileName
+ * The name of the file which will be created in the remote target directory.
+ * @param remoteTargetDirectory
+ * Remote target directory where the file has to end up (optional)
+ * @param mode
+ * a four digit string (e.g., 0644, see "man chmod", "man open")
+ * @throws IOException
+ * in case of network problems
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection ok)
*/
- public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode)
- throws IOException,RemoteScpException {
+ public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode)
+ throws IOException, RemoteScpException {
ChannelExec channel = null;
if ((remoteFileName == null) || (mode == null))
@@ -474,7 +499,7 @@
throw new IllegalArgumentException("Invalid mode.");
String cmd = "scp -t ";
- if(remoteTargetDirectory != null && remoteTargetDirectory.length() > 0) {
+ if (remoteTargetDirectory != null && remoteTargetDirectory.length() > 0) {
cmd = cmd + "-d " + remoteTargetDirectory;
}
@@ -482,11 +507,11 @@
channel = getExecChannel();
channel.setCommand(cmd);
sendBytes(channel, data, remoteFileName, mode);
- //channel.disconnect();
+ // channel.disconnect();
} catch (JSchException e) {
if (channel != null)
channel.disconnect();
- throw (IOException) new IOException("Error during SCP transfer."+e.getMessage())
+ throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
.initCause(e);
}
}
@@ -497,23 +522,29 @@
*/
private ChannelExec getExecChannel() throws JSchException {
ChannelExec channel;
- channel = (ChannelExec)session.openChannel("exec");
+ channel = (ChannelExec) session.openChannel("exec");
return channel;
}
/**
- * Copy a local file to a remote site, uses the specified mode when
- * creating the file on the remote side.
+ * Copy a local file to a remote site, uses the specified mode when creating the file on the
+ * remote side.
*
- * @param localFile Path and name of local file.
- * @param remoteTargetDir Remote target directory where the file has to end up (optional)
- * @param remoteTargetName file name to use on the target system
- * @param mode a four digit string (e.g., 0644, see "man chmod", "man open")
- * @throws IOException in case of network problems
- * @throws RemoteScpException in case of problems on the target system (connection ok)
+ * @param localFile
+ * Path and name of local file.
+ * @param remoteTargetDir
+ * Remote target directory where the file has to end up (optional)
+ * @param remoteTargetName
+ * file name to use on the target system
+ * @param mode
+ * a four digit string (e.g., 0644, see "man chmod", "man open")
+ * @throws IOException
+ * in case of network problems
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection ok)
*/
- public void put(String localFile, String remoteTargetDir, String remoteTargetName, String mode)
- throws IOException,RemoteScpException {
+ public void put(String localFile, String remoteTargetDir, String remoteTargetName, String mode)
+ throws IOException, RemoteScpException {
ChannelExec channel = null;
if ((localFile == null) || (remoteTargetName == null) || (mode == null))
@@ -525,9 +556,9 @@
for (int i = 0; i < mode.length(); i++)
if (Character.isDigit(mode.charAt(i)) == false)
throw new IllegalArgumentException("Invalid mode.");
-
+
String cmd = "scp -t ";
- if(remoteTargetDir != null && remoteTargetDir.length() > 0) {
+ if (remoteTargetDir != null && remoteTargetDir.length() > 0) {
cmd = cmd + "-d " + remoteTargetDir;
}
@@ -539,38 +570,49 @@
} catch (JSchException e) {
if (channel != null)
channel.disconnect();
- throw (IOException) new IOException("Error during SCP transfer."+e.getMessage())
+ throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
.initCause(e);
}
}
/**
* Download a file from the remote server to a local file.
- * @param remoteFile Path and name of the remote file.
- * @param localTarget Local file where to store the data.
- * @throws IOException in case of network problems
- * @throws RemoteScpException in case of problems on the target system (connection ok)
+ *
+ * @param remoteFile
+ * Path and name of the remote file.
+ * @param localTarget
+ * Local file where to store the data.
+ * @throws IOException
+ * in case of network problems
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection ok)
*/
- public void get(String remoteFile, String localTarget) throws IOException,RemoteScpException {
+ public void get(String remoteFile, String localTarget) throws IOException, RemoteScpException {
File f = new File(localTarget);
FileOutputStream fop = new FileOutputStream(f);
- get(remoteFile,fop);
+ get(remoteFile, fop);
}
-
+
/**
* Download a file from the remote server into an OutputStream
- * @param remoteFile Path and name of the remote file.
- * @param localTarget OutputStream to store the data.
- * @throws IOException in case of network problems
- * @throws RemoteScpException in case of problems on the target system (connection ok)
+ *
+ * @param remoteFile
+ * Path and name of the remote file.
+ * @param localTarget
+ * OutputStream to store the data.
+ * @throws IOException
+ * in case of network problems
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection ok)
*/
- public void get(String remoteFile, OutputStream localTarget) throws IOException,RemoteScpException {
+ public void get(String remoteFile, OutputStream localTarget) throws IOException,
+ RemoteScpException {
ChannelExec channel = null;
if ((remoteFile == null) || (localTarget == null))
throw new IllegalArgumentException("Null argument.");
- String cmd = "scp -p -f "+ remoteFile;
+ String cmd = "scp -p -f " + remoteFile;
try {
channel = getExecChannel();
@@ -580,26 +622,30 @@
} catch (JSchException e) {
if (channel != null)
channel.disconnect();
- throw (IOException) new IOException("Error during SCP transfer."+e.getMessage())
+ throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
.initCause(e);
}
}
-
+
/**
* Initiates an SCP sequence but stops after getting fileinformation header
- * @param remoteFile to get information for
+ *
+ * @param remoteFile
+ * to get information for
* @return the file information got
- * @throws IOException in case of network problems
- * @throws RemoteScpException in case of problems on the target system (connection ok)
+ * @throws IOException
+ * in case of network problems
+ * @throws RemoteScpException
+ * in case of problems on the target system (connection ok)
*/
- public FileInfo getFileinfo(String remoteFile) throws IOException,RemoteScpException {
+ public FileInfo getFileinfo(String remoteFile) throws IOException, RemoteScpException {
ChannelExec channel = null;
FileInfo fileInfo = null;
-
+
if (remoteFile == null)
throw new IllegalArgumentException("Null argument.");
- String cmd = "scp -p -f \""+remoteFile+"\"";
+ String cmd = "scp -p -f \"" + remoteFile + "\"";
try {
channel = getExecChannel();
@@ -607,11 +653,11 @@
fileInfo = receiveStream(channel, remoteFile, null);
channel.disconnect();
} catch (JSchException e) {
- throw (IOException) new IOException("Error during SCP transfer."+e.getMessage())
- .initCause(e);
+ throw (IOException) new IOException("Error during SCP transfer." + e.getMessage())
+ .initCause(e);
} finally {
if (channel != null)
- channel.disconnect();
+ 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=544459&r1=544458&r2=544459
==============================================================================
--- 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 Tue Jun 5 05:02:27 2007
@@ -36,31 +36,33 @@
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
-
/**
- * a class to cache SSH Connections and Channel for the SSH Repository
- * each session is defined by 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
+ * a class to cache SSH Connections and Channel for the SSH Repository each session is defined by
+ * 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 {
-
- private SshCache() {};
-
+
+ private SshCache() {
+ };
+
private static SshCache instance = new SshCache();
-
+
public static SshCache getInstance() {
return instance;
}
-
+
private class Entry {
private Session session = null;
+
private ChannelSftp channelSftp = null;
+
private String host = null;
+
private String user = null;
+
private int port = 22;
-
+
/**
* @return the host
*/
@@ -97,21 +99,23 @@
/**
* attach an sftp channel to this cache entry
- * @param channelSftp to attach
+ *
+ * @param channelSftp
+ * 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;
}
-
+
/**
* @return the attached sftp channel
*/
public ChannelSftp getChannelSftp() {
return channelSftp;
}
-
+
/**
* @return the session
*/
@@ -123,85 +127,100 @@
* remove channelSftp and disconnect if necessary
*/
public void releaseChannelSftp() {
- if(channelSftp != null) {
- if(channelSftp.isConnected()) {
- Message.verbose(":: SFTP :: closing sftp connection from "+host+"...");
+ if (channelSftp != null) {
+ if (channelSftp.isConnected()) {
+ Message.verbose(":: SFTP :: closing sftp connection from " + host + "...");
channelSftp.disconnect();
channelSftp = null;
- Message.verbose(":: SFTP :: sftp connection closed from "+host);
+ Message.verbose(":: SFTP :: sftp connection closed from " + host);
}
}
}
}
+
/**
* key is username / host / port
+ *
* @see SshCache.createCacheKey() for details
*/
private Map uriCacheMap = new HashMap();
+
/**
* key is the session itself
*/
private Map sessionCacheMap = new HashMap();
-
+
/**
* retrieves a session entry for a given hostname from the cache
- * @param hostname to retrieve session for
+ *
+ * @param hostname
+ * to retrieve session for
* @return null or the existing entry
*/
private Entry getCacheEntry(String user, String host, int port) {
- return (Entry)uriCacheMap.get(createCacheKey(user, host, port));
+ return (Entry) uriCacheMap.get(createCacheKey(user, host, port));
}
/**
* Creates a cobined cache key from the given key parts
- * @param user name of the user
- * @param host of the connection
- * @param port of the connection
+ *
+ * @param user
+ * name of the user
+ * @param host
+ * of the connection
+ * @param port
+ * of the connection
* @return key for the cache
*/
private static String createCacheKey(String user, String host, int port) {
String portToUse = "22";
- if(port != -1 && port != 22)
+ if (port != -1 && port != 22)
portToUse = Integer.toString(port);
- return user.toLowerCase().trim()+"@"+host.toLowerCase().trim()+":"+portToUse;
+ return user.toLowerCase().trim() + "@" + host.toLowerCase().trim() + ":" + portToUse;
}
-
+
/**
* retrieves a session entry for a given session from the cache
- * @param session to retrieve cache entry for
+ *
+ * @param session
+ * to retrieve cache entry for
* @return null or the existing entry
*/
private Entry getCacheEntry(Session session) {
- return (Entry)sessionCacheMap.get(session);
+ return (Entry) sessionCacheMap.get(session);
}
/**
- * Sets a session to a given combined key into the cache
- * If an old session object already exists, close and remove it
- * @param user of the session
- * @param host of the session
- * @param port of the session
- * @param session Session to save
+ * Sets a session to a given combined key into the cache If an old session object already
+ * exists, close and remove it
+ *
+ * @param user
+ * of the session
+ * @param host
+ * of the session
+ * @param port
+ * of the session
+ * @param session
+ * Session to save
*/
private void setSession(String user, String host, int port, Session newSession) {
- Entry entry = (Entry)uriCacheMap.get(createCacheKey(user, host, port));
+ 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()) {
+ if (oldSession != null && !oldSession.equals(newSession) && oldSession.isConnected()) {
entry.releaseChannelSftp();
String oldhost = oldSession.getHost();
- Message.verbose(":: SSH :: closing ssh connection from "+oldhost+"...");
+ Message.verbose(":: SSH :: closing ssh connection from " + oldhost + "...");
oldSession.disconnect();
- Message.verbose(":: SSH :: ssh connection closed from "+oldhost);
+ Message.verbose(":: SSH :: ssh connection closed from " + oldhost);
}
- if((newSession == null) && (entry != null)) {
+ 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);
+ Entry newEntry = new Entry(newSession, user, host, port);
uriCacheMap.put(createCacheKey(user, host, port), newEntry);
sessionCacheMap.put(newSession, newEntry);
}
@@ -209,80 +228,91 @@
/**
* discardes session entries from the cache
- * @param session to clear
+ *
+ * @param session
+ * to clear
*/
public void clearSession(Session session) {
- Entry entry = (Entry)sessionCacheMap.get(session);
- if(entry != null)
+ Entry entry = (Entry) sessionCacheMap.get(session);
+ if (entry != null)
setSession(entry.getUser(), entry.getHost(), entry.getPort(), null);
}
-
+
/**
* retrieves an sftp channel from the cache
- * @param host to connect to
+ *
+ * @param host
+ * to connect to
* @return channelSftp or null if not successful (channel not existent or dead)
*/
public ChannelSftp getChannelSftp(Session session) throws IOException {
ChannelSftp channel = null;
Entry entry = getCacheEntry(session);
- if(entry != null) {
+ if (entry != null) {
channel = entry.getChannelSftp();
- if(channel != null && !channel.isConnected()) {
+ if (channel != null && !channel.isConnected()) {
entry.releaseChannelSftp();
channel = null;
}
- }
+ }
return channel;
}
-
+
/**
* attaches a channelSftp to an existing session cache entry
- * @param session to attach the channel to
- * @param channelSftp channel to attach
+ *
+ * @param session
+ * to attach the channel to
+ * @param channelSftp
+ * channel to attach
*/
public void attachChannelSftp(Session session, ChannelSftp channel) {
Entry entry = getCacheEntry(session);
- if(entry == null)
- throw new IllegalArgumentException("No entry for "+session+" in the cache");
+ if (entry == null)
+ throw new IllegalArgumentException("No entry for " + session + " in the cache");
entry.setChannelSftp(channel);
}
-
+
/**
* Gets a session from the cache or establishes a new session if necessary
- * @param username for the session to use
- * @param host to connect to
- * @param port to use for session (-1 == use standard port)
- * @param password to use for authentication (optional)
- * @param pemFile File to use for public key authentication
- * @param pemPassword to use for accessing the pemFile (optional)
- * @param passFile to store credentials
+ *
+ * @param username
+ * for the session to use
+ * @param host
+ * to connect to
+ * @param port
+ * to use for session (-1 == use standard port)
+ * @param password
+ * to use for authentication (optional)
+ * @param pemFile
+ * File to use for public key authentication
+ * @param pemPassword
+ * to use for accessing the pemFile (optional)
+ * @param passFile
+ * to store credentials
* @return session or null if not successful
*/
- public Session getSession(String host,
- int port,
- String username,
- String userPassword,
- File pemFile,
- String pemPassword,
- File passFile) throws IOException {
+ public Session getSession(String host, int port, String username, String userPassword,
+ 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+"...");
+ if (session == null || !session.isConnected()) {
+ Message.verbose(":: SSH :: connecting to " + host + "...");
try {
- JSch jsch=new JSch();
- if(port != -1)
- session = jsch.getSession(username,host,port);
+ JSch jsch = new JSch();
+ if (port != -1)
+ session = jsch.getSession(username, host, port);
else
- session = jsch.getSession(username,host);
- if(pemFile != null) {
+ session = jsch.getSession(username, host);
+ if (pemFile != null) {
jsch.addIdentity(pemFile.getAbsolutePath(), pemPassword);
- }
- session.setUserInfo(new cfUserInfo(host,username,userPassword,pemFile,pemPassword,passFile));
+ }
+ session.setUserInfo(new cfUserInfo(host, username, userPassword, pemFile,
+ pemPassword, passFile));
session.connect();
- Message.verbose(":: SSH :: connected to "+host+"!");
+ Message.verbose(":: SSH :: connected to " + host + "!");
setSession(username, host, port, session);
} catch (JSchException e) {
if (passFile.exists()) {
@@ -295,20 +325,26 @@
}
return session;
}
-
+
/**
* feeds in password silently into JSch
*/
private static class cfUserInfo implements UserInfo {
-
+
private String userPassword;
+
private String pemPassword;
+
private String userName;
+
private final File pemFile;
+
private final String host;
+
private final File passfile;
-
- public cfUserInfo(String host, String userName, String userPassword, File pemFile, String pemPassword, File passfile) {
+
+ public cfUserInfo(String host, String userName, String userPassword, File pemFile,
+ String pemPassword, File passfile) {
this.userPassword = userPassword;
this.pemPassword = pemPassword;
this.host = host;
@@ -316,7 +352,7 @@
this.userName = userName;
this.pemFile = pemFile;
}
-
+
public void showMessage(String message) {
Message.info(message);
}
@@ -334,8 +370,9 @@
}
public String getPassword() {
- if(userPassword == null) {
- Credentials c = CredentialsUtil.promptCredentials(new Credentials(null, host, userName, userPassword), passfile);
+ if (userPassword == null) {
+ Credentials c = CredentialsUtil.promptCredentials(new Credentials(null, host,
+ userName, userPassword), passfile);
if (c != null) {
userName = c.getUserName();
userPassword = c.getPasswd();
@@ -345,8 +382,9 @@
}
public String getPassphrase() {
- if(pemPassword == null && pemFile != null) {
- Credentials c = CredentialsUtil.promptCredentials(new Credentials(null, pemFile.getAbsolutePath(), userName, pemPassword), passfile);
+ if (pemPassword == null && pemFile != null) {
+ Credentials c = CredentialsUtil.promptCredentials(new Credentials(null, pemFile
+ .getAbsolutePath(), userName, pemPassword), passfile);
if (c != null) {
userName = c.getUserName();
pemPassword = c.getPasswd();
Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshRepository.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshRepository.java?view=diff&rev=544459&r1=544458&r2=544459
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshRepository.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/ssh/SshRepository.java Tue Jun 5 05:02:27 2007
@@ -17,7 +17,6 @@
*/
package org.apache.ivy.plugins.repository.ssh;
-
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -37,110 +36,125 @@
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
-
/**
* Ivy Repository based on SSH
*/
public class SshRepository extends AbstractSshBasedRepository {
- private char fileSeparator = '/';
+ private char fileSeparator = '/';
+
private String listCommand = "ls -1";
+
private String existCommand = "ls";
+
private String createDirCommand = "mkdir";
+
private final static String ARGUMENT_PLACEHOLDER = "%arg";
- private final static int POLL_SLEEP_TIME = 500;
+
+ private final static int POLL_SLEEP_TIME = 500;
+
/**
* create a new resource with lazy initializing
*/
public Resource getResource(String source) {
- Message.debug("SShRepository:getResource called: "+source);
- return new SshResource(this,source);
+ Message.debug("SShRepository:getResource called: " + source);
+ return new SshResource(this, source);
}
-
+
/**
- * fetch the needed file information for a given file (size, last modification time)
- * and report it back in a SshResource
- * @param uri ssh uri for the file to get info for
+ * fetch the needed file information for a given file (size, last modification time) and report
+ * it back in a SshResource
+ *
+ * @param uri
+ * ssh uri for the file to get info for
* @return SshResource filled with the needed informations
- * @see org.apache.ivy.plugins.repository.Repository#getResource(java.lang.String)
- */
- public SshResource resolveResource(String source) {
- Message.debug("SShRepository:resolveResource called: "+source);
- SshResource result = null;
+ * @see org.apache.ivy.plugins.repository.Repository#getResource(java.lang.String)
+ */
+ public SshResource resolveResource(String source) {
+ Message.debug("SShRepository:resolveResource called: " + source);
+ SshResource result = null;
Session session = null;
- try {
+ try {
session = getSession(source);
Scp myCopy = new Scp(session);
Scp.FileInfo fileInfo = myCopy.getFileinfo(new URI(source).getPath());
- result = new SshResource(this,
- source,
- true,
- fileInfo.getLength(),
- fileInfo.getLastModified());
+ result = new SshResource(this, source, true, fileInfo.getLength(), fileInfo
+ .getLastModified());
} catch (IOException e) {
- if(session != null)
- releaseSession(session,source);
+ if (session != null)
+ releaseSession(session, source);
result = new SshResource();
} catch (URISyntaxException e) {
- if(session != null)
- releaseSession(session,source);
- result = new SshResource();
+ if (session != null)
+ releaseSession(session, source);
+ result = new SshResource();
} catch (RemoteScpException e) {
result = new SshResource();
}
Message.debug("SShRepository:resolveResource end.");
- return result;
- }
+ return result;
+ }
/**
* Reads out the output of a ssh session exec
- * @param channel Channel to read from
- * @param strStdout StringBuffer that receives Session Stdout output
- * @param strStderr StringBuffer that receives Session Stderr output
- * @throws IOException in case of trouble with the network
+ *
+ * @param channel
+ * Channel to read from
+ * @param strStdout
+ * StringBuffer that receives Session Stdout output
+ * @param strStderr
+ * StringBuffer that receives Session Stderr output
+ * @throws IOException
+ * in case of trouble with the network
*/
- private void readSessionOutput(ChannelExec channel, StringBuffer strStdout, StringBuffer strStderr) throws IOException {
+ private void readSessionOutput(ChannelExec channel, StringBuffer strStdout,
+ StringBuffer strStderr) throws IOException {
InputStream stdout = channel.getInputStream();
InputStream stderr = channel.getErrStream();
-
+
try {
channel.connect();
} catch (JSchException e1) {
throw (IOException) new IOException("Channel connection problems").initCause(e1);
}
-
+
byte[] buffer = new byte[8192];
- while(true){
+ while (true) {
int avail = 0;
while ((avail = stdout.available()) > 0) {
- int len = stdout.read(buffer,0,(avail > 8191 ? 8192 : avail));
- strStdout.append(new String(buffer,0,len));
+ int len = stdout.read(buffer, 0, (avail > 8191 ? 8192 : avail));
+ strStdout.append(new String(buffer, 0, len));
}
while ((avail = stderr.available()) > 0) {
- int len = stderr.read(buffer,0,(avail > 8191 ? 8192 : avail));
+ int len = stderr.read(buffer, 0, (avail > 8191 ? 8192 : avail));
strStderr.append(new String(buffer, 0, len));
}
- if(channel.isClosed()){
+ if (channel.isClosed()) {
break;
}
- try{Thread.sleep(POLL_SLEEP_TIME);}catch(Exception ee){}
+ try {
+ Thread.sleep(POLL_SLEEP_TIME);
+ } catch (Exception ee) {
+ }
}
int avail = 0;
while ((avail = stdout.available()) > 0) {
- int len = stdout.read(buffer,0,(avail > 8191 ? 8192 : avail));
- strStdout.append(new String(buffer,0,len));
+ int len = stdout.read(buffer, 0, (avail > 8191 ? 8192 : avail));
+ strStdout.append(new String(buffer, 0, len));
}
while ((avail = stderr.available()) > 0) {
- int len = stderr.read(buffer,0,(avail > 8191 ? 8192 : avail));
+ int len = stderr.read(buffer, 0, (avail > 8191 ? 8192 : avail));
strStderr.append(new String(buffer, 0, len));
}
}
- /* (non-Javadoc)
- * @see org.apache.ivy.repository.Repository#list(java.lang.String)
- */
- public List list(String parent) throws IOException {
- Message.debug("SShRepository:list called: "+parent);
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.ivy.repository.Repository#list(java.lang.String)
+ */
+ public List list(String parent) throws IOException {
+ Message.debug("SShRepository:list called: " + parent);
ArrayList result = new ArrayList();
Session session = null;
ChannelExec channel = null;
@@ -152,24 +166,24 @@
} catch (URISyntaxException e1) {
// failed earlier
}
- String fullCmd = replaceArgument(listCommand,parentUri.getPath());
+ String fullCmd = replaceArgument(listCommand, parentUri.getPath());
channel.setCommand(fullCmd);
StringBuffer stdOut = new StringBuffer();
StringBuffer stdErr = new StringBuffer();
- readSessionOutput(channel,stdOut,stdErr);
- if(channel.getExitStatus() != 0) {
+ readSessionOutput(channel, stdOut, stdErr);
+ if (channel.getExitStatus() != 0) {
Message.error("Ssh ListCommand exited with status != 0");
Message.error(stdErr.toString());
return null;
} else {
BufferedReader br = new BufferedReader(new StringReader(stdOut.toString()));
String line = null;
- while((line = br.readLine()) != null) {
+ while ((line = br.readLine()) != null) {
result.add(line);
}
}
- return result;
- }
+ return result;
+ }
/**
* @param session
@@ -179,7 +193,7 @@
private ChannelExec getExecChannel(Session session) throws IOException {
ChannelExec channel;
try {
- channel = (ChannelExec)session.openChannel("exec");
+ channel = (ChannelExec) session.openChannel("exec");
} catch (JSchException e) {
throw new IOException();
}
@@ -187,27 +201,31 @@
}
/**
- * Replace the argument placeholder with argument or append the argument if no
- * placeholder is present
- * @param command with argument placeholder or not
- * @param argument
+ * Replace the argument placeholder with argument or append the argument if no placeholder is
+ * present
+ *
+ * @param command
+ * with argument placeholder or not
+ * @param argument
* @return replaced full command
*/
private String replaceArgument(String command, String argument) {
String fullCmd;
- if(command.indexOf(ARGUMENT_PLACEHOLDER) == -1) {
+ if (command.indexOf(ARGUMENT_PLACEHOLDER) == -1) {
fullCmd = command + " " + argument;
} else {
- fullCmd = command.replaceAll(ARGUMENT_PLACEHOLDER,argument);
+ fullCmd = command.replaceAll(ARGUMENT_PLACEHOLDER, argument);
}
return fullCmd;
}
- /* (non-Javadoc)
- * @see org.apache.ivy.repository.Repository#put(java.io.File, java.lang.String, boolean)
- */
- public void put(File source, String destination, boolean overwrite) throws IOException {
- Message.debug("SShRepository:put called: "+destination);
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.ivy.repository.Repository#put(java.io.File, java.lang.String, boolean)
+ */
+ public void put(File source, String destination, boolean overwrite) throws IOException {
+ Message.debug("SShRepository:put called: " + destination);
Session session = getSession(destination);
try {
URI destinationUri = null;
@@ -220,87 +238,96 @@
int lastSep = filePath.lastIndexOf(fileSeparator);
String path;
String name;
- if(lastSep == -1) {
+ if (lastSep == -1) {
name = filePath;
path = null;
} else {
- name = filePath.substring(lastSep+1);
- path = filePath.substring(0,lastSep);
+ name = filePath.substring(lastSep + 1);
+ path = filePath.substring(0, lastSep);
}
if (!overwrite) {
- if(checkExistence(filePath,session)) {
+ if (checkExistence(filePath, session)) {
throw new IOException("destination file exists and overwrite == true");
}
}
- if(path != null) {
- makePath(path,session);
+ if (path != null) {
+ makePath(path, session);
}
Scp myCopy = new Scp(session);
- myCopy.put(source.getCanonicalPath(),path,name);
+ myCopy.put(source.getCanonicalPath(), path, name);
} catch (IOException e) {
- if(session != null)
- releaseSession(session,destination);
+ if (session != null)
+ releaseSession(session, destination);
throw e;
} catch (RemoteScpException e) {
throw new IOException(e.getMessage());
}
- }
-
+ }
+
/**
* Tries to create a directory path on the target system
- * @param path to create
- * @param connnection to use
+ *
+ * @param path
+ * to create
+ * @param connnection
+ * to use
*/
private void makePath(String path, Session session) throws IOException {
ChannelExec channel = null;
String trimmed = path;
try {
- while(trimmed.length() > 0 && trimmed.charAt(trimmed.length()-1) == fileSeparator)
- trimmed = trimmed.substring(0,trimmed.length()-1);
- if(trimmed.length() == 0 || checkExistence(trimmed,session)) {
+ while (trimmed.length() > 0 && trimmed.charAt(trimmed.length() - 1) == fileSeparator)
+ trimmed = trimmed.substring(0, trimmed.length() - 1);
+ if (trimmed.length() == 0 || checkExistence(trimmed, session)) {
return;
}
int nextSlash = trimmed.lastIndexOf(fileSeparator);
- if(nextSlash > 0) {
- String parent = trimmed.substring(0,nextSlash);
- makePath(parent,session);
+ if (nextSlash > 0) {
+ String parent = trimmed.substring(0, nextSlash);
+ makePath(parent, session);
}
channel = getExecChannel(session);
- String mkdir = replaceArgument( createDirCommand, trimmed);
+ String mkdir = replaceArgument(createDirCommand, trimmed);
Message.debug("SShRepository: trying to create path: " + mkdir);
channel.setCommand(mkdir);
StringBuffer stdOut = new StringBuffer();
StringBuffer stdErr = new StringBuffer();
- readSessionOutput(channel,stdOut,stdErr);
+ readSessionOutput(channel, stdOut, stdErr);
} finally {
- if(channel != null)
+ if (channel != null)
channel.disconnect();
}
}
/**
* check for existence of file or dir on target system
- * @param filePath to the object to check
- * @param session to use
+ *
+ * @param filePath
+ * to the object to check
+ * @param session
+ * to use
* @return true: object exists, false otherwise
*/
private boolean checkExistence(String filePath, Session session) throws IOException {
Message.debug("SShRepository: checkExistence called: " + filePath);
ChannelExec channel = null;
channel = getExecChannel(session);
- String fullCmd = replaceArgument( existCommand, filePath);
+ String fullCmd = replaceArgument(existCommand, filePath);
channel.setCommand(fullCmd);
StringBuffer stdOut = new StringBuffer();
StringBuffer stdErr = new StringBuffer();
- readSessionOutput(channel,stdOut,stdErr);
+ readSessionOutput(channel, stdOut, stdErr);
return channel.getExitStatus() == 0;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.apache.ivy.repository.Repository#get(java.lang.String, java.io.File)
*/
public void get(String source, File destination) throws IOException {
- Message.debug("SShRepository:get called: "+source+" to "+destination.getCanonicalPath());
+ Message.debug("SShRepository:get called: " + source + " to "
+ + destination.getCanonicalPath());
if (destination.getParentFile() != null) {
destination.getParentFile().mkdirs();
}
@@ -312,15 +339,15 @@
} catch (URISyntaxException e) {
// fails earlier
}
- if(sourceUri == null) {
- Message.error("could not parse URI "+source);
+ if (sourceUri == null) {
+ Message.error("could not parse URI " + source);
return;
}
Scp myCopy = new Scp(session);
- myCopy.get(sourceUri.getPath(),destination.getCanonicalPath());
+ myCopy.get(sourceUri.getPath(), destination.getCanonicalPath());
} catch (IOException e) {
- if(session != null)
- releaseSession(session,source);
+ if (session != null)
+ releaseSession(session, source);
throw e;
} catch (RemoteScpException e) {
throw new IOException(e.getMessage());
@@ -328,21 +355,23 @@
}
/**
- * sets the list command to use for a directory listing
- * listing must be only the filename and each filename on a separate line
- * @param cmd to use. default is "ls -1"
+ * sets the list command to use for a directory listing listing must be only the filename and
+ * each filename on a separate line
+ *
+ * @param cmd
+ * to use. default is "ls -1"
*/
public void setListCommand(String cmd) {
this.listCommand = cmd.trim();
}
-
+
/**
* @return the list command to use
*/
public String getListCommand() {
return listCommand;
}
-
+
/**
* @return the createDirCommand
*/
@@ -351,7 +380,8 @@
}
/**
- * @param createDirCommand the createDirCommand to set
+ * @param createDirCommand
+ * the createDirCommand to set
*/
public void setCreateDirCommand(String createDirCommand) {
this.createDirCommand = createDirCommand;
@@ -365,34 +395,38 @@
}
/**
- * @param existCommand the existCommand to set
+ * @param existCommand
+ * the existCommand to set
*/
public void setExistCommand(String existCommand) {
this.existCommand = existCommand;
}
/**
- * The file separator is the separator to use on the target system
- * On a unix system it is '/', but I don't know, how this is solved
- * on different ssh implementations. Using the default might be fine
- * @param fileSeparator The fileSeparator to use. default '/'
+ * The file separator is the separator to use on the target system On a unix system it is '/',
+ * but I don't know, how this is solved on different ssh implementations. Using the default
+ * might be fine
+ *
+ * @param fileSeparator
+ * The fileSeparator to use. default '/'
*/
public void setFileSeparator(char fileSeparator) {
this.fileSeparator = fileSeparator;
}
/**
- * return ssh as scheme
- * use the Resolver type name here?
- * would be nice if it would be static, so we could use SshResolver.getTypeName()
+ * return ssh as scheme use the Resolver type name here? would be nice if it would be static, so
+ * we could use SshResolver.getTypeName()
*/
protected String getRepositoryScheme() {
return "ssh";
}
-
+
/**
* Not really streaming...need to implement a proper streaming approach?
- * @param resource to stream
+ *
+ * @param resource
+ * to stream
* @return InputStream of the resource data
*/
public InputStream openStream(SshResource resource) throws IOException {
@@ -400,10 +434,10 @@
Scp scp = new Scp(session);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
- scp.get(resource.getName(),os);
+ scp.get(resource.getName(), os);
} catch (IOException e) {
- if(session != null)
- releaseSession(session,resource.getName());
+ if (session != null)
+ releaseSession(session, resource.getName());
throw e;
} catch (RemoteScpException e) {
throw new IOException(e.getMessage());
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=544459&r1=544458&r2=544459
==============================================================================
--- 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 Tue Jun 5 05:02:27 2007
@@ -17,88 +17,100 @@
*/
package org.apache.ivy.plugins.repository.ssh;
-
import java.io.IOException;
import java.io.InputStream;
import org.apache.ivy.plugins.repository.Resource;
import org.apache.ivy.util.Message;
-
/**
* Resource for SSH Ivy Repository
*/
public class SshResource implements Resource {
private boolean resolved = false;
- private String uri = null;
- private boolean bExists = false;
- private long len = 0;
- private long lastModified = 0;
+
+ private String uri = null;
+
+ private boolean bExists = false;
+
+ private long len = 0;
+
+ private long lastModified = 0;
+
private SshRepository repository = null;
-
- public SshResource() {
+
+ public SshResource() {
resolved = true;
- }
-
+ }
+
public SshResource(SshRepository repository, String uri) {
this.uri = uri;
this.repository = repository;
resolved = false;
}
-
- public SshResource(SshRepository repository, String uri, boolean bExists, long len, long lastModified) {
- this.uri = uri;
- this.bExists = bExists;
- this.len = len;
- this.lastModified = lastModified;
+
+ public SshResource(SshRepository repository, String uri, boolean bExists, long len,
+ long lastModified) {
+ this.uri = uri;
+ this.bExists = bExists;
+ this.len = len;
+ this.lastModified = lastModified;
this.repository = repository;
resolved = true;
- }
-
- /* (non-Javadoc)
- * @see org.apache.ivy.repository.Resource#exists()
- */
- public boolean exists() {
- if(!resolved)
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.ivy.repository.Resource#exists()
+ */
+ public boolean exists() {
+ if (!resolved)
resolve();
- return bExists;
- }
+ return bExists;
+ }
- /* (non-Javadoc)
- * @see org.apache.ivy.repository.Resource#getContentLength()
- */
- public long getContentLength() {
- if(!resolved)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.ivy.repository.Resource#getContentLength()
+ */
+ public long getContentLength() {
+ if (!resolved)
resolve();
- return len;
- }
+ return len;
+ }
- /* (non-Javadoc)
- * @see org.apache.ivy.repository.Resource#getLastModified()
- */
- public long getLastModified() {
- if(!resolved)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.ivy.repository.Resource#getLastModified()
+ */
+ public long getLastModified() {
+ if (!resolved)
resolve();
- return lastModified;
- }
+ return lastModified;
+ }
- private void resolve() {
- Message.debug("SShResource: resolving "+uri);
+ private void resolve() {
+ Message.debug("SShResource: resolving " + uri);
SshResource res = repository.resolveResource(uri);
len = res.getContentLength();
lastModified = res.getLastModified();
bExists = res.exists();
resolved = true;
- Message.debug("SShResource: resolved "+this);
+ Message.debug("SShResource: resolved " + this);
}
- /* (non-Javadoc)
- * @see org.apache.ivy.repository.Resource#getName()
- */
- public String getName() {
- return uri;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.ivy.repository.Resource#getName()
+ */
+ public String getName() {
+ return uri;
+ }
public String toString() {
StringBuffer buffer = new StringBuffer();
@@ -109,7 +121,7 @@
buffer.append(")]");
return buffer.toString();
}
-
+
public boolean isLocal() {
return false;
}
Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java?view=diff&rev=544459&r1=544458&r2=544459
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java Tue Jun 5 05:02:27 2007
@@ -34,13 +34,13 @@
import org.apache.ivy.util.FileUtil;
import org.apache.ivy.util.url.ApacheURLLister;
-
public class URLRepository extends AbstractRepository {
private RepositoryCopyProgressListener _progress = new RepositoryCopyProgressListener(this);
+
private Map _resourcesCache = new HashMap();
public Resource getResource(String source) throws IOException {
- Resource res = (Resource)_resourcesCache.get(source);
+ Resource res = (Resource) _resourcesCache.get(source);
if (res == null) {
res = new URLResource(new URL(source));
_resourcesCache.put(source, res);
@@ -69,17 +69,19 @@
}
public void put(File source, String destination, boolean overwrite) throws IOException {
- throw new UnsupportedOperationException("URL repository is not able to put files for the moment");
+ throw new UnsupportedOperationException(
+ "URL repository is not able to put files for the moment");
}
private ApacheURLLister _lister = new ApacheURLLister();
+
public List list(String parent) throws IOException {
if (parent.startsWith("http")) {
List urls = _lister.listAll(new URL(parent));
if (urls != null) {
List ret = new ArrayList(urls.size());
for (ListIterator iter = urls.listIterator(); iter.hasNext();) {
- URL url = (URL)iter.next();
+ URL url = (URL) iter.next();
ret.add(url.toExternalForm());
}
return ret;
@@ -90,7 +92,7 @@
if (file.exists() && file.isDirectory()) {
String[] files = file.list();
List ret = new ArrayList(files.length);
- URL context = path.endsWith("/") ? new URL(parent) : new URL(parent+"/");
+ URL context = path.endsWith("/") ? new URL(parent) : new URL(parent + "/");
for (int i = 0; i < files.length; i++) {
ret.add(new URL(context, files[i]).toExternalForm());
}
Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLResource.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLResource.java?view=diff&rev=544459&r1=544458&r2=544459
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLResource.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLResource.java Tue Jun 5 05:02:27 2007
@@ -26,12 +26,15 @@
import org.apache.ivy.util.url.URLHandlerRegistry;
import org.apache.ivy.util.url.URLHandler.URLInfo;
-
public class URLResource implements Resource {
private URL _url;
+
private boolean _init = false;
+
private long _lastModified;
+
private long _contentLength;
+
private boolean _exists;
public URLResource(URL url) {
@@ -41,13 +44,14 @@
public String getName() {
return _url.toExternalForm();
}
-
+
public Resource clone(String cloneName) {
- try {
- return new URLResource(new URL(cloneName));
- } catch (MalformedURLException e) {
- throw new IllegalArgumentException("bad clone name provided: not suitable for an URLResource: "+cloneName);
- }
+ try {
+ return new URLResource(new URL(cloneName));
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException(
+ "bad clone name provided: not suitable for an URLResource: " + cloneName);
+ }
}
public long getLastModified() {
@@ -82,15 +86,16 @@
public URL getURL() {
return _url;
}
+
public String toString() {
return getName();
}
-
+
public boolean isLocal() {
return false;
}
- public InputStream openStream() throws IOException {
- return _url.openStream();
- }
+ public InputStream openStream() throws IOException {
+ return _url.openStream();
+ }
}
Modified: incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/vfs/IvyWebdavClientFactory.java
URL: http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/vfs/IvyWebdavClientFactory.java?view=diff&rev=544459&r1=544458&r2=544459
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/vfs/IvyWebdavClientFactory.java (original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/vfs/IvyWebdavClientFactory.java Tue Jun 5 05:02:27 2007
@@ -28,64 +28,60 @@
import org.apache.ivy.util.url.HttpClientHandler;
import org.apache.webdav.lib.WebdavResource;
-
/***************************************************************************************************
- * Modified version of the WebdavClientFactory from VFS which adds support for httpclient 3.x.
- * See http://issues.apache.org/jira/browse/VFS-74 for more info.
- *
- * Create a HttpClient instance
- *
+ * Modified version of the WebdavClientFactory from VFS which adds support for httpclient 3.x. See
+ * http://issues.apache.org/jira/browse/VFS-74 for more info. Create a HttpClient instance
*/
class IvyWebdavClientFactory {
- private IvyWebdavClientFactory() {
- }
+ private IvyWebdavClientFactory() {
+ }
- /***********************************************************************************************
- * Creates a new connection to the server.
- */
- public static HttpClient createConnection(String hostname, int port, String username,
- String password, FileSystemOptions fileSystemOptions) throws FileSystemException {
- // Create an Http client
- HttpClient client;
- try {
- final HttpURL url = new HttpURL(username, password, hostname, port, "/");
-
- // WebdavResource resource = null;
- WebdavResource resource = new WebdavResource() {
- };
-
- if (fileSystemOptions != null) {
- String proxyHost = WebdavFileSystemConfigBuilder.getInstance().getProxyHost(
- fileSystemOptions);
- int proxyPort = WebdavFileSystemConfigBuilder.getInstance().getProxyPort(
- fileSystemOptions);
-
- if (proxyHost != null && proxyPort > 0) {
- // resource = new WebdavResource(url, proxyHost, proxyPort);
- resource.setProxy(proxyHost, proxyPort);
- }
- }
-
- /*
- * if (resource == null) { resource = new WebdavResource(url); }
- * resource.setProperties(WebdavResource.NOACTION, 1);
- */
- resource.setHttpURL(url, WebdavResource.NOACTION, 1);
-
- client = resource.retrieveSessionInstance();
- HttpClientHandler handler = new HttpClientHandler();
- int httpClientVersion = handler.getHttpClientMajorVersion();
- if (httpClientVersion == 2) {
- // VFS only supports httpclient v2 for now...
- client.setHttpConnectionManager(new WebdavConnectionManager());
- } else {
- client.setHttpConnectionManager(new IvyWebdavConnectionManager());
- }
- } catch (final IOException e) {
- throw new FileSystemException("vfs.provider.webdav/connect.error", hostname, e);
- }
+ /***********************************************************************************************
+ * Creates a new connection to the server.
+ */
+ public static HttpClient createConnection(String hostname, int port, String username,
+ String password, FileSystemOptions fileSystemOptions) throws FileSystemException {
+ // Create an Http client
+ HttpClient client;
+ try {
+ final HttpURL url = new HttpURL(username, password, hostname, port, "/");
+
+ // WebdavResource resource = null;
+ WebdavResource resource = new WebdavResource() {
+ };
+
+ if (fileSystemOptions != null) {
+ String proxyHost = WebdavFileSystemConfigBuilder.getInstance().getProxyHost(
+ fileSystemOptions);
+ int proxyPort = WebdavFileSystemConfigBuilder.getInstance().getProxyPort(
+ fileSystemOptions);
+
+ if (proxyHost != null && proxyPort > 0) {
+ // resource = new WebdavResource(url, proxyHost, proxyPort);
+ resource.setProxy(proxyHost, proxyPort);
+ }
+ }
+
+ /*
+ * if (resource == null) { resource = new WebdavResource(url); }
+ * resource.setProperties(WebdavResource.NOACTION, 1);
+ */
+ resource.setHttpURL(url, WebdavResource.NOACTION, 1);
+
+ client = resource.retrieveSessionInstance();
+ HttpClientHandler handler = new HttpClientHandler();
+ int httpClientVersion = handler.getHttpClientMajorVersion();
+ if (httpClientVersion == 2) {
+ // VFS only supports httpclient v2 for now...
+ client.setHttpConnectionManager(new WebdavConnectionManager());
+ } else {
+ client.setHttpConnectionManager(new IvyWebdavConnectionManager());
+ }
+ } catch (final IOException e) {
+ throw new FileSystemException("vfs.provider.webdav/connect.error", hostname, e);
+ }
- return client;
- }
+ return client;
+ }
}