001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/presence/tags/sakai_2-4-1/presence-util/util/src/java/org/sakaiproject/util/PresenceObservingCourier.java $
003: * $Id: PresenceObservingCourier.java 8204 2006-04-24 19:35:57Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.util;
021:
022: import java.util.Observable;
023:
024: import org.sakaiproject.event.api.Event;
025: import org.sakaiproject.presence.cover.PresenceService;
026:
027: /**
028: * <p>
029: * PresenceObservingCourier is an EventObservingCourier which watches for only presence service events at a particular location, and delivers a direct refresh delivery.
030: * </p>
031: */
032: public class PresenceObservingCourier extends EventObservingCourier {
033: /**
034: * This variant, watches presense changes at the specified location, and sends the notifications to
035: * that same location. The elementID is null so the main window is refreshed when the notification
036: * is received.
037: *
038: * @param location
039: * The location under observation *and* the location for the delivery of the events.
040: */
041: public PresenceObservingCourier(String location) {
042: super (location, null, PresenceService
043: .presenceReference(location));
044: }
045:
046: /**
047: * This variant watches changes in a window and sends change notifications to the same window.
048: * In the case where there are multiple iframes in the tool, elementID sends notification to
049: * the frame specified by elementId.
050: *
051: * @param location
052: * The location under observation *and* the location for the delivery of the events.
053: * @param elementId
054: * The html element to refresh. If this is null the main window is refreshed.
055: */
056: public PresenceObservingCourier(String location, String elementId) {
057: super (location, elementId, PresenceService
058: .presenceReference(location));
059: }
060:
061: /**
062: * This variant watches changes in one window (watchLocation) and sends the notifications to a different
063: * window (location)..
064: *
065: * @param location
066: * The location which will receive the notifications.
067: * @param elementId
068: * The html element to refresh. If this is null, the main window is refreshed.
069: * @param watchLocation
070: * The location being observed.
071: */
072: public PresenceObservingCourier(String location, String elementId,
073: String watchLocation) {
074: super (location, elementId, PresenceService
075: .presenceReference(watchLocation));
076: }
077:
078: /**
079: * Check to see if we want to process or ignore this update.
080: *
081: * @param arg
082: * The arg from the update.
083: * @return true to continue, false to quit.
084: */
085: public boolean check(Object arg) {
086: // arg is Event
087: if (!(arg instanceof Event))
088: return false;
089: Event event = (Event) arg;
090: String key = event.getResource();
091:
092: // reject non presence events
093: String function = event.getEvent();
094: if (!(function.equals(PresenceService.EVENT_PRESENCE) || function
095: .equals(PresenceService.EVENT_ABSENCE)))
096: return false;
097:
098: // look for matches to the pattern
099: if (m_resourcePattern != null) {
100: if (!key.equals(m_resourcePattern))
101: return false;
102: }
103:
104: return true;
105: }
106:
107: /**
108: * This method is called whenever the observed object is changed. An application calls an <tt>Observable</tt> object's <code>notifyObservers</code> method to have all the object's observers notified of the change. default implementation is to
109: * cause the courier service to deliver to the interface controlled by my controller. Extensions can override.
110: *
111: * @param o
112: * the observable object.
113: * @param arg
114: * an argument passed to the <code>notifyObservers</code> method.
115: */
116: public void update(Observable o, Object arg) {
117: // ignore changes when not enabled
118: if (!getEnabled()) {
119: return;
120: }
121:
122: if (!check(arg))
123: return;
124:
125: if (m_elementId == null) {
126: m_courierService.deliver(new DirectRefreshDelivery(
127: getDeliveryId()));
128: } else {
129: m_courierService.deliver(new DirectRefreshDelivery(
130: getDeliveryId(), m_elementId));
131: }
132: }
133: }
|