1 package org.apache.turbine.services.crypto; 2 3 18 19 import java.security.NoSuchAlgorithmException ; 20 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 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 40 public class TurbineCryptoService 41 extends BaseService 42 implements CryptoService 43 { 44 45 private static final String ALGORITHM = "algorithm"; 46 47 48 private static final String DEFAULT_KEY = "default"; 49 50 51 private static final String DEFAULT_CLASS = 52 JavaCrypt.class.getName(); 53 54 55 private Hashtable algos = null; 56 57 58 private FactoryService factoryService = null; 59 60 67 public void init() 68 throws InitializationException 69 { 70 this.algos = new Hashtable (); 71 72 76 77 algos.put(DEFAULT_KEY, DEFAULT_CLASS); 78 79 80 81 Configuration conf = getConfiguration().subset(ALGORITHM); 82 83 if (conf != null) 84 { 85 for (Iterator it = conf.getKeys(); it.hasNext();) 86 { 87 String key = (String ) it.next(); 88 String val = conf.getString(key); 89 algos.put(key, val); 92 } 93 } 94 95 try 96 { 97 factoryService = (FactoryService) TurbineServices.getInstance(). 98 getService(FactoryService.SERVICE_NAME); 99 } 100 catch (Exception e) 101 { 102 throw new InitializationException( 103 "Failed to get a Factory object: ", e); 104 } 105 106 setInit(true); 107 } 108 109 117 public CryptoAlgorithm getCryptoAlgorithm(String algo) 118 throws NoSuchAlgorithmException 119 { 120 String cryptoClass = (String ) algos.get(algo); 121 CryptoAlgorithm ca = null; 122 123 if (cryptoClass == null) 124 { 125 cryptoClass = (String ) algos.get(DEFAULT_KEY); 126 } 127 128 if (cryptoClass == null || cryptoClass.equalsIgnoreCase("none")) 129 { 130 throw new NoSuchAlgorithmException ( 131 "TurbineCryptoService: No Algorithm for " 132 + algo + " found"); 133 } 134 135 try 136 { 137 ca = (CryptoAlgorithm) factoryService.getInstance(cryptoClass); 138 } 139 catch (Exception e) 140 { 141 throw new NoSuchAlgorithmException ( 142 "TurbineCryptoService: Error instantiating " 143 + cryptoClass + " for " + algo); 144 } 145 146 ca.setCipher(algo); 147 148 return ca; 149 } 150 151 } 152 | Popular Tags |