01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.common.util.factory;
19:
20: /**
21: * Represents a pooling strategy that pools the data into a ThreadLocal object.
22: *
23: * @author Ben Yu
24: */
25: public class ThreadSingletonPool implements Pool {
26: private static final class ThreadLocalCache extends ThreadLocal {
27: protected Object initialValue() {
28: return new CachingPool();
29: }
30:
31: CachingPool getPool() {
32: return (CachingPool) this .get();
33: }
34: }
35:
36: private transient ThreadLocalCache cache = new ThreadLocalCache();
37:
38: private void readObject(java.io.ObjectInputStream in)
39: throws ClassNotFoundException, java.io.IOException {
40: in.defaultReadObject();
41: cache = new ThreadLocalCache();
42: }
43:
44: public Object getInstance(Factory factory) throws Throwable {
45: return cache.getPool().getInstance(factory);
46: /*
47: * Object v = cache.get(); if(v==null){ v = factory.create();
48: * cache.set(v); } return v;
49: */
50: }
51:
52: public Object getPooledInstance(Object def) {
53: return cache.getPool().getPooledInstance(def);
54: }
55:
56: public boolean isPooled() {
57: return cache.getPool().isPooled();
58: }
59: }
|