package org.huihoo.jfox.system;
import org.huihoo.jfox.logging.Logger;
public abstract class ComponentSupport implements Component {
protected String name = null;
protected volatile State state = State.ORIGINAL;
protected transient Logger logger = Logger.getLogger(getClass().getName());
protected volatile long sequence = 0L;
protected Object proxyInstance = null;
public ComponentSupport() {
setName(parseName(this.getClass().getName()));
}
public ComponentSupport(String name) {
setName(name);
}
public String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
public State getState() {
return state;
}
public Logger getLogger() {
return logger;
}
protected void setLogger(Logger logger){
this.logger = logger;
}
public void init() throws Exception {
if(!State.canInit(state)) {
logger.warn(name + " can not initialize, state = " + state);
return;
}
logger.info("initializing...");
state = State.INITIALIZING;
try {
doInit();
}
catch (Exception e) {
state = State.INTERRUPTED;
logger.error("initialze failed", e);
throw e;
}
state = State.INITIALIZED;
logger.info("initialized.");
}
public void destroy() throws Exception {
if(!State.canDestroy(state)) {
logger.warn(name + " can not destroy, state = " + state);
return;
}
logger.info("destroying...");
state = State.DESTROYING;
try {
doDestroy();
}
catch (Exception e) {
state = State.INTERRUPTED;
logger.error("stopping failed", e);
throw e;
}
state = State.DESTROYED;
logger.info("destroyed");
}
protected abstract void doInit() throws Exception;
protected abstract void doDestroy() throws Exception;
protected static String parseName(String className){
int lastDotIndex = className.lastIndexOf(".");
return className.substring(lastDotIndex + 1);
}
}