View Javadoc

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