View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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   * re-maps arbitrary string values. handles pure literal Strings, not Regular Expressions
31   */
32  public class StringRemapperConfigurationInterceptor extends TraversingConfigurationInterceptor {
33      
34      private final Map mappings = new LinkedHashMap();
35  
36      /**
37       * @param mappings - Map<String, String>, key being the string to be replaced, value being the replacement
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; // if value is not accessible, skip it
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; // if value is inaccessible, we better skip it
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  }