/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): Adriana Danes
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jmx.vjadmin.servlet;

// servlet imports
import javax.servlet.*;

import java.util.Set;
import java.util.Vector;
import java.util.Iterator;

import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.context.Context;

// JOnAS JMX imports
import org.objectweb.jonas.jmx.*;
// JMX
import javax.management.ObjectName;
import javax.management.MalformedObjectNameException;

// Jadmin imports
import org.objectweb.jonas.jmx.vjadmin.*;

// monolog imports
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

// JOnAS log imports
import org.objectweb.jonas.common.Log;

public class EntityServletHelper extends EjbServletHelper {

    public void init(ServletConfig config ) throws ServletException {
	super.init(config);
    }

    void getEntityBeanInfo(Context context, ObjectName MBeanName, String entityInfo_error,
			   String passivationTimeOut_name,
			   String shared_name,
			   String minPoolSize_name,
			   String curPoolSize_name,
			   String maxCacheSize_name,
			   String curCacheSize_name,
			   String entityCounters_name) {
	try {
	    // get passivation timeout
	    Long timeout = (Long)JonasManagementRepr.getAttribute(MBeanName, "PassivationTimeOut");
	    context.put(passivationTimeOut_name, timeout);
	    logger.log(BasicLevel.DEBUG, "Context.put " + passivationTimeOut_name + " property");
	    // determe if the bean is shared
	    Boolean shared = (Boolean)JonasManagementRepr.getAttribute(MBeanName, "Shared");
	    context.put(shared_name, shared);
	    // get the min pool size
	    Integer poolSize = (Integer)JonasManagementRepr.getAttribute(MBeanName, "MinPoolSize");
	    context.put(minPoolSize_name, poolSize);
	    // get the current pool size
	    poolSize = (Integer)JonasManagementRepr.getAttribute(MBeanName, "PoolSize");
	    context.put(curPoolSize_name, poolSize);
	    // get the max number of instances (given by the max cache size)
	    Integer cacheSize = (Integer)JonasManagementRepr.getAttribute(MBeanName, "MaxCacheSize");
	    context.put(maxCacheSize_name, cacheSize);
	    // get the current number of instances (given by the current cache size)
	    cacheSize = (Integer)JonasManagementRepr.getAttribute(MBeanName, "CacheSize");
	    context.put(curCacheSize_name, cacheSize);

	    // TO DO after container update
	    // get the entity counters
	    //Integer[] entityCounters = (Integer[])JonasManagementRepr.getAttribute(MBeanName, "EntityCounters");
	    Integer i = new Integer(1);
	    Integer[] entityCounters = new Integer[4];
	    for (int j=0 ; j<4 ; j++) 
		entityCounters[j] = i;
	    context.put(entityCounters_name, entityCounters);
	} catch (Exception e) {
	    entityInfo_error = "Error while getting attribute:" + e.getMessage();
	}
	context.put("info_error", entityInfo_error);
    }
}
