001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)NotificationServiceImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.esb.management.impl.notification;
030:
031: import java.io.IOException;
032: import java.util.ArrayList;
033: import java.util.concurrent.ExecutorService;
034: import java.util.concurrent.Executors;
035:
036: import javax.management.InstanceNotFoundException;
037: import javax.management.MBeanServerConnection;
038: import javax.management.Notification;
039: import javax.management.NotificationListener;
040: import javax.management.ObjectName;
041:
042: import com.sun.esb.management.api.notification.EventNotification;
043: import com.sun.esb.management.api.notification.EventNotificationListener;
044: import com.sun.esb.management.api.notification.NotificationService;
045: import com.sun.esb.management.base.services.BaseServiceImpl;
046: import com.sun.esb.management.common.ManagementRemoteException;
047:
048: /**
049: * @author graj
050: *
051: */
052: public class NotificationServiceImpl extends BaseServiceImpl implements
053: NotificationListener, NotificationService {
054:
055: static final long serialVersionUID = -1L;
056:
057: /**
058: * Store a list of listeners
059: */
060: private ArrayList<EventNotificationListener> listeners = new ArrayList<EventNotificationListener>();
061:
062: /**
063: * Executor service to send notifications on threads from a pool.
064: */
065: private ExecutorService executorService;
066:
067: /** Constructor - Constructs a new instance of NotificationServiceImpl */
068: public NotificationServiceImpl() {
069: super (null, false);
070: }
071:
072: /**
073: * Constructor - Constructs a new instance of NotificationServiceImpl
074: *
075: * @param serverConnection
076: */
077: public NotificationServiceImpl(
078: MBeanServerConnection serverConnection) {
079: super (serverConnection, false);
080: }
081:
082: /**
083: * Constructor - Constructs a new instance of NotificationServiceImpl
084: *
085: * @param serverConnection
086: * @param isRemoteConnection
087: */
088: public NotificationServiceImpl(
089: MBeanServerConnection serverConnection,
090: boolean isRemoteConnection) {
091: super (serverConnection, isRemoteConnection);
092: // Create the thread pool to be used for sending notifications
093: executorService = Executors.newCachedThreadPool();
094: registerForNotifications();
095: }
096:
097: /**
098: * Register for notifications
099: */
100: protected void registerForNotifications() {
101: try {
102: ObjectName mbeanName = getNotificationServiceMBeanObjectName();
103: this .remoteConnection.addNotificationListener(mbeanName,
104: this , null, new Object());
105: } catch (InstanceNotFoundException e) {
106: } catch (ManagementRemoteException e) {
107: } catch (IOException e) {
108: }
109: }
110:
111: /**
112: * Handle the Event Notification
113: * @param the notification
114: * @param the handback object
115: *
116: * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, java.lang.Object)
117: */
118: public void handleNotification(Notification notification,
119: Object handback) {
120: EventNotificationImpl event = new EventNotificationImpl(
121: notification.getSource());
122: event.setNotification(notification);
123: executorService.execute(new NotificationSender(this .listeners,
124: event));
125: }
126:
127: /**
128: * Add notification listener
129: * @param an EventNotificationListener object
130: * @see com.sun.esb.management.api.notification.NotificationService#addNotificationEventListener(com.sun.esb.management.api.notification.EventNotificationListener)
131: */
132: public void addNotificationEventListener(
133: EventNotificationListener aListener)
134: throws ManagementRemoteException {
135: listeners.add(aListener);
136: }
137:
138: /**
139: * Remove notification listener
140: * @param an EventNotificationListener object
141: * @see com.sun.esb.management.api.notification.NotificationService#removeNotificationEventListener(com.sun.esb.management.api.notification.EventNotificationListener)
142: */
143: public void removeNotificationEventListener(
144: EventNotificationListener aListener) {
145: listeners.remove(aListener);
146: }
147:
148: /**
149: * Class to send out notifications on a separate thread
150: * @author graj
151: */
152: class NotificationSender implements Runnable {
153: private ArrayList<EventNotificationListener> listeners = new ArrayList<EventNotificationListener>();
154:
155: /**
156: * Notification instance to be sent.
157: */
158: EventNotification eventNotification;
159:
160: /**
161: * Constructor.
162: *
163: * @param en the instance of EventNotifier that called this constructor.
164: * @param notif the JMX Notification to be sent.
165: */
166: NotificationSender(ArrayList<EventNotificationListener> en,
167: EventNotification notif) {
168: listeners = en;
169: eventNotification = notif;
170: }
171:
172: /**
173: * Run the actual sending of the notification. This runs on a thread
174: * from the pool created by Executors.newCachedThreadPool().
175: */
176: public void run() {
177: ArrayList<EventNotificationListener> list = null;
178: synchronized (this ) {
179: list = (ArrayList<EventNotificationListener>) listeners
180: .clone();
181: }
182: for (EventNotificationListener aListener : list) {
183: aListener.processNotification(eventNotification);
184: }
185: }
186: }
187:
188: }
|