%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.crypto.TurbineCryptoService |
|
|
1 | package org.apache.turbine.services.crypto; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.security.NoSuchAlgorithmException; |
|
20 | ||
21 | import java.util.Hashtable; |
|
22 | import java.util.Iterator; |
|
23 | ||
24 | import org.apache.commons.configuration.Configuration; |
|
25 | ||
26 | import org.apache.turbine.services.BaseService; |
|
27 | import org.apache.turbine.services.InitializationException; |
|
28 | import org.apache.turbine.services.TurbineServices; |
|
29 | import org.apache.turbine.services.crypto.provider.JavaCrypt; |
|
30 | import org.apache.turbine.services.factory.FactoryService; |
|
31 | ||
32 | /** |
|
33 | * An implementation of CryptoService that uses either supplied crypto |
|
34 | * Algorithms (provided in Turbine.Services.properties) or tries to get them via |
|
35 | * the normal java mechanisms if this fails. |
|
36 | * |
|
37 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
38 | * @version $Id: TurbineCryptoService.java 264148 2005-08-29 14:21:04Z henning $ |
|
39 | */ |
|
40 | 20 | public class TurbineCryptoService |
41 | extends BaseService |
|
42 | implements CryptoService |
|
43 | { |
|
44 | /** Key Prefix for our algorithms */ |
|
45 | private static final String ALGORITHM = "algorithm"; |
|
46 | ||
47 | /** Default Key */ |
|
48 | private static final String DEFAULT_KEY = "default"; |
|
49 | ||
50 | /** Default Encryption Class */ |
|
51 | 10 | private static final String DEFAULT_CLASS = |
52 | 5 | JavaCrypt.class.getName(); |
53 | ||
54 | /** Names of the registered algorithms and the wanted classes */ |
|
55 | 10 | private Hashtable algos = null; |
56 | ||
57 | /** A factory to construct CryptoAlgorithm objects */ |
|
58 | 10 | private FactoryService factoryService = null; |
59 | ||
60 | /** |
|
61 | * There is not much to initialize here. This runs |
|
62 | * as early init method. |
|
63 | * |
|
64 | * @throws InitializationException Something went wrong in the init |
|
65 | * stage |
|
66 | */ |
|
67 | public void init() |
|
68 | throws InitializationException |
|
69 | { |
|
70 | 10 | this.algos = new Hashtable(); |
71 | ||
72 | /* |
|
73 | * Set up default (Can be overridden by default key |
|
74 | * from the properties |
|
75 | */ |
|
76 | ||
77 | 10 | algos.put(DEFAULT_KEY, DEFAULT_CLASS); |
78 | ||
79 | /* get the parts of the configuration relevant to us. */ |
|
80 | ||
81 | 10 | Configuration conf = getConfiguration().subset(ALGORITHM); |
82 | ||
83 | 10 | if (conf != null) |
84 | { |
|
85 | 35 | for (Iterator it = conf.getKeys(); it.hasNext();) |
86 | { |
|
87 | 40 | String key = (String) it.next(); |
88 | 40 | String val = conf.getString(key); |
89 | // Log.debug("Registered " + val |
|
90 | // + " for Crypto Algorithm " + key); |
|
91 | 40 | algos.put(key, val); |
92 | } |
|
93 | } |
|
94 | ||
95 | try |
|
96 | { |
|
97 | 10 | factoryService = (FactoryService) TurbineServices.getInstance(). |
98 | getService(FactoryService.SERVICE_NAME); |
|
99 | 5 | } |
100 | 0 | catch (Exception e) |
101 | { |
|
102 | 0 | throw new InitializationException( |
103 | "Failed to get a Factory object: ", e); |
|
104 | 5 | } |
105 | ||
106 | 10 | setInit(true); |
107 | 10 | } |
108 | ||
109 | /** |
|
110 | * Returns a CryptoAlgorithm Object which represents the requested |
|
111 | * crypto algorithm. |
|
112 | * |
|
113 | * @param algo Name of the requested algorithm |
|
114 | * @return An Object representing the algorithm |
|
115 | * @throws NoSuchAlgorithmException Requested algorithm is not available |
|
116 | */ |
|
117 | public CryptoAlgorithm getCryptoAlgorithm(String algo) |
|
118 | throws NoSuchAlgorithmException |
|
119 | { |
|
120 | 16 | String cryptoClass = (String) algos.get(algo); |
121 | 16 | CryptoAlgorithm ca = null; |
122 | ||
123 | 16 | if (cryptoClass == null) |
124 | { |
|
125 | 0 | cryptoClass = (String) algos.get(DEFAULT_KEY); |
126 | } |
|
127 | ||
128 | 16 | if (cryptoClass == null || cryptoClass.equalsIgnoreCase("none")) |
129 | { |
|
130 | 0 | throw new NoSuchAlgorithmException( |
131 | "TurbineCryptoService: No Algorithm for " |
|
132 | + algo + " found"); |
|
133 | } |
|
134 | ||
135 | try |
|
136 | { |
|
137 | 16 | ca = (CryptoAlgorithm) factoryService.getInstance(cryptoClass); |
138 | 8 | } |
139 | 0 | catch (Exception e) |
140 | { |
|
141 | 0 | throw new NoSuchAlgorithmException( |
142 | "TurbineCryptoService: Error instantiating " |
|
143 | + cryptoClass + " for " + algo); |
|
144 | 8 | } |
145 | ||
146 | 16 | ca.setCipher(algo); |
147 | ||
148 | 16 | return ca; |
149 | } |
|
150 | ||
151 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |