/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.huihoo.jfox.system;

import org.huihoo.jfox.logging.Logger;


/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public abstract class ComponentSupport implements Component {
  /**
   * system name of the system
   */
  protected String name = null;
  /**
   * the status value of the system
   */
  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;
    }

//    if(logger == null) logger = Logger.getLogger(getClass().getName());
    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");

  }

  /**
   * do actually create action
   */
  protected abstract void doInit() throws Exception;
  /**
   * do actually destory action
   */
  protected abstract void doDestroy() throws Exception;

  // trip the ServiceName from it's class nameSpace
  protected static String parseName(String className){
    int lastDotIndex = className.lastIndexOf(".");
    return className.substring(lastDotIndex + 1);
  }


}