KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > builders > PlaceholderProcessor


1 /*
2  * $Id: PlaceholderProcessor.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.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 JavaDoc;
26 import org.xml.sax.helpers.AttributesImpl JavaDoc;
27
28 import java.io.File JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
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  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
41  * @version $Revision: 3798 $
42  */

43 public class PlaceholderProcessor
44 {
45     public static final String JavaDoc MULE_ENCRYPTION_PROPERTIES = "org.mule.config.encryption.properties";
46     public static final String JavaDoc DEFAULT_ENCRYPTION_PROPERTIES_FILE = "mule-encryption.properties";
47     /**
48      * logger used by this class
49      */

50     protected static Log logger = LogFactory.getLog(PlaceholderProcessor.class);
51
52     private static boolean strategiesLoaded = false;
53
54     private Map JavaDoc types = new HashMap JavaDoc();
55     private Map JavaDoc schemes = new HashMap JavaDoc();
56     private TemplateParser parser = TemplateParser.createAntStyleParser();
57
58     public PlaceholderProcessor()
59     {
60         types.put("PBE", PasswordBasedEncryptionStrategy.class.getName());
61     }
62
63     public PlaceholderProcessor(Map JavaDoc types)
64     {
65         this.types = types;
66     }
67
68     public Attributes JavaDoc processAttributes(Attributes JavaDoc attributes, String JavaDoc elementName)
69         throws ConfigurationException
70     {
71         AttributesImpl JavaDoc attribs = new AttributesImpl JavaDoc(attributes);
72         String JavaDoc 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 JavaDoc processValue(String JavaDoc 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 JavaDoc processEncryptedValue(String JavaDoc value) throws ConfigurationException
132     {
133         String JavaDoc 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 JavaDoc data = value.substring(y + 1);
152                     byte[] decrypted = strategy.decrypt(data.getBytes(), null);
153                     return new String JavaDoc(decrypted);
154                 }
155                 catch (Exception JavaDoc e)
156                 {
157                     throw new ConfigurationException(e);
158                 }
159             }
160         }
161         else
162         {
163             return value;
164         }
165     }
166
167     public UMOEncryptionStrategy getEncryptionStrategy(String JavaDoc scheme) throws Exception JavaDoc
168     {
169         if (!strategiesLoaded)
170         {
171             loadStrategies();
172         }
173         return (UMOEncryptionStrategy)schemes.get(scheme);
174     }
175
176     private void loadStrategies() throws Exception JavaDoc
177     {
178         String JavaDoc 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 JavaDoc props = PropertiesUtils.loadProperties(path, getClass());
185
186         Map JavaDoc names = new HashMap JavaDoc();
187         PropertiesUtils.getPropertiesWithPrefix(props, "name", names);
188         String JavaDoc name;
189         for (Iterator JavaDoc iterator = names.values().iterator(); iterator.hasNext();)
190         {
191             name = (String JavaDoc)iterator.next();
192             Map JavaDoc schemeConfig = new HashMap JavaDoc();
193             PropertiesUtils.getPropertiesWithPrefix(props, name + ".", schemeConfig);
194             schemeConfig = PropertiesUtils.removeNamespaces(schemeConfig);
195
196             String JavaDoc type = (String JavaDoc)schemeConfig.get("type");
197             String JavaDoc clazz = (String JavaDoc)types.get(type);
198             if (clazz == null)
199             {
200                 throw new IllegalArgumentException JavaDoc("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