KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > cryptor > CryptorFactory


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.cryptor;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.juddi.util.Config;
21 import org.apache.juddi.util.Loader;
22
23 /**
24  * Used to create the org.apache.juddi.cryptor.Cryptor implementation
25  * as specified by the 'juddi.cryptor' property. Defaults to
26  * org.apache.juddi.cryptor.DefaultCryptor if an implementation is not
27  * specified.
28  *
29  * @author Steve Viens (sviens@apache.org)
30  */

31 public abstract class CryptorFactory
32 {
33   // private reference to the jUDDI logger
34
private static Log log = LogFactory.getLog(CryptorFactory.class);
35
36   // Cryptor property key & default implementation
37
private static final String JavaDoc IMPL_KEY = "juddi.cryptor";
38   private static final String JavaDoc DEFAULT_IMPL = "org.apache.juddi.cryptor.DefaultCryptor";
39
40   // the shared Cryptor instance
41
private static Cryptor cryptor = null;
42
43   /**
44    * Returns a new instance of a CryptorFactory.
45    *
46    * @return Cryptor
47    */

48   public static Cryptor getCryptor()
49   {
50     if (cryptor == null)
51       cryptor = createCryptor();
52     return cryptor;
53   }
54
55   /**
56    * Returns a new instance of a Cryptor.
57    *
58    * @return Cryptor
59    */

60   private static synchronized Cryptor createCryptor()
61   {
62     if (cryptor != null)
63       return cryptor;
64
65     // grab class name of the Cryptor implementation to create
66
String JavaDoc className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
67
68     // write the Cryptor implementation name to the log
69
log.debug("Cryptor Implementation = " + className);
70
71     Class JavaDoc cryptorClass = null;
72     try
73     {
74       // Use Loader to locate & load the Cryptor implementation
75
cryptorClass = Loader.getClassForName(className);
76     }
77     catch(ClassNotFoundException JavaDoc e)
78     {
79       log.error("The specified Cryptor class '" + className +
80         "' was not found in classpath.");
81       log.error(e);
82     }
83
84     try
85     {
86       // try to instantiate the Cryptor implementation
87
cryptor = (Cryptor)cryptorClass.newInstance();
88     }
89     catch(Exception JavaDoc e)
90     {
91       log.error("Exception while attempting to instantiate the " +
92         "implementation of Cryptor: " + cryptorClass.getName() +
93         "\n" + e.getMessage());
94       log.error(e);
95     }
96
97     return cryptor;
98   }
99
100
101   /***************************************************************************/
102   /***************************** TEST DRIVER *********************************/
103   /***************************************************************************/
104
105
106   public static void main(String JavaDoc[] args)
107     throws Exception JavaDoc
108   {
109     Cryptor cryptor = CryptorFactory.getCryptor();
110     
111     String JavaDoc encryptedText = cryptor.encrypt("password");
112     System.out.println("EnCrypted text [" + encryptedText + "]");
113
114     String JavaDoc decryptedText = cryptor.decrypt(encryptedText);
115     System.out.println("DeCrypted text " + decryptedText);
116   }
117 }
Popular Tags