001: package org.apache.turbine.services.crypto;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.security.NoSuchAlgorithmException;
023:
024: import java.util.Hashtable;
025: import java.util.Iterator;
026:
027: import org.apache.commons.configuration.Configuration;
028:
029: import org.apache.turbine.services.BaseService;
030: import org.apache.turbine.services.InitializationException;
031: import org.apache.turbine.services.TurbineServices;
032: import org.apache.turbine.services.crypto.provider.JavaCrypt;
033: import org.apache.turbine.services.factory.FactoryService;
034:
035: /**
036: * An implementation of CryptoService that uses either supplied crypto
037: * Algorithms (provided in Turbine.Services.properties) or tries to get them via
038: * the normal java mechanisms if this fails.
039: *
040: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
041: * @version $Id: TurbineCryptoService.java 534527 2007-05-02 16:10:59Z tv $
042: */
043: public class TurbineCryptoService extends BaseService implements
044: CryptoService {
045: /** Key Prefix for our algorithms */
046: private static final String ALGORITHM = "algorithm";
047:
048: /** Default Key */
049: private static final String DEFAULT_KEY = "default";
050:
051: /** Default Encryption Class */
052: private static final String DEFAULT_CLASS = JavaCrypt.class
053: .getName();
054:
055: /** Names of the registered algorithms and the wanted classes */
056: private Hashtable algos = null;
057:
058: /** A factory to construct CryptoAlgorithm objects */
059: private FactoryService factoryService = null;
060:
061: /**
062: * There is not much to initialize here. This runs
063: * as early init method.
064: *
065: * @throws InitializationException Something went wrong in the init
066: * stage
067: */
068: public void init() throws InitializationException {
069: this .algos = new Hashtable();
070:
071: /*
072: * Set up default (Can be overridden by default key
073: * from the properties
074: */
075:
076: algos.put(DEFAULT_KEY, DEFAULT_CLASS);
077:
078: /* get the parts of the configuration relevant to us. */
079:
080: Configuration conf = getConfiguration().subset(ALGORITHM);
081:
082: if (conf != null) {
083: for (Iterator it = conf.getKeys(); it.hasNext();) {
084: String key = (String) it.next();
085: String val = conf.getString(key);
086: // Log.debug("Registered " + val
087: // + " for Crypto Algorithm " + key);
088: algos.put(key, val);
089: }
090: }
091:
092: try {
093: factoryService = (FactoryService) TurbineServices
094: .getInstance().getService(
095: FactoryService.SERVICE_NAME);
096: } catch (Exception e) {
097: throw new InitializationException(
098: "Failed to get a Factory object: ", e);
099: }
100:
101: setInit(true);
102: }
103:
104: /**
105: * Returns a CryptoAlgorithm Object which represents the requested
106: * crypto algorithm.
107: *
108: * @param algo Name of the requested algorithm
109: * @return An Object representing the algorithm
110: * @throws NoSuchAlgorithmException Requested algorithm is not available
111: */
112: public CryptoAlgorithm getCryptoAlgorithm(String algo)
113: throws NoSuchAlgorithmException {
114: String cryptoClass = (String) algos.get(algo);
115: CryptoAlgorithm ca = null;
116:
117: if (cryptoClass == null) {
118: cryptoClass = (String) algos.get(DEFAULT_KEY);
119: }
120:
121: if (cryptoClass == null || cryptoClass.equalsIgnoreCase("none")) {
122: throw new NoSuchAlgorithmException(
123: "TurbineCryptoService: No Algorithm for " + algo
124: + " found");
125: }
126:
127: try {
128: ca = (CryptoAlgorithm) factoryService
129: .getInstance(cryptoClass);
130: } catch (Exception e) {
131: throw new NoSuchAlgorithmException(
132: "TurbineCryptoService: Error instantiating "
133: + cryptoClass + " for " + algo);
134: }
135:
136: ca.setCipher(algo);
137:
138: return ca;
139: }
140:
141: }
|