View Javadoc
1   package org.apache.fulcrum.factory;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * The Factory Service instantiates objects using either default
24   * class loaders or a specified one. Whether specified class
25   * loaders are supported for a class depends on implementation
26   * and can be tested with the isLoaderSupported method.
27   *
28   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
29   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
30   * @version $Id$
31   */
32  public interface FactoryService
33  {
34      /**
35       * The key under which this component is known by an avalon container.
36       */
37      String ROLE = FactoryService.class.getName();
38  
39  
40  	/**
41  	* Gets an instance of a class.
42  	*
43  	* @param <T> Type of the class
44  	* @param clazz the name of the class.
45  	* @return {@inheritDoc} the instance.
46  	* @throws FactoryException if instantiation fails.
47  	*/
48     <T> T getInstance(Class<T> clazz)
49  	   throws FactoryException;
50  
51      /**
52       * Gets an instance of a named class.
53       *
54       * @param <T> Type of the class
55       * @param className the name of the class.
56       * @return {@inheritDoc} the instance.
57       * @throws FactoryException if instantiation fails.
58       */
59      <T> T getInstance(String className)
60          throws FactoryException;
61  
62      /**
63       * Gets an instance of a named class using a specified class loader.
64       *
65       * Class loaders are supported only if the isLoaderSupported
66       * method returns true. Otherwise the loader parameter is ignored.
67       * 
68       * @param <T> Type of the class
69       * @param className the name of the class.
70       * @param loader the class loader.
71       * @return {@inheritDoc} the instance.
72       * @throws FactoryException if instantiation fails.
73       */
74      <T> T getInstance(String className,
75                                ClassLoader loader)
76          throws FactoryException;
77  
78      /**
79       * Gets an instance of a named class.
80       * Parameters for its constructor are given as an array of objects,
81       * primitive types must be wrapped with a corresponding class.
82       *
83       * @param <T> Type of the class
84       * @param className the name of the class.
85       * @param params an array containing the parameters of the constructor.
86       * @param signature an array containing the signature of the constructor.
87       * @return {@inheritDoc} the instance.
88       * @throws FactoryException if instantiation fails.
89       */
90      <T> T getInstance(String className,
91                                Object[] params,
92                                String[] signature)
93          throws FactoryException;
94  
95      /**
96       * Gets an instance of a named class using a specified class loader.
97       * Parameters for its constructor are given as an array of objects,
98       * primitive types must be wrapped with a corresponding class.
99       *
100      * Class loaders are supported only if the isLoaderSupported
101      * method returns true. Otherwise the loader parameter is ignored.
102      *
103      * @param <T> Type of the class
104      * @param className the name of the class.
105      * @param loader the class loader.
106      * @param params an array containing the parameters of the constructor.
107      * @param signature an array containing the signature of the constructor.
108      * @return {@inheritDoc} the instance.
109      * @throws FactoryException if instantiation fails.
110      */
111     <T> T getInstance(String className,
112                               ClassLoader loader,
113                               Object[] params,
114                               String[] signature)
115         throws FactoryException;
116 
117     /**
118      * Tests if specified class loaders are supported for a named class.
119      *
120      * @param className the name of the class.
121      * @return true if class loaders are supported, false otherwise.
122      * @throws FactoryException if test fails.
123      */
124     boolean isLoaderSupported(String className)
125         throws FactoryException;
126 
127     /**
128      * Gets the signature classes for parameters of a method of a class.
129      *
130      * @param clazz the class.
131      * @param params an array containing the parameters of the method.
132      * @param signature an array containing the signature of the method.
133      * 
134      * @return {@inheritDoc} an array of signature classes. Note that in some cases
135      * objects in the parameter array can be switched to the context
136      * of a different class loader
137      * 
138      * @throws ClassNotFoundException if any of the classes is not found.
139      */
140     Class<?>[] getSignature(Class<?> clazz,
141             Object params[],
142             String signature[])
143             throws ClassNotFoundException;
144 }