1 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 ("Stop the server first"); 34 } 35 36 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 controlConnectionPool.initialize(); 45 dataConnectionPool.initialize(); 46 47 if (!bindDataPortListeners()) throw new Exception (); 49 50 if (!bindControlConnector()) throw new Exception (); 52 53 } catch (Throwable e) { 54 boolean noerrors = terminate(); 56 57 if (noerrors) log.info("Server terminated"); 58 else log.warn("Server terminated (with errors)"); 59 60 throw new RuntimeException ("Cannot start server"); 61 } 62 63 status = CoreStatus.RUNNING; 64 log.info("Server started"); 65 } 66 67 68 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 try { 80 Thread.sleep(1000L); 81 } catch (Throwable 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 ("Cannot stop the server, it is stopped already"); 91 } 92 93 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 ("Abnormal server termination"); 101 } 102 103 104 public void poison() { 105 status = CoreStatus.POISONED; 107 } 108 109 110 public CoreStatus getStatus() { 111 return status; 112 } 113 114 115 118 protected boolean bindDataPortListeners() { 119 if (dataPortListenerSet.list().size() == 0) return true; 120 return dataPortListenerSet.bind() > 0; 121 } 122 123 124 127 protected boolean unbindDataPortListeners() { 128 int bound = dataPortListenerSet.boundNumber(); 129 int unbound = dataPortListenerSet.unbind(); 130 return unbound == bound; 131 } 132 133 134 137 protected boolean bindControlConnector() { 138 try { 139 controlConnector.bind(); 140 return true; 141 } catch (Throwable e) { 142 log.fatal("Cannot bind control connector on port "+controlConnector.getPort(), e); 143 return false; 144 } 145 } 146 147 148 151 protected boolean unbindControlConnector() { 152 try { 153 if (controlConnector.isBound()) controlConnector.unbind(); 154 return true; 155 } catch (Throwable e) { 156 log.fatal("Cannot unbind control connector on port "+controlConnector.getPort(), e); 157 return false; 158 } 159 } 160 } 161 | Popular Tags |