查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   http://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 org.jboss.netty.util.internal;
17  
18  import org.jboss.netty.logging.InternalLogger;
19  import org.jboss.netty.logging.InternalLoggerFactory;
20  
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  import java.util.regex.Pattern;
24  
25  /**
26   * A collection of utility methods to retrieve and parse the values of the Java system properties.
27   */
28  public final class SystemPropertyUtil {
29  
30      @SuppressWarnings("all")
31      private static boolean initializedLogger;
32      private static final InternalLogger logger;
33      private static boolean loggedException;
34  
35      static {
36          logger = InternalLoggerFactory.getInstance(SystemPropertyUtil.class);
37          initializedLogger = true;
38      }
39  
40      /**
41       * Returns {@code true} if and only if the system property with the specified {@code key}
42       * exists.
43       */
44      public static boolean contains(String key) {
45          return get(key) != null;
46      }
47  
48      /**
49       * Returns the value of the Java system property with the specified
50       * {@code key}, while falling back to {@code null} if the property access fails.
51       *
52       * @return the property value or {@code null}
53       */
54      public static String get(String key) {
55          return get(key, null);
56      }
57  
58      /**
59       * Returns the value of the Java system property with the specified
60       * {@code key}, while falling back to the specified default value if
61       * the property access fails.
62       *
63       * @return the property value.
64       *         {@code def} if there's no such property or if an access to the
65       *         specified property is not allowed.
66       */
67      public static String get(String key, String def) {
68          if (key == null) {
69              throw new NullPointerException("key");
70          }
71          if (key.length() == 0) {
72              throw new IllegalArgumentException("key must not be empty.");
73          }
74  
75          String value = null;
76          try {
77              value = System.getProperty(key);
78          } catch (Exception e) {
79              if (!loggedException) {
80                  log("Unable to retrieve a system property '" + key + "'; default values will be used.", e);
81                  loggedException = true;
82              }
83          }
84  
85          if (value == null) {
86              return def;
87          }
88  
89          return value;
90      }
91  
92      /**
93       * Returns the value of the Java system property with the specified
94       * {@code key}, while falling back to the specified default value if
95       * the property access fails.
96       *
97       * @return the property value.
98       *         {@code def} if there's no such property or if an access to the
99       *         specified property is not allowed.
100      */
101     public static boolean getBoolean(String key, boolean def) {
102         String value = get(key);
103         if (value == null) {
104             return def;
105         }
106 
107         value = value.trim().toLowerCase();
108         if (value.length() == 0) {
109             return true;
110         }
111 
112         if ("true".equals(value) || "yes".equals(value) || "1".equals(value)) {
113             return true;
114         }
115 
116         if ("false".equals(value) || "no".equals(value) || "0".equals(value)) {
117             return false;
118         }
119 
120         log(
121                 "Unable to parse the boolean system property '" + key + "':" + value + " - " +
122                         "using the default value: " + def);
123 
124         return def;
125     }
126 
127     private static final Pattern INTEGER_PATTERN = Pattern.compile("-?[0-9]+");
128 
129     /**
130      * Returns the value of the Java system property with the specified
131      * {@code key}, while falling back to the specified default value if
132      * the property access fails.
133      *
134      * @return the property value.
135      *         {@code def} if there's no such property or if an access to the
136      *         specified property is not allowed.
137      */
138     public static int getInt(String key, int def) {
139         String value = get(key);
140         if (value == null) {
141             return def;
142         }
143 
144         value = value.trim().toLowerCase();
145         if (INTEGER_PATTERN.matcher(value).matches()) {
146             try {
147                 return Integer.parseInt(value);
148             } catch (Exception e) {
149                 // Ignore
150             }
151         }
152 
153         log(
154                 "Unable to parse the integer system property '" + key + "':" + value + " - " +
155                         "using the default value: " + def);
156 
157         return def;
158     }
159 
160     /**
161      * Returns the value of the Java system property with the specified
162      * {@code key}, while falling back to the specified default value if
163      * the property access fails.
164      *
165      * @return the property value.
166      *         {@code def} if there's no such property or if an access to the
167      *         specified property is not allowed.
168      */
169     public static long getLong(String key, long def) {
170         String value = get(key);
171         if (value == null) {
172             return def;
173         }
174 
175         value = value.trim().toLowerCase();
176         if (INTEGER_PATTERN.matcher(value).matches()) {
177             try {
178                 return Long.parseLong(value);
179             } catch (Exception e) {
180                 // Ignore
181             }
182         }
183 
184         log(
185                 "Unable to parse the long integer system property '" + key + "':" + value + " - " +
186                         "using the default value: " + def);
187 
188         return def;
189     }
190 
191     private static void log(String msg) {
192         if (initializedLogger) {
193             logger.warn(msg);
194         } else {
195             // Use JDK logging if logger was not initialized yet.
196             Logger.getLogger(SystemPropertyUtil.class.getName()).log(Level.WARNING, msg);
197         }
198     }
199 
200     private static void log(String msg, Exception e) {
201         if (initializedLogger) {
202             logger.warn(msg, e);
203         } else {
204             // Use JDK logging if logger was not initialized yet.
205             Logger.getLogger(SystemPropertyUtil.class.getName()).log(Level.WARNING, msg, e);
206         }
207     }
208 
209     private SystemPropertyUtil() {
210         // Unused
211     }
212 }