001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/util/tags/sakai_2-4-1/util-impl/impl/src/java/org/sakaiproject/thread_local/impl/ThreadLocalComponent.java $
003: * $Id: ThreadLocalComponent.java 6832 2006-03-21 20:43:34Z 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.thread_local.impl;
021:
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.sakaiproject.thread_local.api.ThreadLocalManager;
028:
029: /**
030: * <p>
031: * ThreadLocalComponent provides the standard implementation of the Sakai Framework ThreadLocalManager.
032: * </p>
033: * <p>
034: * See the {@link org.sakaiproject.api.kernel.thread_local.ThreadLocalManager}interface for details.
035: * </p>
036: */
037: public class ThreadLocalComponent implements ThreadLocalManager {
038: /** Our log (commons). */
039: private static Log M_log = LogFactory
040: .getLog(ThreadLocalComponent.class);
041:
042: /**
043: * <p>
044: * ThreadBindings is a thread local map of keys to objects, holding the things bound to each thread.
045: * </p>
046: */
047: protected class ThreadBindings extends ThreadLocal {
048: public Object initialValue() {
049: return new HashMap();
050: }
051:
052: public Map getBindings() {
053: return (Map) get();
054: }
055: }
056:
057: /** The bindings for each thread. */
058: protected ThreadBindings m_bindings = new ThreadBindings();
059:
060: /**********************************************************************************************************************************************************************************************************************************************************
061: * Dependencies and their setter methods
062: *********************************************************************************************************************************************************************************************************************************************************/
063:
064: /**********************************************************************************************************************************************************************************************************************************************************
065: * Init and Destroy
066: *********************************************************************************************************************************************************************************************************************************************************/
067:
068: /**
069: * Final initialization, once all dependencies are set.
070: */
071: public void init() {
072: M_log.info("init()");
073: }
074:
075: /**
076: * Final cleanup.
077: */
078: public void destroy() {
079: M_log.info("destroy()");
080: }
081:
082: /**********************************************************************************************************************************************************************************************************************************************************
083: * Work interface methods: org.sakaiproject.api.kernel.thread_local.ThreadLocalManager
084: *********************************************************************************************************************************************************************************************************************************************************/
085:
086: /**
087: * {@inheritDoc}
088: */
089: public void set(String name, Object value) {
090: // find the map that might already exist
091: Map bindings = m_bindings.getBindings();
092: if (bindings == null) {
093: M_log.warn("setInThread: no bindings!");
094: return;
095: }
096:
097: // remove if nulling
098: if (value == null) {
099: bindings.remove(name);
100: }
101:
102: // otherwise bind the object
103: else {
104: bindings.put(name, value);
105: }
106: }
107:
108: /**
109: * {@inheritDoc}
110: */
111: public void clear() {
112: Map bindings = m_bindings.getBindings();
113: if (bindings == null) {
114: M_log.warn("clear: no bindings!");
115: return;
116: }
117:
118: // clear the bindings map associated with this thread
119: bindings.clear();
120: }
121:
122: /**
123: * {@inheritDoc}
124: */
125: public Object get(String name) {
126: Map bindings = m_bindings.getBindings();
127: if (bindings == null) {
128: M_log.warn("get: no bindings!");
129: return null;
130: }
131:
132: return bindings.get(name);
133: }
134: }
|