001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.management.mejb;
023:
024: import org.jboss.logging.Logger;
025:
026: import javax.management.*;
027: import java.rmi.RemoteException;
028: import java.util.Random;
029:
030: /**
031: * Basic Local Listener to receive Notification from a remote JMX Agent
032: *
033: * @author ???
034: * @version $Revision: 57197 $
035: */
036: public abstract class ClientNotificationListener {
037: private ObjectName mSender;
038: private ObjectName mRemoteListener;
039: protected NotificationListener mClientListener;
040: protected Object mHandback;
041: private Random mRandom = new Random();
042:
043: protected Logger log = Logger.getLogger(this .getClass());
044:
045: public ClientNotificationListener(ObjectName pSender,
046: NotificationListener pClientListener, Object pHandback) {
047: mSender = pSender;
048: mClientListener = pClientListener;
049: mHandback = pHandback;
050: }
051:
052: public ObjectName createListener(MEJB pConnector, String pClass,
053: Object[] pParameters, String[] pSignatures)
054: throws MalformedObjectNameException, ReflectionException,
055: MBeanRegistrationException, MBeanException,
056: NotCompliantMBeanException, RemoteException {
057: ObjectName lName = null;
058: while (lName == null) {
059: try {
060: lName = new ObjectName("JMX:type=listener,id="
061: + mRandom.nextLong());
062: ObjectInstance lInstance = pConnector.createMBean(
063: pClass, lName, pParameters, pSignatures);
064: lName = lInstance.getObjectName();
065: } catch (InstanceAlreadyExistsException iaee) {
066: lName = null;
067: }
068: /* A remote exception could cause an endless loop therefore take it out
069: catch( RemoteException re ) {
070: lName = null;
071: }
072: */
073: }
074: mRemoteListener = lName;
075: return lName;
076: }
077:
078: public void addNotificationListener(MEJB pConnector,
079: NotificationFilter pFilter)
080: throws InstanceNotFoundException, RemoteException {
081: pConnector.addNotificationListener(mSender, mRemoteListener,
082: pFilter, null);
083: }
084:
085: public void removeNotificationListener(MEJB pConnector)
086: throws InstanceNotFoundException, RemoteException {
087: try {
088: pConnector.removeNotificationListener(mSender,
089: mRemoteListener);
090: } catch (JMException jme) {
091: }
092: try {
093: pConnector.unregisterMBean(mRemoteListener);
094: } catch (JMException jme) {
095: }
096: }
097:
098: public ObjectName getSenderMBean() {
099: return mSender;
100: }
101:
102: public ObjectName getRemoteListenerName() {
103: return mRemoteListener;
104: }
105:
106: public boolean equals(Object pTest) {
107: if (pTest instanceof ClientNotificationListener) {
108: ClientNotificationListener lListener = (ClientNotificationListener) pTest;
109: return mSender.equals(lListener.mSender)
110: && mClientListener
111: .equals(lListener.mClientListener);
112: }
113: return false;
114: }
115:
116: }
|