KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > util > SerializableThreadLocal


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.util;
5
6 import java.io.Serializable JavaDoc;
7 import java.lang.ref.WeakReference JavaDoc;
8
9 /**
10  * Extends the <code>java.lang.ThreadLocal</code> to be able to add additional functionality. <p/>This classes
11  * enhances the base implementation by: <p/>making it serializable <p/>making it wrap an unwrap the values in a
12  * <code>java.lang.ref.WeakReference</code> to avoid potential memory leaks
13  *
14  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
15  */

16 public class SerializableThreadLocal extends java.lang.ThreadLocal JavaDoc implements Serializable JavaDoc {
17   /**
18    * Constructor. Simply calls the base class constructor.
19    */

20   public SerializableThreadLocal() {
21     super();
22   }
23
24   /**
25    * Overrides the <code>java.lang.ThreadLocal#getDefault()</code> method. Retrieves and returns the value wrapped up in a
26    * <code>java.lang.ref.WeakReference</code> by the <code>SerializableThreadLocal#set(Object value)</code> method
27    *
28    * @return the value wrapped up in a weak reference
29    */

30   public Object JavaDoc get() {
31     Object JavaDoc ref = super.get();
32     if (ref == null) {
33       return ref;
34     } else {
35       return ((WeakReference JavaDoc) ref).get();
36     }
37   }
38
39   /**
40    * Overrides the <code>java.lang.ThreadLoca#set(Object value)</code> method. Wraps the value in a
41    * <code>java.lang.ref.WeakReference</code> before passing it on to the <code>java.lang.ThreadLocal#set(Object
42    * value)</code>
43    * method
44    *
45    * @param value the value that should be wrapped up in a weak reference
46    */

47   public void set(final Object JavaDoc value) {
48     synchronized (this) {
49       super.set(new WeakReference JavaDoc(value));
50     }
51   }
52 }
Popular Tags