/**
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 - JASS: Java Advanced tranSaction Support
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 - This module was originally developed by 
 -
 -    LSD (Distributed Systems Lab, http://lsd.ls.fi.upm.es/lsd/lsd.htm)
 -    at Universidad Politecnica de Madrid (UPM) as an ObjectWeb Consortium
 -    (http://www.objectweb.org) project.
 -
 -    This project has been partially funded by the European Commission under
 -    the IST programme of V FP grant IST-2001-37126 and by the Spanish
 -    Ministry of Science & Technology (MCyT) grants TIC2002-10376-E and
 -    TIC2001-1586-C03-02
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -  The original code and portions created by LSD are 
 -  Copyright (c) 2004 LSD (UPM)  
 -  All rights reserved.
 -  
 - Redistribution and use in source and binary forms, with or without 
 - modification, are permitted provided that the following conditions are met:
 -
 - -Redistributions of source code must retain the above copyright notice, this
 - list of conditions and the following disclaimer. 
 -
 - -Redistributions in binary form must reproduce the above copyright notice, 
 - this list of conditions and the following disclaimer in the documentation 
 - and/or other materials provided with the distribution. 
 -
 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 - POSSIBILITY OF SUCH DAMAGE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 -  Author: Francisco Perez Sorrosal (frperezs)
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.objectweb.jass.as.jboss;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Reference;

import org.apache.log4j.Logger;
import org.jboss.system.ServiceMBeanSupport;
import org.objectweb.jass.as.ActivityIdGenerator;
import org.objectweb.jass.as.UserActivityImpl;

/**
 * MBean that creates and registers the Activity Service  implementation 
 * classes for the JBOSS application server.
 * @author fran
 * Date: Feb 11, 2004
 * org.objectweb.jass.hls.as.ActivityService.java
 */
public class ActivityService
	extends ServiceMBeanSupport
	implements ActivityServiceMBean {

	private static Logger log = Logger.getLogger(ActivityService.class);
	
	// Constants ------------------------------------------------------------------

	// The JNDI name of UserActivity and ActivityManager interfaces
	public static final String UA_JNDI_NAME = "UserActivity";
	public static final String AM_JNDI_NAME = "ActivityManager";

	// ServiceMBeanSupport implementation -------------------------------------

	/**
	 * Invoked when this JBOSS MBean is started.
	 */
	public void startService() throws Exception {
		
		// The services are binded throught references		
		Reference uaRef =
			new Reference(
				"org.objectweb.jass.as.UserActivityImpl",
				"org.objectweb.jass.as.UserActivityObjectFactory",
				null);

		Reference amRef =
			new Reference(
				"org.objectweb.jass.as.ActivityManagerImpl",
				"org.objectweb.jass.as.ActivityManagerObjectFactory",
				null);

		Context ctx = new InitialContext();

		// Get a reference to the ActivityIdGenerator singleton, used to assing
		// unique Ids to activities, and set it in the UserActivityImpl
		log.info("Looking for an Activity Id generator...");
		ActivityIdGenerator activityIdGenerator =
			(ActivityIdGenerator) ctx.lookup("ActivityIdService");
		log.info("A reference to the Activity Id generator has been acquired");
		log.info(
			"Setting generator in UserActivity and ActivityManager implementation...");
		UserActivityImpl.setActivityIdGenerator(activityIdGenerator);
		log.info(
			"The Activity Id generator has been established as default generator");

		log.info("Binding UserActivity Service and ActivityManager Services");
		ctx.bind(UA_JNDI_NAME, uaRef);
		ctx.bind(AM_JNDI_NAME, amRef);
		log.info("Bindings done");
	}

	/**
	 * Invoked when this JBOSS MBean is stopped.
	 */
	public void stopService() {
		try {
			Context ctx = new InitialContext();
			ctx.unbind(UA_JNDI_NAME);
			ctx.unbind(AM_JNDI_NAME);
		} catch (Exception e) {
			log.error("Failed to unbind", e);
		}
	}

	// ActivityServiceMBean implementation ------------------------------------

	/**
	 * Return the number of AS instances that HLSs has created. There is
	 * one AS instance per HLS.
	 * 
	 * @return the number of AS instances. 
	 */
	public int getInstances() {
		return UserActivityImpl.getInstances();
	}

	/**
	 * TODO Not yet implemented.
	 */
	public int numberOfActivities() {
		return 0;
	}

}
