From 13890dab140076c446eaf75a5d3afa4c1a7ff1d0 Mon Sep 17 00:00:00 2001 From: Gunnar Hillert Date: Sat, 23 Jun 2012 16:23:43 -0400 Subject: [PATCH] INT-2521 - SFTP Adapter Session Factory Doc Improvement * fix Missing documentation of "enableDaemonThread" property of the SFTP session factory * Reference documentation clean-up * Document all properties of the DefaultSftpSessionFactory --- .../session/DefaultSftpSessionFactory.java | 201 ++++++++--- src/reference/docbook/sftp.xml | 334 ++++++++++++------ 2 files changed, 378 insertions(+), 157 deletions(-) diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java index e1f335c962..3f33c12e32 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java @@ -38,10 +38,12 @@ import com.jcraft.jsch.UserInfo; * @author Josh Long * @author Mario Gray * @author Oleg Zhurakousky + * @author Gunnar Hillert + * * @since 2.0 */ public class DefaultSftpSessionFactory implements SessionFactory { - + private volatile String host; private volatile int port = 22; // the default @@ -55,94 +57,199 @@ public class DefaultSftpSessionFactory implements SessionFactory { private volatile Resource privateKey; private volatile String privateKeyPassphrase; - + private volatile Properties sessionConfig; - + private volatile Proxy proxy; - + private volatile SocketFactory socketFactory; - + private volatile Integer timeout; - + private volatile String clientVersion; - + private volatile String hostKeyAlias; - + private volatile Integer serverAliveInterval; - + private volatile Integer serverAliveCountMax; - + private volatile Boolean enableDaemonThread; - + private final JSch jsch = new JSch(); - public void setHost(String host) { + /** + * The url of the host you want connect to. This is a mandatory property. + * + * @see JSch#getSession(String, String, int) + */ + public void setHost(String host) { this.host = host; } + /** + * The port over which the SFTP connection shall be established. If not specified, + * this value defaults to 22. If specified, this properties must + * be a positive number. + * + * @see JSch#getSession(String, String, int) + */ public void setPort(int port) { this.port = port; } + /** + * The remote user to use. This is a mandatory property. + * + * @see JSch#getSession(String, String, int) + */ public void setUser(String user) { this.user = user; } + /** + * The password to authenticate against the remote host. If a password is + * not provided, then the {@link DefaultSftpSessionFactory#privateKey} is + * mandatory. + * + * @see com.jcraft.jsch.Session#setPassword(String) + */ public void setPassword(String password) { this.password = password; } + /** + * Specifies the filename that will be used to create a host key repository. + * The resulting file has the same format as OpenSSH's known_hosts file. + * + * @see JSch#setKnownHosts(String) + */ public void setKnownHosts(String knownHosts) { this.knownHosts = knownHosts; } + /** + * Allows you to set a {@link Resource}, which represents the location of the + * private key used for authenticating against the remote host. If the privateKey + * is not provided, then the {@link DefaultSftpSessionFactory#setPassword(String)} + * property is mandatory. + * + * @see JSch#addIdentity(String) + * @see JSch#addIdentity(String, String) + * + */ public void setPrivateKey(Resource privateKey) { this.privateKey = privateKey; } + /** + * The password for the private key. Optional. + * + * @see JSch#addIdentity(String, String) + */ public void setPrivateKeyPassphrase(String privateKeyPassphrase) { this.privateKeyPassphrase = privateKeyPassphrase; } - + + /** + * Using {@link Properties}, you can set additional configuration settings on + * the underlying JSch {@link com.jcraft.jsch.Session}. + * + * @see com.jcraft.jsch.Session#setConfig(Properties) + */ public void setSessionConfig(Properties sessionConfig) { this.sessionConfig = sessionConfig; } - - public void setProxy(Proxy proxy){ + + /** + * Allows for specifying a JSch-based {@link Proxy}. If set, then the proxy + * object is used to create the connection to the remote host. + * + * @see com.jcraft.jsch.Session#setProxy(Proxy) + */ + public void setProxy(Proxy proxy){ this.proxy = proxy; } - - public void setSocketFactory(SocketFactory socketFactory){ + + /** + * Allows you to pass in a {@link SocketFactory}. The socket factory is used + * to create a socket to the target host. When a {@link Proxy} is used, the + * socket factory is passed to the proxy. By default plain TCP sockets are used. + * + * @see com.jcraft.jsch.Session#setSocketFactory(SocketFactory) + */ + public void setSocketFactory(SocketFactory socketFactory){ this.socketFactory = socketFactory; } - - public void setTimeout(Integer timeout) { - this.timeout = timeout; - } - - public void setClientVersion(String clientVersion){ - this.clientVersion = clientVersion; - } - - public void setHostKeyAlias(String hostKeyAlias){ - this.hostKeyAlias = hostKeyAlias; - } - - public void setServerAliveInterval(Integer serverAliveInterval){ - this.serverAliveInterval = serverAliveInterval; - } - - public void setServerAliveCountMax(Integer serverAliveCountMax){ - this.serverAliveCountMax = serverAliveCountMax; - } - - public void setEnableDaemonThread(Boolean enableDaemonThread){ - this.enableDaemonThread = enableDaemonThread; - } - - + + /** + * The timeout property is used as the socket timeout parameter, as well as + * the default connection timeout. Defaults to 0, which means, + * that no timeout will occur. + * + * @see com.jcraft.jsch.Session#setTimeout(int) + */ + public void setTimeout(Integer timeout) { + this.timeout = timeout; + } + + /** + * Allows you to set the client version property. It's default depends on the + * underlying JSch version but it will look like SSH-2.0-JSCH-0.1.45 + * + * @see com.jcraft.jsch.Session#setClientVersion(String) + */ + public void setClientVersion(String clientVersion){ + this.clientVersion = clientVersion; + } + + /** + * Sets the host key alias, used when comparing the host key to the known + * hosts list. + * + * @see com.jcraft.jsch.Session#setHostKeyAlias(String) + */ + public void setHostKeyAlias(String hostKeyAlias){ + this.hostKeyAlias = hostKeyAlias; + } + + /** + * Sets the timeout interval (milliseconds) before a server alive message is + * sent, in case no message is received from the server. + * + * @see com.jcraft.jsch.Session#setServerAliveInterval(int) + */ + public void setServerAliveInterval(Integer serverAliveInterval){ + this.serverAliveInterval = serverAliveInterval; + } + + /** + * Specifies the number of server-alive messages, which will be sent without + * any reply from the server before disconnecting. If not set, this property + * defaults to 1. + * + * @see com.jcraft.jsch.Session#setServerAliveCountMax(int) + */ + public void setServerAliveCountMax(Integer serverAliveCountMax){ + this.serverAliveCountMax = serverAliveCountMax; + } + + /** + * If true, all threads will be daemon threads. If set to false, + * normal non-daemon threads will be used. This property will be set on the + * underlying {@link com.jcraft.jsch.Session} using + * {@link com.jcraft.jsch.Session#setDaemonThread(boolean)}. There, this + * property will default to false, if not explicitly set. + * + * @see com.jcraft.jsch.Session#setDaemonThread(boolean) + */ + public void setEnableDaemonThread(Boolean enableDaemonThread){ + this.enableDaemonThread = enableDaemonThread; + } + + public Session getSession() { Assert.hasText(this.host, "host must not be empty"); Assert.hasText(this.user, "user must not be empty"); @@ -160,9 +267,9 @@ public class DefaultSftpSessionFactory implements SessionFactory { } } - private com.jcraft.jsch.Session initJschSession() throws Exception { + private com.jcraft.jsch.Session initJschSession() throws Exception { JSch.setLogger(new JschLogger()); - + if (this.port <= 0) { this.port = 22; } @@ -183,12 +290,12 @@ public class DefaultSftpSessionFactory implements SessionFactory { com.jcraft.jsch.Session jschSession = this.jsch.getSession(this.user, this.host, this.port); if (this.sessionConfig != null){ jschSession.setConfig(this.sessionConfig); - } + } if (StringUtils.hasText(this.password)) { jschSession.setPassword(this.password); } jschSession.setUserInfo(new OptimisticUserInfoImpl(this.password)); - + try { if (proxy != null){ jschSession.setProxy(proxy); diff --git a/src/reference/docbook/sftp.xml b/src/reference/docbook/sftp.xml index 8cf96ab3be..72214bf3ef 100644 --- a/src/reference/docbook/sftp.xml +++ b/src/reference/docbook/sftp.xml @@ -8,71 +8,218 @@
Introduction - The Secure File Transfer Protocol (SFTP) is a network protocol which allows you to transfer + The Secure File Transfer Protocol (SFTP) is a network protocol which allows you to transfer files between two computers on the Internet over any reliable stream. - + The SFTP protocol requires a secure channel, such as SSH, as well as visibility to a client's identity throughout the SFTP session. - + Spring Integration supports sending and receiving files over SFTP by providing three client side endpoints: Inbound Channel Adapter, Outbound Channel Adapter, and Outbound Gateway It also provides convenient namespace configuration to define these client components. - +
- -
- SFTP Session Factory - - Before configuring SFTP adapters you must configure an SFTP Session Factory. You can configure - the SFTP Session Factory via a regular bean definition: - Below is a basic configuration: - - - - - - - -]]> - - - Every time an adapter requests a session object from its SessionFactory the session is - returned from a session pool maintained by a caching wrapper around the factory. A Session in the session pool might go stale - (if it has been disconnected by the server due to inactivity) so the SessionFactory - will perform validation to make sure that it never returns a stale session to the adapter. If a stale session was encountered, - it will be removed from the pool, and a new one will be created. - - If you experience connectivity problems and would like to trace Session creation as well as see which Sessions are - polled you may enable it by setting the logger to TRACE level (e.g., log4j.category.org.springframework.integration.file=TRACE) - - - Now all you need to do is inject this SFTP Session Factory into your adapters. +
+ SFTP Session Factory + + Before configuring SFTP adapters, you must configure an SFTP Session + Factory. You can configure the SFTP Session + Factory via a regular bean definition: + + + + + + + +]]> + + + Every time an adapter requests a session object from its + SessionFactory, a new SFTP session is being + created. Under the covers, the SFTP Session Factory relies on the + JSch library to provide + the SFTP capabilities. + + + However, Spring Integration also supports the caching of SFTP + sessions, please see for more information. + + + If you experience connectivity problems and would like to trace Session + creation as well as see which Sessions are polled you may enable it by + setting the logger to TRACE level (e.g., log4j.category.org.springframework.integration.file=TRACE). + Please also see . + + + Now all you need to do is inject this SFTP Session Factory into your adapters. + + + A more practical way to provide values for the SFTP Session Factory would be via Spring's + property + placeholder support. + +
+ Configuration Properties + + Below you will find all properties that are exposed by the + DefaultSftpSessionFactory. + + clientVersion + + Allows you to set the client version property. It's default + depends on the underlying JSch version but it will look like: + SSH-2.0-JSCH-0.1.45 + + enableDaemonThread + + If true, all threads will be daemon threads. If set + to false, normal non-daemon threads will be used + instead. This property will be set on the underlying + JSch + Session. There, this property will default + to false, if not explicitly set. + + + host + + The url of the host you want connect to. Mandatory. + + hostKeyAlias + + Sets the host key alias, used when comparing the host key to the + known hosts list. + + knownHosts + + Specifies the filename that will be used to create a host key + repository. The resulting file has the same format as OpenSSH's + known_hosts file. + + password + + The password to authenticate against the remote host. If a + password is not provided, then the + privateKey property is mandatory. + + port + + The port over which the SFTP connection shall be established. If + not specified, this value defaults to 22. If specified, + this properties must be a positive number. + + privateKey + + Allows you to set a + Resource, + which represents the location of the private key used for + authenticating against the remote host. If the + privateKey is not provided, then the + password property is mandatory. + + privateKeyPassphrase + + The password for the private key. Optional. + + + proxy + + Allows for specifying a JSch-based + Proxy. + If set, then the proxy object is used to create the connection to + the remote host. + + serverAliveCountMax + + Specifies the number of server-alive messages, which will be sent + without any reply from the server before disconnecting. If not + set, this property defaults to 1. + + serverAliveInterval + + Sets the timeout interval (milliseconds) before a server alive + message is sent, in case no message is received from the server. + + sessionConfig + + Using Properties, you can set additional + configuration setting on the underlying JSch Session. + + socketFactory + + Allows you to pass in a + SocketFactory. + The socket factory is used to create a socket to the target host. + When a proxy is used, the socket factory is passed to the proxy. + By default plain TCP sockets are used. + + timeout + + The timeout property is used as the socket timeout parameter, as + well as the default connection timeout. Defaults to 0, which means, + that no timeout will occur. + + user + + The remote user to use. Mandatory. + +
+
+ +
+ SFTP Session Caching + + As of version 2.1 we've exposed more flexibility with regard to session management for remote file adapters (e.g., FTP, SFTP etc). + In previous versions the sessions were cached automatically by default. We did expose a cache-sessions attribute for + disabling the auto caching, but that solution did not provide a way to configure other session caching attributes. For example, one + of the requested features was to support a limit on the number of sessions created since a remote server may impose a limit on the + number of client connections. To support that requirement and other configuration options, we decided to promote explicit definition + of the CachingSessionFactory instance. That provides the sessionCacheSize and sessionWaitTimeout + properties. As its name suggests, the sessionCacheSize property controls how many active sessions this adapter will + maintain in its cache (the DEFAULT is unbounded). If the sessionCacheSize threshold has been reached, any attempt to + acquire another session will block until either one of the cached sessions becomes available or until the wait time for a Session + expires (the DEFAULT wait time is Integer.MAX_VALUE). The sessionWaitTimeout property enables configuration of that value. + + + If you want your Sessions to be cached, simply configure your default Session Factory as described above and then + wrap it in an instance of CachingSessionFactory where you may provide those additional properties. + + + + + + + + + +]]> + + In the above example you see a CachingSessionFactory created with the + sessionCacheSize set to 10 and the sessionWaitTimeout set to 1 second (its value is in millliseconds). + - - - A more practical way to provide values for the SFTP Session Factory would be via Spring's property - placeholder support (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer) - -
- +
SFTP Inbound Channel Adapter - The SFTP Inbound Channel Adapter is a special listener that will connect to the server and listen for + The SFTP Inbound Channel Adapter is a special listener that will connect to the server and listen for the remote directory events (e.g., new file created) at which point it will initiate a file transfer. - + ]]> As you can see from the configuration above you can configure the SFTP Inbound Channel Adapter via the - inbound-channel-adapter element while also providing values for various attributes such as local-directory + inbound-channel-adapter element while also providing values for various attributes such as local-directory - where files are going to be transferred TO and remote-directory - the remote source directory where files are going to be transferred FROM - - as well as other attributes including a session-factory reference to the bean we configured earlier. + as well as other attributes including a session-factory reference to the bean we configured earlier. - By default the transferred file will carry the same name as the original file. If you want to override this behavior you - can set the local-filename-generator-expression attribute which allows you to provide a SpEL Expression to generate - the name of the local file. Unlike outbound gateways and adapters where the root object of the SpEL Evaluation Context + By default the transferred file will carry the same name as the original file. If you want to override this behavior you + can set the local-filename-generator-expression attribute which allows you to provide a SpEL Expression to generate + the name of the local file. Unlike outbound gateways and adapters where the root object of the SpEL Evaluation Context is a Message, this inbound adapter does not yet have the Message at the time of evaluation since that's what it ultimately generates with the transferred file as its payload. So, the root object of the SpEL Evaluation Context is the original name of the remote file (String). @@ -108,13 +255,13 @@ xsi:schemaLocation="http://www.springframework.org/schema/integration/sftp list of files. - Please refer to the schema for more detail on these attributes. + Please refer to the schema for more detail on these attributes. - + - It is also important to understand that SFTP Inbound Channel Adapter is a Polling Consumer and therefore + It is also important to understand that SFTP Inbound Channel Adapter is a Polling Consumer and therefore you must configure a poller (either a global default or a local sub-element). - Once the file has been transferred to a local directory, a Message with java.io.File as its + Once the file has been transferred to a local directory, a Message with java.io.File as its payload type will be generated and sent to the channel identified by the channel attribute. @@ -128,54 +275,54 @@ xsi:schemaLocation="http://www.springframework.org/schema/integration/sftp and filter attributes. If you need a custom filter implementation simply include a reference in your adapter via the filter attribute. - + - + ]]>
- +
SFTP Outbound Channel Adapter - + - The SFTP Outbound Channel Adapteris a special MessageHandler that will connect to the - remote directory and will initiate a file transfer for every file it will receive as the payload of an incoming Message. - It also supports several representations of the File so you are not limited to the File object. - Similar to the FTP outbound adapter, the SFTP Outbound Channel Adapter supports the following payloads: - 1) java.io.File - the actual file object; 2) byte[] - byte array that represents + The SFTP Outbound Channel Adapteris a special MessageHandler that will connect to the + remote directory and will initiate a file transfer for every file it will receive as the payload of an incoming Message. + It also supports several representations of the File so you are not limited to the File object. + Similar to the FTP outbound adapter, the SFTP Outbound Channel Adapter supports the following payloads: + 1) java.io.File - the actual file object; 2) byte[] - byte array that represents the file contents; 3) java.lang.String - text that represents the file contents. - + ]]> - - As you can see from the configuration above you can configure the SFTP Outbound Channel Adapter via - the outbound-channel-adapter element. - Please refer to the schema for more detail on these attributes. + + As you can see from the configuration above you can configure the SFTP Outbound Channel Adapter via + the outbound-channel-adapter element. + Please refer to the schema for more detail on these attributes. SpEL and the SFTP Outbound Adapter - As with many other components in Spring Integration, you can benefit from the Spring Expression Language (SpEL) support when configuring - an SFTP Outbound Channel Adapter, by specifying two attributes remote-directory-expression and - remote-filename-generator-expression (see above). The expression evaluation context will have the Message as its root object, thus allowing - you to provide expressions which can dynamically compute the file name or the existing directory path + As with many other components in Spring Integration, you can benefit from the Spring Expression Language (SpEL) support when configuring + an SFTP Outbound Channel Adapter, by specifying two attributes remote-directory-expression and + remote-filename-generator-expression (see above). The expression evaluation context will have the Message as its root object, thus allowing + you to provide expressions which can dynamically compute the file name or the existing directory path based on the data in the Message (either from 'payload' or 'headers'). In the example above we are defining - the remote-filename-generator-expression attribute with an expression + the remote-filename-generator-expression attribute with an expression value that computes the file name based on its original name while also appending a suffix: '-foo'. @@ -314,47 +461,14 @@ xsi:schemaLocation="http://www.springframework.org/schema/integration/sftp SFTP/JSCH Logging Since we use JSch libraries (http://www.jcraft.com/jsch/) to provide SFTP support, at times you may require - more information from the JSch API itself, especially if something is not working properly - (e.g., Authentication exceptions). Unfortunately JSch does not use commons-logging but instead - relies on custom implementations of their com.jcraft.jsch.Logger interface. - As of Spring Integration 2.0.1, we have implemented this interface. So, now all you need to do to enable - JSch logging is to configure your logger the way you usually do. For example, here is valid configuration of a + more information from the JSch API itself, especially if something is not working properly + (e.g., Authentication exceptions). Unfortunately JSch does not use commons-logging but instead + relies on custom implementations of their com.jcraft.jsch.Logger interface. + As of Spring Integration 2.0.1, we have implemented this interface. So, now all you need to do to enable + JSch logging is to configure your logger the way you usually do. For example, here is valid configuration of a logger using Log4J. - - - - -
-
- SFTP Session Caching - - As of version 2.1 we've exposed more flexibility with regard to session management for remote file adapters (e.g., FTP, SFTP etc). - In previous versions the sessions were cached automatically by default. We did expose a cache-sessions attribute for - disabling the auto caching, but that solution did not provide a way to configure other session caching attributes. For example, one - of the requested features was to support a limit on the number of sessions created since a remote server may impose a limit on the - number of client connections. To support that requirement and other configuration options, we decided to promote explicit definition - of the CachingSessionFactory instance. That provides the sessionCacheSize and sessionWaitTimeout - properties. As its name suggests, the sessionCacheSize property controls how many active sessions this adapter will - maintain in its cache (the DEFAULT is unbounded). If the sessionCacheSize threshold has been reached, any attempt to - acquire another session will block until either one of the cached sessions becomes available or until the wait time for a Session - expires (the DEFAULT wait time is Integer.MAX_VALUE). The sessionWaitTimeout property enables configuration of that value. - - - If you want your Sessions to be cached, simply configure your default Session Factory as described above and then - wrap it in an instance of CachingSessionFactory where you may provide those additional properties. - - - - - - - - - ]]> - - In the above example you see a CachingSessionFactory created with the - sessionCacheSize set to 10 and the sessionWaitTimeout set to 1 second (its value is in millliseconds). +