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.ejb.CreateException;
027: import javax.management.InstanceNotFoundException;
028: import javax.management.ListenerNotFoundException;
029: import javax.management.NotificationFilter;
030: import javax.management.NotificationListener;
031: import javax.management.ObjectName;
032: import javax.management.j2ee.ManagementHome;
033: import java.rmi.RemoteException;
034: import java.security.InvalidParameterException;
035: import java.util.ArrayList;
036: import java.util.List;
037:
038: /**
039: * Root class of the JBoss JSR-77 implementation of
040: * {@link javax.management.j2ee.ListenerRegistration ListenerRegistration}.
041: *
042: * @author Andreas Schaefer.
043: * @version $Revision: 57197 $
044: */
045: public class ListenerRegistration implements
046: javax.management.j2ee.ListenerRegistration {
047: // Constants -----------------------------------------------------
048:
049: public static final int NOTIFICATION_TYPE_RMI = 0;
050: public static final int NOTIFICATION_TYPE_JMS = 1;
051: public static final int NOTIFICATION_TYPE_POLLING = 2;
052:
053: // Attributes ----------------------------------------------------
054:
055: private ManagementHome mHome;
056: private int mEventType = NOTIFICATION_TYPE_RMI;
057: private String[] mOptions;
058: private List mListeners = new ArrayList();
059:
060: // Static --------------------------------------------------------
061:
062: private static final Logger log = Logger
063: .getLogger(ListenerRegistration.class);
064:
065: // Constructors --------------------------------------------------
066:
067: public ListenerRegistration(ManagementHome pHome, String[] pOptions) {
068: if (pHome == null) {
069: throw new InvalidParameterException(
070: "Home Interface must be specified");
071: }
072: mHome = pHome;
073: mOptions = pOptions;
074: }
075:
076: // Public --------------------------------------------------------
077:
078: // javax.management.j2ee.ListenerRegistration implementation -----
079:
080: public void addNotificationListener(ObjectName pName,
081: NotificationListener pListener, NotificationFilter pFilter,
082: Object pHandback) throws InstanceNotFoundException,
083: RemoteException {
084: MEJB lManagement = null;
085: // Create the remote MBean and register it
086: try {
087: // Get EJB
088: lManagement = getMEJB();
089: ClientNotificationListener lListener = null;
090: switch (mEventType) {
091: case NOTIFICATION_TYPE_RMI:
092: lListener = new RMIClientNotificationListener(pName,
093: pListener, pHandback, pFilter, lManagement);
094: break;
095: case NOTIFICATION_TYPE_JMS:
096: lListener = new JMSClientNotificationListener(pName,
097: pListener, pHandback, pFilter, mOptions[0],
098: mOptions[1], // JNDI-Server name
099: lManagement);
100: break;
101: case NOTIFICATION_TYPE_POLLING:
102: lListener = new PollingClientNotificationListener(
103: pName, pListener, pHandback, pFilter, 5000, // Sleeping Period
104: 2500, // Maximum Pooled List Size
105: lManagement);
106: }
107: // Add this listener on the client to remove it when the client goes down
108: mListeners.add(lListener);
109: } catch (Exception e) {
110: if (e instanceof RuntimeException) {
111: throw (RuntimeException) e;
112: }
113: if (e instanceof InstanceNotFoundException) {
114: throw (InstanceNotFoundException) e;
115: }
116: throw new RuntimeException(
117: "Remote access to perform this operation failed: "
118: + e.getMessage());
119: } finally {
120: if (lManagement != null) {
121: try {
122: lManagement.remove();
123: } catch (Exception e) {
124: log.error("operation failed", e);
125: }
126: }
127: }
128: }
129:
130: public void removeNotificationListener(ObjectName pName,
131: NotificationListener pListener)
132: throws InstanceNotFoundException,
133: ListenerNotFoundException, RemoteException {
134: MEJB lManagement = null;
135: try {
136: // Get EJB
137: lManagement = getMEJB();
138:
139: ClientNotificationListener lCheck = new SearchClientNotificationListener(
140: pName, pListener);
141: int i = mListeners.indexOf(lCheck);
142: if (i >= 0) {
143: ClientNotificationListener lListener = (ClientNotificationListener) mListeners
144: .get(i);
145: lListener.removeNotificationListener(lManagement);
146: }
147: } catch (Exception e) {
148: if (e instanceof RuntimeException) {
149: throw (RuntimeException) e;
150: }
151: if (e instanceof InstanceNotFoundException) {
152: throw (InstanceNotFoundException) e;
153: }
154: throw new RuntimeException(
155: "Remote access to perform this operation failed: "
156: + e.getMessage());
157: } finally {
158: if (lManagement != null) {
159: try {
160: lManagement.remove();
161: } catch (Exception e) {
162: log.error("operation failed", e);
163: }
164: }
165: }
166: }
167:
168: // Y overrides ---------------------------------------------------
169:
170: // Package protected ---------------------------------------------
171:
172: // Protected -----------------------------------------------------
173:
174: // Private -------------------------------------------------------
175:
176: private MEJB getMEJB() throws CreateException, RemoteException {
177: Object lTemp = mHome.create();
178: MEJB lReturn = (MEJB) lTemp;
179: return lReturn;
180: }
181:
182: // Inner classes -------------------------------------------------
183: }
|