KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > InstanceFactory


1 /**
2  * $Id: InstanceFactory.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import java.lang.reflect.Constructor JavaDoc;
32 import java.util.Hashtable JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35
36 /**
37  * Utility class that acts as a simple name to implementation class mapping and
38  * factory all in one. Like a standard <span class="src">Hashtable</span>, instances
39  * of this class are protected against concurrent access and/or modification.
40  *
41  * @since JWare/AntX 0.5
42  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
43  * @version 0.5
44  * @.safety guarded
45  * @.group impl,helper
46  * @.pattern GoF.Factory
47  **/

48
49 public final class InstanceFactory extends Hashtable JavaDoc
50 {
51     /** Signature of general parameter-based constructor. **/
52     public static final Class JavaDoc[] CTOR_SIG0= new Class JavaDoc[] {GenericParameters.class};
53
54
55
56     /**
57      * Initializea new empty instance factory.
58      **/

59     public InstanceFactory()
60     {
61     }
62
63
64
65     /**
66      * Initializea new empty instance factory that uses a
67      * specific default class for unmatched categories.
68      * @param defaultClass the default class reference (non-null)
69      **/

70     public InstanceFactory(Class JavaDoc defaultClass)
71     {
72        setDefaultInstanceClass(defaultClass);
73     }
74
75
76
77
78     /**
79      * Initializes the fallback class of unmatched categories.
80      * @param c the class reference (non-null)
81      * @throws IllegalArgumentException is class is <i>null</i>
82      **/

83     public void setDefaultInstanceClass(Class JavaDoc c)
84     {
85         if (c==null) {
86             throw new IllegalArgumentException JavaDoc();
87         }
88         m_defaultClass = c;
89     }
90
91
92
93     /**
94      * Returns this factory's fallback class for unmatched
95      * categories. Will never return <i>null</i>.
96      **/

97     public Class JavaDoc getDefaultInstanceClass()
98     {
99         return m_defaultClass;
100     }
101
102
103
104     /**
105      * Installs a new class mapping under a specific name.
106      * @param key the category name (non-null)
107      * @param value the <span class="src">Class</span> reference
108      * @throws IllegalArgumentException if value is not a valid class reference.
109      **/

110     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
111     {
112         if (value==null) {
113             return super.remove(key);
114         }
115         if (!(value instanceof Class JavaDoc)) {
116             throw new IllegalArgumentException JavaDoc("Only class references allowed!");
117         }
118         return super.put(key,value);
119     }
120
121
122
123     private Class JavaDoc getc(Object JavaDoc key)
124     {
125         Object JavaDoc rc = get(key);
126         Class JavaDoc c;
127         if (rc==null) {
128             c = getDefaultInstanceClass();
129         } else {
130             c = (Class JavaDoc)rc;
131         }
132         return c;
133     }
134
135
136
137     /**
138      * Creates a new instance of the named category.
139      * @param key the category's name (non-null)
140      * @return new instance
141      * @throws BuildException if unable to create new instance.
142      **/

143     public Object JavaDoc newInstance(Object JavaDoc key)
144     {
145         Class JavaDoc c = getc(key);
146         try {
147             return c.newInstance();
148         } catch(Exception JavaDoc anyX) {
149             throw new BuildException(anyX);
150         }
151     }
152
153
154
155     /**
156      * Creates a new custom instance of the named category.
157      * @param key the category's name (non-null)
158      * @param args the constructor's single argument (non-null)
159      * @return new cstom instance
160      * @throws BuildException if unable to create new instance.
161      **/

162     public Object JavaDoc newInstance(Object JavaDoc key, GenericParameters args)
163     {
164         Class JavaDoc c = getc(key);
165         try {
166             Constructor JavaDoc ctor = c.getConstructor(CTOR_SIG0);
167             Object JavaDoc[] initargs = new Object JavaDoc[] {args};
168             return ctor.newInstance(initargs);
169         } catch(Exception JavaDoc anyX) {
170             throw new BuildException(anyX);
171         }
172     }
173
174
175     private Class JavaDoc m_defaultClass = Object JavaDoc.class;
176 }
177
178
179 /* end-of-InstanceFactory.java */
Popular Tags