KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > core > impl > GenericCore


1 /**
2  * @see com.coldcore.coloradoftp.core.Core
3  */

4 package com.coldcore.coloradoftp.core.impl;
5
6 import com.coldcore.coloradoftp.connection.ConnectionPool;
7 import com.coldcore.coloradoftp.connection.ControlConnector;
8 import com.coldcore.coloradoftp.connection.DataPortListenerSet;
9 import com.coldcore.coloradoftp.core.Core;
10 import com.coldcore.coloradoftp.core.CoreStatus;
11 import com.coldcore.coloradoftp.factory.ObjectFactory;
12 import com.coldcore.coloradoftp.factory.ObjectName;
13 import org.apache.log4j.Logger;
14
15 public class GenericCore implements Core {
16
17   private static Logger log = Logger.getLogger(GenericCore.class);
18   protected CoreStatus status;
19   protected ControlConnector controlConnector;
20   protected DataPortListenerSet dataPortListenerSet;
21   protected ConnectionPool controlConnectionPool;
22   protected ConnectionPool dataConnectionPool;
23
24
25   public GenericCore() {
26     status = CoreStatus.STOPPED;
27   }
28
29
30   synchronized public void start() {
31     if (status != CoreStatus.STOPPED) {
32       log.warn("Server was running when start routine was submitted");
33       throw new IllegalStateException JavaDoc("Stop the server first");
34     }
35
36     //Get all required objects
37
controlConnector = (ControlConnector) ObjectFactory.getObject(ObjectName.CONTROL_CONNECTOR);
38     dataPortListenerSet = (DataPortListenerSet) ObjectFactory.getObject(ObjectName.DATA_PORT_LISTENER_SET);
39     controlConnectionPool = (ConnectionPool) ObjectFactory.getObject(ObjectName.CONTROL_CONNECTION_POOL);
40     dataConnectionPool = (ConnectionPool) ObjectFactory.getObject(ObjectName.DATA_CONNECTION_POOL);
41
42     try {
43       //Initialize connection pools
44
controlConnectionPool.initialize();
45       dataConnectionPool.initialize();
46
47       //Bind data port listeners
48
if (!bindDataPortListeners()) throw new Exception JavaDoc();
49
50       //Bind control connector
51
if (!bindControlConnector()) throw new Exception JavaDoc();
52
53     } catch (Throwable JavaDoc e) {
54       //Terminate everything
55
boolean noerrors = terminate();
56
57       if (noerrors) log.info("Server terminated");
58       else log.warn("Server terminated (with errors)");
59
60       throw new RuntimeException JavaDoc("Cannot start server");
61     }
62
63     status = CoreStatus.RUNNING;
64     log.info("Server started");
65   }
66
67
68   /** Terminate everything
69    * @return TRUE if there were no errors, FALSE otherwise
70    */

71   protected boolean terminate() {
72     boolean noerrors = true;
73     if (!unbindControlConnector()) noerrors = false;
74     if (!unbindDataPortListeners()) noerrors = false;
75     dataConnectionPool.destroy();
76     controlConnectionPool.destroy();
77
78     //Wait a bit (just in case)
79
try {
80       Thread.sleep(1000L);
81     } catch (Throwable JavaDoc e) {}
82
83     return noerrors;
84   }
85
86
87   synchronized public void stop() {
88     if (status == CoreStatus.STOPPED) {
89       log.warn("Server was stopped when stop routine was submitted");
90       throw new IllegalStateException JavaDoc("Cannot stop the server, it is stopped already");
91     }
92
93     //Terminate everything
94
boolean noerrors = terminate();
95
96     status = CoreStatus.STOPPED;
97     if (noerrors) log.info("Server stopped");
98     else log.warn("Server stopped (with errors)");
99
100     if (!noerrors) throw new RuntimeException JavaDoc("Abnormal server termination");
101   }
102
103
104   public void poison() {
105     //Setting status to poisoned should tell connection pools and control connector to poison all connections
106
status = CoreStatus.POISONED;
107   }
108
109
110   public CoreStatus getStatus() {
111     return status;
112   }
113
114
115   /** Bind data port listeners to ports. This method ignores those who refuse to bind.
116    * @return TRUE if one or more listeners were bound or if there are no listeners at all, FALSE if all listeners failed
117    */

118   protected boolean bindDataPortListeners() {
119     if (dataPortListenerSet.list().size() == 0) return true;
120     return dataPortListenerSet.bind() > 0;
121   }
122
123
124   /** Unbind data port listeners. This method ignores those who refuse to unbind.
125    * @return TRUE if all bound listeners unbound, FALSE if some listeners failed to be unbound
126    */

127   protected boolean unbindDataPortListeners() {
128     int bound = dataPortListenerSet.boundNumber();
129     int unbound = dataPortListenerSet.unbind();
130     return unbound == bound;
131   }
132
133
134   /** Bind control connector
135    * @return TRUE if connector was bound, FALSE if connector failed
136    */

137   protected boolean bindControlConnector() {
138     try {
139       controlConnector.bind();
140       return true;
141     } catch (Throwable JavaDoc e) {
142       log.fatal("Cannot bind control connector on port "+controlConnector.getPort(), e);
143       return false;
144     }
145   }
146
147
148   /** Unbind control connector
149    * @return TRUE if connector was unbound, FALSE if connector failed to unbind
150    */

151   protected boolean unbindControlConnector() {
152     try {
153       if (controlConnector.isBound()) controlConnector.unbind();
154       return true;
155     } catch (Throwable JavaDoc e) {
156       log.fatal("Cannot unbind control connector on port "+controlConnector.getPort(), e);
157       return false;
158     }
159   }
160 }
161
Popular Tags