KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > connection > impl > GenericConnectionPool


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 JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15
16 /**
17  * @see com.coldcore.coloradoftp.connection.ConnectionPool
18  *
19  * This class is thread safe as it takes care of all synchronizations.
20  */

21 public class GenericConnectionPool implements ConnectionPool, Runnable JavaDoc {
22
23   private static Logger log = Logger.getLogger(GenericConnectionPool.class);
24   protected Set JavaDoc<Connection> connections;
25   protected Core core;
26   protected Thread JavaDoc thr;
27   protected long sleep;
28   protected boolean running;
29
30
31   public GenericConnectionPool() {
32     sleep = 1000L;
33   }
34
35
36   /** Get thread sleep time
37    * @return Time in mills
38    */

39   public long getSleep() {
40     return sleep;
41   }
42
43
44   /** Set thread sleep time
45    * @param sleep Time in mills
46    */

47   public void setSleep(long sleep) {
48     this.sleep = sleep;
49   }
50
51
52   public void initialize() throws Exception JavaDoc {
53     if (connections == null) connections = new HashSet JavaDoc<Connection>();
54     core = (Core) ObjectFactory.getObject(ObjectName.CORE);
55
56     //Start this class
57
if (!running) {
58       running = true;
59       thr = new Thread JavaDoc(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 JavaDoc<Connection> it = connections.iterator();
92         while (it.hasNext())
93           try {
94             connection = it.next();
95
96             //Remove if destroyed
97
if (connection.isDestroyed()) {
98               it.remove();
99               continue;
100             }
101
102             //If a server is poisoned then spread the poison to all its connections
103
if (core.getStatus() == CoreStatus.POISONED && !connection.isPoisoned()) {
104               connection.poison();
105             }
106
107             //Service connection
108
connection.service();
109
110           } catch (TerminatedException e) {
111             //Normal termination (exception has a message)
112
log.debug(e);
113             try {
114               connection.destroy();
115             } catch (Throwable JavaDoc ex) {}
116
117           } catch (Throwable JavaDoc e) {
118             //Failed connection
119
log.error("Connection failed", e);
120             try {
121               connection.destroy();
122             } catch (Throwable JavaDoc ex) {}
123           }
124       }
125
126       try {
127         Thread.sleep(sleep);
128       } catch (Throwable JavaDoc 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 JavaDoc e) {}
143
144       connections.clear();
145     }
146
147     //Wait for this class to stop (just in case)
148
try {
149       thr.join(30000);
150     } catch (Throwable JavaDoc e) {}
151   }
152
153
154   public Set JavaDoc<Connection> list() {
155     synchronized (connections) {
156       return new HashSet JavaDoc<Connection>(connections);
157     }
158   }
159 }
160
Popular Tags