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.NotifyPublishOperations;
033:
034: /**
035: * Utility class that manages subscriptions of NotifyPublishers and broadcasting of offer_change requests.
036: *
037: * @author Alphonse Bendt
038: * @version $Id: OfferManager.java,v 1.9 2006/01/29 21:19:23 alphonse.bendt Exp $
039: */
040:
041: public class OfferManager extends EventTypeSet implements
042: NotifyPublishOperations {
043: public static final OfferManager NULL_MANAGER = new OfferManager(
044: Collections.EMPTY_LIST);
045:
046: private final List listeners_;
047:
048: private final Logger logger_ = LogUtil.getLogger(getClass()
049: .getName());
050:
051: public OfferManager() {
052: this (new ArrayList());
053: }
054:
055: private OfferManager(List list) {
056: listeners_ = list;
057: }
058:
059: ////////////////////////////////////////
060:
061: public void addListener(NotifyPublishOperations listener) {
062: synchronized (listeners_) {
063: listeners_.add(listener);
064: }
065: }
066:
067: public void removeListener(NotifyPublishOperations listener) {
068: synchronized (listeners_) {
069: listeners_.remove(listener);
070: }
071: }
072:
073: public void actionSetChanged(EventType[] added, EventType[] removed) {
074: synchronized (listeners_) {
075: // use a iterator on a copy of the original list here.
076: // otherwise the iterator would fail if the list would be
077: // modified concurrently which happens during offer_change.
078: Iterator _i = new ArrayList(listeners_).iterator();
079:
080: while (_i.hasNext()) {
081: NotifyPublishOperations _listener = (NotifyPublishOperations) _i
082: .next();
083:
084: try {
085: _listener.offer_change(added, removed);
086: } catch (Exception e) {
087: logger_.warn(
088: "offer_change failed for " + _listener, e);
089: }
090: }
091: }
092: }
093:
094: public void offer_change(EventType[] added, EventType[] removed)
095: throws InvalidEventType {
096: changeSet(added, removed);
097: }
098:
099: public EventType[] obtain_offered_types() {
100: return getAllTypes();
101: }
102: }
|