/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2007 Bull S.A.S.
 * Contact: jonas-team@ow2.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.ow2.jonas.lib.reconfig;

import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;
/**
 *
 * This class is to be extended by classes which need to send reconfiguration JMX notifications.
 * It extends {@link NotificationBroadcasterSupport} which provides addNotificationListener implementation.
 * Listeners are MBeans interested by handling the reconfiguration notifications emitted
 * by the sub-classes instances.
 * This class replaces ReconfigDispatcher class.
 * @author Adriana Danes
 */
public class ReconfigEmitter extends NotificationBroadcasterSupport {

    /**
     * The logger provided by the emitter MBean
     */
    private static Logger slogger = null;

    /**
     * Management notification type for <i>reconfiguration</i> events (notify that the management application made a reconfiguration operation)
     */
    public static final String RECONFIG_TYPE = "jonas.management.reconfiguration";

    /**
     * Management notification type for <i>save configuration</i> events (notify that the management application asks for save changes)
     */
    public static final String SAVE_RECONFIG_TYPE = "jonas.management.reconfiguration.save";

    /**
     * Initialize the logger with a Logger provided by the reconfigurable resource
     * or service.
     * @param mylogger the logger provided by a sub-class
     */
    public void initLogger(Logger mylogger) {
        slogger = mylogger;
    }

    /**
     * Send a <i>save configuration</i> notification to the registerd listeners.
     * @param sequenceNumber notification attribute
     * @param resourceName the name of a reconfigurable resource or of a JOnAS service
     */
    public void sendSaveNotification(long sequenceNumber, String resourceName) {
         // create save notification
        Notification saveNotif = new Notification(SAVE_RECONFIG_TYPE, this, sequenceNumber, resourceName);
        if (slogger != null && slogger.isLoggable(BasicLevel.DEBUG)) {
            slogger.log(BasicLevel.DEBUG, saveNotif.getType() + " notification object created");
        }
        sendNotification(saveNotif);
    }

    /**
     * Send a <i>reconfiguration</i> notification to the registerd listener.
     * @param sequenceNumber notification attribute
     * @param resourceName the name of a reconfigurable resource or of a JOnAS service
     * @param userData data containing the name and the value of the reconfigured property(or properties)
     */
    public void sendReconfigNotification(long sequenceNumber, String resourceName, Object userData) {
        // create notification and set the userData
        Notification configNotif =  new Notification(RECONFIG_TYPE, this, sequenceNumber, resourceName);
        configNotif.setUserData(userData);
        if (slogger != null && slogger.isLoggable(BasicLevel.DEBUG)) {
            slogger.log(BasicLevel.DEBUG, configNotif.getType() + " notification object created");
        }
        sendNotification(configNotif);
    }
}