KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)AbstractSingleStore.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
30
31 /**
32  * Aids pluggable factories and related classes by being a central repository
33  * for storing a singleton, and creating means to load and change the singleton.
34  *
35  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
36  * @version $Date: 2003/02/10 22:52:36 $
37  * @since March 30, 2002
38  */

39 public abstract class AbstractSingleStore
40 {
41     private Class JavaDoc instanceOf;
42     private Object JavaDoc singleton;
43     
44     
45     /**
46      * Constructor specifying all the parameters for using a singleton in this
47      * framework.
48      *
49      * @param instanceOf singletons must be of this class.
50      */

51     public AbstractSingleStore( Class JavaDoc instanceOf )
52     {
53         this.instanceOf = instanceOf;
54     }
55     
56     
57     /**
58      * Returns the current inner singleton. If it has never been set, then
59      * the default will be used instead.
60      *
61      * @return the inner singleton instance.
62      * @exception IllegalStateException if no singleton was created.
63      */

64     public Object JavaDoc getSingleton()
65     {
66         synchronized( this )
67         {
68             if (this.singleton == null)
69             {
70                 setDefaultSingleton();
71                 if (this.singleton == null)
72                 {
73                     throw new IllegalStateException JavaDoc( "No singleton created." );
74                 }
75             }
76         }
77         return this.singleton;
78     }
79     
80     
81     /**
82      * Sets the singleton. It must be of the correct class, and non-null.
83      *
84      * @param singleton the singleton to set.
85      * @exception IllegalArgumentException if <tt>singleton</tt> is
86      * <tt>null</tt>, or is not of the correct type.
87      */

88     public synchronized void setSingleton( Object JavaDoc singleton )
89     {
90         if (singleton == null)
91         {
92             throw new IllegalArgumentException JavaDoc("no null arguments");
93         }
94         if (this.instanceOf != null &&
95             !this.instanceOf.isInstance( singleton ))
96         {
97             throw new IllegalArgumentException JavaDoc( "Passed-in singleton "+
98                 singleton+" is not assignable to class "+
99                 this.instanceOf.getName()+", but is of class "+
100                 singleton.getClass().getName() );
101         }
102         this.singleton = singleton;
103     }
104     
105     
106     /**
107      * Sets the inner singleton to the default, which is an implementation
108      * specific method.
109      */

110     protected abstract void setDefaultSingleton();
111     
112     
113     /**
114      * Helper method to load an object from the class specified in the given
115      * system property; if the class is invalid, then the given default
116      * class will be used instead. No cast testing is performed.
117      *
118      * @param key the System property to reference for the classname to
119      * instantiate. It is passed to <tt>ClassLoadHelper</tt>.
120      * @param defaultClass class to instantiate if the class defined in the
121      * system property is invalid.
122      * @return the generated object.
123      * @exception IllegalArgumentException if <tt>key</tt> is <tt>null</tt>.
124      * @see ClassLoadHelper#createObjectFromProperty( String, Class, boolean )
125      */

126     protected static Object JavaDoc createFromProperty( String JavaDoc key,
127             Class JavaDoc defaultClass )
128     {
129         if (key == null)
130         {
131             throw new IllegalArgumentException JavaDoc("no null args");
132         }
133         ClassLoadHelper clh = new ClassLoadHelper();
134         return clh.createObjectFromProperty( key, defaultClass, false );
135     }
136 }
137
138
Popular Tags