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