1 package com.coldcore.coloradoftp.connection.impl; 2 3 import com.coldcore.coloradoftp.connection.Connection; 4 import com.coldcore.coloradoftp.connection.ConnectionPool; 5 import com.coldcore.coloradoftp.connection.TerminatedException; 6 import com.coldcore.coloradoftp.core.Core; 7 import com.coldcore.coloradoftp.core.CoreStatus; 8 import com.coldcore.coloradoftp.factory.ObjectFactory; 9 import com.coldcore.coloradoftp.factory.ObjectName; 10 import org.apache.log4j.Logger; 11 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Set ; 15 16 21 public class GenericConnectionPool implements ConnectionPool, Runnable { 22 23 private static Logger log = Logger.getLogger(GenericConnectionPool.class); 24 protected Set <Connection> connections; 25 protected Core core; 26 protected Thread thr; 27 protected long sleep; 28 protected boolean running; 29 30 31 public GenericConnectionPool() { 32 sleep = 1000L; 33 } 34 35 36 39 public long getSleep() { 40 return sleep; 41 } 42 43 44 47 public void setSleep(long sleep) { 48 this.sleep = sleep; 49 } 50 51 52 public void initialize() throws Exception { 53 if (connections == null) connections = new HashSet <Connection>(); 54 core = (Core) ObjectFactory.getObject(ObjectName.CORE); 55 56 if (!running) { 58 running = true; 59 thr = new Thread (this); 60 thr.start(); 61 } 62 } 63 64 65 public void add(Connection connection) { 66 synchronized (connections) { 67 connections.add(connection); 68 } 69 } 70 71 72 public void remove(Connection connection) { 73 synchronized (connections) { 74 connections.remove(connection); 75 } 76 } 77 78 79 public int size() { 80 synchronized (connections) { 81 return connections.size(); 82 } 83 } 84 85 86 public void run() { 87 while (running) { 88 89 synchronized (connections) { 90 Connection connection = null; 91 Iterator <Connection> it = connections.iterator(); 92 while (it.hasNext()) 93 try { 94 connection = it.next(); 95 96 if (connection.isDestroyed()) { 98 it.remove(); 99 continue; 100 } 101 102 if (core.getStatus() == CoreStatus.POISONED && !connection.isPoisoned()) { 104 connection.poison(); 105 } 106 107 connection.service(); 109 110 } catch (TerminatedException e) { 111 log.debug(e); 113 try { 114 connection.destroy(); 115 } catch (Throwable ex) {} 116 117 } catch (Throwable e) { 118 log.error("Connection failed", e); 120 try { 121 connection.destroy(); 122 } catch (Throwable ex) {} 123 } 124 } 125 126 try { 127 Thread.sleep(sleep); 128 } catch (Throwable e) {} 129 130 } 131 log.debug("Connection pool thread finished"); 132 } 133 134 135 public void destroy() { 136 running = false; 137 138 synchronized (connections) { 139 for (Connection connection : connections) 140 try { 141 connection.destroy(); 142 } catch (Throwable e) {} 143 144 connections.clear(); 145 } 146 147 try { 149 thr.join(30000); 150 } catch (Throwable e) {} 151 } 152 153 154 public Set <Connection> list() { 155 synchronized (connections) { 156 return new HashSet <Connection>(connections); 157 } 158 } 159 } 160 | Popular Tags |