View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration;
19  
20  import java.util.Iterator;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * A configuration based on the system properties.
27   *
28   * @author Emmanuel Bourg
29   * @version $Id: SystemConfiguration.java 1210204 2011-12-04 20:38:02Z oheger $
30   * @since 1.1
31   */
32  public class SystemConfiguration extends MapConfiguration
33  {
34      /** The logger. */
35      private static Log log = LogFactory.getLog(SystemConfiguration.class);
36  
37      /**
38       * Create a Configuration based on the system properties.
39       *
40       * @see System#getProperties
41       */
42      public SystemConfiguration()
43      {
44          super(System.getProperties());
45      }
46  
47      /**
48       * The method allows system properties to be set from a property file.
49       * @param fileName The name of the property file.
50       * @throws Exception if an error occurs.
51       * @since 1.6
52       */
53      public static void setSystemProperties(String fileName) throws Exception
54      {
55          setSystemProperties(null, fileName);
56      }
57  
58      /**
59       * The method allows system properties to be set from a property file.
60       * @param basePath The base path to look for the property file.
61       * @param fileName The name of the property file.
62       * @throws Exception if an error occurs.
63       * @since 1.6
64       */
65      public static void setSystemProperties(String basePath, String fileName) throws Exception
66      {
67          PropertiesConfiguration config = fileName.endsWith(".xml")
68              ? new XMLPropertiesConfiguration() : new PropertiesConfiguration();
69          if (basePath != null)
70          {
71              config.setBasePath(basePath);
72          }
73          config.setFileName(fileName);
74          config.load();
75          setSystemProperties(config);
76      }
77  
78      /**
79       * Set System properties from a configuration file.
80       * @param systemConfig The configuration containing the properties to be set.
81       * @since 1.6
82       */
83      public static void setSystemProperties(PropertiesConfiguration systemConfig)
84      {
85          Iterator<String> iter = systemConfig.getKeys();
86          while (iter.hasNext())
87          {
88              String key = iter.next();
89              String value = (String) systemConfig.getProperty(key);
90              if (log.isDebugEnabled())
91              {
92                  log.debug("Setting system property " + key + " to " + value);
93              }
94              System.setProperty(key, value);
95          }
96      }
97  }