/* 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 org.jfox.framework.util.ObjectUUID;

/**
 * 生成唯一的 ObjectID
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EJBObjectIDGenerator {
    private String ejbName;
    private String homeInterf;
    private String remoteInterf;
    private ObjectUUID homeUUID;


    private EJBObjectIDGenerator(String ejbName, String homeIntef, String remoteInterf) {
        this.ejbName = ejbName;
        this.homeInterf = homeIntef;
        this.remoteInterf = remoteInterf;
        this.homeUUID = ObjectUUID.randomUUID();
    }

    /**
     * @param homeInterf Bean 的类名
     * @return
     */
    public static EJBObjectIDGenerator newInstance(String ejbName, String homeInterf, String remoteInterf) {
        return new EJBObjectIDGenerator(ejbName, homeInterf, remoteInterf);
    }

    public EJBObjectId getHomeObjectId() {
        return new EJBObjectId(ejbName, homeInterf, remoteInterf, homeUUID, true);
    }

    public EJBObjectId nextBeanObjectId() {
        return new EJBObjectId(ejbName, homeInterf, remoteInterf, ObjectUUID.randomUUID(), false);
    }

    public static void main(String[] args) throws Exception {
        EJBObjectIDGenerator oig = EJBObjectIDGenerator.newInstance("ejb/test", "org.jfox.TestHome", "org.jfox.Test");
        System.out.println(oig.getHomeObjectId());
        System.out.println(oig.nextBeanObjectId());
        oig = EJBObjectIDGenerator.newInstance("ejb/test", "org.jfox.TestHome", "org.jfox.Test");
        System.out.println(oig.getHomeObjectId());
        System.out.println(oig.nextBeanObjectId());

    }
}
