/*
 * 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 EjbServletHelper extends VelocityServlet {
    static Logger logger = null;

    public void init(ServletConfig config ) throws ServletException {
	super.init(config);
	logger = Log.getLogger(ServletLog.JONAS_VELO_SERVLET_PREFIX);
    }

    void getDatasourceInfo(Context context, ObjectName MBeanName, String datasource_error, String datasourceNb_name, String datasourceNames_name) {
	try {
	   // Get data sources
	    Vector dataSources = new Vector();
	    Set allDataSources = (Set)JonasManagementRepr.getAttribute(MBeanName, "AllDataSourceName"); 
	    context.put(datasourceNb_name, new Integer(allDataSources.size()));
	    String dataSourceName = null;
	    for (Iterator i = allDataSources.iterator(); i.hasNext() ;) {
		dataSourceName = (String)i.next();
		dataSources.add(dataSourceName);
	    }
	    context.put(datasourceNames_name, dataSources);
	} catch (Exception e) {
	    datasource_error = "Error while getting attribute:" + e.getMessage();
	}
	context.put("datasource_error", datasource_error);
    }

    void getConnFactoryInfo(Context context, ObjectName MBeanName, String jmsConnFact_error, String jmsConnFactoriesNb_name, String jmsConnFactoryNames_name) {
	try {
	    // Get JMS Connection Factories
	    Vector jmsConnFactories = new Vector();
	    Set allJmsConnFactories = (Set)JonasManagementRepr.getAttribute(MBeanName, "AllJMSConnectionFactoryName");
	    context.put(jmsConnFactoriesNb_name, new Integer(allJmsConnFactories.size()));
	    String jmsConnFactoryName = null;
	    for (Iterator i = allJmsConnFactories.iterator(); i.hasNext() ;) {
		jmsConnFactoryName = (String)i.next();
		jmsConnFactories.add(jmsConnFactoryName);
	    }
	    context.put(jmsConnFactoryNames_name, jmsConnFactories);
	} catch (Exception e) {
	    jmsConnFact_error = "Error while getting attribute:" + e.getMessage();
	}
	context.put("jmsConnFact_error", jmsConnFact_error);
    }

    void getDestinationInfo(Context context, ObjectName MBeanName, String jmsDest_error, String jmsDestinationsNb_name, String jmsDestinationNames_name) {
	try {
	    // Get JMS Destinations
	    Vector destinations = new Vector();
	    java.util.Set allDestinations = (java.util.Set)JonasManagementRepr.getAttribute(MBeanName, "AllJMSDestinationName");
	    if (allDestinations.isEmpty())
		logger.log(BasicLevel.DEBUG, "Found no destinations for this bean");
	    else
		logger.log(BasicLevel.DEBUG, "Found " + allDestinations.size() + " destinations");
	    
	    context.put(jmsDestinationsNb_name, new Integer(allDestinations.size()));
	    String destinationName = null;
	    for (Iterator i = allDestinations.iterator(); i.hasNext() ;) {
		destinationName = (String)i.next();
		destinations.add(destinationName);
	    }
	    context.put(jmsDestinationNames_name, destinations);
	} catch (Exception e) {
	    jmsDest_error = "Error while getting attribute:" + e.getMessage();
	}
	context.put("jmsDest_error", jmsDest_error);
    }

}
