KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > factory > ThreadLocalScope


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solution. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8
9 /*
10  * Created on Jun 8, 2005
11  *
12  * Author Ben Yu
13  * ZBS
14  */

15 package jfun.yan.factory;
16
17 /**
18  * Represents a pooling strategy that pools the data
19  * into a ThreadLocal object.
20  * <p>
21  * Zephyr Business Solution
22  *
23  * @author Ben Yu
24  *
25  */

26 public class ThreadLocalScope<T> implements Pool<T> {
27   private static final class ThreadLocalCache<T> extends ThreadLocal JavaDoc{
28     protected Object JavaDoc initialValue() {
29       return new CachingPool<T>();
30     }
31     CachingPool<T> getPool(){
32       return (CachingPool<T>)this.get();
33     }
34   }
35   private transient ThreadLocalCache<T> cache = new ThreadLocalCache();
36   private void readObject(java.io.ObjectInputStream JavaDoc in)
37   throws ClassNotFoundException JavaDoc, java.io.IOException JavaDoc{
38     in.defaultReadObject();
39     cache = new ThreadLocalCache<T>();
40   }
41   
42   public T getInstance(Factory<T> factory) {
43     return cache.getPool().getInstance(factory);
44     /*Object v = cache.get();
45     if(v==null){
46       v = factory.create();
47       cache.set(v);
48     }
49     return v;*/

50   }
51   public T getPooledInstance(T def) {
52     return cache.getPool().getPooledInstance(def);
53   }
54   public boolean isPooled(){
55     return cache.getPool().isPooled();
56   }
57 }
58
Popular Tags