01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.catalog.adaptable;
17:
18: import java.util.ArrayList;
19: import java.util.Collection;
20: import java.util.Collections;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: import org.geotools.catalog.ResolveAdapterFactory;
25:
26: /**
27: * Class for locating instances of {@link ResolveAdapterFactory}.
28: * <p>
29: * This class should be subclassed to provide a particular lookup mechanism.
30: * </p>
31: *
32: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
33: *
34: */
35: public abstract class ResolveAdapterFactoryFinder {
36:
37: /**
38: * Performs an adapter lookup.
39: * <p>
40: * This method should be extended or overriden.
41: * </p>
42: *
43: * @param decorator The adapting resolve decorator.
44: * @param adaptee The class being adapted to.
45: *
46: * @return The adapting factory, otherwise <code>null</code> if non could
47: * be found.
48: *
49: * @throws IllegalStateException If multiple adapter factories are found
50: * which can support the adaptation.
51: */
52: public ResolveAdapterFactory find(AdaptingResolve decorator,
53: Class adaptee) throws IllegalStateException {
54:
55: if (decorator.resolve == null)
56: return null;
57:
58: Collection factories = getResolveAdapterFactories();
59: List matches = new ArrayList();
60: for (Iterator f = factories.iterator(); f.hasNext();) {
61: ResolveAdapterFactory factory = (ResolveAdapterFactory) f
62: .next();
63: if (factory instanceof AdaptingResolveAware) {
64: ((AdaptingResolveAware) factory)
65: .setAdaptingResolve(decorator);
66: }
67:
68: if (factory.canAdapt(decorator.resolve, adaptee)) {
69: matches.add(factory);
70: }
71: }
72:
73: if (matches.isEmpty())
74: return null;
75:
76: if (matches.size() != 1) {
77: String msg = "Multiple adapters found.";
78: throw new IllegalStateException(msg);
79: }
80:
81: return (ResolveAdapterFactory) matches.get(0);
82: }
83:
84: /**
85: * Located all available adapter factories.
86: * <p>
87: * This method is intended to be overiden.
88: * </p>
89: * @return A collection of adapters, may be empty, should not be null.
90: *
91: * TODO: default to factory spi?
92: */
93: public Collection getResolveAdapterFactories() {
94: return Collections.EMPTY_LIST;
95: }
96: }
|