Coverage Report - org.mule.config.builders.PlaceholderProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
PlaceholderProcessor
0%
0/60
0%
0/18
2.857
 
 1  
 /*
 2  
  * $Id: PlaceholderProcessor.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.config.builders;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.config.ConfigurationException;
 15  
 import org.mule.config.builders.i18n.BuildersMessages;
 16  
 import org.mule.impl.security.PasswordBasedEncryptionStrategy;
 17  
 import org.mule.umo.UMOEncryptionStrategy;
 18  
 import org.mule.util.BeanUtils;
 19  
 import org.mule.util.ClassUtils;
 20  
 import org.mule.util.PropertiesUtils;
 21  
 import org.mule.util.TemplateParser;
 22  
 
 23  
 import java.io.File;
 24  
 import java.util.HashMap;
 25  
 import java.util.Iterator;
 26  
 import java.util.Map;
 27  
 import java.util.Properties;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.xml.sax.Attributes;
 32  
 import org.xml.sax.helpers.AttributesImpl;
 33  
 
 34  
 /**
 35  
  * Placeholders are ant-like tags that are embedded in Mule Xml configuration i.e.
 36  
  * ${property.name} and are used to swap in property values registered with the Mule
 37  
  * container instance when the configuration is loaded. This is a helper class used
 38  
  * for parsing these tags.
 39  
  */
 40  
 public class PlaceholderProcessor
 41  
 {
 42  
     public static final String MULE_ENCRYPTION_PROPERTIES = "org.mule.config.encryption.properties";
 43  
     public static final String DEFAULT_ENCRYPTION_PROPERTIES_FILE = "mule-encryption.properties";
 44  
 
 45  
     /**
 46  
      * logger used by this class
 47  
      */
 48  0
     protected static final Log logger = LogFactory.getLog(PlaceholderProcessor.class);
 49  
 
 50  0
     private static boolean strategiesLoaded = false;
 51  
 
 52  
     private final Map types;
 53  0
     private final Map schemes = new HashMap();
 54  0
     private final TemplateParser parser = TemplateParser.createAntStyleParser();
 55  
 
 56  
     public PlaceholderProcessor()
 57  0
     {
 58  0
         types = new HashMap();
 59  0
         types.put("PBE", PasswordBasedEncryptionStrategy.class.getName());
 60  0
     }
 61  
 
 62  
     public PlaceholderProcessor(Map types)
 63  0
     {
 64  0
         this.types = types;
 65  0
     }
 66  
 
 67  
     public Attributes processAttributes(Attributes attributes, String elementName)
 68  
         throws ConfigurationException
 69  
     {
 70  0
         AttributesImpl attribs = new AttributesImpl(attributes);
 71  0
         String value = null;
 72  
 
 73  0
         for (int i = 0; i < attribs.getLength(); i++)
 74  
         {
 75  0
             value = attribs.getValue(i);
 76  0
             value = processValue(value);
 77  0
             if (value == null)
 78  
             {
 79  0
                 throw new ConfigurationException(
 80  
                     BuildersMessages.propertyTemplateMalformed(
 81  
                         "<" + elementName + attribs.getLocalName(i) + "='" + value + "' ...>"));
 82  
             }
 83  0
             attribs.setValue(i, value);
 84  
         }
 85  0
         return attribs;
 86  
     }
 87  
 
 88  
     public String processValue(String value) throws ConfigurationException
 89  
     {
 90  0
         return parser.parse(MuleManager.getInstance().getProperties(), value);
 91  
     }
 92  
 
 93  
     // public String processValue(String value) throws ConfigurationException {
 94  
     // String realValue = null;
 95  
     // String key = null;
 96  
     //
 97  
     // UMOManager manager = MuleManager.getInstance();
 98  
     //
 99  
     // parser.parse(manager.getProperties(), value);
 100  
     // int x = value.indexOf("${");
 101  
     // while(x > -1) {
 102  
     // int y = value.indexOf("}", x +1);
 103  
     // if(y==-1) {
 104  
     // return null;
 105  
     // }
 106  
     // key = value.substring(x+2, y);
 107  
     // realValue = (String)manager.getProperty(key);
 108  
     // if(logger.isDebugEnabled()) {
 109  
     // logger.debug("Param is '" + value + "', Property key is '" + key + "',
 110  
     // Property value is '" + realValue + "'");
 111  
     // }
 112  
     // if(realValue!=null) {
 113  
     // realValue = processEncryptedValue(realValue);
 114  
     // if(realValue==null) {
 115  
     // return null;
 116  
     // }
 117  
     // value = value.substring(0, x) + realValue + value.substring(y+1);
 118  
     //
 119  
     // // fix for a bug when realValue.length <= key.length
 120  
     // y = y - 3 + (realValue.length() - key.length());
 121  
     // } else {
 122  
     // logger.info("Property for placeholder: '" + key + "' was not found. Leaving
 123  
     // place holder as is. This is not necessarily a problem as the placeholder may
 124  
     // not be a Mule placeholder.");
 125  
     // }
 126  
     // x = value.indexOf("${", y);
 127  
     // }
 128  
     // return value;
 129  
     // }
 130  
 
 131  
     protected String processEncryptedValue(String value) throws ConfigurationException
 132  
     {
 133  
         String scheme;
 134  0
         int x = value.indexOf("{encrypt:");
 135  0
         if (x > -1)
 136  
         {
 137  0
             logger.debug("Value contains encrypted data.");
 138  0
             int y = value.indexOf("}");
 139  0
             if (y == -1)
 140  
             {
 141  0
                 logger.error("Encryption tag is malformed: " + value);
 142  0
                 return null;
 143  
             }
 144  
             else
 145  
             {
 146  0
                 scheme = value.substring((x + 9), y);
 147  0
                 logger.debug("look up encryption scheme: " + scheme);
 148  
                 try
 149  
                 {
 150  0
                     UMOEncryptionStrategy strategy = getEncryptionStrategy(scheme);
 151  0
                     String data = value.substring(y + 1);
 152  0
                     byte[] decrypted = strategy.decrypt(data.getBytes(), null);
 153  0
                     return new String(decrypted);
 154  
                 }
 155  0
                 catch (Exception e)
 156  
                 {
 157  0
                     throw new ConfigurationException(e);
 158  
                 }
 159  
             }
 160  
         }
 161  
         else
 162  
         {
 163  0
             return value;
 164  
         }
 165  
     }
 166  
 
 167  
     public UMOEncryptionStrategy getEncryptionStrategy(String scheme) throws Exception
 168  
     {
 169  0
         if (!strategiesLoaded)
 170  
         {
 171  0
             loadStrategies();
 172  
         }
 173  0
         return (UMOEncryptionStrategy)schemes.get(scheme);
 174  
     }
 175  
 
 176  
     private void loadStrategies() throws Exception
 177  
     {
 178  0
         String path = System.getProperty(MULE_ENCRYPTION_PROPERTIES, MuleManager.getConfiguration()
 179  
             .getWorkingDirectory()
 180  
                                                                      + File.separator
 181  
                                                                      + DEFAULT_ENCRYPTION_PROPERTIES_FILE);
 182  
 
 183  0
         logger.info("Attempting to load encryption properties from: " + path);
 184  0
         Properties props = PropertiesUtils.loadProperties(path, getClass());
 185  
 
 186  0
         Map names = new HashMap();
 187  0
         PropertiesUtils.getPropertiesWithPrefix(props, "name", names);
 188  
         String name;
 189  0
         for (Iterator iterator = names.values().iterator(); iterator.hasNext();)
 190  
         {
 191  0
             name = (String)iterator.next();
 192  0
             Map schemeConfig = new HashMap();
 193  0
             PropertiesUtils.getPropertiesWithPrefix(props, name + ".", schemeConfig);
 194  0
             schemeConfig = PropertiesUtils.removeNamespaces(schemeConfig);
 195  
 
 196  0
             String type = (String)schemeConfig.get("type");
 197  0
             String clazz = (String)types.get(type);
 198  0
             if (clazz == null)
 199  
             {
 200  0
                 throw new IllegalArgumentException("Unknown encryption type: " + type);
 201  
             }
 202  0
             logger.debug("Found Class: " + clazz + " for type: " + type);
 203  0
             UMOEncryptionStrategy strat = (UMOEncryptionStrategy)ClassUtils.instanciateClass(clazz,
 204  
                 ClassUtils.NO_ARGS, PlaceholderProcessor.class);
 205  0
             BeanUtils.populateWithoutFail(strat, schemeConfig, true);
 206  0
             schemes.put(name, strat);
 207  0
         }
 208  0
     }
 209  
 }