001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/courier/tags/sakai_2-4-1/courier-util/util/src/java/org/sakaiproject/util/ObservingCourier.java $
003: * $Id: ObservingCourier.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 java.util.Observable;
023: import java.util.Observer;
024:
025: import org.sakaiproject.courier.api.CourierService;
026: import org.sakaiproject.tool.cover.SessionManager;
027:
028: /**
029: * <p>
030: * ObservingCourier is an observer which uses the courier service to notify when things change.
031: * </p>
032: */
033: public abstract class ObservingCourier implements
034: org.sakaiproject.courier.api.ObservingCourier, Observer {
035: /** Constructor discovered injected CourierService. */
036: protected CourierService m_courierService = null;
037:
038: /** The location (id not ref). */
039: protected String m_location = null;
040:
041: /**
042: * Construct.
043: *
044: * @param location
045: * The key identifying the client window to which this courier delivers updates.
046: * @param elementId
047: * The key identifying the element on the Portal Page that would need a courier delivered message when things change.
048: */
049: public ObservingCourier(String location, String elementId) {
050: m_deliveryId = SessionManager.getCurrentSession().getId()
051: + location;
052: m_elementId = elementId;
053: m_location = location;
054:
055: // "inject" a CourierService
056: m_courierService = org.sakaiproject.courier.cover.CourierService
057: .getInstance();
058: }
059:
060: /** The key identifying the Portal PageSession. */
061: protected String m_deliveryId = "";
062:
063: public String getDeliveryId() {
064: return m_deliveryId;
065: }
066:
067: public void setDeliveryId(String id) {
068: m_deliveryId = id;
069: }
070:
071: /** The key identifying the element on the Portal Page. */
072: protected String m_elementId = "";
073:
074: public String getElementId() {
075: return m_elementId;
076: }
077:
078: public void setElementId(String id) {
079: m_elementId = id;
080: }
081:
082: /** The enabled state. */
083: protected boolean m_enabled = true;
084:
085: public boolean getEnabled() {
086: return m_enabled;
087: }
088:
089: public void enable() {
090: m_enabled = true;
091: }
092:
093: public void disable() {
094: m_enabled = false;
095: }
096:
097: /**
098: * Accept notification that the portal element has just been delivered. If there are pending requests to deliver, they can be cleared.
099: */
100: public void justDelivered() {
101: m_courierService.clear(getDeliveryId(), getElementId());
102: }
103:
104: /**
105: * Check to see if we want to process or ignore this update.
106: *
107: * @param arg
108: * The arg from the update.
109: * @return true to continue, false to quit.
110: */
111: public boolean check(Object arg) {
112: return true;
113: }
114:
115: /**
116: * Access the location this observer is observing.
117: *
118: * @return the location this observer is observing.
119: */
120: public String getLocation() {
121: return m_location;
122: }
123:
124: /**********************************************************************************************************************************************************************************************************************************************************
125: * Observer implementation
126: *********************************************************************************************************************************************************************************************************************************************************/
127:
128: /**
129: * 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
130: * cause the courier service to deliver to the interface controlled by my controller. Extensions can override.
131: *
132: * @param o
133: * the observable object.
134: * @param arg
135: * an argument passed to the <code>notifyObservers</code> method.
136: */
137: public void update(Observable o, Object arg) {
138: // ignore changes when not enabled
139: if (!getEnabled()) {
140: return;
141: }
142:
143: if (!check(arg))
144: return;
145:
146: m_courierService.deliver(new DirectRefreshDelivery(
147: getDeliveryId(), getElementId()));
148: }
149: }
|