001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/presence/trunk/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.chat2.tool;
021:
022: import org.sakaiproject.chat2.model.PresenceObserver;
023:
024: import java.util.List;
025: import java.util.Observable;
026: import java.util.Observer;
027:
028: import org.sakaiproject.event.api.Event;
029: import org.sakaiproject.event.api.EventTrackingService;
030: import org.sakaiproject.presence.cover.PresenceService;
031:
032: /**
033: * <p>
034: * PresenceObservingCourier is an EventObservingCourier which watches for only presence service events at a particular location, and delivers a direct refresh delivery.
035: * </p>
036: */
037: public class PresenceObserverHelper implements Observer {
038: /** Constructor discovered injected EventTrackingService. */
039: protected EventTrackingService m_eventTrackingService = null;
040:
041: private String m_resourcePattern;
042: private String location;
043:
044: private PresenceObserver presenceObserver;
045:
046: /**
047: * This variant, watches presense changes at
048: * the specified location, and sends the notifications to
049: * that same location. The elementID is null so the main window is refreshed when the notification
050: * is received.
051: *
052: * @param location
053: * The location under observation *and* the location for the delivery of the events.
054: */
055: public PresenceObserverHelper(PresenceObserver presenceObserver,
056: String location) {
057: this .presenceObserver = presenceObserver;
058: this .location = location;
059: m_resourcePattern = PresenceService.presenceReference(location);
060:
061: // "inject" a eventTrackingService
062: m_eventTrackingService = org.sakaiproject.event.cover.EventTrackingService
063: .getInstance();
064:
065: // register to listen to events
066: m_eventTrackingService.addObserver(this );
067: // %%% add the pattern to have it filtered there?
068: }
069:
070: protected void finalize() {
071: // stop observing the presence location
072: m_eventTrackingService.deleteObserver(this );
073: }
074:
075: public void endObservation() {
076: // stop observing the presence location
077: m_eventTrackingService.deleteObserver(this );
078: }
079:
080: public void updatePresence() {
081: PresenceService.setPresence(location);
082: }
083:
084: public void removePresence() {
085: PresenceService.removePresence(location);
086: }
087:
088: public List getPresentUsers() {
089: return PresenceService.getPresentUsers(location);
090: }
091:
092: public String getLocation() {
093: return location;
094: }
095:
096: /**
097: * Check to see if we want to process or ignore this update.
098: *
099: * @param arg
100: * The arg from the update.
101: * @return true to continue, false to quit.
102: */
103: public boolean check(Object arg) {
104: // arg is Event
105: if (!(arg instanceof Event))
106: return false;
107: Event event = (Event) arg;
108: String key = event.getResource();
109:
110: // reject non presence events
111: String function = event.getEvent();
112: if (!(function.equals(PresenceService.EVENT_PRESENCE) || function
113: .equals(PresenceService.EVENT_ABSENCE)))
114: return false;
115:
116: // look for matches to the pattern
117: if (m_resourcePattern != null) {
118: if (!key.equals(m_resourcePattern))
119: return false;
120: }
121:
122: return true;
123: }
124:
125: /**
126: * 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
127: * cause the courier service to deliver to the interface controlled by my controller. Extensions can override.
128: *
129: * @param o
130: * the observable object.
131: * @param arg
132: * an argument passed to the <code>notifyObservers</code> method.
133: */
134: public void update(Observable o, Object arg) {
135: if (!check(arg))
136: return;
137:
138: Event event = (Event) arg;
139:
140: if (event.getEvent().equals(PresenceService.EVENT_PRESENCE))
141: presenceObserver.userJoined(location, "");
142: else
143: presenceObserver.userLeft(location, "");
144:
145: }
146: }
|