01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.mx.loading;
23:
24: import java.security.PrivilegedAction;
25: import java.security.AccessController;
26: import java.security.CodeSource;
27:
28: /** An encapsulation creating a to string rep for a class using a
29: * PrivilegedAction for getting the ProtectionDomain.
30: *
31: * @version $Revision: 57200 $
32: * @author Scott.Stark@jboss.org
33: */
34: class ClassToStringAction implements PrivilegedAction {
35: private StringBuffer buffer;
36: private Class clazz;
37:
38: ClassToStringAction(Class clazz, StringBuffer buffer) {
39: this .clazz = clazz;
40: this .buffer = buffer;
41: }
42:
43: public Object run() {
44: if (clazz != null) {
45: buffer.append(clazz.getName());
46: buffer.append("@" + Integer.toHexString(clazz.hashCode()));
47: CodeSource cs = clazz.getProtectionDomain().getCodeSource();
48: buffer.append("<CodeSource: " + cs + ">");
49: } else {
50: buffer.append("null");
51: }
52: return null;
53: }
54:
55: static void toString(Class clazz, StringBuffer buffer) {
56: PrivilegedAction action = new ClassToStringAction(clazz, buffer);
57: AccessController.doPrivileged(action);
58: }
59:
60: static class SysPropertyAction implements PrivilegedAction {
61: private String key;
62: private String def;
63:
64: SysPropertyAction(String key, String def) {
65: this .key = key;
66: this .def = def;
67: }
68:
69: public Object run() {
70: return System.getProperty(key, def);
71: }
72: }
73:
74: static String getProperty(String key, String def) {
75: PrivilegedAction action = new SysPropertyAction(key, def);
76: String value = (String) AccessController.doPrivileged(action);
77: return value;
78: }
79: }
|