1 package com.coldcore.coloradoftp.connection.impl; 2 3 import com.coldcore.coloradoftp.connection.DataPortListener; 4 import com.coldcore.coloradoftp.connection.DataPortListenerSet; 5 import org.apache.log4j.Logger; 6 7 import java.util.HashSet ; 8 import java.util.Set ; 9 10 13 public class GenericDataPortListenerSet implements DataPortListenerSet { 14 15 private static Logger log = Logger.getLogger(GenericDataPortListenerSet.class); 16 private Set<DataPortListener> listeners; 17 18 19 public GenericDataPortListenerSet(Set<DataPortListener> listeners) { 20 this.listeners = new HashSet <DataPortListener>(listeners); 21 } 22 23 24 public synchronized int bind() { 25 if (listeners.isEmpty()) { 26 log.warn("No data port listeners configured, PASV command will be disabled"); 27 return 0; 28 } 29 30 int bound = 0; 31 for (DataPortListener listener : listeners) 32 try { 33 listener.bind(); 34 bound++; 35 } catch (Throwable e) { 36 log.error("Cannot bind data port listener on port "+listener.getPort()+" (ignoring)", e); 37 } 38 39 if (bound == 0) { 40 log.fatal("Could not bind any of data port listeners"); 41 return 0; 42 } 43 44 log.info("Data port listeners bound: "+bound+"/"+listeners.size()); 45 return bound; 46 } 47 48 49 public synchronized int unbind() { 50 int bound = 0; 51 int unbound = 0; 52 for (DataPortListener listener : listeners) 53 if (listener.isBound()) 54 try { 55 bound++; 56 listener.unbind(); 57 unbound++; 58 } catch (Throwable e) { 59 log.error("Cannot unbind data port listener on port "+listener.getPort()+" (ignoring)", e); 60 } 61 62 log.info("Data port listeners unbound: "+unbound+"/"+bound); 63 return unbound; 64 } 65 66 67 public int boundNumber() { 68 int bound = 0; 69 for (DataPortListener listener : listeners) 70 if (listener.isBound()) bound++; 71 return bound; 72 } 73 74 75 public Set<DataPortListener> list() { 76 return new HashSet <DataPortListener>(listeners); 77 } 78 } 79 | Popular Tags |