01: /*
02: * XML 2 Java Binding (X2JB) - the excellent Java tool.
03: * Copyright 2007, by Richard Opalka.
04: *
05: * This is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as
07: * published by the Free Software Foundation; either version 2.1 of
08: * the License, or (at your option) any later version.
09: *
10: * This software is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this software; if not see the FSF site:
17: * http://www.fsf.org/ and search for the LGPL License document there.
18: */
19: package org.x2jb.bind;
20:
21: import java.lang.ref.SoftReference;
22: import java.lang.reflect.Method;
23: import java.util.HashMap;
24: import java.util.Map;
25: import org.x2jb.bind.provider.BindingDefinition;
26: import org.x2jb.bind.provider.BindingFactory;
27:
28: /**
29: * BindingFactory provider implementation
30: *
31: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
32: * @version 1.0
33: */
34: final class FactoryProvider {
35:
36: private static final String FACTORY_PROPERTY = "org.x2jb.bind.provider.BindingFactory";
37: public static final BindingFactory INSTANCE = new CachingBindingFactory(
38: (BindingFactory) FactoryFinder.find(FACTORY_PROPERTY));
39:
40: private static final class CachingBindingFactory implements
41: BindingFactory {
42:
43: private final Map cache = new HashMap();
44: private final BindingFactory delegee;
45:
46: CachingBindingFactory(BindingFactory delegee) {
47: this .delegee = delegee;
48: }
49:
50: public synchronized BindingDefinition getBinding(Method method)
51: throws BindingException {
52: SoftReference reference = (SoftReference) cache.get(method);
53: BindingDefinition retVal = null;
54:
55: if (reference != null) {
56: retVal = (BindingDefinition) reference.get();
57: }
58:
59: if (retVal == null) {
60: retVal = delegee.getBinding(method);
61: if (retVal != null)
62: cache.put(method, new SoftReference(retVal));
63: }
64:
65: return retVal;
66: }
67:
68: }
69:
70: }
|