1 10 11 package org.mule.config.builders; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.mule.MuleManager; 16 import org.mule.config.ConfigurationException; 17 import org.mule.config.i18n.Message; 18 import org.mule.config.i18n.Messages; 19 import org.mule.impl.security.PasswordBasedEncryptionStrategy; 20 import org.mule.umo.UMOEncryptionStrategy; 21 import org.mule.util.BeanUtils; 22 import org.mule.util.ClassUtils; 23 import org.mule.util.PropertiesUtils; 24 import org.mule.util.TemplateParser; 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 28 import java.io.File ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 34 43 public class PlaceholderProcessor 44 { 45 public static final String MULE_ENCRYPTION_PROPERTIES = "org.mule.config.encryption.properties"; 46 public static final String DEFAULT_ENCRYPTION_PROPERTIES_FILE = "mule-encryption.properties"; 47 50 protected static Log logger = LogFactory.getLog(PlaceholderProcessor.class); 51 52 private static boolean strategiesLoaded = false; 53 54 private Map types = new HashMap (); 55 private Map schemes = new HashMap (); 56 private TemplateParser parser = TemplateParser.createAntStyleParser(); 57 58 public PlaceholderProcessor() 59 { 60 types.put("PBE", PasswordBasedEncryptionStrategy.class.getName()); 61 } 62 63 public PlaceholderProcessor(Map types) 64 { 65 this.types = types; 66 } 67 68 public Attributes processAttributes(Attributes attributes, String elementName) 69 throws ConfigurationException 70 { 71 AttributesImpl attribs = new AttributesImpl (attributes); 72 String value = null; 73 74 for (int i = 0; i < attribs.getLength(); i++) 75 { 76 value = attribs.getValue(i); 77 value = processValue(value); 78 if (value == null) 79 { 80 throw new ConfigurationException(new Message(Messages.PROPERTY_TEMPLATE_MALFORMED_X, 81 "<" + elementName + attribs.getLocalName(i) + "='" + value + "' ...>")); 82 } 83 attribs.setValue(i, value); 84 } 85 return attribs; 86 } 87 88 public String processValue(String value) throws ConfigurationException 89 { 90 return parser.parse(MuleManager.getInstance().getProperties(), value); 91 } 92 93 131 protected String processEncryptedValue(String value) throws ConfigurationException 132 { 133 String scheme; 134 int x = value.indexOf("{encrypt:"); 135 if (x > -1) 136 { 137 logger.debug("Value contains encrypted data."); 138 int y = value.indexOf("}"); 139 if (y == -1) 140 { 141 logger.error("Encryption tag is malformed: " + value); 142 return null; 143 } 144 else 145 { 146 scheme = value.substring((x + 9), y); 147 logger.debug("look up encryption scheme: " + scheme); 148 try 149 { 150 UMOEncryptionStrategy strategy = getEncryptionStrategy(scheme); 151 String data = value.substring(y + 1); 152 byte[] decrypted = strategy.decrypt(data.getBytes(), null); 153 return new String (decrypted); 154 } 155 catch (Exception e) 156 { 157 throw new ConfigurationException(e); 158 } 159 } 160 } 161 else 162 { 163 return value; 164 } 165 } 166 167 public UMOEncryptionStrategy getEncryptionStrategy(String scheme) throws Exception 168 { 169 if (!strategiesLoaded) 170 { 171 loadStrategies(); 172 } 173 return (UMOEncryptionStrategy)schemes.get(scheme); 174 } 175 176 private void loadStrategies() throws Exception 177 { 178 String path = System.getProperty(MULE_ENCRYPTION_PROPERTIES, MuleManager.getConfiguration() 179 .getWorkingDirectory() 180 + File.separator 181 + DEFAULT_ENCRYPTION_PROPERTIES_FILE); 182 183 logger.info("Attempting to load encryption properties from: " + path); 184 Properties props = PropertiesUtils.loadProperties(path, getClass()); 185 186 Map names = new HashMap (); 187 PropertiesUtils.getPropertiesWithPrefix(props, "name", names); 188 String name; 189 for (Iterator iterator = names.values().iterator(); iterator.hasNext();) 190 { 191 name = (String )iterator.next(); 192 Map schemeConfig = new HashMap (); 193 PropertiesUtils.getPropertiesWithPrefix(props, name + ".", schemeConfig); 194 schemeConfig = PropertiesUtils.removeNamespaces(schemeConfig); 195 196 String type = (String )schemeConfig.get("type"); 197 String clazz = (String )types.get(type); 198 if (clazz == null) 199 { 200 throw new IllegalArgumentException ("Unknown encryption type: " + type); 201 } 202 logger.debug("Found Class: " + clazz + " for type: " + type); 203 UMOEncryptionStrategy strat = (UMOEncryptionStrategy)ClassUtils.instanciateClass(clazz, 204 ClassUtils.NO_ARGS, PlaceholderProcessor.class); 205 BeanUtils.populateWithoutFail(strat, schemeConfig, true); 206 schemes.put(name, strat); 207 } 208 } 209 } 210 | Popular Tags |