/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This 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 (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.jmx.connector.notification;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.management.JMException;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;

import org.jboss.jmx.connector.RemoteMBeanServer;

/**
 * Local Polling Listener to receive the message and send to the listener
 *
 * @version <tt>$Revision: 1.5.2.1 $</tt>
 * @author <A href="mailto:andreas@jboss.org">Andreas &quot;Mad&quot; Schaefer</A>
 **/
public class PollingClientNotificationListener
   extends ClientNotificationListener
   implements Runnable
{
   private RemoteMBeanServer                mConnector;
   private int                         mSleepingPeriod = 2000;
   
   public PollingClientNotificationListener(
      ObjectName pSender,
      NotificationListener pClientListener,
      Object pHandback,
      NotificationFilter pFilter,
      int pSleepingPeriod,
      int pMaximumListSize,
      RemoteMBeanServer pConnector
   ) throws
      JMException
   {
      super( pSender, pClientListener, pHandback );
      if( pSleepingPeriod > 0 ) {
         mSleepingPeriod = pSleepingPeriod;
      }
      mConnector = pConnector;
      // Register the listener as MBean on the remote JMX server
      createListener(
         pConnector,
         "org.jboss.jmx.connector.notification.PollingNotificationListener",
         new Object[] { new Integer( pMaximumListSize ), new Integer( pMaximumListSize ) },
         new String[] { Integer.TYPE.getName(), Integer.TYPE.getName() }
      );
      addNotificationListener( pConnector, pFilter );
      new Thread( this ).start();
   }

   public void run() {
      while( true ) {
         try {
            try {
               List lNotifications = (List) mConnector.getAttribute(
                  getRemoteListenerName(),
                  "Notifications"
               );
               Iterator i = lNotifications.iterator();
               while( i.hasNext() ) {
                  Notification lNotification = (Notification) i.next();
                  mClientListener.handleNotification(
                     lNotification,
                     mHandback
                  );
               }
            }
            catch( Exception e ) {
               log.error("operation failed; continuing", e);
            }
            Thread.sleep( mSleepingPeriod );
         }
         catch( InterruptedException ie ) {
         }
      }
   }
}
