From c61ae015f4379dee394d9ba45ebfbd3d8019bf3e Mon Sep 17 00:00:00 2001 From: Iwein Fuld Date: Mon, 11 Aug 2008 18:45:29 +0000 Subject: [PATCH] added first attempt at FtpTarget, created FTPClientPool to move connection concerns to. --- .../adapter/ftp/FTPClientFactory.java | 12 +++ .../adapter/ftp/FTPClientPool.java | 98 +++++++++++++++++++ .../integration/adapter/ftp/FtpSource.java | 2 - .../integration/adapter/ftp/FtpTarget.java | 76 ++++++++++++++ 4 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientFactory.java create mode 100644 org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientPool.java create mode 100644 org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpTarget.java diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientFactory.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientFactory.java new file mode 100644 index 0000000000..1e50192576 --- /dev/null +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientFactory.java @@ -0,0 +1,12 @@ +package org.springframework.integration.adapter.ftp; + +import java.io.IOException; +import java.net.SocketException; + +import org.apache.commons.net.ftp.FTPClient; + +public interface FTPClientFactory { + + FTPClient getClient() throws SocketException, IOException; + +} diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientPool.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientPool.java new file mode 100644 index 0000000000..ca93f7f4bd --- /dev/null +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FTPClientPool.java @@ -0,0 +1,98 @@ +package org.springframework.integration.adapter.ftp; + +import java.io.IOException; +import java.net.SocketException; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; +import java.util.Stack; +import java.util.concurrent.ArrayBlockingQueue; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPClientConfig; + +public class FTPClientPool { + + private static final int DEFAULT_POOL_SIZE = 5; + + private final Queue pool; + + private FTPClientConfig config; + + private String host; + + private int port = FTP.DEFAULT_PORT; + + private String user; + + private String pass; + + private FTPClientFactory factory = new DefaultFactory(); + + private final Log log = LogFactory.getLog(this.getClass()); + + public FTPClientPool() { + this(DEFAULT_POOL_SIZE); + } + + public FTPClientPool(int maxPoolSize) { + pool = new ArrayBlockingQueue(maxPoolSize); + } + + public synchronized FTPClient getClient() throws SocketException, IOException { + return pool.isEmpty() ? factory.getClient() : pool.element(); + } + + public synchronized void releaseClient(FTPClient client) { + if (client != null) { + if (!pool.offer(client)) { + try { + client.disconnect(); + } + catch (IOException e) { + log.warn("Error disconnecting ftpclient", e); + } + } + } + } + + private class DefaultFactory implements FTPClientFactory { + + public FTPClient getClient() throws SocketException, IOException { + FTPClient client = new FTPClient(); + client.configure(config); + client.connect(host, port); + client.login(user, pass); + return client; + } + } + + public void setConfig(FTPClientConfig config) { + this.config = config; + } + + public void setHost(String host) { + this.host = host; + } + + public void setPort(int port) { + this.port = port; + } + + public void setUser(String user) { + this.user = user; + } + + public void setPass(String pass) { + this.pass = pass; + } + + public void setFactory(FTPClientFactory factory) { + this.factory = factory; + } +} diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java index 90bfed9829..231a356068 100644 --- a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java @@ -20,9 +20,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpTarget.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpTarget.java new file mode 100644 index 0000000000..fef2f555ed --- /dev/null +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/ftp/FtpTarget.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.adapter.ftp; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.commons.net.ftp.FTPClient; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageDeliveryException; +import org.springframework.integration.message.MessageMapper; +import org.springframework.integration.message.MessageTarget; + +/** + * Target adapter for sending files to an ftp server. + * + * @author Iwein Fuld + * + */ +public class FtpTarget implements MessageTarget { + + private final MessageMapper messageMapper; + + private FTPClientPool ftpClientPool = new FTPClientPool(); + + public FtpTarget(MessageMapper messageMapper) { + this.messageMapper = messageMapper; + } + + public boolean send(Message message) { +boolean sent = false; + File file = messageMapper.mapMessage(message); + if (file.exists()) { + FTPClient client=null; + FileInputStream fileInputStream = null; + try { + fileInputStream = new FileInputStream(file); + client = ftpClientPool.getClient(); + sent = client.storeFile(file.getName(), fileInputStream); + fileInputStream.close(); + } + catch (FileNotFoundException e) { + throw new MessageDeliveryException(message, "File " + file + " lost from local working directory", e); + } + catch (IOException e) { + throw new MessageDeliveryException(message, "Error transferring " + file + + " from local working directory to remote ftp directory", e); + } + finally { + ftpClientPool.releaseClient(client); + } + } + return sent; + } + + public void setFtpClientPool(FTPClientPool ftpClientPool) { + this.ftpClientPool = ftpClientPool; + } + +}