View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util.internal;
17  
18  import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
19  
20  import io.netty.util.internal.logging.InternalLogger;
21  import io.netty.util.internal.logging.InternalLoggerFactory;
22  
23  import java.security.AccessController;
24  import java.security.PrivilegedAction;
25  
26  /**
27   * A collection of utility methods to retrieve and parse the values of the Java system properties.
28   */
29  public final class SystemPropertyUtil {
30  
31      private static final InternalLogger logger = InternalLoggerFactory.getInstance(SystemPropertyUtil.class);
32  
33      /**
34       * Returns {@code true} if and only if the system property with the specified {@code key}
35       * exists.
36       */
37      public static boolean contains(String key) {
38          return get(key) != null;
39      }
40  
41      /**
42       * Returns the value of the Java system property with the specified
43       * {@code key}, while falling back to {@code null} if the property access fails.
44       *
45       * @return the property value or {@code null}
46       */
47      public static String get(String key) {
48          return get(key, null);
49      }
50  
51      /**
52       * Returns the value of the Java system property with the specified
53       * {@code key}, while falling back to the specified default value if
54       * the property access fails.
55       *
56       * @return the property value.
57       *         {@code def} if there's no such property or if an access to the
58       *         specified property is not allowed.
59       */
60      public static String get(final String key, String def) {
61          checkNonEmpty(key, "key");
62  
63          String value = null;
64          try {
65              if (System.getSecurityManager() == null) {
66                  value = System.getProperty(key);
67              } else {
68                  value = AccessController.doPrivileged(new PrivilegedAction<String>() {
69                      @Override
70                      public String run() {
71                          return System.getProperty(key);
72                      }
73                  });
74              }
75          } catch (SecurityException e) {
76              logger.warn("Unable to retrieve a system property '{}'; default values will be used.", key, e);
77          }
78  
79          if (value == null) {
80              return def;
81          }
82  
83          return value;
84      }
85  
86      /**
87       * Returns the value of the Java system property with the specified
88       * {@code key}, while falling back to the specified default value if
89       * the property access fails.
90       *
91       * @return the property value.
92       *         {@code def} if there's no such property or if an access to the
93       *         specified property is not allowed.
94       */
95      public static boolean getBoolean(String key, boolean def) {
96          String value = get(key);
97          if (value == null) {
98              return def;
99          }
100 
101         value = value.trim().toLowerCase();
102         if (value.isEmpty()) {
103             return def;
104         }
105 
106         if ("true".equals(value) || "yes".equals(value) || "1".equals(value)) {
107             return true;
108         }
109 
110         if ("false".equals(value) || "no".equals(value) || "0".equals(value)) {
111             return false;
112         }
113 
114         logger.warn(
115                 "Unable to parse the boolean system property '{}':{} - using the default value: {}",
116                 key, value, def
117         );
118 
119         return def;
120     }
121 
122     /**
123      * Returns the value of the Java system property with the specified
124      * {@code key}, while falling back to the specified default value if
125      * the property access fails.
126      *
127      * @return the property value.
128      *         {@code def} if there's no such property or if an access to the
129      *         specified property is not allowed.
130      */
131     public static int getInt(String key, int def) {
132         String value = get(key);
133         if (value == null) {
134             return def;
135         }
136 
137         value = value.trim();
138         try {
139             return Integer.parseInt(value);
140         } catch (Exception e) {
141             // Ignore
142         }
143 
144         logger.warn(
145                 "Unable to parse the integer system property '{}':{} - using the default value: {}",
146                 key, value, def
147         );
148 
149         return def;
150     }
151 
152     /**
153      * Returns the value of the Java system property with the specified
154      * {@code key}, while falling back to the specified default value if
155      * the property access fails.
156      *
157      * @return the property value.
158      *         {@code def} if there's no such property or if an access to the
159      *         specified property is not allowed.
160      */
161     public static long getLong(String key, long def) {
162         String value = get(key);
163         if (value == null) {
164             return def;
165         }
166 
167         value = value.trim();
168         try {
169             return Long.parseLong(value);
170         } catch (Exception e) {
171             // Ignore
172         }
173 
174         logger.warn(
175                 "Unable to parse the long integer system property '{}':{} - using the default value: {}",
176                 key, value, def
177         );
178 
179         return def;
180     }
181 
182     private SystemPropertyUtil() {
183         // Unused
184     }
185 }