author | weijun |
Tue, 21 Feb 2012 15:11:27 +0800 | |
changeset 11912 | 49b066b32693 |
parent 10788 | 680a3dbfcaba |
child 13796 | 792402bbac8d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10788
680a3dbfcaba
7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents:
6122
diff
changeset
|
2 |
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.util; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
import java.util.regex.Pattern; |
|
30 |
import java.util.regex.Matcher; |
|
6122
16fa7ed7ff1b
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents:
5506
diff
changeset
|
31 |
import java.util.Locale; |
2 | 32 |
|
33 |
/** |
|
34 |
* A utility class for debuging. |
|
35 |
* |
|
36 |
* @author Roland Schemers |
|
37 |
*/ |
|
38 |
public class Debug { |
|
39 |
||
40 |
private String prefix; |
|
41 |
||
42 |
private static String args; |
|
43 |
||
44 |
static { |
|
45 |
args = java.security.AccessController.doPrivileged |
|
46 |
(new sun.security.action.GetPropertyAction |
|
47 |
("java.security.debug")); |
|
48 |
||
49 |
String args2 = java.security.AccessController.doPrivileged |
|
50 |
(new sun.security.action.GetPropertyAction |
|
51 |
("java.security.auth.debug")); |
|
52 |
||
53 |
if (args == null) { |
|
54 |
args = args2; |
|
55 |
} else { |
|
56 |
if (args2 != null) |
|
57 |
args = args + "," + args2; |
|
58 |
} |
|
59 |
||
60 |
if (args != null) { |
|
61 |
args = marshal(args); |
|
62 |
if (args.equals("help")) { |
|
63 |
Help(); |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
public static void Help() |
|
69 |
{ |
|
70 |
System.err.println(); |
|
71 |
System.err.println("all turn on all debugging"); |
|
72 |
System.err.println("access print all checkPermission results"); |
|
73 |
System.err.println("combiner SubjectDomainCombiner debugging"); |
|
74 |
System.err.println("gssloginconfig"); |
|
75 |
System.err.println("configfile JAAS ConfigFile loading"); |
|
76 |
System.err.println("configparser JAAS ConfigFile parsing"); |
|
77 |
System.err.println(" GSS LoginConfigImpl debugging"); |
|
78 |
System.err.println("jar jar verification"); |
|
79 |
System.err.println("logincontext login context results"); |
|
80 |
System.err.println("policy loading and granting"); |
|
81 |
System.err.println("provider security provider debugging"); |
|
82 |
System.err.println("scl permissions SecureClassLoader assigns"); |
|
10788
680a3dbfcaba
7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents:
6122
diff
changeset
|
83 |
System.err.println("ts timestamping"); |
2 | 84 |
System.err.println(); |
85 |
System.err.println("The following can be used with access:"); |
|
86 |
System.err.println(); |
|
87 |
System.err.println("stack include stack trace"); |
|
88 |
System.err.println("domain dump all domains in context"); |
|
89 |
System.err.println("failure before throwing exception, dump stack"); |
|
90 |
System.err.println(" and domain that didn't have permission"); |
|
91 |
System.err.println(); |
|
92 |
System.err.println("The following can be used with stack and domain:"); |
|
93 |
System.err.println(); |
|
94 |
System.err.println("permission=<classname>"); |
|
95 |
System.err.println(" only dump output if specified permission"); |
|
96 |
System.err.println(" is being checked"); |
|
97 |
System.err.println("codebase=<URL>"); |
|
98 |
System.err.println(" only dump output if specified codebase"); |
|
99 |
System.err.println(" is being checked"); |
|
100 |
||
101 |
System.err.println(); |
|
102 |
System.err.println("Note: Separate multiple options with a comma"); |
|
103 |
System.exit(0); |
|
104 |
} |
|
105 |
||
106 |
||
107 |
/** |
|
108 |
* Get a Debug object corresponding to whether or not the given |
|
109 |
* option is set. Set the prefix to be the same as option. |
|
110 |
*/ |
|
111 |
||
112 |
public static Debug getInstance(String option) |
|
113 |
{ |
|
114 |
return getInstance(option, option); |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Get a Debug object corresponding to whether or not the given |
|
119 |
* option is set. Set the prefix to be prefix. |
|
120 |
*/ |
|
121 |
public static Debug getInstance(String option, String prefix) |
|
122 |
{ |
|
123 |
if (isOn(option)) { |
|
124 |
Debug d = new Debug(); |
|
125 |
d.prefix = prefix; |
|
126 |
return d; |
|
127 |
} else { |
|
128 |
return null; |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* True if the system property "security.debug" contains the |
|
134 |
* string "option". |
|
135 |
*/ |
|
136 |
public static boolean isOn(String option) |
|
137 |
{ |
|
138 |
if (args == null) |
|
139 |
return false; |
|
140 |
else { |
|
141 |
if (args.indexOf("all") != -1) |
|
142 |
return true; |
|
143 |
else |
|
144 |
return (args.indexOf(option) != -1); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* print a message to stderr that is prefixed with the prefix |
|
150 |
* created from the call to getInstance. |
|
151 |
*/ |
|
152 |
||
153 |
public void println(String message) |
|
154 |
{ |
|
155 |
System.err.println(prefix + ": "+message); |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* print a blank line to stderr that is prefixed with the prefix. |
|
160 |
*/ |
|
161 |
||
162 |
public void println() |
|
163 |
{ |
|
164 |
System.err.println(prefix + ":"); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* print a message to stderr that is prefixed with the prefix. |
|
169 |
*/ |
|
170 |
||
171 |
public static void println(String prefix, String message) |
|
172 |
{ |
|
173 |
System.err.println(prefix + ": "+message); |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* return a hexadecimal printed representation of the specified |
|
178 |
* BigInteger object. the value is formatted to fit on lines of |
|
179 |
* at least 75 characters, with embedded newlines. Words are |
|
180 |
* separated for readability, with eight words (32 bytes) per line. |
|
181 |
*/ |
|
182 |
public static String toHexString(BigInteger b) { |
|
183 |
String hexValue = b.toString(16); |
|
184 |
StringBuffer buf = new StringBuffer(hexValue.length()*2); |
|
185 |
||
186 |
if (hexValue.startsWith("-")) { |
|
187 |
buf.append(" -"); |
|
188 |
hexValue = hexValue.substring(1); |
|
189 |
} else { |
|
190 |
buf.append(" "); // four spaces |
|
191 |
} |
|
192 |
if ((hexValue.length()%2) != 0) { |
|
193 |
// add back the leading 0 |
|
194 |
hexValue = "0" + hexValue; |
|
195 |
} |
|
196 |
int i=0; |
|
197 |
while (i < hexValue.length()) { |
|
198 |
// one byte at a time |
|
199 |
buf.append(hexValue.substring(i, i+2)); |
|
200 |
i+=2; |
|
201 |
if (i!= hexValue.length()) { |
|
202 |
if ((i%64) == 0) { |
|
203 |
buf.append("\n "); // line after eight words |
|
204 |
} else if (i%8 == 0) { |
|
205 |
buf.append(" "); // space between words |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
return buf.toString(); |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* change a string into lower case except permission classes and URLs. |
|
214 |
*/ |
|
215 |
private static String marshal(String args) { |
|
216 |
if (args != null) { |
|
217 |
StringBuffer target = new StringBuffer(); |
|
218 |
StringBuffer source = new StringBuffer(args); |
|
219 |
||
220 |
// obtain the "permission=<classname>" options |
|
221 |
// the syntax of classname: IDENTIFIER.IDENTIFIER |
|
222 |
// the regular express to match a class name: |
|
223 |
// "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*" |
|
224 |
String keyReg = "[Pp][Ee][Rr][Mm][Ii][Ss][Ss][Ii][Oo][Nn]="; |
|
225 |
String keyStr = "permission="; |
|
226 |
String reg = keyReg + |
|
227 |
"[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*"; |
|
228 |
Pattern pattern = Pattern.compile(reg); |
|
229 |
Matcher matcher = pattern.matcher(source); |
|
230 |
StringBuffer left = new StringBuffer(); |
|
231 |
while (matcher.find()) { |
|
232 |
String matched = matcher.group(); |
|
233 |
target.append(matched.replaceFirst(keyReg, keyStr)); |
|
234 |
target.append(" "); |
|
235 |
||
236 |
// delete the matched sequence |
|
237 |
matcher.appendReplacement(left, ""); |
|
238 |
} |
|
239 |
matcher.appendTail(left); |
|
240 |
source = left; |
|
241 |
||
242 |
// obtain the "codebase=<URL>" options |
|
243 |
// the syntax of URL is too flexible, and here assumes that the |
|
244 |
// URL contains no space, comma(','), and semicolon(';'). That |
|
245 |
// also means those characters also could be used as separator |
|
246 |
// after codebase option. |
|
247 |
// However, the assumption is incorrect in some special situation |
|
248 |
// when the URL contains comma or semicolon |
|
249 |
keyReg = "[Cc][Oo][Dd][Ee][Bb][Aa][Ss][Ee]="; |
|
250 |
keyStr = "codebase="; |
|
251 |
reg = keyReg + "[^, ;]*"; |
|
252 |
pattern = Pattern.compile(reg); |
|
253 |
matcher = pattern.matcher(source); |
|
254 |
left = new StringBuffer(); |
|
255 |
while (matcher.find()) { |
|
256 |
String matched = matcher.group(); |
|
257 |
target.append(matched.replaceFirst(keyReg, keyStr)); |
|
258 |
target.append(" "); |
|
259 |
||
260 |
// delete the matched sequence |
|
261 |
matcher.appendReplacement(left, ""); |
|
262 |
} |
|
263 |
matcher.appendTail(left); |
|
264 |
source = left; |
|
265 |
||
266 |
// convert the rest to lower-case characters |
|
6122
16fa7ed7ff1b
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents:
5506
diff
changeset
|
267 |
target.append(source.toString().toLowerCase(Locale.ENGLISH)); |
2 | 268 |
|
269 |
return target.toString(); |
|
270 |
} |
|
271 |
||
272 |
return null; |
|
273 |
} |
|
274 |
||
275 |
private final static char[] hexDigits = "0123456789abcdef".toCharArray(); |
|
276 |
||
277 |
public static String toString(byte[] b) { |
|
278 |
if (b == null) { |
|
279 |
return "(null)"; |
|
280 |
} |
|
281 |
StringBuilder sb = new StringBuilder(b.length * 3); |
|
282 |
for (int i = 0; i < b.length; i++) { |
|
283 |
int k = b[i] & 0xff; |
|
284 |
if (i != 0) { |
|
285 |
sb.append(':'); |
|
286 |
} |
|
287 |
sb.append(hexDigits[k >>> 4]); |
|
288 |
sb.append(hexDigits[k & 0xf]); |
|
289 |
} |
|
290 |
return sb.toString(); |
|
291 |
} |
|
292 |
||
293 |
} |