KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > axis > security > JBossCryptoFactory


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  *
7  * Created on Jan 20, 2004
8  */

9 package org.jboss.net.axis.security;
10
11 import java.lang.reflect.Constructor JavaDoc;
12 import java.security.KeyStore JavaDoc;
13
14 import org.apache.log4j.Logger;
15 import org.apache.ws.security.components.crypto.CryptoFactory;
16
17 /**
18  * <dl>
19  * <dt><b>Title: </b><dd>Crypto Factory for use within JBoss.Net</dd>
20  * <p>
21  * <dt><b>Description: </b><dd>Crypto factory that returns JBossCrypto implementations rather than Crypto
22  * implementations</dd>
23  * <p>
24  * </dl>
25  * @author <a HREF="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
26  * @version $Revision: 1.2 $
27  */

28 public class JBossCryptoFactory extends CryptoFactory
29 {
30    private static Logger log = Logger.getLogger(JBossCryptoFactory.class);
31
32    /**
33     * Gets an instance of the given Crypto class
34     * @param cryptoClassName name of the crypto class to load. This class should have a constructor that takes a
35     * keystore as a parameter
36     * @param keystore this is the keystore that will be used by the crypto class.
37     * @return
38     */

39    public static JBossCrypto getInstance(String JavaDoc cryptoClassName, KeyStore JavaDoc keystore)
40    {
41       if (log.isDebugEnabled())
42          log.debug("Attempting to get a Crypto instance of type " + cryptoClassName);
43       return loadClass(cryptoClassName, keystore);
44    }
45
46    /**
47     * This is prety much the way a new crypto instance is created via the super class, only this one doesn't depend
48     * upon properties, it just has the keystore fed in directly.
49     * @param cryptoClassName
50     * @param keystore
51     * @return
52     */

53    private static JBossCrypto loadClass(String JavaDoc cryptoClassName, KeyStore JavaDoc keystore)
54    {
55       Class JavaDoc cryptogenClass = null;
56       JBossCrypto crypto = null;
57       try
58       {
59          // instruct the class loader to load the crypto implementation
60
cryptogenClass = java.lang.Class.forName(cryptoClassName);
61       }
62       catch (ClassNotFoundException JavaDoc e)
63       {
64          throw new RuntimeException JavaDoc(cryptoClassName + " Not Found");
65       }
66
67       log.info("Using Crypto Engine [" + cryptoClassName + "]");
68
69       try
70       {
71          Class JavaDoc[] classes = new Class JavaDoc[]{KeyStore JavaDoc.class};
72          Constructor JavaDoc c = cryptogenClass.getConstructor(classes);
73          crypto = (JBossCrypto) c.newInstance(new Object JavaDoc[]{keystore});
74          return crypto;
75       }
76       catch (java.lang.Exception JavaDoc e)
77       {
78          log.debug(e);
79          log.debug(cryptoClassName + " cannot create instance with KeyStore constructor");
80       }
81
82       try
83       {
84          // try to instantiate the Crypto subclass
85
crypto = (JBossCrypto) cryptogenClass.newInstance();
86          return crypto;
87       }
88       catch (java.lang.Exception JavaDoc e)
89       {
90          throw new RuntimeException JavaDoc(cryptoClassName + " cannot create instance");
91       }
92
93    }
94 }
95
Popular Tags