View Javadoc

1   /*
2    * Copyright 2004-2005 Dan Washusen
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.reactive.beans.factory.config.commons;
18  
19  import org.reactive.beans.factory.config.PlaceholderEvaluator;
20  import org.reactive.beans.factory.config.Placeholder;
21  import org.reactive.beans.factory.config.AbstractPlaceholderEvaluator;
22  import org.apache.commons.configuration.Configuration;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import java.util.*;
27  
28  /***
29   * Used to evaluate a placeholder using the Commons-Configuration configuration agent.
30   *
31   * @author Dan Washusen
32   * @version $Id: CommonsPlaceholderEvaluator.java,v 1.3 2004/12/30 07:35:28 dan_washusen Exp $
33   * @since 20/12/2004
34   */
35  public class CommonsPlaceholderEvaluator
36          extends AbstractPlaceholderEvaluator {
37      private static final Log log = LogFactory.getLog(CommonsPlaceholderEvaluator.class);
38  
39      protected Configuration configuration;
40  
41      public Configuration getConfiguration() {
42          return configuration;
43      }
44  
45      public void setConfiguration(Configuration configuration) {
46          this.configuration = configuration;
47      }
48  
49      protected String evaluateString(Placeholder placeholder) {
50          return getConfiguration().getString(placeholder.getKey());
51      }
52  
53      protected String[] evaluateArray(Placeholder placeholder) {
54          return getConfiguration().getStringArray(placeholder.getKey());
55      }
56  
57      protected List evaluateList(Placeholder placeholder) {
58          return getConfiguration().getList(placeholder.getKey());
59      }
60  
61      protected Set evaluateSet(Placeholder placeholder) {
62          // we are going to assume that the user has garanteed a set
63          List list = getConfiguration().getList(placeholder.getKey());
64          Set set = new HashSet(list);
65  
66          // log an debug message if we detect duplicate elements in the list
67          if (log.isDebugEnabled()) {
68              if (list.size() != set.size())
69                  log.debug("The set with the key: '" + placeholder.getKey() + "' contained duplicates, they have removed.");
70          }
71  
72          return set;
73      }
74  
75      protected Map evaluateMap(Placeholder placeholder) {
76          // the properties object extends map, so why not use it
77          return (Map) getConfiguration().getProperties(placeholder.getKey());
78      }
79  
80      protected Properties evaluateProperties(Placeholder placeholder) {
81          return getConfiguration().getProperties(placeholder.getKey());
82      }
83  
84      protected Object evaluateObject(Placeholder placeholder) {
85          return getConfiguration().getString(placeholder.getKey());
86      }
87  }