博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FTPClient与commons-pool2
阅读量:7105 次
发布时间:2019-06-28

本文共 4341 字,大约阅读时间需要 14 分钟。

hot3.png

设计模式

FTPClient与commons-pool2整合过程中涉及到的设计模式有:工厂方法模式、单例模式。

工厂方法模式

工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个,工厂方法让实例化推迟到子类。当需要增加一个新的产品时,只需要增加一个具体的产品类和与之对应的具体工厂即可,无须修改原有系统。但是由于每新增一个新产品时就需要增加两个类,这样会导致系统的复杂度增加。

094704_1C4q_657390.jpg

详细信息见:

单例模式

单例模式确保一个类只有一个实例,并提供一个全局访问点。

单例模式有五种实现方法:双重校验锁、枚举、静态内部类、恶汉、懒汉。

在这里选择静态内部类。

详细信息见:

代码

主要有FTPClientFactory和FTPClientUtil两个类

使用commons-pool2
import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.pool2.BasePooledObjectFactory;import org.apache.commons.pool2.PooledObject;import org.apache.commons.pool2.impl.DefaultPooledObject;import java.util.Properties;/** * Created by udbwcso on 2016/3/14. */public class FTPClientFactory extends BasePooledObjectFactory
 {    private final String host;    private final int port;    private final String username;    private final String password;    public FTPClientFactory(final String host, final int port, final String username, final String password){        this.host = host;        this.port = port;        this.username = username;        this.password = password;    }    public FTPClientFactory(Properties properties){        this.host = properties.getProperty("host");        this.port = Integer.parseInt(properties.getProperty("port"));        this.username = properties.getProperty("username");        this.password = properties.getProperty("password");    }    /**     * Creates an object instance, to be wrapped in a {@link PooledObject}.     * 

This method must support concurrent, multi-threaded     * activation.

     *     * @return an instance to be served by the pool     * @throws Exception if there is a problem creating a new instance,     *                   this will be propagated to the code requesting an object.     */    public FTPClient create() throws Exception {        FTPClient client = new FTPClient();        client.connect(host, port);        client.login(username, password);        return client;    }    /**     * Wrap the provided instance with an implementation of     * {@link PooledObject}.     *     * @param obj the instance to wrap     * @return The provided instance, wrapped by a {@link PooledObject}     */    public PooledObject
 wrap(FTPClient obj) {        return new DefaultPooledObject
(obj);    }    /**     * destroy object     */    @Override    public void destroyObject(PooledObject
 p) throws Exception {        FTPClient ftpClient = p.getObject();        ftpClient.logout();        ftpClient.disconnect();    }}
工具类
import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.pool2.ObjectPool;import org.apache.commons.pool2.impl.GenericObjectPool;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** * Created by udbwcso on 2016/3/15. */public class FTPClientUtil {    private static final String FTP_PROPERTIES = "/ftp.properties";    private FTPClientUtil(){    }    public static FTPClientUtil getInstance(){        return SingletonHolder.instance;    }    /**     * Returns the pathname of the current working directory.     * @return     */    public String getWorkingDirectory() throws Exception {        FTPClient client = getClientPool().borrowObject();        String workingDir = client.printWorkingDirectory();        getClientPool().returnObject(client);        return workingDir;    }    private ObjectPool
 getClientPool(){        return SingletonHolder.POOL;    }    private static class SingletonHolder {        private static final ObjectPool
 POOL;        static {            InputStream resourceAsStream = FTPClientUtil.class.getResourceAsStream(FTP_PROPERTIES);            Properties p = null;            if (resourceAsStream != null) {                p = new Properties();                try {                    p.load(resourceAsStream);                } catch (IOException e) {                } finally {                    try {                        resourceAsStream.close();                    } catch (IOException e) {                        // Ignored                    }                }            }            POOL = new GenericObjectPool
(new FTPClientFactory(p));        }        private static FTPClientUtil instance = new FTPClientUtil();    }}

目前只有获取工作目录的方法,有待后续开发。

转载于:https://my.oschina.net/u/657390/blog/638507

你可能感兴趣的文章
如何将两个列表变成一个python字典
查看>>
js math函数解释
查看>>
2018年7月28日笔记
查看>>
Implementing multi-level trees in MS SQL Server
查看>>
重温微积分1|散度定理的证明
查看>>
linux磁盘管理系列二:软RAID的实现
查看>>
我的重构步骤:重构两份过程一致、中间数据类型不一致的超长函数
查看>>
yii框中findOne()的用法
查看>>
FOI冬令营 Day1
查看>>
Linux源码学习(5) 2013-2-27
查看>>
基于python的web应用开发-添加关注者
查看>>
聊聊Dubbo(六):核心源码-Filter链原理
查看>>
Ubuntu下使用Docker搭建MySQL步骤备忘
查看>>
透视投影矩阵理解
查看>>
JSch远程执行脚本
查看>>
迷宫求解
查看>>
pycharm修改hosts文件
查看>>
iOS代理模式(delegate)的使用
查看>>
冯.诺依曼结构与哈佛结构
查看>>
类继承
查看>>