1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.james.container.spring.examples.configuration;
20
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.configuration.MutableConfiguration;
23 import org.apache.commons.lang.StringUtils;
24
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29
30
31
32 public class StringRemapperConfigurationInterceptor extends TraversingConfigurationInterceptor {
33
34 private final Map mappings = new LinkedHashMap();
35
36
37
38
39 public void setMappings(Map mappings) {
40 this.mappings.putAll(mappings);
41 }
42
43 protected void process(MutableConfiguration mutableConfiguration) {
44
45 processAttributes(mutableConfiguration);
46 processValue(mutableConfiguration);
47 }
48
49 private void processAttributes(MutableConfiguration mutableConfiguration) {
50 String[] attributeNames = mutableConfiguration.getAttributeNames();
51 for (int i = 0; i < attributeNames.length; i++) {
52 String attributeName = attributeNames[i];
53 String attributeValue = null;
54 try {
55 attributeValue = mutableConfiguration.getAttribute(attributeName);
56 } catch (ConfigurationException e) {
57 continue;
58 }
59
60 Iterator iterator = mappings.keySet().iterator();
61 while (iterator.hasNext()) {
62 String find = (String) iterator.next();
63 String replacement = (String) mappings.get(find);
64 String replaced = StringUtils.replace(attributeValue, find, replacement);
65 mutableConfiguration.setAttribute(attributeName, replaced);
66 }
67 }
68 }
69
70 private void processValue(MutableConfiguration mutableConfiguration) {
71 String value = null;
72 try {
73 value = mutableConfiguration.getValue();
74 } catch (ConfigurationException e) {
75 return;
76 }
77 if (value == null) return;
78
79 Iterator iterator = mappings.keySet().iterator();
80 while (iterator.hasNext()) {
81 String find = (String) iterator.next();
82 String replacement = (String) mappings.get(find);
83 String replaced = StringUtils.replace(value, find, replacement);
84 mutableConfiguration.setValue(replaced);
85 }
86 }
87 }