KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > crypto > TurbineCryptoService


1 package org.apache.turbine.services.crypto;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
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,v 1.5.2.2 2004/05/20 03:05:17 seade Exp $
39  */

40 public class TurbineCryptoService
41         extends BaseService
42         implements CryptoService
43 {
44     /** Key Prefix for our algorithms */
45     private static final String JavaDoc ALGORITHM = "algorithm";
46
47     /** Default Key */
48     private static final String JavaDoc DEFAULT_KEY = "default";
49
50     /** Default Encryption Class */
51     private static final String JavaDoc DEFAULT_CLASS =
52             JavaCrypt.class.getName();
53
54     /** Names of the registered algorithms and the wanted classes */
55     private Hashtable JavaDoc algos = null;
56
57     /** A factory to construct CryptoAlgorithm objects */
58     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         this.algos = new Hashtable JavaDoc();
71
72         /*
73          * Set up default (Can be overridden by default key
74          * from the properties
75          */

76
77         algos.put(DEFAULT_KEY, DEFAULT_CLASS);
78
79         /* get the parts of the configuration relevant to us. */
80
81         Configuration conf = getConfiguration().subset(ALGORITHM);
82
83         if (conf != null)
84         {
85             for (Iterator JavaDoc it = conf.getKeys(); it.hasNext();)
86             {
87                 String JavaDoc key = (String JavaDoc) it.next();
88                 String JavaDoc val = conf.getString(key);
89                 // Log.debug("Registered " + val
90
// + " for Crypto Algorithm " + key);
91
algos.put(key, val);
92             }
93         }
94
95         try
96         {
97             factoryService = (FactoryService) TurbineServices.getInstance().
98                     getService(FactoryService.SERVICE_NAME);
99         }
100         catch (Exception JavaDoc e)
101         {
102             throw new InitializationException(
103                     "Failed to get a Factory object: ", e);
104         }
105
106         setInit(true);
107     }
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 JavaDoc algo)
118             throws NoSuchAlgorithmException JavaDoc
119     {
120         String JavaDoc cryptoClass = (String JavaDoc) algos.get(algo);
121         CryptoAlgorithm ca = null;
122
123         if (cryptoClass == null)
124         {
125             cryptoClass = (String JavaDoc) algos.get(DEFAULT_KEY);
126         }
127
128         if (cryptoClass == null || cryptoClass.equalsIgnoreCase("none"))
129         {
130             throw new NoSuchAlgorithmException JavaDoc(
131                     "TurbineCryptoService: No Algorithm for "
132                     + algo + " found");
133         }
134
135         try
136         {
137             ca = (CryptoAlgorithm) factoryService.getInstance(cryptoClass);
138         }
139         catch (Exception JavaDoc e)
140         {
141             throw new NoSuchAlgorithmException JavaDoc(
142                     "TurbineCryptoService: Error instantiating "
143                     + cryptoClass + " for " + algo);
144         }
145
146         ca.setCipher(algo);
147
148         return ca;
149     }
150     
151 }
152
Popular Tags