01: /*
02: * @(#)SPISingletonStore.java
03: *
04: * Copyright (C) 2002-2003 Matt Albrecht
05: * groboclown@users.sourceforge.net
06: * http://groboutils.sourceforge.net
07: *
08: * Permission is hereby granted, free of charge, to any person obtaining a
09: * copy of this software and associated documentation files (the "Software"),
10: * to deal in the Software without restriction, including without limitation
11: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12: * and/or sell copies of the Software, and to permit persons to whom the
13: * Software is furnished to do so, subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in
16: * all copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24: * DEALINGS IN THE SOFTWARE.
25: */
26:
27: package net.sourceforge.groboutils.util.classes.v1;
28:
29: import java.io.IOException;
30:
31: import org.apache.log4j.Logger;
32:
33: /**
34: * Aids pluggable factories and related classes by being a central repository
35: * for storing SPI singletons, and creating means to load and change the
36: * singletons.
37: * <P>
38: * This uses the same pattern as the <tt>SingletonStore</tt> class, but this
39: * one allows for multiple singleton instances, and loads them from the
40: * SPILoader class.
41: *
42: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43: * @version $Date: 2003/02/10 22:52:36 $
44: * @since June 28, 2002
45: * @see SingletonStore
46: */
47: public class SPISingletonStore extends AbstractMultipleStore {
48: private static final Logger LOG = Logger
49: .getLogger(SPISingletonStore.class.getName());
50:
51: private Class baseClass;
52:
53: /**
54: * Constructor specifying all the parameters for using a singleton in this
55: * framework.
56: *
57: * @param instanceOf singletons must be of this class. This is also used
58: * to load the SPI classes.
59: * @param ama <tt>true</tt> if this store should allow
60: * multiple instances of the exact same class, or <tt>false</tt>
61: * if it should prevent multiple instances sharing the exact
62: * same class. This helps to enforce the idea of 'singleton'.
63: */
64: public SPISingletonStore(Class instanceOf, AllowMultiplesAction ama) {
65: super (instanceOf, ama);
66:
67: if (instanceOf == null) {
68: throw new IllegalArgumentException("no null arguments");
69: }
70:
71: this .baseClass = instanceOf;
72: }
73:
74: /**
75: * Adds the default inner singletons, which is an implementation
76: * specific method.
77: */
78: protected void addDefaultSingletons() {
79: try {
80: addSPI(null);
81: } catch (IOException ioe) {
82: LOG.warn("adding SPI caused IOException", ioe);
83: throw new IllegalStateException(ioe.toString());
84: }
85: }
86:
87: /**
88: * Add a set of SPIs from the given class loader.
89: */
90: public void addSPI(ClassLoader cl) throws IOException {
91: SPILoader spil = new SPILoader(this.baseClass, cl);
92:
93: while (spil.hasNext()) {
94: addSingleton(spil.nextProvier());
95: }
96: }
97: }
|