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: * A thread-unsafe implementation of Pool that does simple caching.
22: * <p>
23: *
24: * @author Ben Yu Feb 2, 2006 12:13:08 PM
25: */
26: public class CachingPool implements Pool {
27: private transient Object v;
28:
29: private transient boolean pooled;
30:
31: private void readObject(java.io.ObjectInputStream in)
32: throws ClassNotFoundException, java.io.IOException {
33: in.defaultReadObject();
34: this .pooled = false;
35: }
36:
37: public Object getInstance(Factory factory) throws Throwable {
38: if (!pooled) {
39: v = factory.create();
40: pooled = true;
41: }
42: return v;
43: }
44:
45: public Object getPooledInstance(Object def) {
46: return pooled ? v : def;
47: }
48:
49: /**
50: * Is this pool currently having something in cache?
51: */
52: public boolean isPooled() {
53: return pooled;
54: }
55: }
|