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  package org.apache.logging.log4j.util;
18  
19  import org.apache.logging.log4j.status.StatusLogger;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.Properties;
24  
25  /**
26   * Utility class to help with accessing System Properties.
27   */
28  public class PropertiesUtil {
29  
30      private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil("log4j2.component.properties");
31  
32      private final Properties props;
33  
34      public PropertiesUtil(final Properties props) {
35          this.props = props;
36      }
37  
38      public PropertiesUtil(final String propsLocn) {
39          this.props = new Properties();
40          final ClassLoader loader = ProviderUtil.findClassLoader();
41          final InputStream in = loader.getResourceAsStream(propsLocn);
42          if (null != in) {
43              try {
44                  this.props.load(in);
45              } catch (final IOException e) {
46                  // ignored
47              } finally {
48                  try {
49                      in.close();
50                  } catch (final IOException e) {
51                      // ignored
52                  }
53              }
54          }
55      }
56  
57      public static PropertiesUtil getProperties() {
58          return LOG4J_PROPERTIES;
59      }
60  
61      public String getStringProperty(final String name) {
62          String prop = null;
63          try {
64              prop = System.getProperty(name);
65          } catch (final SecurityException e) {
66              // Ignore
67          }
68          return (prop == null) ? props.getProperty(name) : prop;
69      }
70  
71  
72      public int getIntegerProperty(final String name, final int defaultValue) {
73          String prop = null;
74          try {
75              prop = System.getProperty(name);
76          } catch (final SecurityException e) {
77              // Ignore
78          }
79          if (prop == null) {
80              prop = props.getProperty(name);
81          }
82          if (prop != null) {
83              try {
84                  return Integer.parseInt(prop);
85              } catch (Exception ex) {
86                  return defaultValue;
87              }
88          }
89          return defaultValue;
90      }
91  
92  
93      public long getLongProperty(final String name, final long defaultValue) {
94          String prop = null;
95          try {
96              prop = System.getProperty(name);
97          } catch (final SecurityException e) {
98              // Ignore
99          }
100         if (prop == null) {
101             prop = props.getProperty(name);
102         }
103         if (prop != null) {
104             try {
105                 return Long.parseLong(prop);
106             } catch (Exception ex) {
107                 return defaultValue;
108             }
109         }
110         return defaultValue;
111     }
112 
113     public String getStringProperty(final String name, final String defaultValue) {
114         final String prop = getStringProperty(name);
115         return (prop == null) ? defaultValue : prop;
116     }
117 
118     public boolean getBooleanProperty(final String name) {
119         return getBooleanProperty(name, false);
120     }
121 
122     public boolean getBooleanProperty(final String name, final boolean defaultValue) {
123         final String prop = getStringProperty(name);
124         return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
125     }
126 
127     /**
128      * Return the system properties or an empty Properties object if an error occurs.
129      * @return The system properties.
130      */
131     public static Properties getSystemProperties() {
132         try {
133             return new Properties(System.getProperties());
134         } catch (final SecurityException ex) {
135             StatusLogger.getLogger().error("Unable to access system properties.");
136             // Sandboxed - can't read System Properties
137             return new Properties();
138         }
139     }
140 }