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.properties;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.reactive.beans.factory.config.*;
22  import org.springframework.beans.factory.BeanInitializationException;
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.core.io.Resource;
25  import org.springframework.util.DefaultPropertiesPersister;
26  import org.springframework.util.PropertiesPersister;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.InputStreamReader;
31  import java.util.Enumeration;
32  import java.util.Properties;
33  
34  /***
35   * Used to evaluate a placeholder using the {@link java.util.Properties} class.  Only placeholders of type String can
36   * be evaluated using this class.
37   *
38   * <p>
39   * Note: This class is basically equivelant to Spring's org.springframework.beans.factory.config.PropertyResourceConfigurer
40   * by Juergen Hoeller.
41   * </p>
42   *
43   * @author Dan Washusen
44   * @version $Id: PropertiesPlaceholderEvaluator.java,v 1.2 2004/12/30 23:28:28 dan_washusen Exp $
45   * @since 20/12/2004
46   * @see org.reactive.configuration.PropertiesConfigurationFactory
47   */
48  public class PropertiesPlaceholderEvaluator
49          implements PlaceholderEvaluator {
50      protected final Log logger = LogFactory.getLog(PropertiesPlaceholderEvaluator.class);
51  
52  	private Properties properties;
53  
54      public Properties getProperties() {
55          return properties;
56      }
57  
58      public void setProperties(Properties properties) {
59          this.properties = properties;
60      }
61  
62      public Object evaluate(Placeholder placeholder) {
63          if (String.class.equals(placeholder.getType())) {
64              return getProperties().getProperty(placeholder.getKey());
65          }
66          else {
67              throw new InvalidPlaceholderException("Only placeholders of type " + DefaultPlaceholder.TYPE_STRING + " are allowed when using " + this.getClass().getName());
68          }
69      }
70  }