001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/presence/tags/sakai_2-4-1/presence-impl/impl/src/java/org/sakaiproject/presence/impl/ClusterPresenceService.java $
003: * $Id: ClusterPresenceService.java 15112 2006-09-21 19:10:54Z 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.presence.impl;
021:
022: import java.util.List;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.sakaiproject.db.api.SqlService;
027:
028: /**
029: * <p>
030: * ClusterPresenceService extends the BasePresenceService with a Storage model that keeps track of presence for a cluster of Sakai app servers, backed by a shared DB table.
031: * </p>
032: */
033: public class ClusterPresenceService extends BasePresenceService {
034: /** Our log (commons). */
035: private static Log M_log = LogFactory
036: .getLog(ClusterPresenceService.class);
037:
038: /**
039: * Allocate a new storage object.
040: *
041: * @return A new storage object.
042: */
043: protected Storage newStorage() {
044: return new ClusterStorage();
045: }
046:
047: /** Dependency: SqlService */
048: protected SqlService m_sqlService = null;
049:
050: /**
051: * Dependency: SqlService.
052: *
053: * @param service
054: * The SqlService.
055: */
056: public void setSqlService(SqlService service) {
057: m_sqlService = service;
058: }
059:
060: /** Configuration: to run the ddl on init or not. */
061: protected boolean m_autoDdl = false;
062:
063: /**
064: * Configuration: to run the ddl on init or not.
065: *
066: * @param value
067: * the auto ddl value.
068: */
069: public void setAutoDdl(String value) {
070: m_autoDdl = new Boolean(value).booleanValue();
071: }
072:
073: /**********************************************************************************************************************************************************************************************************************************************************
074: * Init and Destroy
075: *********************************************************************************************************************************************************************************************************************************************************/
076:
077: /**
078: * Final initialization, once all dependencies are set.
079: */
080: public void init() {
081: try {
082: // if we are auto-creating our schema, check and create
083: if (m_autoDdl) {
084: m_sqlService.ddl(this .getClass().getClassLoader(),
085: "sakai_presence");
086: }
087:
088: super .init();
089: } catch (Throwable t) {
090: M_log.warn("init(): ", t);
091: }
092: }
093:
094: /**********************************************************************************************************************************************************************************************************************************************************
095: * Storage
096: *********************************************************************************************************************************************************************************************************************************************************/
097:
098: protected class ClusterStorage implements Storage {
099: /**
100: * {@inheritDoc}
101: */
102: public void setPresence(String sessionId, String locationId) {
103: // send this to the database
104: String statement = "insert into SAKAI_PRESENCE (SESSION_ID,LOCATION_ID) values ( ?, ?)";
105:
106: // collect the fields
107: Object fields[] = new Object[2];
108: fields[0] = sessionId;
109: fields[1] = locationId;
110:
111: // process the insert
112: boolean ok = m_sqlService.dbWrite(statement, fields);
113: if (!ok) {
114: M_log.warn("setPresence(): dbWrite failed");
115: }
116: }
117:
118: /**
119: * {@inheritDoc}
120: */
121: public void removePresence(String sessionId, String locationId) {
122: // form the SQL delete statement
123: String statement = "delete from SAKAI_PRESENCE"
124: + " where ( SESSION_ID = ? and LOCATION_ID = ?)";
125:
126: // setup the fields
127: Object[] fields = new Object[1];
128: fields[0] = sessionId;
129:
130: // process the remove
131: boolean ok = m_sqlService.dbWrite(statement, fields,
132: locationId);
133: if (!ok) {
134: M_log.warn("removePresence(): dbWrite failed");
135: }
136: }
137:
138: /**
139: * {@inheritDoc}
140: */
141: public List getSessions(String locationId) {
142: // Note: this assumes
143: // 1) the UsageSessionService has a db component selected.
144: // 2) the presence table and the session table are in the same db.
145:
146: // to join the presence table to the session
147: String joinTable = "SAKAI_PRESENCE";
148: String joinAlias = "A";
149: String joinColumn = "SESSION_ID";
150: String joinCriteria = "A.LOCATION_ID = ?";
151:
152: // send in the locationId
153: Object[] fields = new Object[1];
154: fields[0] = locationId;
155:
156: // get these from usage session
157: List sessions = m_usageSessionService.getSessions(
158: joinTable, joinAlias, joinColumn, joinCriteria,
159: fields);
160:
161: return sessions;
162: }
163:
164: /**
165: * {@inheritDoc}
166: */
167: public List getLocations() {
168: // form the SQL query
169: String statement = "select DISTINCT LOCATION_ID from SAKAI_PRESENCE";
170:
171: List locs = m_sqlService.dbRead(statement);
172:
173: return locs;
174: }
175: }
176: }
|