/**
 * 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
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.management.j2eemanagement;

// JMX
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.objectweb.jonas.jmx.J2eeObjectName;
import org.objectweb.jonas.jmx.ManagementReprFactory;

/**
 * A J2EE MBean module used on server (EJB container).
 *
 * @author Michel-Ange Anton
 */
public class J2EEModule extends J2EEDeployedObject {

// ------------------------------------------------------------- Private Constants

// ------------------------------------------------------------- Privates Variables
    private MBeanServerConnection localManagementRepr = ManagementReprFactory.getLocalManagementRepr();
// ------------------------------------------------------------- Properties

// ------------------------------------------------------------- Contructors

    /**
     * MBean constructor.
     * @param p_ObjectName The object name of the deployed object
     */
    protected J2EEModule(String p_ObjectName) {
        super(p_ObjectName);
    }

    /**
     * MBean constructor.
     * @param p_ObjectName The object name of the module object
     * @param p_StateManageable If true, this object implements J2EE State Management Model
     * @param p_StatisticsProvider If true, this object implements the J2EE StatisticProvide Model
     * @param p_EventProvider If true, this object implements the J2EE EventProvider Model
     */
    protected J2EEModule(String p_ObjectName, boolean p_StateManageable
        , boolean p_StatisticsProvider, boolean p_EventProvider) {
        super(p_ObjectName, p_StateManageable, p_StatisticsProvider, p_EventProvider);
    }

// ------------------------------------------------------------- Public Methods

    /**
     * Return the JVM used by this J2EEModule.
     * @return A array of strings of the object name of the JVMs.
     */
    public String[] getJavaVMs() {
        String[] as = new String[0];
        try {
            ObjectName on = new ObjectName(getObjectName());
            ObjectName onServer = J2eeObjectName.J2EEServer(on.getDomain()
                , on.getKeyProperty(J2EE_TYPE_SERVER));
            as = (String[]) ((MBeanServer) localManagementRepr).getAttribute(onServer, "javaVMs");
        }
        catch (MalformedObjectNameException e) {
            // none action
        } catch (AttributeNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MBeanException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ReflectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return as;
    }

}
