/*
 * 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;

// JNDI
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

// JMX
import javax.management.NotificationListener;
import javax.management.Notification;

// JOnAS Log
import org.objectweb.jonas.common.Log;
// Monolog
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

// MEJB
import org.objectweb.jonas.mejb.ListenerProxy;

/**
 * This MBean is created by the Management EJB in order to listen to notifications emitted
 * by the MBeans on which the a listener was add by the MEJB's client.
 */
public class ManagementListener implements ManagementListenerMBean, NotificationListener {
    static private Logger logger = null;

    // Remote proxy implementing the javax.management.NotificationListener interface
    // allowing to deliver the notifications to the MEJB's client.
    ListenerProxy proxy = null;
    String proxyName = null;

    public ManagementListener(String proxyName) {
        super();
        this.proxyName = proxyName;
        logger = Log.getLogger("org.objectweb.jonas.management.j2eemanagement.event");
    }

    public void handleNotification(Notification notification, Object handback) {
        if (logger.isLoggable(BasicLevel.DEBUG)) {
            logger.log(BasicLevel.DEBUG, "");
        }
        if (proxy == null) {
            try {
                Context context = new InitialContext();
                proxy = (ListenerProxy)javax.rmi.PortableRemoteObject.narrow(context.lookup(proxyName), ListenerProxy.class);
            } catch (NamingException e) {
                // Could not retrieve listener proxy
                // ManagementListener is not able to deliver notification
                logger.log(BasicLevel.WARN, "ManagementListener MBean cannot deliver notification to the remote ListenerProxy:" + e);
                return;
            }
        }
        // deliver notification to the remote ListenerProxy
        try {
            proxy.handleNotification(notification, handback);
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "Notification deliverd to ListenerProxy with handback: " + handback.toString());
            }
        } catch (java.rmi.RemoteException e) {
            // Try to retrive proxy from JNDI
            try {
                Context context = new InitialContext();
                proxy = (ListenerProxy)javax.rmi.PortableRemoteObject.narrow(context.lookup(proxyName), ListenerProxy.class);
                proxy.handleNotification(notification, handback);
                if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG, "Notification deliverd to ListenerProxy with handback: " + handback.toString());
                }
            } catch (NamingException ne) {
                // Could not retrieve listener proxy
                // ManagementListener is not able to deliver notification
                logger.log(BasicLevel.WARN, "ManagementListener MBean cannot deliver notification to the remote ListenerProxy:" + e);
                return;
            } catch (java.rmi.RemoteException re) {
                // Could not retrieve listener proxy deliver notification
                logger.log(BasicLevel.WARN, "ManagementListener MBean cannot deliver notification to the remote ListenerProxy:" + e);
                return;
            }
        }
    }

}
