1 package org.apache.turbine.services.factory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.turbine.services.TurbineServices;
23 import org.apache.turbine.util.TurbineException;
24
25 /***
26 * The Factory Service instantiates objects using either default
27 * class loaders or a specified one. Whether specified class
28 * loaders are supported for a class depends on implementation
29 * and can be tested with the isLoaderSupported method.
30 *
31 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
32 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
33 * @version $Id: TurbineFactory.java 534527 2007-05-02 16:10:59Z tv $
34 */
35 public abstract class TurbineFactory
36 {
37 /***
38 * Utility method for accessing the service
39 * implementation
40 *
41 * @return An AssemblerBroker implementation instance
42 */
43 public static FactoryService getService()
44 {
45 return (FactoryService) TurbineServices.getInstance()
46 .getService(FactoryService.SERVICE_NAME);
47 }
48 /***
49 * Gets an instance of a named class.
50 *
51 * @param className the name of the class.
52 * @return the instance.
53 * @throws TurbineException if instantiation fails.
54 */
55 public static Object getInstance(String className)
56 throws TurbineException
57 {
58 return getService().getInstance(className);
59 }
60
61 /***
62 * Gets an instance of a named class using a specified class loader.
63 *
64 * <p>Class loaders are supported only if the isLoaderSupported
65 * method returns true. Otherwise the loader parameter is ignored.
66 *
67 * @param className the name of the class.
68 * @param loader the class loader.
69 * @return the instance.
70 * @throws TurbineException if instantiation fails.
71 */
72 public static Object getInstance(String className,
73 ClassLoader loader)
74 throws TurbineException
75 {
76 return getService().getInstance(className,
77 loader);
78 }
79
80 /***
81 * Gets an instance of a named class.
82 * Parameters for its constructor are given as an array of objects,
83 * primitive types must be wrapped with a corresponding class.
84 *
85 * @param className the name of the class.
86 * @param params an array containing the parameters of the constructor.
87 * @param signature an array containing the signature of the constructor.
88 * @return the instance.
89 * @throws TurbineException if instantiation fails.
90 */
91 public static Object getInstance(String className,
92 Object[] params,
93 String[] signature)
94 throws TurbineException
95 {
96 return getService().getInstance(className,
97 params,
98 signature);
99 }
100
101 /***
102 * Gets an instance of a named class using a specified class loader.
103 * Parameters for its constructor are given as an array of objects,
104 * primitive types must be wrapped with a corresponding class.
105 *
106 * <p>Class loaders are supported only if the isLoaderSupported
107 * method returns true. Otherwise the loader parameter is ignored.
108 *
109 * @param className the name of the class.
110 * @param loader the class loader.
111 * @param params an array containing the parameters of the constructor.
112 * @param signature an array containing the signature of the constructor.
113 * @return the instance.
114 * @throws TurbineException if instantiation fails.
115 */
116 public static Object getInstance(String className,
117 ClassLoader loader,
118 Object[] params,
119 String[] signature)
120 throws TurbineException
121 {
122 return getService().getInstance(className,
123 loader,
124 params,
125 signature);
126 }
127
128
129 /***
130 * Tests if specified class loaders are supported for a named class.
131 *
132 * @param className the name of the class.
133 * @return true if class loaders are supported, false otherwise.
134 * @throws TurbineException if test fails.
135 */
136 public static boolean isLoaderSupported(String className)
137 throws TurbineException
138 {
139 return getService().isLoaderSupported(className);
140 }
141
142 /***
143 * Gets the signature classes for parameters of a method of a class.
144 *
145 * @param clazz the class.
146 * @param params an array containing the parameters of the method.
147 * @param signature an array containing the signature of the method.
148 * @return an array of signature classes. Note that in some cases
149 * objects in the parameter array can be switched to the context
150 * of a different class loader.
151 * @throws ClassNotFoundException if any of the classes is not found.
152 */
153 public static Class[] getSignature(Class clazz,
154 Object params[],
155 String signature[])
156 throws ClassNotFoundException
157 {
158 return getService().getSignature(clazz, params, signature);
159 }
160 }