FTPClient與commons-pool2整合過程當中涉及到的設計模式有:工廠方法模式、單例模式。java
工廠方法模式定義了一個建立對象的接口,但由子類決定要實例化的類是哪個,工廠方法讓實例化推遲到子類。當須要增長一個新的產品時,只須要增長一個具體的產品類和與之對應的具體工廠便可,無須修改原有系統。可是因爲每新增一個新產品時就須要增長兩個類,這樣會致使系統的複雜度增長。git
詳細信息見:工廠模式
github
單例模式確保一個類只有一個實例,並提供一個全局訪問點。apache
單例模式有五種實現方法:雙重校驗鎖、枚舉、靜態內部類、惡漢、懶漢。設計模式
在這裏選擇靜態內部類。app
詳細信息見:單例模式
ide
主要有FTPClientFactory和FTPClientUtil兩個類工具
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<FTPClient> { 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}. * <p>This method <strong>must</strong> support concurrent, multi-threaded * activation.</p> * * @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<FTPClient> wrap(FTPClient obj) { return new DefaultPooledObject<FTPClient>(obj); } /** * destroy object */ @Override public void destroyObject(PooledObject<FTPClient> 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<FTPClient> getClientPool(){ return SingletonHolder.POOL; } private static class SingletonHolder { private static final ObjectPool<FTPClient> 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<FTPClient>(new FTPClientFactory(p)); } private static FTPClientUtil instance = new FTPClientUtil(); } }
目前只有獲取工做目錄的方法,有待後續開發。this
代碼
spa