KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > mejb > ClientNotificationListener


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.management.mejb;
23
24 import org.jboss.logging.Logger;
25
26 import javax.management.*;
27 import java.rmi.RemoteException JavaDoc;
28 import java.util.Random JavaDoc;
29
30 /**
31  * Basic Local Listener to receive Notification from a remote JMX Agent
32  *
33  * @author ???
34  * @version $Revision: 37459 $
35  */

36 public abstract class ClientNotificationListener
37 {
38    private ObjectName mSender;
39    private ObjectName mRemoteListener;
40    protected NotificationListener mClientListener;
41    protected Object JavaDoc mHandback;
42    private Random JavaDoc mRandom = new Random JavaDoc();
43
44    protected Logger log = Logger.getLogger(this.getClass());
45
46    public ClientNotificationListener(ObjectName pSender,
47                                      NotificationListener pClientListener,
48                                      Object JavaDoc pHandback)
49    {
50       mSender = pSender;
51       mClientListener = pClientListener;
52       mHandback = pHandback;
53    }
54
55    public ObjectName createListener(MEJB pConnector,
56                                     String JavaDoc pClass,
57                                     Object JavaDoc[] pParameters,
58                                     String JavaDoc[] pSignatures) throws
59            MalformedObjectNameException,
60            ReflectionException,
61            MBeanRegistrationException,
62            MBeanException,
63            NotCompliantMBeanException,
64            RemoteException JavaDoc
65    {
66       ObjectName lName = null;
67       while (lName == null)
68       {
69          try
70          {
71             lName = new ObjectName("JMX:type=listener,id=" + mRandom.nextLong());
72             ObjectInstance lInstance = pConnector.createMBean(pClass,
73                     lName,
74                     pParameters,
75                     pSignatures);
76             lName = lInstance.getObjectName();
77          }
78          catch (InstanceAlreadyExistsException iaee)
79          {
80             lName = null;
81          }
82 /* A remote exception could cause an endless loop therefore take it out
83          catch( RemoteException re ) {
84             lName = null;
85          }
86 */

87       }
88       mRemoteListener = lName;
89       return lName;
90    }
91
92    public void addNotificationListener(MEJB pConnector,
93                                        NotificationFilter pFilter) throws
94            InstanceNotFoundException,
95            RemoteException JavaDoc
96    {
97       pConnector.addNotificationListener(mSender,
98               mRemoteListener,
99               pFilter,
100               null);
101    }
102
103    public void removeNotificationListener(MEJB pConnector) throws
104            InstanceNotFoundException,
105            RemoteException JavaDoc
106    {
107       try
108       {
109          pConnector.removeNotificationListener(mSender,
110                  mRemoteListener);
111       }
112       catch (JMException jme)
113       {
114       }
115       try
116       {
117          pConnector.unregisterMBean(mRemoteListener);
118       }
119       catch (JMException jme)
120       {
121       }
122    }
123
124    public ObjectName getSenderMBean()
125    {
126       return mSender;
127    }
128
129    public ObjectName getRemoteListenerName()
130    {
131       return mRemoteListener;
132    }
133
134    public boolean equals(Object JavaDoc pTest)
135    {
136       if (pTest instanceof ClientNotificationListener)
137       {
138          ClientNotificationListener lListener = (ClientNotificationListener) pTest;
139          return
140                  mSender.equals(lListener.mSender) &&
141                  mClientListener.equals(lListener.mClientListener);
142       }
143       return false;
144    }
145
146 }
147
Popular Tags