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 simple implementation of Pool that uses null to indicate non-existent pool
22: * entry.
23: * <p>
24: * This implementation synchronizes on {@link #getMutex()} for thread safety.
25: * </p>
26: *
27: * @author Ben Yu Feb 2, 2006 3:14:45 PM
28: */
29: public abstract class AbstractPool implements Pool {
30: public Object getInstance(Factory factory) throws Throwable {
31: synchronized (getMutex()) {
32: Object ret = get();
33: if (ret == null) {
34: ret = factory.create();
35: set(ret);
36: }
37: return ret;
38: }
39: }
40:
41: public Object getPooledInstance(Object def) {
42: synchronized (getMutex()) {
43: return ifnull(get(), def);
44: }
45: }
46:
47: public boolean isPooled() {
48: synchronized (getMutex()) {
49: return get() != null;
50: }
51: }
52:
53: protected static Object ifnull(Object obj, Object def) {
54: return obj == null ? def : obj;
55: }
56:
57: /**
58: * Get the pooled instance. null if not found.
59: *
60: * @return the pooled instance.
61: */
62: public abstract Object get();
63:
64: /**
65: * set an value to the pool.
66: *
67: * @param val the value to be pooled.
68: */
69: public abstract void set(Object val);
70:
71: /**
72: * Get the object that can be used to synchronize.
73: */
74: protected abstract Object getMutex();
75: }
|