001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/event/tags/sakai_2-4-1/event-impl/impl/src/java/org/sakaiproject/event/impl/BaseEventTrackingService.java $
003: * $Id: BaseEventTrackingService.java 7520 2006-04-09 13:02:09Z 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.event.impl;
021:
022: import java.io.Serializable;
023: import java.util.Observable;
024: import java.util.Observer;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.event.api.Event;
029: import org.sakaiproject.event.api.EventTrackingService;
030: import org.sakaiproject.event.api.NotificationService;
031: import org.sakaiproject.event.api.UsageSession;
032: import org.sakaiproject.event.api.UsageSessionService;
033: import org.sakaiproject.time.api.Time;
034: import org.sakaiproject.user.api.User;
035: import org.sakaiproject.tool.api.SessionManager;
036:
037: /**
038: * <p>
039: * BaseEventTrackingService is the base implmentation for the EventTracking service.
040: * </p>
041: */
042: public abstract class BaseEventTrackingService implements
043: EventTrackingService {
044: /** Our logger. */
045: private static Log M_log = LogFactory
046: .getLog(BaseEventTrackingService.class);
047:
048: /** An observable object helper. */
049: protected MyObservable m_observableHelper = new MyObservable();
050:
051: /** An observable object helper for see-it-first priority observers. */
052: protected MyObservable m_priorityObservableHelper = new MyObservable();
053:
054: /** An observable object helper for see-only-local-events observers. */
055: protected MyObservable m_localObservableHelper = new MyObservable();
056:
057: /**********************************************************************************************************************************************************************************************************************************************************
058: * Observable implementation
059: *********************************************************************************************************************************************************************************************************************************************************/
060:
061: /**
062: * Extend Observable to "public"ize setChanges, so we can set it. Why a helper object? Cause the service (which is observable) already 'extends' TurbineBaseService, and cannot also 'extend' Observable.
063: */
064: protected class MyObservable extends Observable {
065: public void setChanged() {
066: super .setChanged();
067: }
068: }
069:
070: /**********************************************************************************************************************************************************************************************************************************************************
071: * Event post / flow - override
072: *********************************************************************************************************************************************************************************************************************************************************/
073:
074: /**
075: * Cause this new event to get to wherever it has to go for persistence, etc.
076: *
077: * @param event
078: * The new event to post.
079: */
080: protected abstract void postEvent(Event event);
081:
082: /**********************************************************************************************************************************************************************************************************************************************************
083: * Observer notification
084: *********************************************************************************************************************************************************************************************************************************************************/
085:
086: /**
087: * Send notification about a new event to observers.
088: *
089: * @param event
090: * The event to send notification about.
091: * @param local
092: * True if the event originated on this server, false if it came from another server.
093: */
094: protected void notifyObservers(Event event, boolean local) {
095: // %%% inline like this, or on a new thread?
096:
097: if (M_log.isDebugEnabled())
098: M_log.debug(this + " Notification - Event: " + event);
099:
100: // first, notify all priority observers
101: m_priorityObservableHelper.setChanged();
102: m_priorityObservableHelper.notifyObservers(event);
103:
104: // notify the normal observers
105: m_observableHelper.setChanged();
106: m_observableHelper.notifyObservers(event);
107:
108: // if the event is local, notify local observers
109: if (local) {
110: m_localObservableHelper.setChanged();
111: m_localObservableHelper.notifyObservers(event);
112: }
113: }
114:
115: /**********************************************************************************************************************************************************************************************************************************************************
116: * Dependencies
117: *********************************************************************************************************************************************************************************************************************************************************/
118:
119: /**
120: * @return the UsageSessionService collaborator.
121: */
122: protected abstract UsageSessionService usageSessionService();
123:
124: /**
125: * @return the SessionManager collaborator.
126: */
127: protected abstract SessionManager sessionManager();
128:
129: /**********************************************************************************************************************************************************************************************************************************************************
130: * Init and Destroy
131: *********************************************************************************************************************************************************************************************************************************************************/
132:
133: /**
134: * Final initialization, once all dependencies are set.
135: */
136: public void init() {
137: M_log.info(this + ".init()");
138: }
139:
140: /**
141: * Final cleanup.
142: */
143: public void destroy() {
144: M_log.info(this + ".destroy()");
145: }
146:
147: /**********************************************************************************************************************************************************************************************************************************************************
148: * EventTracking implementation
149: *********************************************************************************************************************************************************************************************************************************************************/
150:
151: /**
152: * Construct a Event object.
153: *
154: * @param event
155: * The Event id.
156: * @param resource
157: * The resource reference.
158: * @param modify
159: * Set to true if this event caused a resource modification, false if it was just an access.
160: * @return A new Event object that can be used with this service.
161: */
162: public Event newEvent(String event, String resource, boolean modify) {
163: return new BaseEvent(event, resource, modify,
164: NotificationService.NOTI_OPTIONAL);
165: }
166:
167: /**
168: * Construct a Event object.
169: *
170: * @param event
171: * The Event id.
172: * @param resource
173: * The resource reference.
174: * @param modify
175: * Set to true if this event caused a resource modification, false if it was just an access.
176: * @param priority
177: * The Event's notification priority.
178: * @return A new Event object that can be used with this service.
179: */
180: public Event newEvent(String event, String resource,
181: boolean modify, int priority) {
182: return new BaseEvent(event, resource, modify, priority);
183: }
184:
185: /**
186: * Post an event
187: *
188: * @param event
189: * The event object (created with newEvent()). Note: the current session user will be used as the user responsible for the event.
190: */
191: public void post(Event event) {
192: // get the session id or user id
193: String id = usageSessionService().getSessionId();
194: if (id != null) {
195: ((BaseEvent) event).setSessionId(id);
196: postEvent(event);
197: }
198:
199: // post for the session "thread" user
200: else {
201: id = sessionManager().getCurrentSessionUserId();
202: if (id == null) {
203: id = "?";
204: }
205:
206: ((BaseEvent) event).setUserId(id);
207: postEvent(event);
208: }
209: }
210:
211: /**
212: * Post an event on behalf of a user's session
213: *
214: * @param event
215: * The event object (created with newEvent()).
216: * @param session
217: * The usage session object of the user session responsible for the event.
218: */
219: public void post(Event event, UsageSession session) {
220: String id = "?";
221: if (session != null)
222: id = session.getId();
223:
224: ((BaseEvent) event).setSessionId(id);
225: postEvent(event);
226: }
227:
228: /**
229: * Post an event on behalf of a user.
230: *
231: * @param event
232: * The event object (created with newEvent()).
233: * @param user
234: * The User object of the user responsible for the event.
235: */
236: public void post(Event event, User user) {
237: String id = "?";
238: if (user != null)
239: id = user.getId();
240:
241: ((BaseEvent) event).setUserId(id);
242: postEvent(event);
243: }
244:
245: /**
246: * Add an observer of events. The observer will be notified whenever there are new events.
247: *
248: * @param observer
249: * The class observing.
250: */
251: public void addObserver(Observer observer) {
252: // keep this observer in one list only
253: m_priorityObservableHelper.deleteObserver(observer);
254: m_localObservableHelper.deleteObserver(observer);
255:
256: m_observableHelper.addObserver(observer);
257: }
258:
259: /**
260: * Add an observer of events. The observer will be notified whenever there are new events. Priority observers get notified first, before normal observers.
261: *
262: * @param observer
263: * The class observing.
264: */
265: public void addPriorityObserver(Observer observer) {
266: // keep this observer in one list only
267: m_observableHelper.deleteObserver(observer);
268: m_localObservableHelper.deleteObserver(observer);
269:
270: m_priorityObservableHelper.addObserver(observer);
271: }
272:
273: /**
274: * Add an observer of events. The observer will be notified whenever there are new events. Local observers get notified only of event generated on this application server, not on those generated elsewhere.
275: *
276: * @param observer
277: * The class observing.
278: */
279: public void addLocalObserver(Observer observer) {
280: // keep this observer in one list only
281: m_observableHelper.deleteObserver(observer);
282: m_priorityObservableHelper.deleteObserver(observer);
283:
284: m_localObservableHelper.addObserver(observer);
285: }
286:
287: /**
288: * Delete an observer of events.
289: *
290: * @param observer
291: * The class observing to delete.
292: */
293: public void deleteObserver(Observer observer) {
294: m_observableHelper.deleteObserver(observer);
295: m_priorityObservableHelper.deleteObserver(observer);
296: m_localObservableHelper.deleteObserver(observer);
297: }
298:
299: /**********************************************************************************************************************************************************************************************************************************************************
300: * Event implementation
301: *********************************************************************************************************************************************************************************************************************************************************/
302:
303: /**
304: * <p>
305: * BaseEvent is the implementation of the Event interface.
306: * </p>
307: * <p>
308: * Event objects are posted to the EventTracking service, and may be listened for.
309: * </p>
310: */
311: protected class BaseEvent implements Event, Serializable {
312: /**
313: * Be a good Serializable citizen
314: */
315: private static final long serialVersionUID = 3690761674282252600L;
316:
317: /** The Event's sequence number. */
318: protected long m_seq = 0;
319:
320: /** The Event's id string. */
321: protected String m_id = "";
322:
323: /** The Event's resource reference string. */
324: protected String m_resource = "";
325:
326: /** The Event's session id string. May be null. */
327: protected String m_session = null;
328:
329: /** The Event's user id string. May be null. */
330: protected String m_user = null;
331:
332: /** The Event's modify flag (true if the event caused a resource modification). */
333: protected boolean m_modify = false;
334:
335: /** The Event's notification priority. */
336: protected int m_priority = NotificationService.NOTI_OPTIONAL;
337:
338: /** Event creation time. */
339: protected Time m_time = null;
340:
341: /**
342: * Access the event id string
343: *
344: * @return The event id string.
345: */
346: public String getEvent() {
347: return m_id;
348: }
349:
350: /**
351: * Access the resource reference.
352: *
353: * @return The resource reference string.
354: */
355: public String getResource() {
356: return m_resource;
357: }
358:
359: /**
360: * Access the UsageSession id. If null, check for a User id.
361: *
362: * @return The UsageSession id string.
363: */
364: public String getSessionId() {
365: return m_session;
366: }
367:
368: /**
369: * Access the User id. If null, check for a session id.
370: *
371: * @return The User id string.
372: */
373: public String getUserId() {
374: return m_user;
375: }
376:
377: /**
378: * Is this event one that caused a modify to the resource, or just an access.
379: *
380: * @return true if the event caused a modify to the resource, false if it was just an access.
381: */
382: public boolean getModify() {
383: return m_modify;
384: }
385:
386: /**
387: * Access the event's notification priority.
388: *
389: * @return The event's notification priority.
390: */
391: public int getPriority() {
392: return m_priority;
393: }
394:
395: /**
396: * Construct
397: *
398: * @param event
399: * The Event id.
400: * @param resource
401: * The resource id.
402: * @param modify
403: * If the event caused a modify, true, if it was just an access, false.
404: * @param priority
405: * The Event's notification priority.
406: */
407: public BaseEvent(String event, String resource, boolean modify,
408: int priority) {
409: setEvent(event);
410: setResource(resource);
411: m_modify = modify;
412: m_priority = priority;
413: }
414:
415: /**
416: * Construct
417: *
418: * @param seq
419: * The event sequence number.
420: * @param event
421: * The Event id.
422: * @param resource
423: * The resource id.
424: * @param modify
425: * If the event caused a modify, true, if it was just an access, false.
426: * @param priority
427: * The Event's notification priority.
428: */
429: public BaseEvent(long seq, String event, String resource,
430: boolean modify, int priority) {
431: this (event, resource, modify, priority);
432: m_seq = seq;
433: }
434:
435: /**
436: * Set the event id.
437: *
438: * @param id
439: * The event id string.
440: */
441: protected void setEvent(String id) {
442: if (id != null) {
443: m_id = id;
444: } else {
445: m_id = "";
446: }
447: }
448:
449: /**
450: * Set the resource id.
451: *
452: * @param id
453: * The resource id string.
454: */
455: protected void setResource(String id) {
456: if (id != null) {
457: m_resource = id;
458: } else {
459: m_resource = "";
460: }
461: }
462:
463: /**
464: * Set the session id.
465: *
466: * @param id
467: * The session id string.
468: */
469: protected void setSessionId(String id) {
470: if ((id != null) && (id.length() > 0)) {
471: m_session = id;
472: } else {
473: m_session = null;
474: }
475: }
476:
477: /**
478: * Set the user id.
479: *
480: * @param id
481: * The user id string.
482: */
483: protected void setUserId(String id) {
484: if ((id != null) && (id.length() > 0)) {
485: m_user = id;
486: } else {
487: m_user = null;
488: }
489: }
490:
491: /**
492: * @return A representation of this event's values as a string.
493: */
494: public String toString() {
495: return m_seq + ":" + getEvent() + "@" + getResource() + "["
496: + (getModify() ? "m" : "a") + ", " + getPriority()
497: + "]";
498: }
499: }
500: }
|