View Javadoc

1   /*
2    * Copyright 2009-2010 Steve Chaloner
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  package be.objectify.led;
17  
18  import be.objectify.led.util.ContractUtils;
19  import be.objectify.led.util.StringUtils;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.util.Properties;
24  
25  /**
26   * Default implementation of {@link PropertyContext} which
27   *
28   * @author Steve Chaloner
29   */
30  public class DefaultPropertyContext implements PropertyContext
31  {
32      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPropertyContext.class);
33  
34      private final Properties properties = new Properties();
35  
36      public DefaultPropertyContext(Properties... properties)
37      {
38          ContractUtils.nonNull(properties, "properties");
39  
40          for (Properties propertiesInstance : properties)
41          {
42              this.properties.putAll(propertiesInstance);
43          }
44      }
45  
46      /** {@inheritDoc} */
47      public String getValue(String propertyName)
48      {
49          String value = null;
50  
51          if (properties.containsKey(propertyName))
52          {
53              value = properties.getProperty(propertyName);
54          }
55  
56          String property = System.getProperty(propertyName);
57          if (!StringUtils.isEmpty(property))
58          {
59              value = property;
60          }
61  
62          if (value != null)
63          {
64              LOGGER.debug("Found value [{}] for property [{}]",
65                           value,
66                           propertyName);
67          }
68  
69          return value;
70      }
71  }