/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 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.web.jetty50.jmx;

import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.mortbay.jetty.servlet.WebApplicationContext;
import org.mortbay.util.LogSupport;
import org.mortbay.util.jmx.ModelMBeanImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Fake (at least not complete) implementation of the WebModule MBean.
 * TODO for a full implementation, the MBean should be able to return
 * a real ObjectName array for Servlets inside the WebApp.
 *
 * @author Guillaume Sauthier
 */
public class J2EEWebModuleMBean extends ModelMBeanImpl {

    /**
     * logger
     */
    private static Log log = LogFactory.getLog(J2EEWebModuleMBean.class);

    /**
     * Monitored WebApp
     */
    private WebApplicationContext waContext = null;

    /**
     * list of Servlet's ObjectNames
     */
    private ObjectName[] servlets = null;

    /**
     * Default public MBean Constructor
     * @throws MBeanException required by the spec
     */
    public J2EEWebModuleMBean() throws MBeanException {
        super();
    }

    /**
     * @see org.mortbay.util.jmx.ModelMBeanImpl#defineManagedResource()
     */
    protected void defineManagedResource() {
        // TODO Auto-generated method stub
        super.defineManagedResource();
        defineAttribute("servlets", READ_ONLY, ON_MBEAN);
        waContext  = (WebApplicationContext) getManagedResource();
    }

    /**
     * @return Returns the servlets.
     */
    public ObjectName[] getServlets() {
        return servlets;
    }
    
    /**
     * @param servlets
     */
    public void setServlets(ObjectName[] servlets) {
        this.servlets = servlets;
    }

    /**
     *
     * @see org.mortbay.jetty.servlet.jsr77.jmx.Jsr77ServletHolderMBean#uniqueObjectName(javax.management.MBeanServer,
     *      java.lang.String)
     */
    public synchronized ObjectName uniqueObjectName(MBeanServer mbeanServer, String baseObjectName) {
        ObjectName jsr77Name = null;

        // get the context name
        String context = waContext.getHttpContext().getContextPath();
        if (context.length() == 0) {
            context = "/";
        }

        try {
            jsr77Name = new ObjectName(waContext.getAttribute("J2EEDomainName")
                    + ":J2EEServer=" + waContext.getAttribute("J2EEServerName")
                    + ",J2EEApplication=" + waContext.getAttribute("J2EEApplicationName")
                    + ",j2eeType=WebModule,name=" + context);
        } catch (Exception e) {
            log.warn(LogSupport.EXCEPTION, e);
        }
        return jsr77Name;
    }

}
