001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/courier/tags/sakai_2-4-1/courier-util/util/src/java/org/sakaiproject/util/EventObservingCourier.java $
003: * $Id: EventObservingCourier.java 7855 2006-04-17 15:33:53Z 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 org.sakaiproject.event.api.Event;
023: import org.sakaiproject.event.api.EventTrackingService;
024: import org.sakaiproject.tool.api.SessionBindingEvent;
025: import org.sakaiproject.tool.api.SessionBindingListener;
026:
027: /**
028: * <p>
029: * EventObservingCourier is an ObservingCourier that watches Events, of a particular reference root. It automatically registers / un-registers as an observer with the event service.
030: * </p>
031: */
032: public class EventObservingCourier extends
033: org.sakaiproject.util.ObservingCourier implements
034: SessionBindingListener {
035: /** Constructor discovered injected EventTrackingService. */
036: protected EventTrackingService m_eventTrackingService = null;
037:
038: /**
039: * Construct.
040: *
041: * @param location
042: * The key identifying the Portal Page Instance.
043: * @param elementId
044: * The key identifying the element on the Portal Page that would need a courier delivered message when things change.
045: * @param The
046: * event resource pattern - we watch for only events whose ref start with this.
047: */
048: public EventObservingCourier(String location, String elementId,
049: String resourcePattern) {
050: super (location, elementId);
051: m_resourcePattern = resourcePattern;
052:
053: // "inject" a eventTrackingService
054: m_eventTrackingService = org.sakaiproject.event.cover.EventTrackingService
055: .getInstance();
056:
057: // register to listen to events
058: m_eventTrackingService.addObserver(this );
059: // %%% add the pattern to have it filtered there?
060: }
061:
062: /** The event resource pattern - we watch for only events that start with this */
063: protected String m_resourcePattern = null;
064:
065: public String getResourcePattern() {
066: return m_resourcePattern;
067: }
068:
069: public void setResourcePattern(String pattern) {
070: m_resourcePattern = pattern;
071: }
072:
073: // %%% re-register? add the pattern to have it filtered there?
074:
075: /**
076: * Check to see if we want to process or ignore this update.
077: *
078: * @param arg
079: * The arg from the update.
080: * @return true to continue, false to quit.
081: */
082: public boolean check(Object arg) {
083: // arg is Event
084: if (!(arg instanceof Event))
085: return false;
086: Event event = (Event) arg;
087:
088: // if this is just a read, not a modify event, we can ignore it
089: if (!event.getModify())
090: return false;
091:
092: String key = null;
093:
094: // filter out events not for us
095: if (m_resourcePattern != null) {
096: key = event.getResource();
097:
098: // if this resource is not in my pattern of resources, we can ignore it
099: if (!key.startsWith(m_resourcePattern))
100: return false;
101: }
102:
103: return true;
104: }
105:
106: protected void finalize() {
107: // stop observing the presence location
108: m_eventTrackingService.deleteObserver(this );
109: }
110:
111: /**********************************************************************************************************************************************************************************************************************************************************
112: * SessionBindingListener implementation
113: *********************************************************************************************************************************************************************************************************************************************************/
114:
115: public void valueBound(SessionBindingEvent event) {
116: }
117:
118: public void valueUnbound(SessionBindingEvent event) {
119: // stop observing the presence location
120: m_eventTrackingService.deleteObserver(this);
121: }
122: }
|