/*
 * 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): ____________________________________.
 * Contributor(s): Michel Bruno and Guillaume Riviere
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.container;

import java.util.Set;
import java.util.HashSet;
import org.objectweb.jonas_ejb.container.JFactory;
import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
import org.objectweb.jonas_ejb.deployment.api.ResourceRefDesc;
import org.objectweb.jonas_ejb.deployment.api.ResourceEnvRefDesc;
import org.objectweb.jonas_ejb.lib.BeanNaming;

/**
 * MBean Class for general EJB Management 
 * MBean type: Standard
 * MBean model: Delegate (ManagableEjb: JFactory)
 */
public class JmxBeanFactory implements JmxBeanFactoryMBean {
   
    /**
     * The Bean to manage
     */
    protected JFactory ejbToManage = null;
    protected BeanDesc desc = null;
    protected String fileName = null;

    /**
     * Constructor
     * @param ManagableEjb the bean to manage
     */
    public JmxBeanFactory(JFactory ejbToManage) {
	this.ejbToManage = ejbToManage;
	this.fileName = ejbToManage.getContainer().getFileName();
	this.desc = ejbToManage.getDeploymentDescriptor();
    } 

    /**
     * @return String The Name of this JFactory
     */ 
    public String getEJBName() {
	return desc.getEjbName();
    }

    /**
     * @return Ejb File Name
     */
    public String getEJBFileName() {
	return fileName;
    }

    /**
     * @return String Set The Name Ejb DataSource
     */
    public Set getAllDataSourceName() {
	Set result= new HashSet();
	ResourceRefDesc[] rrDesc = desc.getResourceRefDesc();
	for (int i = 0; i < rrDesc.length; i ++) {
	    if (rrDesc[i].isJdbc()) {
		result.add(rrDesc[i].getJndiName()); 
	    }
	}
	return result;
    }

 
    /**
     * @return String Set The Name Ejb Connection Factory
     */   
    public Set getAllJMSConnectionFactoryName(){
	Set result= new HashSet();
	ResourceRefDesc[] rrDesc = desc.getResourceRefDesc();
	for (int i = 0; i < rrDesc.length; i ++) {
	    if ((rrDesc[i].getType() == javax.jms.TopicConnectionFactory.class)
		||(rrDesc[i].getType() == javax.jms.QueueConnectionFactory.class)) {
		result.add(rrDesc[i].getJndiName()); 
	    }
	}
	return result;
    }


    /**
     * @return String Set The Name Ejb Destination
     */  
    public Set getAllJMSDestinationName(){
	Set result= new HashSet();
	ResourceEnvRefDesc[] rrDesc = desc.getResourceEnvRefDesc();
	for (int i = 0; i < rrDesc.length; i ++) {
	    if ((rrDesc[i].getType() == javax.jms.Topic.class )
		||(rrDesc[i].getType() == javax.jms.Queue.class )) { 
		result.add(rrDesc[i].getJndiName()); 
	    }
	}
	return result;
    }

    /**
     * @return Integer The current instance pool size
     */  
    public  Integer getCurrentInstancePoolSize(){
	return new Integer(ejbToManage.getPoolSize());
    }

    /**
     * @return String the  JFactory Class
     */
    public String getEJBClass(){
	return desc.getEjbClass().getName();
    }
}
