001 package org.apache.fulcrum.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 import java.util.Hashtable; 024 025 import org.apache.avalon.framework.activity.Initializable; 026 import org.apache.avalon.framework.configuration.Configurable; 027 import org.apache.avalon.framework.configuration.Configuration; 028 import org.apache.avalon.framework.configuration.ConfigurationException; 029 import org.apache.avalon.framework.logger.AbstractLogEnabled; 030 import org.apache.avalon.framework.thread.ThreadSafe; 031 032 /** 033 * An implementation of CryptoService that uses either supplied crypto 034 * Algorithms (provided in the component config xml file) or tries to get them via 035 * the normal java mechanisms if this fails. 036 * 037 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> 038 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 039 * @version $Id: DefaultCryptoService.java 581797 2007-10-04 08:26:18Z sgoeschl $ 040 * 041 * @avalon.component name="crypto" lifestyle="singleton" 042 * @avalon.service type="org.apache.fulcrum.crypto.CryptoService" 043 */ 044 public class DefaultCryptoService 045 extends AbstractLogEnabled 046 implements CryptoService, Configurable, Initializable, ThreadSafe 047 { 048 // 049 // SJM: removed Component and Contextualizable, Startable 050 // 051 052 /** Key Prefix for our algorithms */ 053 private static final String ALGORITHM = "algorithm"; 054 /** Default Key */ 055 private static final String DEFAULT_KEY = "default"; 056 /** Default Encryption Class */ 057 private static final String DEFAULT_CLASS = 058 "org.apache.fulcrum.crypto.provider.JavaCrypt"; 059 /** Names of the registered algorithms and the wanted classes */ 060 private Hashtable algos = null; 061 062 /** 063 * Returns a CryptoAlgorithm Object which represents the requested 064 * crypto algorithm. 065 * 066 * @param algo Name of the requested algorithm 067 * 068 * @return An Object representing the algorithm 069 * 070 * @throws NoSuchAlgorithmException Requested algorithm is not available 071 * 072 */ 073 public CryptoAlgorithm getCryptoAlgorithm( String algo ) 074 throws NoSuchAlgorithmException 075 { 076 String cryptoClass = (String) algos.get(algo); 077 CryptoAlgorithm ca = null; 078 if (cryptoClass == null) 079 { 080 cryptoClass = (String) algos.get(DEFAULT_KEY); 081 } 082 if (cryptoClass == null || cryptoClass.equalsIgnoreCase("none")) 083 { 084 throw new NoSuchAlgorithmException( 085 "TurbineCryptoService: No Algorithm for " + algo + " found"); 086 } 087 try 088 { 089 //@todo should be created via factory service. 090 //Just trying to get something to work. 091 //ca = (CryptoAlgorithm) factoryService.getInstance(cryptoClass); 092 ca = (CryptoAlgorithm) Class.forName(cryptoClass).newInstance(); 093 } 094 catch (Exception e) 095 { 096 throw new NoSuchAlgorithmException( 097 "TurbineCryptoService: Error instantiating " 098 + cryptoClass + " for " + algo); 099 } 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 }