com.pureinfo.force.runtime.engine.EngineBean.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.force.runtime.engine.EngineBean.java

Source

/**
 * PureInfo Force
 * @(#)EngineBean.java   1.0 Oct 11, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.force.runtime.engine;

import org.dom4j.Element;

import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.exception.PureRuntimeException;
import com.pureinfo.force.xml.IXMLSupporter;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on Oct 11, 2005 6:37:33 AM <BR>
 * Last modified on Oct 11, 2005
 * </P>
 * EngineBean: engine bean.
 * 
 * @author Why
 * @version 1.0, Oct 11, 2005
 * @since Force 1.0
 */
public class EngineBean implements IXMLSupporter {

    private String m_sId;

    private String m_sName;

    private Class m_type;

    private boolean m_bIgnoreFailure;

    //runtime cache
    IEngine m_engine = null;

    /**
     * Constructor: default
     */
    public EngineBean() {
        super();
    }

    /**
     * Returns id of this engine.
     * 
     * @return id of this engine
     */
    public String getId() {
        return m_sId;
    }

    /**
     * Sets id of this engine
     * 
     * @param _sId
     *            id of this engine
     */
    public void setId(String _sId) {
        m_sId = _sId;
    }

    /**
     * Returns the name of this engine.
     * 
     * @return the name of this engine.
     */
    public String getName() {
        return m_sName;
    }

    /**
     * Sets the name of this engine.
     * 
     * @param _sName
     *            the name of this engine.
     */
    public void setName(String _sName) {
        m_sName = _sName;
    }

    /**
     * Returns the type (class) of this engine.
     * 
     * @return the type (class) of this engine.
     */
    public Class getType() {
        return m_type;
    }

    /**
     * Sets the type (class) of this engine.
     * 
     * @param _clazz
     *            the class
     * @throws PureException
     *             if the class is not assigable from IEgine.
     */
    public void setType(Class _clazz) throws PureException {
        if (!IEngine.class.isAssignableFrom(_clazz)) {
            throw new PureException(PureException.TYPE_MISMATCHED,
                    _clazz.getName() + " is not assignable from " + IEngine.class.getName());
        }

        //else
        m_type = _clazz;
    }

    /**
     * Sets the type (class) of this engine.
     * 
     * @param _sClassName
     *            the class name
     * @throws PureException
     *             if the class is not found, or is not assigable from IEgine.
     */
    public void setType(String _sClassName) throws PureException {
        //to validate the class
        Class clazz;
        try {
            clazz = Class.forName(_sClassName);
        } catch (Exception ex) {
            throw new PureException(PureException.CLASS_NOTFOUND, _sClassName);
        }

        this.setType(clazz);
    }

    /**
     * 
     * @return
     */
    public boolean isIgnoreFailure() {
        return m_bIgnoreFailure;
    }

    /**
     * Sets if to ingore when failed.
     * 
     * @param _bIgnoreFailure
     */
    public void setIgnoreFailure(boolean _bIgnoreFailure) {
        m_bIgnoreFailure = _bIgnoreFailure;
    }

    /**
     * Returns the engine instance.
     * 
     * @param _bCreateIfNecessary
     *            <code>true</code>, to create new engine if no engine
     *            instance found.
     * @return the engine instance.
     */
    public IEngine getEngine(boolean _bCreateIfNecessary) {
        if (m_engine == null && _bCreateIfNecessary) {
            synchronized (this) {
                if (m_engine == null) {
                    try {
                        m_engine = (IEngine) m_type.newInstance();
                    } catch (Exception ex) {
                        throw new PureRuntimeException(PureException.NEW_INSTANCE_ERROR, m_type.getName(), ex);
                    }
                }
            }
        }
        return m_engine;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return "id=" + m_sId + ", name=" + m_sName + ", type=" + m_type.getName() + ", ignore-failure="
                + m_bIgnoreFailure;
    }

    //=========================================================================
    //XML support

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("id", m_sId);
        _element.addAttribute("name", m_sName);
        _element.addAttribute("type", m_type.getName());
        _element.addAttribute("ignore-failure", String.valueOf(m_bIgnoreFailure));
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        this.setId(_element.attributeValue("id"));
        this.setName(_element.attributeValue("name"));
        this.setType(_element.attributeValue("type"));
        this.setIgnoreFailure(XMLUtil.getAttributeValueAsBool(_element, "ignore-failure", false));
    }

}