/**
 * 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.ServletHolder;
import org.mortbay.jetty.servlet.jsr77.jmx.Jsr77ServletHolderMBean;
import org.mortbay.util.LogSupport;

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

/**
 * Fix the uniqueObjectName method and generate a corrent ObjectName for the Servlet.
 *
 * @author Guillaume Sauthier
 */
public class FixedJsr77ServletHolderMBean extends Jsr77ServletHolderMBean {

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

    /**
     * Monitored resource
     */
    private ServletHolder servletHolder = null;

    /**
     * domain name
     */
    private String j2EEDomainName = null;

    /**
     * server name
     */
    private String j2EEServerName = null;

    /**
     * application name
     */
    private String j2EEApplicationName = null;

    /**
     * Default public Constrcuctor
     * @throws MBeanException required by the spec
     */
    public FixedJsr77ServletHolderMBean() throws MBeanException {
        super();
    }

    /**
     *
     * @see org.mortbay.jetty.servlet.jsr77.jmx.Jsr77ServletHolderMBean#defineManagedResource()
     */
    protected void defineManagedResource() {
        // TODO Auto-generated method stub
        super.defineManagedResource();
        servletHolder = (ServletHolder) getManagedResource();
    }

    /**
     * @return Returns the j2EEApplicationName.
     */
    public String getJ2EEApplicationName() {
        return j2EEApplicationName;
    }

    /**
     * @param applicationName The j2EEApplicationName to set.
     */
    public void setJ2EEApplicationName(String applicationName) {
        j2EEApplicationName = applicationName;
    }

    /**
     * @return Returns the j2EEDomainName.
     */
    public String getJ2EEDomainName() {
        return j2EEDomainName;
    }

    /**
     * @param domainName The j2EEDomainName to set.
     */
    public void setJ2EEDomainName(String domainName) {
        j2EEDomainName = domainName;
    }

    /**
     * @return Returns the j2EEServerName.
     */
    public String getJ2EEServerName() {
        return j2EEServerName;
    }

    /**
     * @param serverName The j2EEServerName to set.
     */
    public void setJ2EEServerName(String serverName) {
        j2EEServerName = serverName;
    }

    /**
     *
     * @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;
        String context = servletHolder.getHttpContext().getContextPath();
        if (context.length() == 0) {
            context = "/";
        }

        try {
            jsr77Name = new ObjectName(getJ2EEDomainName()
                    + ":J2EEServer=" + getJ2EEServerName()
                    + ",J2EEApplication=" + getJ2EEApplicationName()
                    + ",WebModule=" + context
                    + ",j2eeType=Servlet,name=" + servletHolder.getName());
        } catch (Exception e) {
            log.warn(LogSupport.EXCEPTION, e);
        }
        return jsr77Name;
    }

}
