001: package org.apache.turbine.services.factory;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.turbine.services.TurbineServices;
023: import org.apache.turbine.util.TurbineException;
024:
025: /**
026: * The Factory Service instantiates objects using either default
027: * class loaders or a specified one. Whether specified class
028: * loaders are supported for a class depends on implementation
029: * and can be tested with the isLoaderSupported method.
030: *
031: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
032: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
033: * @version $Id: TurbineFactory.java 534527 2007-05-02 16:10:59Z tv $
034: */
035: public abstract class TurbineFactory {
036: /**
037: * Utility method for accessing the service
038: * implementation
039: *
040: * @return An AssemblerBroker implementation instance
041: */
042: public static FactoryService getService() {
043: return (FactoryService) TurbineServices.getInstance()
044: .getService(FactoryService.SERVICE_NAME);
045: }
046:
047: /**
048: * Gets an instance of a named class.
049: *
050: * @param className the name of the class.
051: * @return the instance.
052: * @throws TurbineException if instantiation fails.
053: */
054: public static Object getInstance(String className)
055: throws TurbineException {
056: return getService().getInstance(className);
057: }
058:
059: /**
060: * Gets an instance of a named class using a specified class loader.
061: *
062: * <p>Class loaders are supported only if the isLoaderSupported
063: * method returns true. Otherwise the loader parameter is ignored.
064: *
065: * @param className the name of the class.
066: * @param loader the class loader.
067: * @return the instance.
068: * @throws TurbineException if instantiation fails.
069: */
070: public static Object getInstance(String className,
071: ClassLoader loader) throws TurbineException {
072: return getService().getInstance(className, loader);
073: }
074:
075: /**
076: * Gets an instance of a named class.
077: * Parameters for its constructor are given as an array of objects,
078: * primitive types must be wrapped with a corresponding class.
079: *
080: * @param className the name of the class.
081: * @param params an array containing the parameters of the constructor.
082: * @param signature an array containing the signature of the constructor.
083: * @return the instance.
084: * @throws TurbineException if instantiation fails.
085: */
086: public static Object getInstance(String className, Object[] params,
087: String[] signature) throws TurbineException {
088: return getService().getInstance(className, params, signature);
089: }
090:
091: /**
092: * Gets an instance of a named class using a specified class loader.
093: * Parameters for its constructor are given as an array of objects,
094: * primitive types must be wrapped with a corresponding class.
095: *
096: * <p>Class loaders are supported only if the isLoaderSupported
097: * method returns true. Otherwise the loader parameter is ignored.
098: *
099: * @param className the name of the class.
100: * @param loader the class loader.
101: * @param params an array containing the parameters of the constructor.
102: * @param signature an array containing the signature of the constructor.
103: * @return the instance.
104: * @throws TurbineException if instantiation fails.
105: */
106: public static Object getInstance(String className,
107: ClassLoader loader, Object[] params, String[] signature)
108: throws TurbineException {
109: return getService().getInstance(className, loader, params,
110: signature);
111: }
112:
113: /**
114: * Tests if specified class loaders are supported for a named class.
115: *
116: * @param className the name of the class.
117: * @return true if class loaders are supported, false otherwise.
118: * @throws TurbineException if test fails.
119: */
120: public static boolean isLoaderSupported(String className)
121: throws TurbineException {
122: return getService().isLoaderSupported(className);
123: }
124:
125: /**
126: * Gets the signature classes for parameters of a method of a class.
127: *
128: * @param clazz the class.
129: * @param params an array containing the parameters of the method.
130: * @param signature an array containing the signature of the method.
131: * @return an array of signature classes. Note that in some cases
132: * objects in the parameter array can be switched to the context
133: * of a different class loader.
134: * @throws ClassNotFoundException if any of the classes is not found.
135: */
136: public static Class[] getSignature(Class clazz, Object params[],
137: String signature[]) throws ClassNotFoundException {
138: return getService().getSignature(clazz, params, signature);
139: }
140: }
|