41 |
41 |
42 import java.security.InvalidAlgorithmParameterException; |
42 import java.security.InvalidAlgorithmParameterException; |
43 import java.security.NoSuchAlgorithmException; |
43 import java.security.NoSuchAlgorithmException; |
44 import java.security.NoSuchProviderException; |
44 import java.security.NoSuchProviderException; |
45 import java.security.Provider; |
45 import java.security.Provider; |
|
46 import java.security.Provider.Service; |
46 import java.security.Security; |
47 import java.security.Security; |
47 import java.util.List; |
48 import java.util.List; |
48 |
49 |
49 import sun.security.jca.*; |
|
50 import sun.security.jca.GetInstance.Instance; |
|
51 |
50 |
52 /** |
51 /** |
53 * A factory for creating {@link XMLSignature} objects from scratch or |
52 * A factory for creating {@link XMLSignature} objects from scratch or |
54 * for unmarshalling an <code>XMLSignature</code> object from a corresponding |
53 * for unmarshalling an <code>XMLSignature</code> object from a corresponding |
55 * XML representation. |
54 * XML representation. |
196 */ |
195 */ |
197 public static XMLSignatureFactory getInstance(String mechanismType) { |
196 public static XMLSignatureFactory getInstance(String mechanismType) { |
198 if (mechanismType == null) { |
197 if (mechanismType == null) { |
199 throw new NullPointerException("mechanismType cannot be null"); |
198 throw new NullPointerException("mechanismType cannot be null"); |
200 } |
199 } |
201 Instance instance; |
200 Provider[] provs = Security.getProviders(); |
202 try { |
201 for (Provider p : provs) { |
203 instance = GetInstance.getInstance |
202 Service s = p.getService("XMLSignatureFactory", mechanismType); |
204 ("XMLSignatureFactory", null, mechanismType); |
203 if (s != null) { |
205 } catch (NoSuchAlgorithmException nsae) { |
204 Object obj = null; |
206 throw new NoSuchMechanismException(nsae); |
205 try { |
|
206 obj = s.newInstance(null); |
|
207 } catch (NoSuchAlgorithmException nsae) { |
|
208 throw new NoSuchMechanismException(nsae); |
|
209 } |
|
210 if (obj instanceof XMLSignatureFactory) { |
|
211 XMLSignatureFactory factory = (XMLSignatureFactory) obj; |
|
212 factory.mechanismType = mechanismType; |
|
213 factory.provider = p; |
|
214 return factory; |
|
215 } |
|
216 } |
207 } |
217 } |
208 XMLSignatureFactory factory = (XMLSignatureFactory) instance.impl; |
218 throw new NoSuchMechanismException |
209 factory.mechanismType = mechanismType; |
219 ("Mechanism " + mechanismType + " not available"); |
210 factory.provider = instance.provider; |
|
211 return factory; |
|
212 } |
220 } |
213 |
221 |
214 /** |
222 /** |
215 * Returns an <code>XMLSignatureFactory</code> that supports the |
223 * Returns an <code>XMLSignatureFactory</code> that supports the |
216 * requested XML processing mechanism and representation type (ex: "DOM"), |
224 * requested XML processing mechanism and representation type (ex: "DOM"), |
238 throw new NullPointerException("mechanismType cannot be null"); |
246 throw new NullPointerException("mechanismType cannot be null"); |
239 } else if (provider == null) { |
247 } else if (provider == null) { |
240 throw new NullPointerException("provider cannot be null"); |
248 throw new NullPointerException("provider cannot be null"); |
241 } |
249 } |
242 |
250 |
243 Instance instance; |
251 Service s = provider.getService("XMLSignatureFactory", mechanismType); |
244 try { |
252 if (s != null) { |
245 instance = GetInstance.getInstance |
253 Object obj = null; |
246 ("XMLSignatureFactory", null, mechanismType, provider); |
254 try { |
247 } catch (NoSuchAlgorithmException nsae) { |
255 obj = s.newInstance(null); |
248 throw new NoSuchMechanismException(nsae); |
256 } catch (NoSuchAlgorithmException nsae) { |
|
257 throw new NoSuchMechanismException(nsae); |
|
258 } |
|
259 |
|
260 if (obj instanceof XMLSignatureFactory) { |
|
261 XMLSignatureFactory factory = (XMLSignatureFactory) obj; |
|
262 factory.mechanismType = mechanismType; |
|
263 factory.provider = provider; |
|
264 return factory; |
|
265 } |
249 } |
266 } |
250 XMLSignatureFactory factory = (XMLSignatureFactory) instance.impl; |
267 throw new NoSuchMechanismException |
251 factory.mechanismType = mechanismType; |
268 ("Mechanism " + mechanismType + " not available from " + |
252 factory.provider = instance.provider; |
269 provider.getName()); |
253 return factory; |
|
254 } |
270 } |
255 |
271 |
256 /** |
272 /** |
257 * Returns an <code>XMLSignatureFactory</code> that supports the |
273 * Returns an <code>XMLSignatureFactory</code> that supports the |
258 * requested XML processing mechanism and representation type (ex: "DOM"), |
274 * requested XML processing mechanism and representation type (ex: "DOM"), |
286 throw new NullPointerException("provider cannot be null"); |
302 throw new NullPointerException("provider cannot be null"); |
287 } else if (provider.length() == 0) { |
303 } else if (provider.length() == 0) { |
288 throw new NoSuchProviderException(); |
304 throw new NoSuchProviderException(); |
289 } |
305 } |
290 |
306 |
291 Instance instance; |
307 Provider p = Security.getProvider(provider); |
292 try { |
308 Service s = p.getService("XMLSignatureFactory", mechanismType); |
293 instance = GetInstance.getInstance |
309 if (s != null) { |
294 ("XMLSignatureFactory", null, mechanismType, provider); |
310 Object obj = null; |
295 } catch (NoSuchAlgorithmException nsae) { |
311 try { |
296 throw new NoSuchMechanismException(nsae); |
312 obj = s.newInstance(null); |
|
313 } catch (NoSuchAlgorithmException nsae) { |
|
314 throw new NoSuchMechanismException(nsae); |
|
315 } |
|
316 if (obj instanceof XMLSignatureFactory) { |
|
317 XMLSignatureFactory factory = (XMLSignatureFactory) obj; |
|
318 factory.mechanismType = mechanismType; |
|
319 factory.provider = p; |
|
320 return factory; |
|
321 } |
297 } |
322 } |
298 XMLSignatureFactory factory = (XMLSignatureFactory) instance.impl; |
323 throw new NoSuchMechanismException |
299 factory.mechanismType = mechanismType; |
324 ("Mechanism " + mechanismType + " not available from " + provider); |
300 factory.provider = instance.provider; |
|
301 return factory; |
|
302 } |
325 } |
303 |
326 |
304 /** |
327 /** |
305 * Returns an <code>XMLSignatureFactory</code> that supports the |
328 * Returns an <code>XMLSignatureFactory</code> that supports the |
306 * default XML processing mechanism and representation type ("DOM"). |
329 * default XML processing mechanism and representation type ("DOM"). |