KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > classes > v1 > SPISingletonStore


1 /*
2  * @(#)SPISingletonStore.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * 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 JavaDoc;
30
31 import org.apache.log4j.Logger;
32
33
34
35 /**
36  * Aids pluggable factories and related classes by being a central repository
37  * for storing SPI singletons, and creating means to load and change the
38  * singletons.
39  * <P>
40  * This uses the same pattern as the <tt>SingletonStore</tt> class, but this
41  * one allows for multiple singleton instances, and loads them from the
42  * SPILoader class.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version $Date: 2003/02/10 22:52:36 $
46  * @since June 28, 2002
47  * @see SingletonStore
48  */

49 public class SPISingletonStore extends AbstractMultipleStore
50 {
51     private static final Logger LOG = Logger.getLogger(
52         SPISingletonStore.class.getName() );
53     
54     private Class JavaDoc baseClass;
55     
56     
57     /**
58      * Constructor specifying all the parameters for using a singleton in this
59      * framework.
60      *
61      * @param instanceOf singletons must be of this class. This is also used
62      * to load the SPI classes.
63      * @param ama <tt>true</tt> if this store should allow
64      * multiple instances of the exact same class, or <tt>false</tt>
65      * if it should prevent multiple instances sharing the exact
66      * same class. This helps to enforce the idea of 'singleton'.
67      */

68     public SPISingletonStore( Class JavaDoc instanceOf, AllowMultiplesAction ama )
69     {
70         super( instanceOf, ama );
71         
72         if (instanceOf == null)
73         {
74             throw new IllegalArgumentException JavaDoc("no null arguments");
75         }
76         
77         this.baseClass = instanceOf;
78     }
79     
80     
81     /**
82      * Adds the default inner singletons, which is an implementation
83      * specific method.
84      */

85     protected void addDefaultSingletons()
86     {
87         try
88         {
89             addSPI( null );
90         }
91         catch (IOException JavaDoc ioe)
92         {
93             LOG.warn( "adding SPI caused IOException", ioe );
94             throw new IllegalStateException JavaDoc( ioe.toString() );
95         }
96     }
97     
98     
99     /**
100      * Add a set of SPIs from the given class loader.
101      */

102     public void addSPI( ClassLoader JavaDoc cl )
103             throws IOException JavaDoc
104     {
105         SPILoader spil = new SPILoader( this.baseClass, cl );
106         
107         while (spil.hasNext())
108         {
109             addSingleton( spil.nextProvier() );
110         }
111     }
112 }
113
114
Popular Tags