KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > loading > ClassToStringAction


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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 JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.CodeSource JavaDoc;
27
28 /** An encapsulation creating a to string rep for a class using a
29  * PrivilegedAction for getting the ProtectionDomain.
30  *
31  * @version $Revision: 44794 $
32  * @author Scott.Stark@jboss.org
33  */

34 class ClassToStringAction implements PrivilegedAction JavaDoc
35 {
36    private StringBuffer JavaDoc buffer;
37    private Class JavaDoc clazz;
38    ClassToStringAction(Class JavaDoc clazz, StringBuffer JavaDoc buffer)
39    {
40       this.clazz = clazz;
41       this.buffer = buffer;
42    }
43    public Object JavaDoc run()
44    {
45       if( clazz != null )
46       {
47          buffer.append(clazz.getName());
48          buffer.append("@"+Integer.toHexString(clazz.hashCode()));
49          CodeSource JavaDoc cs = clazz.getProtectionDomain().getCodeSource();
50          buffer.append("<CodeSource: "+cs+">");
51       }
52       else
53       {
54          buffer.append("null");
55       }
56       return null;
57    }
58
59    static void toString(Class JavaDoc clazz, StringBuffer JavaDoc buffer)
60    {
61       PrivilegedAction JavaDoc action = new ClassToStringAction(clazz, buffer);
62       AccessController.doPrivileged(action);
63    }
64
65    static class SysPropertyAction implements PrivilegedAction JavaDoc
66    {
67       private String JavaDoc key;
68       private String JavaDoc def;
69       SysPropertyAction(String JavaDoc key, String JavaDoc def)
70       {
71          this.key = key;
72          this.def = def;
73       }
74       public Object JavaDoc run()
75       {
76          return System.getProperty(key, def);
77       }
78    }
79    static String JavaDoc getProperty(String JavaDoc key, String JavaDoc def)
80    {
81       PrivilegedAction JavaDoc action = new SysPropertyAction(key, def);
82       String JavaDoc value = (String JavaDoc) AccessController.doPrivileged(action);
83       return value;
84    }
85 }
86
Popular Tags