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.configuration2;
19  
20  import java.util.Iterator;
21  
22  import org.apache.commons.configuration2.ex.ConfigurationException;
23  import org.apache.commons.configuration2.io.FileHandler;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * A configuration based on the system properties.
29   *
30   * @since 1.1
31   */
32  public class SystemConfiguration extends MapConfiguration {
33      /** The logger. */
34      private static final Log LOG = LogFactory.getLog(SystemConfiguration.class);
35  
36      /**
37       * Create a Configuration based on the system properties.
38       *
39       * @see System#getProperties
40       */
41      public SystemConfiguration() {
42          super(System.getProperties());
43      }
44  
45      /**
46       * Sets system properties from a file specified by its file name. This is just a short cut for
47       * {@code setSystemProperties(null, fileName)}.
48       *
49       * @param fileName The name of the property file.
50       * @throws ConfigurationException if an error occurs.
51       * @since 1.6
52       */
53      public static void setSystemProperties(final String fileName) throws ConfigurationException {
54          setSystemProperties(null, fileName);
55      }
56  
57      /**
58       * Sets system properties from a file specified using its base path and file name. The file can either be a properties
59       * file or an XML properties file. It is loaded, and all properties it contains are added to system properties.
60       *
61       * @param basePath The base path to look for the property file.
62       * @param fileName The name of the property file.
63       * @throws ConfigurationException if an error occurs.
64       * @since 1.6
65       */
66      public static void setSystemProperties(final String basePath, final String fileName) throws ConfigurationException {
67          final FileBasedConfiguration config = fileName.endsWith(".xml") ? new XMLPropertiesConfiguration() : new PropertiesConfiguration();
68  
69          final FileHandler handler = new FileHandler(config);
70          handler.setBasePath(basePath);
71          handler.setFileName(fileName);
72          handler.load();
73          setSystemProperties(config);
74      }
75  
76      /**
77       * Sets System properties from a configuration object.
78       *
79       * @param systemConfig The configuration containing the properties to be set.
80       * @since 1.6
81       */
82      public static void setSystemProperties(final Configuration systemConfig) {
83          final Iterator<String> iter = systemConfig.getKeys();
84          while (iter.hasNext()) {
85              final String key = iter.next();
86              final String value = (String) systemConfig.getProperty(key);
87              if (LOG.isDebugEnabled()) {
88                  LOG.debug("Setting system property " + key + " to " + value);
89              }
90              System.setProperty(key, value);
91          }
92      }
93  
94      /**
95       * {@inheritDoc} This implementation returns a snapshot of the keys in the system properties. If another thread modifies
96       * system properties concurrently, these changes are not reflected by the iterator returned by this method.
97       */
98      @Override
99      protected Iterator<String> getKeysInternal() {
100         return System.getProperties().stringPropertyNames().iterator();
101     }
102 }