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