View Javadoc

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      protected static final Log logger = LogFactory.getLog(PlaceholderProcessor.class);
49  
50      private static boolean strategiesLoaded = false;
51  
52      private final Map types;
53      private final Map schemes = new HashMap();
54      private final TemplateParser parser = TemplateParser.createAntStyleParser();
55  
56      public PlaceholderProcessor()
57      {
58          types = new HashMap();
59          types.put("PBE", PasswordBasedEncryptionStrategy.class.getName());
60      }
61  
62      public PlaceholderProcessor(Map types)
63      {
64          this.types = types;
65      }
66  
67      public Attributes processAttributes(Attributes attributes, String elementName)
68          throws ConfigurationException
69      {
70          AttributesImpl attribs = new AttributesImpl(attributes);
71          String value = null;
72  
73          for (int i = 0; i < attribs.getLength(); i++)
74          {
75              value = attribs.getValue(i);
76              value = processValue(value);
77              if (value == null)
78              {
79                  throw new ConfigurationException(
80                      BuildersMessages.propertyTemplateMalformed(
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      // 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         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 }