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        /**
023         * @see railo.runtime.net.ftp.FTPPool#get(railo.runtime.net.ftp.FTPConnection)
024         */
025        public FTPClient get(FTPConnection conn) throws IOException, ApplicationException {
026            FTPClient client = _get(conn).getClient();
027            if(client==null)throw new ApplicationException("can't connect to server ["+conn.getServer()+"]");
028            
029            FTPWrap.setConnectionSettings(client,conn);
030            
031            return client;
032        }
033    
034        /**
035         * returns a client from given connection
036         * @param conn
037         * @return 
038         * @return matching wrap
039         * @throws IOException
040         * @throws ApplicationException
041         */
042        protected FTPWrap _get(FTPConnection conn) throws IOException, ApplicationException {
043            FTPWrap wrap=null;
044            
045          
046            
047            if(!conn.hasLoginData()) {
048                    if(StringUtil.isEmpty(conn.getName())){
049                            throw new ApplicationException("can't connect ftp server, missing connection defintion");
050                    }
051                    
052                    wrap=wraps.get(conn.getName());
053                if(wrap==null) {
054                    throw new ApplicationException("can't connect ftp server, missing connection ["+conn.getName()+"]");
055                }
056                else if(!wrap.getClient().isConnected() || wrap.getConnection().getTransferMode()!=conn.getTransferMode()) {
057                    wrap.reConnect(conn.getTransferMode());
058                }
059                return wrap;
060            }
061            String name=conn.hasName()?conn.getName():"__noname__";
062            
063            wrap=wraps.get(name);
064            if(wrap!=null) {
065                if(conn.loginEquals(wrap.getConnection())) {
066                    return _get(new FTPConnectionImpl(name,null,null,null,conn.getPort(),conn.getTimeout(),conn.getTransferMode(),conn.isPassive(),
067                                    conn.getProxyServer(),conn.getProxyPort(),conn.getProxyUser(),conn.getProxyPassword()));
068                }
069                disconnect(wrap.getClient());
070            }
071                    
072            wrap=new FTPWrap(conn);
073            wraps.put(name,wrap);
074            
075            
076          
077                    
078                    
079                    
080                    
081            return wrap;
082        }
083    
084        /**
085         * disconnect a client
086         * @param client
087         */
088        private void disconnect(FTPClient client) {
089            try {
090                if(client!=null && client.isConnected()) {
091                            client.quit();
092                    client.disconnect();
093                }
094            }
095            catch(IOException ioe) {}
096        }
097    
098        /**
099         * @see railo.runtime.net.ftp.FTPPool#remove(railo.runtime.net.ftp.FTPConnection)
100         */
101        public FTPClient remove(FTPConnection conn) {
102            return remove(conn.getName());
103        }
104    
105        /**
106         * @see railo.runtime.net.ftp.FTPPool#remove(java.lang.String)
107         */
108        public FTPClient remove(String name) {
109            FTPWrap wrap=wraps.remove(name);
110            if(wrap==null) return null;
111            
112            FTPClient client = wrap.getClient();
113            disconnect(client);
114            return client;
115        }
116    
117        /**
118         * @see railo.runtime.net.ftp.FTPPool#clear()
119         */
120        public void clear() {
121            if(!wraps.isEmpty()) {
122                Iterator<Entry<String, FTPWrap>> it = wraps.entrySet().iterator();
123                while(it.hasNext()) {
124                    try {
125                        Entry<String, FTPWrap> entry = it.next();
126                        FTPWrap wrap=entry.getValue();
127                        if(wrap!=null && wrap.getClient().isConnected())wrap.getClient().disconnect();
128                    } catch (IOException e) {}
129                }
130                wraps.clear();
131            }
132        }
133    }