001: /**********************************************************************************
002: *
003: * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
004: * Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
005: *
006: * Licensed under the Educational Community License Version 1.0 (the "License");
007: * By obtaining, using and/or copying this Original Work, you agree that you have read,
008: * understand, and will comply with the terms and conditions of the Educational Community License.
009: * You may obtain a copy of the License at:
010: *
011: * http://cvs.sakaiproject.org/licenses/license_1_0.html
012: *
013: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
014: * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
015: * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
016: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
017: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
018: *
019: **********************************************************************************/package edu.indiana.lib.twinpeaks.util;
020:
021: import edu.indiana.lib.twinpeaks.search.*;
022:
023: import java.io.*;
024: import java.net.*;
025: import java.lang.*;
026: import java.sql.*;
027: import java.util.*;
028:
029: import net.sf.ehcache.*;
030:
031: /**
032: * Expose the session context block
033: */
034: public class SessionContext {
035:
036: private static org.apache.commons.logging.Log _log = LogUtils
037: .getLog(SessionContext.class);
038: /*
039: * ehcache constants
040: */
041: /**
042: * Cache name
043: */
044: private final static String CACHENAME = "org.sakai.sakaibrary.http-osid-sessioncache";
045: /**
046: * Items held in memory
047: */
048: private static int CACHE_MEMORY_ELEMENTS = 50;
049: /**
050: * Cache entry maximum time-to-live (seconds). Zero is infinite.
051: */
052: private static int CACHE_TTL = 12 * (60 * 60);
053: /**
054: * Cache entry idle time (seconds)
055: */
056: private static int CACHE_IDLE_TIME = 15 * 60;
057: /**
058: * Session id (passed by caller)
059: */
060: private String _sessionId = null;
061: /**
062: * Local storage for name=value pairs
063: */
064: private HashMap _parameterMap = null;
065:
066: /**
067: * Private constructor - set up the cache as required
068: */
069: private SessionContext(String id) {
070: Element element;
071:
072: try {
073: CacheManager cacheManager;
074: Cache cache;
075:
076: /*
077: * Fetch singleton cache manager
078: */
079: cacheManager = CacheManager.create();
080: /*
081: * And create our cache (if necessary)
082: */
083: if ((cache = cacheManager.getCache(CACHENAME)) == null) {
084: cache = new Cache(CACHENAME, // Name
085: CACHE_MEMORY_ELEMENTS, // Elements held in memory
086: true, // Overflow to disk?
087: false, // Disk elements live forever?
088: CACHE_TTL, // Element maximum time to live
089: CACHE_IDLE_TIME, // Element idle time removal
090: false, // Disk content exists across VM restart?
091: 240); // Disk content idle check frequency
092:
093: cacheManager.addCache(cache);
094: _log.debug("Cache created: " + CACHENAME);
095: }
096: _log.debug(CACHENAME + " size = " + cache.getSize());
097: _log.debug(CACHENAME + " keys = "
098: + cache.getKeys().toString());
099: /*
100: * Fetch the session cache (create a new one if necessary)
101: */
102: element = cache.get(id);
103: _log.debug("cache.get(" + id + ") finds " + element);
104:
105: if (element == null) {
106: element = new Element(id, new HashMap());
107: cache.put(element);
108:
109: _log.debug("HashMap() created for id " + id);
110:
111: }
112: } catch (Exception exception) {
113: throw new SearchException(exception.toString());
114: }
115:
116: _sessionId = id;
117: _parameterMap = (HashMap) element.getValue();
118: }
119:
120: /**
121: * Get a Session Context instance
122: */
123: public static SessionContext getInstance(String id) {
124: return new SessionContext(id);
125: }
126:
127: /**
128: * Normalize a parameter name (add a simple prefix to the parameter name; this
129: * differentiates our names from those generated
130: * by ehcache)
131: * @param name Parameter name
132: * @return Normalized name
133: */
134: private static String normalize(String name) {
135: return "_" + name.trim();
136: }
137:
138: /**
139: * Fetch a value
140: * @param name Attribute name
141: * @return Requested value
142: */
143: public Object get(String name) {
144: return _parameterMap.get(normalize(name));
145: }
146:
147: /**
148: * Set a name=value pair
149: * @param name Attribute name
150: * @param value Attribute value
151: */
152: public void put(String name, Object value) {
153: _parameterMap.put(normalize(name), value);
154: }
155:
156: /**
157: * Fetch an int value
158: * @param name Attribute name
159: * @return Requested value
160: */
161: public int getInt(String name) {
162: String value = (String) _parameterMap.get(normalize(name));
163:
164: try {
165: return Integer.parseInt(value);
166: } catch (Exception NumberFormatException) {
167: throw new SearchException("Invalid number: " + value);
168: }
169: }
170:
171: /**
172: * Set a name=value pair
173: * @param name Attribute name
174: * @param value Attribute value
175: */
176: public void putInt(String name, int value) {
177: _parameterMap.put(normalize(name), String.valueOf(value));
178: }
179:
180: /**
181: * Delete a name=value pair
182: * @param name Attribute name
183: */
184: public void remove(String name) {
185: _parameterMap.remove(normalize(name));
186: }
187:
188: /*
189: * Helpers for the "search source" handlers
190: */
191:
192: /**
193: * Construct a session-wide unique name (unique within a browser session)
194: * @parameter The parent (calling) Object
195: */
196: public static String uniqueSessionName(Object parent) {
197: /*
198: * With ehcache, we don't need the following level of "unique detail":
199: *
200: * return StringUtils.replace(parent.getClass().getName(), "\\.", ":");
201: */
202: return "";
203: }
204: }
|