001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/courier/tags/sakai_2-4-1/courier-impl/impl/src/java/org/sakaiproject/courier/impl/BasicCourierService.java $
003: * $Id: BasicCourierService.java 6940 2006-03-23 16:35:31Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.courier.impl;
021:
022: // imports
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Vector;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.sakaiproject.courier.api.CourierService;
032: import org.sakaiproject.courier.api.Delivery;
033: import org.sakaiproject.util.StringUtil;
034:
035: /**
036: * <p>
037: * BasicCourierService is the implementation for the CourierService.
038: * </p>
039: */
040: public class BasicCourierService implements CourierService {
041: /** Our logger. */
042: private static Log M_log = LogFactory
043: .getLog(BasicCourierService.class);
044:
045: /** Stores a List of Deliveries for each address, keyed by address. */
046: protected Map m_addresses = new Hashtable();
047:
048: /**********************************************************************************************************************************************************************************************************************************************************
049: * Dependencies and their setter methods
050: *********************************************************************************************************************************************************************************************************************************************************/
051:
052: /**********************************************************************************************************************************************************************************************************************************************************
053: * Init and Destroy
054: *********************************************************************************************************************************************************************************************************************************************************/
055:
056: /**
057: * Final initialization, once all dependencies are set.
058: */
059: public void init() {
060: m_addresses.clear();
061: M_log.info("init()");
062: }
063:
064: /**
065: * Returns to uninitialized state.
066: */
067: public void destroy() {
068: m_addresses.clear();
069: M_log.info("destroy()");
070: }
071:
072: /**********************************************************************************************************************************************************************************************************************************************************
073: * CourierService implementation
074: *********************************************************************************************************************************************************************************************************************************************************/
075:
076: /**
077: * Queue up a delivery for the client window identified in the Delivery object. The particular form of delivery is determined by the type of Delivery object sent.
078: *
079: * @param delivery
080: * The Delivery (or extension) object to deliver.
081: */
082: public void deliver(Delivery delivery) {
083: if (M_log.isDebugEnabled())
084: M_log.debug("deliver(): " + delivery);
085:
086: String address = delivery.getAddress();
087:
088: // find the entry in m_addresses
089: List deliveries = (List) m_addresses.get(address);
090:
091: // create if needed
092: if (deliveries == null) {
093: synchronized (m_addresses) {
094: deliveries = (List) m_addresses.get(address);
095: if (deliveries == null) {
096: deliveries = new Vector();
097: m_addresses.put(address, deliveries);
098: }
099: }
100: }
101:
102: // if this doesn't exist in the list already, add it
103: synchronized (deliveries) {
104: if (!deliveries.contains(delivery)) {
105: deliveries.add(delivery);
106: }
107: }
108: }
109:
110: /**
111: * Clear any pending delivery requests to the particular client window for this element.
112: *
113: * @param address
114: * The address of the client window.
115: * @param elementId
116: * The id of the html element.
117: */
118: public void clear(String address, String elementId) {
119: if (M_log.isDebugEnabled())
120: M_log.debug("clear(): " + address + ", " + elementId);
121:
122: // find the entry in m_addresses
123: List deliveries = (List) m_addresses.get(address);
124:
125: // if not there we are done
126: if (deliveries == null)
127: return;
128:
129: // remove any Deliveries with this elementId
130: synchronized (deliveries) {
131: for (Iterator it = deliveries.iterator(); it.hasNext();) {
132: Delivery delivery = (Delivery) it.next();
133: if (!StringUtil.different(delivery.getElement(),
134: elementId)) {
135: it.remove();
136: }
137: }
138: }
139:
140: // if none left, remove it from the list
141: if (deliveries.isEmpty()) {
142: synchronized (m_addresses) {
143: m_addresses.remove(address);
144: }
145: }
146: }
147:
148: /**
149: * Clear any pending delivery requests to this session client window.
150: *
151: * @param address
152: * The address of client window.
153: */
154: public void clear(String address) {
155: if (M_log.isDebugEnabled())
156: M_log.debug("clear(): " + address);
157:
158: // remove this portal from m_addresses
159: synchronized (m_addresses) {
160: m_addresses.remove(address);
161: }
162: }
163:
164: /**
165: * Access and de-queue the Deliveries queued up for a particular session client window.
166: *
167: * @param address
168: * The address of client window.
169: * @return a List of Delivery objects addressed to this session client window.
170: */
171: public List getDeliveries(String address) {
172: if (M_log.isDebugEnabled())
173: M_log.debug("getDeliveries(): " + address);
174:
175: // find the entry in m_addresses
176: List deliveries = null;
177: synchronized (m_addresses) {
178: deliveries = (List) m_addresses.get(address);
179: if (deliveries != null) {
180: m_addresses.remove(address);
181: }
182: }
183:
184: // if empty, return something
185: if (deliveries == null) {
186: deliveries = new Vector();
187: }
188:
189: // "act" all the deliveries
190: for (Iterator it = deliveries.iterator(); it.hasNext();) {
191: Delivery delivery = (Delivery) it.next();
192: delivery.act();
193: }
194:
195: return deliveries;
196: }
197:
198: /**
199: * Check to see if there are any deliveries queued up for a particular session client window.
200: *
201: * @param address
202: * The address of the client window.
203: * @return true if there are deliveries for this client window, false if not.
204: */
205: public boolean hasDeliveries(String address) {
206: if (M_log.isDebugEnabled())
207: M_log.debug("hasDeliveries(): " + address);
208:
209: // find the entry in m_addresses
210: List deliveries = (List) m_addresses.get(address);
211: if (deliveries == null)
212: return false;
213:
214: return (!deliveries.isEmpty());
215: }
216: }
|