View Javadoc

1   package org.apache.fulcrum.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  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      // SJM: removed Component and Contextualizable, Startable
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              //@todo should be created via factory service.
90              //Just trying to get something to work.
91              //ca = (CryptoAlgorithm) factoryService.getInstance(cryptoClass);
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     // ---------------- Avalon Lifecycle Methods ---------------------
105 
106     /**
107      * Avalon component lifecycle method
108      */
109     public void configure(Configuration conf) throws ConfigurationException
110     {
111         this.algos = new Hashtable();
112         // Set up default (Can be overridden by default key
113         // from the properties
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                 // getLogger.debug("Registered " + val
124                 //            + " for Crypto Algorithm " + key);
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 }