1 29 30 package com.caucho.server.deploy; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.lifecycle.Lifecycle; 34 import com.caucho.loader.Environment; 35 import com.caucho.loader.EnvironmentClassLoader; 36 import com.caucho.loader.EnvironmentListener; 37 import com.caucho.util.L10N; 38 import com.caucho.util.Log; 39 import com.caucho.vfs.Dependency; 40 41 import javax.annotation.PostConstruct; 42 import java.util.Set ; 43 import java.util.logging.Level ; 44 import java.util.logging.Logger ; 45 46 49 abstract public class DeployGenerator<E extends DeployController> 50 implements Dependency, EnvironmentListener { 51 private static final Logger log = Log.open(DeployGenerator.class); 52 private static final L10N L = new L10N(DeployGenerator.class); 53 54 private DeployContainer<E> _container; 56 57 private ClassLoader _parentClassLoader; 58 59 private String _startupMode = DeployController.STARTUP_AUTOMATIC; 60 private String _redeployMode = DeployController.REDEPLOY_AUTOMATIC; 61 62 private Throwable _configException; 63 64 private final Lifecycle _lifecycle = new Lifecycle(getLog()); 65 66 69 public DeployGenerator(DeployContainer<E> container) 70 { 71 _parentClassLoader = Thread.currentThread().getContextClassLoader(); 72 _container = container; 73 74 _lifecycle.setName(toString()); 75 _lifecycle.setLevel(Level.FINER); 76 } 77 78 81 public DeployContainer<E> getDeployContainer() 82 { 83 return _container; 84 } 85 86 89 public ClassLoader getParentClassLoader() 90 { 91 return _parentClassLoader; 92 } 93 94 97 public void setStartupMode(String mode) 98 throws ConfigException 99 { 100 _startupMode = DeployController.toStartupCode(mode); 101 } 102 103 106 public String getStartupMode() 107 throws ConfigException 108 { 109 return _startupMode; 110 } 111 112 115 public void setRedeployMode(String mode) 116 throws ConfigException 117 { 118 _redeployMode = DeployController.toRedeployCode(mode); 119 } 120 121 124 public String getRedeployMode() 125 throws ConfigException 126 { 127 return _redeployMode; 128 } 129 130 @PostConstruct 131 final public void init() 132 throws ConfigException 133 { 134 if (! _lifecycle.toInitializing()) 135 return; 136 137 try { 138 initImpl(); 139 } 140 catch (RuntimeException ex) { 141 _configException = ex; 142 throw ex; 143 } 144 145 _lifecycle.setName(toString()); 146 147 _lifecycle.toInit(); 148 } 149 150 153 protected void initImpl() 154 { 155 } 156 157 160 public boolean isModified() 161 { 162 return false; 163 } 164 165 public String getState() 166 { 167 return _lifecycle.getStateName(); 168 } 169 170 173 final public void start() 174 { 175 try { 176 init(); 177 } catch (Throwable e) { 178 log.log(Level.WARNING, e.toString(), e); 179 } 180 181 if (!_lifecycle.toStarting()) 182 return; 183 184 startImpl(); 185 186 _lifecycle.toActive(); 187 } 188 189 public boolean isActive() 190 { 191 return _lifecycle.isActive(); 192 } 193 194 197 protected void startImpl() 198 { 199 Environment.addEnvironmentListener(this); 200 } 201 202 205 public void request() 206 { 207 } 208 209 212 public void update() 213 { 214 } 215 216 219 protected void fillDeployedKeys(Set <String > keys) 220 { 221 } 222 223 226 protected E generateController(String key) 227 { 228 return null; 229 } 230 231 235 protected E mergeController(E controller, String key) 236 { 237 return controller; 238 } 239 240 243 protected Logger getLog() 244 { 245 return log; 246 } 247 248 251 final public void stop() 252 { 253 if (!_lifecycle.toStopping()) 254 return; 255 256 stopImpl(); 257 258 _lifecycle.toStop(); 259 } 260 261 264 protected void stopImpl() 265 { 266 } 267 268 public Throwable getConfigException() 269 { 270 return _configException; 271 } 272 273 276 final public void destroy() 277 { 278 try { 279 stop(); 280 } catch (Throwable e) { 281 log.log(Level.WARNING, e.toString(), e); 282 } 283 284 if (!_lifecycle.toDestroying()) 285 return; 286 287 destroyImpl(); 288 289 _lifecycle.toDestroy(); 290 } 291 292 295 protected void destroyImpl() 296 { 297 _container.remove(this); 298 } 299 300 303 public void environmentStart(EnvironmentClassLoader loader) 304 { 305 start(); 306 } 307 308 311 public void environmentStop(EnvironmentClassLoader loader) 312 { 313 destroy(); 314 } 315 316 public String toString() 317 { 318 String name = getClass().getName(); 319 int p = name.lastIndexOf('.'); 320 if (p > 0) 321 name = name.substring(p + 1); 322 323 return name + "[]"; 324 } 325 326 } 327 | Popular Tags |