001: package org.jacorb.notification;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.avalon.framework.logger.Logger;
029: import org.jacorb.notification.util.LogUtil;
030: import org.omg.CosNotification.EventType;
031: import org.omg.CosNotifyComm.InvalidEventType;
032: import org.omg.CosNotifyComm.NotifySubscribeOperations;
033:
034: /**
035: * Utility class that manages subscriptions of NotifySubscribers and broadcasting of subscription_change
036: * requests.
037: *
038: * @author Alphonse Bendt
039: * @version $Id: SubscriptionManager.java,v 1.10 2006/01/29 21:19:23 alphonse.bendt Exp $
040: */
041:
042: public class SubscriptionManager extends EventTypeSet implements
043: NotifySubscribeOperations {
044: public static final SubscriptionManager NULL_MANAGER = new SubscriptionManager(
045: Collections.EMPTY_LIST);
046:
047: private final Logger logger_ = LogUtil.getLogger(getClass()
048: .getName());
049:
050: private final List listeners_;
051:
052: public SubscriptionManager() {
053: this (new ArrayList());
054: }
055:
056: private SubscriptionManager(List list) {
057: listeners_ = list;
058: }
059:
060: ////////////////////////////////////////
061:
062: public void addListener(NotifySubscribeOperations listener) {
063: synchronized (listeners_) {
064: listeners_.add(listener);
065: }
066: }
067:
068: public void removeListener(NotifySubscribeOperations listener) {
069: synchronized (listeners_) {
070: listeners_.remove(listener);
071: }
072: }
073:
074: public void actionSetChanged(EventType[] added, EventType[] removed) {
075: synchronized (listeners_) {
076: // use a iterator on a copy of the original list here.
077: // otherwise the iterator would fail if the list would be
078: // modified concurrently which may happen during
079: // subscription_change.
080: Iterator _i = new ArrayList(listeners_).iterator();
081:
082: while (_i.hasNext()) {
083: NotifySubscribeOperations _listener = (NotifySubscribeOperations) _i
084: .next();
085:
086: try {
087: _listener.subscription_change(added, removed);
088: } catch (Exception e) {
089: logger_.warn("subscription_change failed for "
090: + _listener, e);
091: }
092: }
093: }
094: }
095:
096: public void subscription_change(EventType[] added,
097: EventType[] removed) throws InvalidEventType {
098: changeSet(added, removed);
099: }
100:
101: public EventType[] obtain_subscription_types() {
102: return getAllTypes();
103: }
104: }
|