|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* FROM mail.jar */ |
|
27 package com.sun.xml.internal.org.jvnet.mimepull; |
|
28 |
|
29 import java.util.*; |
|
30 |
|
31 /** |
|
32 * Utilities to make it easier to get property values. |
|
33 * Properties can be strings or type-specific value objects. |
|
34 * |
|
35 * @author Bill Shannon |
|
36 */ |
|
37 final class PropUtil { |
|
38 |
|
39 // No one should instantiate this class. |
|
40 private PropUtil() { |
|
41 } |
|
42 |
|
43 /** |
|
44 * Get a boolean valued System property. |
|
45 */ |
|
46 public static boolean getBooleanSystemProperty(String name, boolean def) { |
|
47 try { |
|
48 return getBoolean(getProp(System.getProperties(), name), def); |
|
49 } catch (SecurityException sex) { |
|
50 // fall through... |
|
51 } |
|
52 |
|
53 /* |
|
54 * If we can't get the entire System Properties object because |
|
55 * of a SecurityException, just ask for the specific property. |
|
56 */ |
|
57 try { |
|
58 String value = System.getProperty(name); |
|
59 if (value == null) { |
|
60 return def; |
|
61 } |
|
62 if (def) { |
|
63 return !value.equalsIgnoreCase("false"); |
|
64 } else { |
|
65 return value.equalsIgnoreCase("true"); |
|
66 } |
|
67 } catch (SecurityException sex) { |
|
68 return def; |
|
69 } |
|
70 } |
|
71 |
|
72 /** |
|
73 * Get the value of the specified property. |
|
74 * If the "get" method returns null, use the getProperty method, |
|
75 * which might cascade to a default Properties object. |
|
76 */ |
|
77 private static Object getProp(Properties props, String name) { |
|
78 Object val = props.get(name); |
|
79 if (val != null) { |
|
80 return val; |
|
81 } else { |
|
82 return props.getProperty(name); |
|
83 } |
|
84 } |
|
85 |
|
86 /** |
|
87 * Interpret the value object as a boolean, |
|
88 * returning def if unable. |
|
89 */ |
|
90 private static boolean getBoolean(Object value, boolean def) { |
|
91 if (value == null) { |
|
92 return def; |
|
93 } |
|
94 if (value instanceof String) { |
|
95 /* |
|
96 * If the default is true, only "false" turns it off. |
|
97 * If the default is false, only "true" turns it on. |
|
98 */ |
|
99 if (def) { |
|
100 return !((String)value).equalsIgnoreCase("false"); |
|
101 } else { |
|
102 return ((String)value).equalsIgnoreCase("true"); |
|
103 } |
|
104 } |
|
105 if (value instanceof Boolean) { |
|
106 return ((Boolean)value).booleanValue(); |
|
107 } |
|
108 return def; |
|
109 } |
|
110 } |