001 package railo.runtime.net.ftp; 002 003 import java.io.IOException; 004 import java.util.HashMap; 005 import java.util.Iterator; 006 import java.util.Map; 007 import java.util.Map.Entry; 008 009 import org.apache.commons.net.ftp.FTPClient; 010 011 import railo.commons.lang.StringUtil; 012 import railo.runtime.exp.ApplicationException; 013 014 /** 015 * Pool of FTP Client 016 */ 017 public final class FTPPoolImpl implements FTPPool { 018 019 Map<String,FTPWrap> wraps=new HashMap<String, FTPWrap>(); 020 //ArrayList arr=new ArrayList(); 021 022 @Override 023 public FTPClient get(FTPConnection conn) throws IOException, ApplicationException { 024 FTPClient client = _get(conn).getClient(); 025 if(client==null)throw new ApplicationException("can't connect to server ["+conn.getServer()+"]"); 026 027 FTPWrap.setConnectionSettings(client,conn); 028 029 return client; 030 } 031 032 /** 033 * returns a client from given connection 034 * @param conn 035 * @return 036 * @return matching wrap 037 * @throws IOException 038 * @throws ApplicationException 039 */ 040 protected FTPWrap _get(FTPConnection conn) throws IOException, ApplicationException { 041 FTPWrap wrap=null; 042 043 044 045 if(!conn.hasLoginData()) { 046 if(StringUtil.isEmpty(conn.getName())){ 047 throw new ApplicationException("can't connect ftp server, missing connection defintion"); 048 } 049 050 wrap=wraps.get(conn.getName()); 051 if(wrap==null) { 052 throw new ApplicationException("can't connect ftp server, missing connection ["+conn.getName()+"]"); 053 } 054 else if(!wrap.getClient().isConnected() || wrap.getConnection().getTransferMode()!=conn.getTransferMode()) { 055 wrap.reConnect(conn.getTransferMode()); 056 } 057 return wrap; 058 } 059 String name=conn.hasName()?conn.getName():"__noname__"; 060 061 wrap=wraps.get(name); 062 if(wrap!=null) { 063 if(conn.loginEquals(wrap.getConnection())) { 064 return _get(new FTPConnectionImpl(name,null,null,null,conn.getPort(),conn.getTimeout(),conn.getTransferMode(),conn.isPassive(), 065 conn.getProxyServer(),conn.getProxyPort(),conn.getProxyUser(),conn.getProxyPassword())); 066 } 067 disconnect(wrap.getClient()); 068 } 069 070 wrap=new FTPWrap(conn); 071 wraps.put(name,wrap); 072 073 074 075 076 077 078 079 return wrap; 080 } 081 082 /** 083 * disconnect a client 084 * @param client 085 */ 086 private void disconnect(FTPClient client) { 087 try { 088 if(client!=null && client.isConnected()) { 089 client.quit(); 090 client.disconnect(); 091 } 092 } 093 catch(IOException ioe) {} 094 } 095 096 @Override 097 public FTPClient remove(FTPConnection conn) { 098 return remove(conn.getName()); 099 } 100 101 @Override 102 public FTPClient remove(String name) { 103 FTPWrap wrap=wraps.remove(name); 104 if(wrap==null) return null; 105 106 FTPClient client = wrap.getClient(); 107 disconnect(client); 108 return client; 109 } 110 111 @Override 112 public void clear() { 113 if(!wraps.isEmpty()) { 114 Iterator<Entry<String, FTPWrap>> it = wraps.entrySet().iterator(); 115 while(it.hasNext()) { 116 try { 117 Entry<String, FTPWrap> entry = it.next(); 118 FTPWrap wrap=entry.getValue(); 119 if(wrap!=null && wrap.getClient().isConnected())wrap.getClient().disconnect(); 120 } catch (IOException e) {} 121 } 122 wraps.clear(); 123 } 124 } 125 }