Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / java / security / Engine.java
blob4b6bd10d9dd52e3990b591649dc3cfc6cea1dafc
1 /* Engine -- generic getInstance method.
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.security;
40 import java.lang.reflect.Constructor;
41 import java.lang.reflect.InvocationTargetException;
43 import java.security.NoSuchAlgorithmException;
44 import java.security.Provider;
46 /**
47 * Generic implementation of the getInstance methods in the various
48 * engine classes in java.security.
49 * <p>
50 * These classes ({@link java.security.Signature} for example) can be
51 * thought of as the "chrome, upholstery, and steering wheel", and the SPI
52 * (service provider interface, e.g. {@link java.security.SignatureSpi})
53 * classes can be thought of as the "engine" -- providing the actual
54 * functionality of whatever cryptographic algorithm the instance
55 * represents.
57 * @see Provider
58 * @author Casey Marshall
60 public final class Engine
63 // Constants.
64 // ------------------------------------------------------------------------
66 /** Prefix for aliases. */
67 private static final String ALG_ALIAS = "Alg.Alias.";
69 /** Maximum number of aliases to try. */
70 private static final int MAX_ALIASES = 5;
72 /** Argument list for no-argument constructors. */
73 private static final Object[] NO_ARGS = new Object[0];
75 // Constructor.
76 // ------------------------------------------------------------------------
78 /** This class cannot be instantiated. */
79 private Engine() { }
81 // Class method.
82 // ------------------------------------------------------------------------
84 /**
85 * Get the implementation for <i>algorithm</i> for service
86 * <i>service</i> from <i>provider</i>. The service is e.g.
87 * "Signature", and the algorithm "DSA".
89 * @param service The service name.
90 * @param algorithm The name of the algorithm to get.
91 * @param provider The provider to get the implementation from.
92 * @return The engine class for the specified algorithm; the object
93 * returned is typically a subclass of the SPI class for that
94 * service, but callers should check that this is so.
95 * @throws NoSuchAlgorithmException If the implementation cannot be
96 * found or cannot be instantiated.
97 * @throws InvocationTargetException If the SPI class's constructor
98 * throws an exception.
99 * @throws IllegalArgumentException If any of the three arguments are null.
101 public static Object getInstance(String service, String algorithm,
102 Provider provider)
103 throws InvocationTargetException, NoSuchAlgorithmException
105 return getInstance(service, algorithm, provider, NO_ARGS);
109 * Get the implementation for <i>algorithm</i> for service
110 * <i>service</i> from <i>provider</i>, passing <i>initArgs</i> to the
111 * SPI class's constructor (which cannot be null; pass a zero-length
112 * array if the SPI takes no arguments). The service is e.g.
113 * "Signature", and the algorithm "DSA".
115 * @param service The service name.
116 * @param algorithm The name of the algorithm to get.
117 * @param provider The provider to get the implementation from.
118 * @param initArgs The arguments to pass to the SPI class's
119 * constructor (cannot be null).
120 * @return The engine class for the specified algorithm; the object
121 * returned is typically a subclass of the SPI class for that
122 * service, but callers should check that this is so.
123 * @throws NoSuchAlgorithmException If the implementation cannot be
124 * found or cannot be instantiated.
125 * @throws InvocationTargetException If the SPI class's constructor
126 * throws an exception.
127 * @throws IllegalArgumentException If any of the four arguments are null.
129 public static Object getInstance(String service, String algorithm,
130 Provider provider, Object[] initArgs)
131 throws InvocationTargetException, NoSuchAlgorithmException
133 if (service != null)
134 service = service.trim();
136 if (algorithm != null)
137 algorithm = algorithm.trim();
139 if (service == null || service.length() == 0
140 || algorithm == null || algorithm.length() == 0
141 || provider == null || initArgs == null)
142 throw new IllegalArgumentException();
144 // If there is no property "service.algorithm"
145 if (provider.getProperty(service + "." + algorithm) == null)
147 // Iterate through aliases, until we find the class name or resolve
148 // too many aliases.
149 String alias = null;
150 int count = 0;
151 while ((alias = provider.getProperty(
152 ALG_ALIAS + service + "." + algorithm)) != null)
154 if (algorithm.equals(alias)) // Refers to itself!
155 break;
156 algorithm = alias;
157 if (count++ > MAX_ALIASES)
158 throw new NoSuchAlgorithmException("too many aliases");
160 if (provider.getProperty(service + "." + algorithm) == null)
161 throw new NoSuchAlgorithmException(algorithm);
164 // Find and instantiate the implementation.
165 Class clazz = null;
166 ClassLoader loader = provider.getClass().getClassLoader();
167 Constructor constructor = null;
168 String error = algorithm;
172 if (loader != null)
173 clazz = loader.loadClass(provider.getProperty(service+"."+algorithm));
174 else
175 clazz = Class.forName(provider.getProperty(service+"."+algorithm));
176 constructor = getCompatibleConstructor(clazz, initArgs);
177 return constructor.newInstance(initArgs);
179 catch (ClassNotFoundException cnfe)
181 error = "class not found: " + algorithm;
183 catch (IllegalAccessException iae)
185 error = "illegal access: " + iae.getMessage();
187 catch (InstantiationException ie)
189 error = "instantiation exception: " + ie.getMessage();
191 catch (ExceptionInInitializerError eiie)
193 error = "exception in initializer: " + eiie.getMessage();
195 catch (SecurityException se)
197 error = "security exception: " + se.getMessage();
199 catch (NoSuchMethodException nsme)
201 error = "no appropriate constructor found";
204 throw new NoSuchAlgorithmException(error);
207 // Own methods.
208 // ------------------------------------------------------------------------
211 * Find a constructor in the given class that can take the specified
212 * argument list, allowing any of which to be null.
214 * @param clazz The class from which to get the constructor.
215 * @param initArgs The argument list to be passed to the constructor.
216 * @return The constructor.
217 * @throws NoSuchMethodException If no constructor of the given class
218 * can take the specified argument array.
220 private static Constructor getCompatibleConstructor(Class clazz,
221 Object[] initArgs)
222 throws NoSuchMethodException
224 Constructor[] c = clazz.getConstructors();
225 outer:for (int i = 0; i < c.length; i++)
227 Class[] argTypes = c[i].getParameterTypes();
228 if (argTypes.length != initArgs.length)
229 continue;
230 for (int j = 0; j < argTypes.length; j++)
232 if (initArgs[j] != null &&
233 !argTypes[j].isAssignableFrom(initArgs[j].getClass()))
234 continue outer;
236 // If we reach this point, we know this constructor (c[i]) has
237 // the same number of parameters as the target parameter list,
238 // and all our parameters are either (1) null, or (2) assignable
239 // to the target parameter type.
240 return c[i];
242 throw new NoSuchMethodException();