/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.ejb;

import java.io.Serializable;

import org.jfox.framework.util.ObjectUUID;

/**
 * ObjectId 用来保证 Container 中的 ejb object 的唯一性
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EJBObjectId implements Serializable {

    private String ejbName;
    private String remoteInterf;
    private String homeInterf;

    private ObjectUUID uuid;
    private boolean isHome;

    EJBObjectId(String ejbName, String homeInterf, String remoteInterf, ObjectUUID hash, boolean isHome) {
        this.ejbName = ejbName;
        this.homeInterf = homeInterf;
        this.remoteInterf = remoteInterf;
        this.uuid = hash;
        this.isHome = isHome;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        if(isHome) {
            sb.append(homeInterf);
        }
        else {
            sb.append(remoteInterf);
        }
        sb.append("@").append(uuid);
        return sb.toString();
    }

    /**
     * @return 这个 ejb 的 name ，作为 ejb container 中的 key
     */
    public String getRemoteInterfaceName() {
        return remoteInterf;
    }

    public String getHomeInterfName() {
        return homeInterf;
    }

    public boolean isHome() {
        return isHome;
    }

    public String getEjbName() {
        return ejbName;
    }

    public int hashCode() {
        return uuid.hashCode();
    }

    /**
     * @return 完整的 Object ID, name@hashCode
     */
    public String getStringObjectId() {
        return this.toString();
    }

    public ObjectUUID getObjectUUID() {
        return uuid;
    }

    public byte[] getAddress() {
        return uuid.getVMID().getAddress();
    }

    /**
     * is this ObjectId's VM in
     *
     * @return
     */
    public boolean isCurrentVM() {
        return uuid.getVMID().isCurrentVM();
    }

    public boolean equals(Object obj) {
        if(!(obj instanceof EJBObjectId)) return false;
        return ((EJBObjectId) obj).getStringObjectId().equals(this.getStringObjectId());
    }

/*
  public String getIdentity(){
    return identity;
  }
*/

}
