1 6 7 package org.jfox.ioc.common; 8 9 import org.jfox.ioc.Component; 10 import org.jfox.ioc.ComponentContext; 11 import org.jfox.ioc.annotation.Managable; 12 import org.jfox.ioc.ext.InitializableComponent; 13 import org.jfox.ioc.logger.Logger; 14 15 18 19 public abstract class AbstractComponent implements Component, InitializableComponent { 20 23 protected String name = null; 24 27 protected volatile State state = State.ORIGINAL; 28 29 protected transient Logger logger = Logger.getLogger(getClass().getName()); 30 protected volatile long sequence = 0L; 31 32 protected transient Object proxyInstance = null; 33 34 protected transient ComponentContext context = null; 35 36 public AbstractComponent() { 37 setName(parseName(this.getClass().getName())); 38 } 39 40 public AbstractComponent(String name) { 41 setName(name); 42 } 43 44 public void setComponentContext(ComponentContext ctx) { 45 this.context = ctx; 46 } 47 48 public String getName() { 49 return name; 50 } 51 52 protected void setName(String name) { 53 this.name = name; 54 } 55 56 public State getState() { 57 return state; 58 } 59 60 public Logger getLogger() { 61 return logger; 62 } 63 64 protected void setLogger(Logger logger) { 65 this.logger = logger; 66 } 67 68 public boolean isInitialized() { 69 return state.equals(State.INITIALIZED) 70 || state.equals(State.STARTING) 71 || state.equals(State.STARTED) 72 || state.equals(State.STOPPING) 73 || state.equals(State.STOPPED); 74 } 75 76 @Managable 77 public void init() throws Exception { 78 if(!State.canInit(state)) { 79 logger.warn(name + " can not initialize, state = " + state); 80 return; 81 } 82 83 logger.info("initializing..."); 85 state = State.INITIALIZING; 86 87 try { 88 doInit(); 89 } 90 catch(Exception e) { 91 state = State.INTERRUPTED; 92 logger.error("initialze failed", e); 93 } 94 95 state = State.INITIALIZED; 96 logger.info("initialized."); 97 98 } 99 100 @Managable 101 public void destroy() throws Exception { 102 if(!State.canDestroy(state)) { 103 logger.warn(name + " can not destroy, state = " + state); 104 return; 105 } 106 107 logger.info("destroying..."); 108 state = State.DESTROYING; 109 try { 110 doDestroy(); 111 } 112 catch(Exception e) { 113 state = State.INTERRUPTED; 114 logger.error("stopping failed", e); 115 throw e; 116 } 117 state = State.DESTROYED; 118 logger.info("destroyed"); 119 120 } 121 122 125 protected abstract void doInit() throws Exception ; 126 127 130 protected abstract void doDestroy() throws Exception ; 131 132 protected static String parseName(String className) { 134 int lastDotIndex = className.lastIndexOf("."); 135 return className.substring(lastDotIndex + 1); 136 } 137 } 138 139 | Popular Tags |