added first attempt at FtpTarget, created FTPClientPool to move connection concerns to.
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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<FTPClient> 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<FTPClient>(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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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<?, File> messageMapper;
|
||||
|
||||
private FTPClientPool ftpClientPool = new FTPClientPool();
|
||||
|
||||
public FtpTarget(MessageMapper<?, File> 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user