1 package org.apache.fulcrum.crypto;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.security.NoSuchAlgorithmException;
23 import java.util.Hashtable;
24
25 import org.apache.avalon.framework.activity.Initializable;
26 import org.apache.avalon.framework.configuration.Configurable;
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.logger.AbstractLogEnabled;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31
32 /**
33 * An implementation of CryptoService that uses either supplied crypto
34 * Algorithms (provided in the component config xml file) or tries to get them via
35 * the normal java mechanisms if this fails.
36 *
37 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
38 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39 * @version $Id: DefaultCryptoService.java 581797 2007-10-04 08:26:18Z sgoeschl $
40 *
41 * @avalon.component name="crypto" lifestyle="singleton"
42 * @avalon.service type="org.apache.fulcrum.crypto.CryptoService"
43 */
44 public class DefaultCryptoService
45 extends AbstractLogEnabled
46 implements CryptoService, Configurable, Initializable, ThreadSafe
47 {
48
49
50
51
52 /** Key Prefix for our algorithms */
53 private static final String ALGORITHM = "algorithm";
54 /** Default Key */
55 private static final String DEFAULT_KEY = "default";
56 /** Default Encryption Class */
57 private static final String DEFAULT_CLASS =
58 "org.apache.fulcrum.crypto.provider.JavaCrypt";
59 /** Names of the registered algorithms and the wanted classes */
60 private Hashtable algos = null;
61
62 /**
63 * Returns a CryptoAlgorithm Object which represents the requested
64 * crypto algorithm.
65 *
66 * @param algo Name of the requested algorithm
67 *
68 * @return An Object representing the algorithm
69 *
70 * @throws NoSuchAlgorithmException Requested algorithm is not available
71 *
72 */
73 public CryptoAlgorithm getCryptoAlgorithm( String algo )
74 throws NoSuchAlgorithmException
75 {
76 String cryptoClass = (String) algos.get(algo);
77 CryptoAlgorithm ca = null;
78 if (cryptoClass == null)
79 {
80 cryptoClass = (String) algos.get(DEFAULT_KEY);
81 }
82 if (cryptoClass == null || cryptoClass.equalsIgnoreCase("none"))
83 {
84 throw new NoSuchAlgorithmException(
85 "TurbineCryptoService: No Algorithm for " + algo + " found");
86 }
87 try
88 {
89
90
91
92 ca = (CryptoAlgorithm) Class.forName(cryptoClass).newInstance();
93 }
94 catch (Exception e)
95 {
96 throw new NoSuchAlgorithmException(
97 "TurbineCryptoService: Error instantiating "
98 + cryptoClass + " for " + algo);
99 }
100 ca.setCipher(algo);
101 return ca;
102 }
103
104
105
106 /**
107 * Avalon component lifecycle method
108 */
109 public void configure(Configuration conf) throws ConfigurationException
110 {
111 this.algos = new Hashtable();
112
113
114 algos.put(DEFAULT_KEY, DEFAULT_CLASS);
115 final Configuration algorithms = conf.getChild(ALGORITHM, false);
116 if (algorithms != null)
117 {
118 Configuration[] nameVal = algorithms.getChildren();
119 for (int i = 0; i < nameVal.length; i++)
120 {
121 String key = nameVal[i].getName();
122 String val = nameVal[i].getValue();
123
124
125 algos.put(key, val);
126 }
127 }
128 }
129
130 /**
131 * @see org.apache.avalon.framework.activity.Initializable#initialize()
132 */
133 public void initialize()
134 throws Exception
135 {
136 getLogger().debug("initialize()");
137 }
138
139 /**
140 * Avalon component lifecycle method
141 */
142 public void dispose()
143 {
144 algos = null;
145 }
146
147 }