[jboss-svn-commits] JBL Code SVN: r8937 - labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Jan 20 08:19:16 EST 2007
Author: mark.little at jboss.com
Date: 2007-01-20 08:19:16 -0500 (Sat, 20 Jan 2007)
New Revision: 8937
Modified:
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java
Log:
Method signature changes for finer granularity exceptions.
Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java 2007-01-20 11:35:00 UTC (rev 8936)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/util/SecureFtpImpl.java 2007-01-20 13:19:16 UTC (rev 8937)
@@ -26,6 +26,7 @@
package org.jboss.internal.soa.esb.util;
import java.io.File;
+import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
@@ -34,6 +35,7 @@
import java.util.Vector;
import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.addressing.eprs.SFTPEpr;
import org.jboss.soa.esb.common.Environment;
import org.jboss.soa.esb.common.ModulePropertyManager;
@@ -45,8 +47,10 @@
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
+import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;
import com.jcraft.jsch.ChannelSftp.LsEntry;
@@ -101,7 +105,7 @@
* @param connect If true create a new sftp session
*
*/
- public SecureFtpImpl(ConfigTree p_oP, boolean p_bConnect) throws Exception
+ public SecureFtpImpl(ConfigTree p_oP, boolean p_bConnect) throws ConfigurationException, RemoteFileSystemException
{
m_oParms = p_oP;
initialize(p_bConnect);
@@ -115,7 +119,7 @@
* @param connect If true create a new sftp session
*
*/
- public SecureFtpImpl(SFTPEpr p_oP, boolean p_bConnect) throws Exception
+ public SecureFtpImpl(SFTPEpr p_oP, boolean p_bConnect) throws ConfigurationException, RemoteFileSystemException
{
m_oEpr = p_oP;
@@ -160,24 +164,35 @@
m_bPassive = false;
- try {
+ try
+ {
m_bPassive = m_oEpr.getPassive();
- } catch (URISyntaxException e) {
+ } catch (URISyntaxException e)
+ {
_logger.warn(e);
}
try
{
m_oCertificate = m_oEpr.getCertificateURL();
- } catch (URISyntaxException e) {
+ }
+ catch (MalformedURLException ex)
+ {
+ _logger.error(ex);
+
+ throw new ConfigurationException(ex);
+ }
+ catch (URISyntaxException e)
+ {
_logger.warn(e);
+
+ throw new ConfigurationException(e);
}
// TODO there is still a bit of space for improvements here.
configTreeFromEpr();
initialize(p_bConnect);
-
}
/*
@@ -189,52 +204,76 @@
*
*/
public SecureFtpImpl(List<KeyValuePair> attribs, boolean connect)
- throws Exception {
+ throws ConfigurationException, RemoteFileSystemException
+ {
m_oParms = new ConfigTree("fromProps");
for (KeyValuePair oCurr : attribs)
m_oParms.setAttribute(oCurr.getKey(), oCurr.getValue());
initialize(connect);
}
- private void initialize(boolean bConnect) throws Exception {
+ private void initialize(boolean bConnect) throws ConfigurationException, RemoteFileSystemException
+ {
checkParms();
- if (bConnect) {
- session = m_oJSch.getSession(m_sUser, m_sFtpServer, m_iPort);
-
- UserInfo ui = new SecureFtpUserInfo(m_sPasswd);
- session.setUserInfo(ui);
-
- session.connect();
-
- m_oChannel = session.openChannel(SECURE_CHANNEL);
-
- m_oChannel.connect();
-
- m_oSftpChannel = (ChannelSftp) m_oChannel;
-
- for (int i1 = 0; i1 < 10 && !session.isConnected(); i1++)
- Thread.sleep(200);
- if (!session.isConnected())
- throw new Exception("Can't connect to FTP server");
-
- m_bConnected = this.session.isConnected();
+
+ if (bConnect)
+ {
+ try
+ {
+ session = m_oJSch.getSession(m_sUser, m_sFtpServer, m_iPort);
+
+ UserInfo ui = new SecureFtpUserInfo(m_sPasswd);
+ session.setUserInfo(ui);
+
+ session.connect();
+
+ m_oChannel = session.openChannel(SECURE_CHANNEL);
+
+ m_oChannel.connect();
+
+ m_oSftpChannel = (ChannelSftp) m_oChannel;
+
+ for (int i1 = 0; i1 < 10 && !session.isConnected(); i1++)
+ {
+ try
+ {
+ Thread.sleep(200); // TODO magic number
+ }
+ catch (InterruptedException ex)
+ {
+ // try again?
+ }
+ }
+
+ if (!session.isConnected())
+ throw new RemoteFileSystemException("Can't connect to FTP server");
+
+ m_bConnected = this.session.isConnected();
+ }
+ catch (JSchException ex)
+ {
+ _logger.error("Caught Secure FTP Exception.", ex);
+
+ throw new RemoteFileSystemException(ex);
+ }
}
// TODO set connection Mode [PASSIVE|ACTIVE]using m_bPassive ?
}
- private void checkParms() throws Exception {
+ private void checkParms() throws ConfigurationException
+ {
m_sFtpServer = m_oParms.getAttribute(PARMS_FTP_SERVER);
if (null == m_sFtpServer)
- throw new Exception("No SFTP server specified");
+ throw new ConfigurationException("No SFTP server specified");
m_sUser = m_oParms.getAttribute(PARMS_USER);
if (null == m_sUser)
- throw new Exception("No username specified for SFTP");
+ throw new ConfigurationException("No username specified for SFTP");
m_sPasswd = m_oParms.getAttribute(PARMS_PASSWD);
if (null == m_sPasswd)
- throw new Exception("No password specified for SFTP");
+ throw new ConfigurationException("No password specified for SFTP");
m_sRemoteDir = m_oParms.getAttribute(PARMS_REMOTE_DIR);
if (null == m_sRemoteDir)
@@ -245,18 +284,33 @@
m_sLocalDir = ".";
String sAux = m_oParms.getAttribute(PARMS_PORT);
- m_iPort = (null == sAux) ? 22 : Integer.parseInt(sAux);
+
+ try
+ {
+ m_iPort = (null == sAux) ? 22 : Integer.parseInt(sAux);
+ }
+ catch (Exception ex)
+ {
+ throw new ConfigurationException(ex);
+ }
m_bPassive = false;
sAux = m_oParms.getAttribute(PARMS_PASSIVE);
m_bPassive = (null != sAux) && Boolean.parseBoolean(sAux);
final String certificate = m_oParms.getAttribute(PARMS_CERTIFICATE) ;
+
if (certificate != null)
{
- m_oCertificate = new URL(certificate);
+ try
+ {
+ m_oCertificate = new URL(certificate);
+ }
+ catch (MalformedURLException ex)
+ {
+ throw new ConfigurationException(ex);
+ }
}
- return;
}
/*
@@ -266,8 +320,16 @@
*
* @see org.jboss.soa.esb.util.RemoteFileSystem#deleteRemoteFile(java.lang.String)
*/
- public void deleteRemoteFile(String p_sFile) throws Exception {
- m_oSftpChannel.rm(getRemoteDir() + "/" + new File(p_sFile).getName());
+ public void deleteRemoteFile(String p_sFile) throws RemoteFileSystemException
+ {
+ try
+ {
+ m_oSftpChannel.rm(getRemoteDir() + "/" + new File(p_sFile).getName());
+ }
+ catch (SftpException ex)
+ {
+ throw new RemoteFileSystemException(ex);
+ }
}
/*
@@ -277,8 +339,16 @@
*
* @see org.jboss.soa.esb.util.RemoteFileSystem#remoteDelete(java.io.File)
*/
- public void remoteDelete(File p_oFile) throws Exception {
- m_oSftpChannel.rm(FtpUtils.fileToFtpString(p_oFile));
+ public void remoteDelete(File p_oFile) throws RemoteFileSystemException
+ {
+ try
+ {
+ m_oSftpChannel.rm(FtpUtils.fileToFtpString(p_oFile));
+ }
+ catch (SftpException ex)
+ {
+ throw new RemoteFileSystemException(ex);
+ }
}
/*
@@ -288,22 +358,37 @@
*
* @see org.jboss.soa.esb.util.RemoteFileSystem#getFileListFromRemoteDir(java.lang.String)
*/
- public String[] getFileListFromRemoteDir(String p_sSuffix) throws Exception {
- String sSuffix = (null == p_sSuffix) ? "*" : "*" + p_sSuffix;
- List<String> lFileList = new ArrayList<String>();
- Vector vFileList = m_oSftpChannel.ls(sSuffix);
- if (vFileList != null) {
- for (int i = 0; i < vFileList.size(); i++) {
- Object obj = vFileList.elementAt(i);
- if (obj instanceof LsEntry) {
- SftpATTRS oSftAttr = ((LsEntry) obj).getAttrs();
- if (!oSftAttr.isDir()) {
- lFileList.add(((LsEntry) obj).getFilename());
+ public String[] getFileListFromRemoteDir(String p_sSuffix) throws RemoteFileSystemException
+ {
+ try
+ {
+ String sSuffix = (null == p_sSuffix) ? "*" : "*" + p_sSuffix;
+ List<String> lFileList = new ArrayList<String>();
+ Vector vFileList = m_oSftpChannel.ls(sSuffix);
+
+ if (vFileList != null)
+ {
+ for (int i = 0; i < vFileList.size(); i++)
+ {
+ Object obj = vFileList.elementAt(i);
+
+ if (obj instanceof LsEntry)
+ {
+ SftpATTRS oSftAttr = ((LsEntry) obj).getAttrs();
+ if (!oSftAttr.isDir())
+ {
+ lFileList.add(((LsEntry) obj).getFilename());
+ }
}
}
}
+
+ return (String[]) lFileList.toArray(new String[lFileList.size()]);
}
- return (String[]) lFileList.toArray(new String[lFileList.size()]);
+ catch (SftpException ex)
+ {
+ throw new RemoteFileSystemException(ex);
+ }
}
/*
@@ -313,8 +398,16 @@
*
* @see org.jboss.soa.esb.util.RemoteFileSystem#setRemoteDir(java.lang.String)
*/
- public void setRemoteDir(String p_sDir) throws Exception {
- m_oSftpChannel.cd(p_sDir);
+ public void setRemoteDir(String p_sDir) throws RemoteFileSystemException
+ {
+ try
+ {
+ m_oSftpChannel.cd(p_sDir);
+ }
+ catch (SftpException ex)
+ {
+ throw new RemoteFileSystemException(ex);
+ }
}
/*
@@ -328,19 +421,23 @@
* java.lang.String)
*/
public void renameInRemoteDir(String p_sFrom, String p_sTo)
- throws Exception {
+ throws RemoteFileSystemException
+ {
String sRmtFrom = new File(p_sFrom).getName();
String sRmtTo = new File(p_sTo).getName();
- try {
+ try
+ {
m_oSftpChannel.rename(getRemoteDir() + "/" + sRmtFrom,
getRemoteDir() + "/" + sRmtTo);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
String sMess = this.getClass().getSimpleName()
+ " can't rename in remote directory <" + e.getMessage()
+ ">";
- throw new Exception(sMess);
+ throw new RemoteFileSystemException(sMess);
}
}
@@ -354,15 +451,19 @@
* @see org.jboss.soa.esb.util.RemoteFileSystem#remoteRename(java.io.File,
* java.io.File)
*/
- public void remoteRename(File p_oFrom, File p_oTo) throws Exception {
- try {
+ public void remoteRename(File p_oFrom, File p_oTo) throws RemoteFileSystemException
+ {
+ try
+ {
m_oSftpChannel.rename(FtpUtils.fileToFtpString(p_oFrom), FtpUtils
.fileToFtpString(p_oTo));
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
String sMess = this.getClass().getSimpleName()
+ " can't rename in remote directory <" + e.getMessage()
+ ">";
- throw new Exception(sMess);
+ throw new RemoteFileSystemException(sMess);
}
}
@@ -377,11 +478,19 @@
* @see org.jboss.soa.esb.util.RemoteFileSystem#uploadFile(java.io.File,
* java.lang.String)
*/
- public void uploadFile(File p_oFile, String p_sRemoteName) throws Exception {
- String sRemoteOK = getRemoteDir() + "/" + p_sRemoteName;
- String sRemoteTmp = sRemoteOK + TMP_SUFFIX;
- m_oSftpChannel.put(FtpUtils.fileToFtpString(p_oFile), sRemoteTmp);
- m_oSftpChannel.rename(sRemoteTmp, sRemoteOK);
+ public void uploadFile(File p_oFile, String p_sRemoteName) throws RemoteFileSystemException
+ {
+ try
+ {
+ String sRemoteOK = getRemoteDir() + "/" + p_sRemoteName;
+ String sRemoteTmp = sRemoteOK + TMP_SUFFIX;
+ m_oSftpChannel.put(FtpUtils.fileToFtpString(p_oFile), sRemoteTmp);
+ m_oSftpChannel.rename(sRemoteTmp, sRemoteOK);
+ }
+ catch (SftpException ex)
+ {
+ throw new RemoteFileSystemException(ex);
+ }
}
/*
@@ -394,23 +503,35 @@
* @see org.jboss.soa.esb.util.RemoteFileSystem#downloadFile(java.lang.String,
* java.lang.String)
*/
- public void downloadFile(String p_sFile, String p_sFinalName)
- throws Exception {
+ public void downloadFile(String p_sFile, String p_sFinalName) throws IOException, RemoteFileSystemException
+ {
File oLocalDir = new File(m_sLocalDir);
File oLclFile = File.createTempFile("Rosetta_", TMP_SUFFIX, oLocalDir);
- try {
+ try
+ {
oLclFile.delete();
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
+ _logger.warn("Could not delete file: "+oLclFile, e);
}
// TODO check if we have to set the Transfer Type with JSch impl =>
// m_oXferType
- m_oSftpChannel.get(p_sFile, FtpUtils.fileToFtpString(oLclFile));
-
- File oNew = new File(oLocalDir, p_sFinalName);
- if (oNew.exists())
- oNew.delete();
- oLclFile.renameTo(oNew);
+
+ try
+ {
+ m_oSftpChannel.get(p_sFile, FtpUtils.fileToFtpString(oLclFile));
+
+ File oNew = new File(oLocalDir, p_sFinalName);
+ if (oNew.exists())
+ oNew.delete();
+ oLclFile.renameTo(oNew);
+ }
+ catch (Exception ex)
+ {
+ throw new RemoteFileSystemException(ex);
+ }
}
/*
@@ -418,7 +539,8 @@
*
* @see org.jboss.soa.esb.util.RemoteFileSystem#getRemoteDir()
*/
- public String getRemoteDir() {
+ public String getRemoteDir()
+ {
return m_sRemoteDir;
}
@@ -427,13 +549,16 @@
*
* @see org.jboss.soa.esb.util.RemoteFileSystem#quit()
*/
- public void quit() {
+ public void quit()
+ {
m_oSftpChannel.quit();
}
- private void configTreeFromEpr() throws RemoteFileSystemException {
+ private void configTreeFromEpr() throws ConfigurationException
+ {
m_oParms = new ConfigTree("fromEpr");
- try {
+ try
+ {
m_oParms.setAttribute(RemoteFileSystem.PARMS_FTP_SERVER,
m_sFtpServer);
m_oParms.setAttribute(RemoteFileSystem.PARMS_USER, m_sUser);
@@ -451,8 +576,10 @@
{
m_oParms.setAttribute(RemoteFileSystem.PARMS_CERTIFICATE, m_oCertificate.toString());
}
- } catch (Exception e) {
- throw new RemoteFileSystemException(e);
+ }
+ catch (Exception e)
+ {
+ throw new ConfigurationException(e);
}
}
More information about the jboss-svn-commits
mailing list