title: Not able to run Dyamic FTP example- Spring integration
body: <p>I am trying to run the Dynamic FTP example in Spring Integration. But changed the mode to SFTP. While running the test it is showing the expression evaluation is failed due to below message</p>
  
  <p><em>org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 30 in XML document from class path resource [META-INF/spring/integration/dynamic-ftp-outbound-adapter-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 31; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'int-sftp:outbound-channel-adapter'.</em></p>
  
  <p>My files are as follow</p>
  
  <pre><code>**pom.xml**
  
      &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
        &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
        &lt;groupId&gt;org.springframework.integration.samples&lt;/groupId&gt;
        &lt;artifactId&gt;DynamicFtp&lt;/artifactId&gt;
        &lt;version&gt;0.0.1&lt;/version&gt;
        &lt;name&gt;Dynamic FTP &lt;/name&gt;
        &lt;description&gt;Dynamic FTP&lt;/description&gt;
  
  
        &lt;!-- &lt;repositories&gt;
          &lt;repository&gt;
            &lt;id&gt;repo.spring.io.milestone&lt;/id&gt;
            &lt;name&gt;Spring Framework Maven Milestone Repository&lt;/name&gt;
            &lt;url&gt;https://repo.spring.io/libs-milestone&lt;/url&gt;
          &lt;/repository&gt;
        &lt;/repositories&gt; --&gt;
        &lt;dependencies&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.11&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
            &lt;artifactId&gt;hamcrest-all&lt;/artifactId&gt;
            &lt;version&gt;1.3&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.integration&lt;/groupId&gt;
            &lt;artifactId&gt;spring-integration-sftp&lt;/artifactId&gt;
            &lt;version&gt;4.1.2.RELEASE&lt;/version&gt;
            &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.integration&lt;/groupId&gt;
            &lt;artifactId&gt;spring-integration-ftp&lt;/artifactId&gt;
            &lt;version&gt;4.1.0.RELEASE&lt;/version&gt;
            &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.integration&lt;/groupId&gt;
          &lt;artifactId&gt;spring-integration-core&lt;/artifactId&gt;
          &lt;version&gt;4.1.0.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
  
  
  
  
  
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
            &lt;version&gt;4.1.1.RELEASE&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j&lt;/artifactId&gt;
            &lt;version&gt;1.2.17&lt;/version&gt;
            &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.mockito&lt;/groupId&gt;
            &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
            &lt;version&gt;1.9.5&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
        &lt;/dependencies&gt;
      &lt;/project&gt;
  
   **DynamicFtpChannelResolver**
  
  public class DynamicFtpChannelResolver {
  
      //In production environment this value will be significantly higher
      //This is just to demonstrate the concept of limiting the max number of
      //Dynamically created application contexts we'll hold in memory when we execute
      //the code from a junit
      public static final int MAX_CACHE_SIZE = 2;
  
      private final LinkedHashMap&lt;String, MessageChannel&gt; channels =
                  new LinkedHashMap&lt;String, MessageChannel&gt;() {
  
                      private static final long serialVersionUID = 1L;
  
                      @Override
                      protected boolean removeEldestEntry(
                              Entry&lt;String, MessageChannel&gt; eldest) {
                          //This returning true means the least recently used
                          //channel and its application context will be closed and removed
                          boolean remove = size() &gt; MAX_CACHE_SIZE;
                          if(remove) {
                              MessageChannel channel = eldest.getValue();
                              ConfigurableApplicationContext ctx = contexts.get(channel);
                              if(ctx != null) { //shouldn't be null ideally
                                  ctx.close();
                                  contexts.remove(channel);
                              }
                          }
                          return remove;
                      }
  
                  };
  
      private final Map&lt;MessageChannel, ConfigurableApplicationContext&gt; contexts =
                  new HashMap&lt;MessageChannel, ConfigurableApplicationContext&gt;();
  
  
  
      /**
       * Resolve a customer to a channel, where each customer gets a private
       * application context and the channel is the inbound channel to that
       * application context.
       *
       * @param customer
       * @return a channel
       */
      public MessageChannel resolve(String customer) {
          MessageChannel channel = this.channels.get(customer);
          if (channel == null) {
              channel = createNewCustomerChannel(customer);
          }
          return channel;
      }
  
      private synchronized MessageChannel createNewCustomerChannel(String customer) {
          MessageChannel channel = this.channels.get(customer);
          if (channel == null) {
              ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
                      new String[] { "/META-INF/spring/integration/dynamic-ftp-outbound-adapter-context.xml" },
                      false);
              this.setEnvironmentForCustomer(ctx, customer);
              ctx.refresh();
              channel = ctx.getBean("toFtpChannel", MessageChannel.class);
              this.channels.put(customer, channel);
              //Will works as the same reference is presented always
              this.contexts.put(channel, ctx);
          }
          return channel;
      }
  
      /**
       * Use Spring 3.1. environment support to set properties for the
       * customer-specific application context.
       *
       * @param ctx
       * @param customer
       */
      private void setEnvironmentForCustomer(ConfigurableApplicationContext ctx,
              String customer) {
          StandardEnvironment env = new StandardEnvironment();
          Properties props = new Properties();
          // populate properties for customer
  //      props.setProperty("host", "host.for." + customer);
  //      props.setProperty("user", "user");
  //      props.setProperty("password", "password");
          props.setProperty("host", "172.18.554.23");
          props.setProperty("user", "iuser");
          props.setProperty("port", "22");
          props.setProperty("password", "ipwddd");
  
          props.setProperty("remote.directory", "/u01/dpp/dppuser/ftp/");
          PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
          env.getPropertySources().addLast(pps);
          ctx.setEnvironment(env);
          System.out.println("the properties are set" +pps);
      }
  }
  
  
  **dynamic-ftp-outbound-context.xml**
  
          &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:int="http://www.springframework.org/schema/integration"
              xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
              xmlns:int-sftp="http://www.springframework.org/schema/integration   /sftp"
              xmlns:context="http://www.springframework.org/schema/context"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                  http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                  http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
                  http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/ftp/spring-integration-sftp.xsd"&gt;
  
              &lt;context:property-placeholder /&gt;
  
              &lt;bean id="ftpClientFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"&gt;
                  &lt;property name="host" value="${host}"/&gt;
                  &lt;property name="port" value="${port}"/&gt;
                  &lt;property name="username" value="${user}"/&gt;
                  &lt;property name="password" value="${password}"/&gt;
              &lt;/bean&gt;
  
              &lt;int:channel id="toFtpChannel"/&gt;
  
              &lt;int-sftp:outbound-channel-adapter id="ftpOutbound"
                          channel="toFtpChannel"
                          remote-directory="${remote.directory}"
                          session-factory="sftpSessionFactory"
                          mode="REPLACE"
                          remote-file-separator="/"&gt;
              &lt;int:poller fixed-rate="100" time-unit="MILLISECONDS" max-messages-per-poll="100"/&gt;         
          &lt;/int-sftp:outbound-channel-adapter&gt;            
  
  
                  &lt;bean id="sftpSessionFactory"
                  class="org.springframework.integration.file.remote.session.CachingSessionFactory"&gt;
                  &lt;constructor-arg ref="ftpClientFactory" /&gt;
              &lt;/bean&gt;
  
          &lt;/beans&gt;
      **dyamic-ftp-ountbound-sample-context.xml**
  
          &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:int="http://www.springframework.org/schema/integration"
              xmlns:context="http://www.springframework.org/schema/context"
              xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
                  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
              &lt;bean id="channelResolver" class="com.tcs.DynamicFtp.DynamicFtpChannelResolver" /&gt;
  
              &lt;int:channel id="toDynRouter" /&gt;
  
              &lt;int:router input-channel="toDynRouter"
                  expression="@channelResolver.resolve(headers['customer'])"/&gt;
  
          &lt;/beans&gt;
  
      **The test file**
  
      import static org.hamcrest.Matchers.instanceOf;
      import static org.junit.Assert.assertEquals;
      import static org.junit.Assert.assertThat;
      import static org.junit.Assert.assertTrue;
  
      import java.io.BufferedWriter;
      import java.io.File;
      import java.io.FileWriter;
      import java.net.UnknownHostException;
  
      import org.junit.Test;
      import org.springframework.context.ConfigurableApplicationContext;
  
  
  
   import org.springframework.context.support.ClassPathXmlApplicationContext;
      import org.springframework.integration.support.MessageBuilder;
      import org.springframework.messaging.Message;
      import org.springframework.messaging.MessageChannel;
      import org.springframework.messaging.MessagingException;
  
  
      public class FtpOutboundChannelAdapterSample {
  
          @Test
          public void runDemo() throws Exception{
              ConfigurableApplicationContext ctx =
                  new ClassPathXmlApplicationContext("META-INF/spring/integration  /DynamicFtpOutboundChannelAdapterSample-context.xml");
              MessageChannel channel = ctx.getBean("toDynRouter", MessageChannel.class);
              File file = File.createTempFile("temp", "txt");
  
              BufferedWriter bw = new BufferedWriter(new FileWriter(file));
              bw.write("This is the temporary file content");
              bw.close();
  
  
              Message&lt;File&gt; message = MessageBuilder.withPayload(file)
                              .setHeader("customer", "cust1")
                              .build();
              try {
                  channel.send(message);
              }
              catch (MessagingException e) {
                  assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
                  assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust1"));
              }
              // send another so we can see in the log we don't create the ac again.
              try {
                  channel.send(message);
              }
              catch (MessagingException e) {
                  assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
                  assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust1"));
              }
              // send to a different customer; again, check the log to see a new ac is built
              message = MessageBuilder.withPayload(file)
                      .setHeader("customer", "cust2").build();
              try {
                  channel.send(message);
              }
              catch (MessagingException e) {
                  assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
                  assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust2"));
              }
  
              // send to a different customer; again, check the log to see a new ac is built
              //and the first one created (cust1) should be closed and removed as per the max cache size restriction
              message = MessageBuilder.withPayload(file)
                      .setHeader("customer", "cust3").build();
              try {
                  channel.send(message);
              }
              catch (MessagingException e) {
                  assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
                  assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust3"));
              }
  
              //send to cust1 again, since this one has been invalidated before, we should
              //see a new ac created (with ac of cust2 destroyed and removed)
              message = MessageBuilder.withPayload(file)
                      .setHeader("customer", "cust1").build();
              try {
                  channel.send(message);
              }
              catch (MessagingException e) {
                  assertThat(e.getCause().getCause().getCause(), 
  
      instanceOf(UnknownHostException.class));
                      assertEquals("host.for.cust1", 
  
  e.getCause().getCause().getCause().getMessage());
                  }
  
                  ctx.close();
              }
          }
  </code></pre>