KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb.CreateException JavaDoc;
27 import javax.management.InstanceNotFoundException JavaDoc;
28 import javax.management.ListenerNotFoundException JavaDoc;
29 import javax.management.NotificationFilter JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.j2ee.ManagementHome JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34 import java.security.InvalidParameterException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * Root class of the JBoss JSR-77 implementation of
40  * {@link javax.management.j2ee.ListenerRegistration ListenerRegistration}.
41  *
42  * @author Andreas Schaefer.
43  * @version $Revision: 37459 $
44  */

45 public class ListenerRegistration
46         implements javax.management.j2ee.ListenerRegistration JavaDoc
47 {
48    // Constants -----------------------------------------------------
49

50    public static final int NOTIFICATION_TYPE_RMI = 0;
51    public static final int NOTIFICATION_TYPE_JMS = 1;
52    public static final int NOTIFICATION_TYPE_POLLING = 2;
53
54    // Attributes ----------------------------------------------------
55

56    private ManagementHome JavaDoc mHome;
57    private int mEventType = NOTIFICATION_TYPE_RMI;
58    private String JavaDoc[] mOptions;
59    private List JavaDoc mListeners = new ArrayList JavaDoc();
60
61    // Static --------------------------------------------------------
62

63    private static final Logger log = Logger.getLogger(ListenerRegistration.class);
64
65    // Constructors --------------------------------------------------
66

67    public ListenerRegistration(ManagementHome JavaDoc pHome, String JavaDoc[] pOptions)
68    {
69       if (pHome == null)
70       {
71          throw new InvalidParameterException JavaDoc("Home Interface must be specified");
72       }
73       mHome = pHome;
74       mOptions = pOptions;
75    }
76
77    // Public --------------------------------------------------------
78

79    // javax.management.j2ee.ListenerRegistration implementation -----
80

81    public void addNotificationListener(ObjectName JavaDoc pName,
82                                        NotificationListener JavaDoc pListener,
83                                        NotificationFilter JavaDoc pFilter,
84                                        Object JavaDoc pHandback)
85            throws
86            InstanceNotFoundException JavaDoc,
87            RemoteException JavaDoc
88    {
89       MEJB lManagement = null;
90       // Create the remote MBean and register it
91
try
92       {
93          // Get EJB
94
lManagement = getMEJB();
95          ClientNotificationListener lListener = null;
96          switch (mEventType)
97          {
98             case NOTIFICATION_TYPE_RMI:
99                lListener = new RMIClientNotificationListener(pName,
100                        pListener,
101                        pHandback,
102                        pFilter,
103                        lManagement);
104                break;
105             case NOTIFICATION_TYPE_JMS:
106                lListener = new JMSClientNotificationListener(pName,
107                        pListener,
108                        pHandback,
109                        pFilter,
110                        mOptions[0],
111                        mOptions[1], // JNDI-Server name
112
lManagement);
113                break;
114             case NOTIFICATION_TYPE_POLLING:
115                lListener = new PollingClientNotificationListener(pName,
116                        pListener,
117                        pHandback,
118                        pFilter,
119                        5000, // Sleeping Period
120
2500, // Maximum Pooled List Size
121
lManagement);
122          }
123          // Add this listener on the client to remove it when the client goes down
124
mListeners.add(lListener);
125       }
126       catch (Exception JavaDoc e)
127       {
128          if (e instanceof RuntimeException JavaDoc)
129          {
130             throw (RuntimeException JavaDoc) e;
131          }
132          if (e instanceof InstanceNotFoundException JavaDoc)
133          {
134             throw (InstanceNotFoundException JavaDoc) e;
135          }
136          throw new RuntimeException JavaDoc("Remote access to perform this operation failed: " + e.getMessage());
137       }
138       finally
139       {
140          if (lManagement != null)
141          {
142             try
143             {
144                lManagement.remove();
145             }
146             catch (Exception JavaDoc e)
147             {
148                log.error("operation failed", e);
149             }
150          }
151       }
152    }
153
154    public void removeNotificationListener(ObjectName JavaDoc pName,
155                                           NotificationListener JavaDoc pListener)
156            throws
157            InstanceNotFoundException JavaDoc,
158            ListenerNotFoundException JavaDoc,
159            RemoteException JavaDoc
160    {
161       MEJB lManagement = null;
162       try
163       {
164          // Get EJB
165
lManagement = getMEJB();
166
167          ClientNotificationListener lCheck = new SearchClientNotificationListener(pName, pListener);
168          int i = mListeners.indexOf(lCheck);
169          if (i >= 0)
170          {
171             ClientNotificationListener lListener = (ClientNotificationListener) mListeners.get(i);
172             lListener.removeNotificationListener(lManagement);
173          }
174       }
175       catch (Exception JavaDoc e)
176       {
177          if (e instanceof RuntimeException JavaDoc)
178          {
179             throw (RuntimeException JavaDoc) e;
180          }
181          if (e instanceof InstanceNotFoundException JavaDoc)
182          {
183             throw (InstanceNotFoundException JavaDoc) e;
184          }
185          throw new RuntimeException JavaDoc("Remote access to perform this operation failed: " + e.getMessage());
186       }
187       finally
188       {
189          if (lManagement != null)
190          {
191             try
192             {
193                lManagement.remove();
194             }
195             catch (Exception JavaDoc e)
196             {
197                log.error("operation failed", e);
198             }
199          }
200       }
201    }
202
203    // Y overrides ---------------------------------------------------
204

205    // Package protected ---------------------------------------------
206

207    // Protected -----------------------------------------------------
208

209    // Private -------------------------------------------------------
210

211    private MEJB getMEJB()
212            throws
213            CreateException JavaDoc,
214            RemoteException JavaDoc
215    {
216       Object JavaDoc lTemp = mHome.create();
217       MEJB lReturn = (MEJB) lTemp;
218       return lReturn;
219    }
220
221    // Inner classes -------------------------------------------------
222
}
223
Popular Tags