KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > util > Factory


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.util;
20
21 import java.lang.reflect.*;
22
23 /**
24  * General purpose factory method based on {@link ConfigParameters}
25  * and Java's Reflection API.
26  *
27  * @author Franz-Josef Elmer
28  */

29 public class Factory {
30   /** The constant defining the key <tt>className</tt>. */
31   public static final String JavaDoc CLASS_NAME_KEY = "className";
32
33   /** No public constructor necessary. */
34   private Factory() {}
35   
36   /**
37    * Creates an instance of the specified class.
38    * @param className Fully-qualified name of a class with a default
39    * constructor.
40    * @return a new instance.
41    * @throws IllegalArgumentException if the instance could be created.
42    */

43   public static Object JavaDoc create(String JavaDoc className) {
44     try {
45       return Class.forName(className).newInstance();
46     } catch (Throwable JavaDoc t) {
47       throw new IllegalArgumentException JavaDoc("Could not create an instance of "
48                                          + className + " because of " + t);
49     }
50   }
51
52   /**
53    * Creates an object based on the specified configuration
54    * parameters. The class of the object is determined by the
55    * parameter with the key {@link #CLASS_NAME_KEY}.
56    * The constructor with a single argument of the type
57    * <tt>ConfigParameter</tt> is invoked with the argument
58    * <tt>configParameters</tt>. If such a constructor
59    * does not exists the default constructor is invoked. If
60    * neither of these constructors exist a {@link FactoryException}
61    * is thrown.
62    * @param configParameters Configuration parameters.
63    * @return the newly created object.
64    * @throws IllegalArgumentException if key <tt>className</tt> is missing.
65    * @throws FactoryException wrapping any kind of exception or error occured.
66    */

67   public static Object JavaDoc create(ConfigParameters configParameters) {
68     String JavaDoc className = configParameters.get(CLASS_NAME_KEY);
69     return createObject(configParameters, className);
70   }
71
72   /**
73    * Creates an object based on the specified configuration
74    * parameters and default class name. If the
75    * parameter with the key {@link #CLASS_NAME_KEY} is missed in
76    * <tt>configParameters</tt> <tt>defaultClassName</tt> is used.
77    * Otherwise it works as {@link #create(jcckit.util.ConfigParameters)}.
78    * @param configParameters Configuration parameters.
79    * @param defaultClassName Default class name.
80    * @return the newly created object.
81    * @throws FactoryException wrapping any kind of exception or error occured.
82    */

83   public static Object JavaDoc create(ConfigParameters configParameters,
84                               String JavaDoc defaultClassName) {
85     String JavaDoc className = configParameters.get(CLASS_NAME_KEY, defaultClassName);
86     return createObject(configParameters, className);
87   }
88
89   /**
90    * Creates an object based on the specified configuration
91    * parameters or returns the default object. This method behaves
92    * as {@link #create(jcckit.util.ConfigParameters)}, except that is does
93    * not throw an <tt>IllegalArgumentException</tt> if key <tt>className</tt>
94    * is missing. Instead <tt>defaultObject</tt> is returned.
95    */

96   public static Object JavaDoc createOrGet(ConfigParameters configParameters,
97                                    Object JavaDoc defaultObject) {
98     String JavaDoc className = configParameters.get(CLASS_NAME_KEY, null);
99     return className == null ? defaultObject
100                              : createObject(configParameters, className);
101   }
102
103   private static Object JavaDoc createObject(ConfigParameters configParameters,
104                                      String JavaDoc className) {
105     try {
106       Class JavaDoc c = Class.forName(className);
107       Object JavaDoc result = null;
108       Constructor constructor = null;
109       try {
110         constructor = c.getConstructor(new Class JavaDoc[] {ConfigParameters.class});
111         result = constructor.newInstance(new Object JavaDoc[] {configParameters});
112       } catch (NoSuchMethodException JavaDoc e) {
113         result = c.newInstance();
114       }
115       return result;
116     } catch (Throwable JavaDoc t) {
117       throw new FactoryException(configParameters, CLASS_NAME_KEY, t);
118     }
119   }
120 }
121
Popular Tags