001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.management.server;
031:
032: import com.caucho.jmx.Description;
033: import com.caucho.jmx.Jmx;
034:
035: import javax.management.*;
036: import java.util.*;
037: import java.util.logging.Level;
038: import java.util.logging.Logger;
039:
040: /**
041: * Managed object which is a notification emitter.
042: */
043: abstract public class AbstractEmitterObject extends
044: AbstractManagedObject implements NotificationEmitter {
045: private static final Logger log = Logger
046: .getLogger(AbstractEmitterObject.class.getName());
047:
048: private ArrayList<Listener> _listeners;
049: private Listener[] _listenerArray;
050:
051: protected AbstractEmitterObject() {
052: }
053:
054: protected AbstractEmitterObject(ClassLoader loader) {
055: super (loader);
056: }
057:
058: public MBeanNotificationInfo[] getNotificationInfo() {
059: return new MBeanNotificationInfo[0];
060: }
061:
062: public void addNotificationListener(NotificationListener listener,
063: NotificationFilter filter, Object handback)
064: throws IllegalArgumentException {
065: if (listener == null)
066: throw new IllegalArgumentException();
067:
068: if (_listeners == null)
069: _listeners = new ArrayList<Listener>();
070:
071: synchronized (_listeners) {
072: _listeners.add(new Listener(listener, filter, handback));
073:
074: _listenerArray = null;
075: }
076: }
077:
078: public void removeNotificationListener(NotificationListener listener) {
079: if (_listeners != null) {
080: synchronized (_listeners) {
081: for (int i = _listeners.size() - 1; i >= 0; i--) {
082: Listener item = _listeners.get(i);
083:
084: if (item.getListener() == listener) {
085: _listeners.remove(i);
086: }
087: }
088:
089: _listenerArray = null;
090: }
091: }
092: }
093:
094: public void removeNotificationListener(
095: NotificationListener listener, NotificationFilter filter,
096: Object handback) {
097: if (_listeners != null) {
098: synchronized (_listeners) {
099: for (int i = _listeners.size() - 1; i >= 0; i--) {
100: Listener item = _listeners.get(i);
101:
102: if (item.getListener() == listener
103: && item.getFilter() == filter
104: && item.getHandback() == handback) {
105: _listeners.remove(i);
106: }
107: }
108:
109: _listenerArray = null;
110: }
111: }
112: }
113:
114: protected void handleNotification(Notification notification) {
115: Listener[] listeners = null;
116:
117: if (_listeners != null) {
118: synchronized (_listeners) {
119: if (_listenerArray == null && _listeners.size() > 0) {
120: _listenerArray = new Listener[_listeners.size()];
121: _listeners.toArray(_listenerArray);
122: }
123: }
124: }
125:
126: if (listeners != null) {
127: for (int i = 0; i < listeners.length; i++) {
128: listeners[i].handleNotification(notification);
129: }
130: }
131: }
132:
133: static class Listener {
134: private final NotificationListener _listener;
135: private final NotificationFilter _filter;
136: private final Object _handback;
137:
138: Listener(NotificationListener listener,
139: NotificationFilter filter, Object handback) {
140: _listener = listener;
141: _filter = filter;
142: _handback = handback;
143: }
144:
145: NotificationListener getListener() {
146: return _listener;
147: }
148:
149: NotificationFilter getFilter() {
150: return _filter;
151: }
152:
153: Object getHandback() {
154: return _handback;
155: }
156:
157: void handleNotification(Notification notification) {
158: if (_filter == null
159: || _filter.isNotificationEnabled(notification))
160: _listener.handleNotification(notification, _handback);
161: }
162: }
163: }
|