KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > TestsPolicyPlugin


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.test.security;
23
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.security.CodeSource JavaDoc;
27 import java.security.PermissionCollection JavaDoc;
28 import java.security.Permission JavaDoc;
29 import java.security.ProtectionDomain JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.lang.reflect.Constructor JavaDoc;
36
37 import org.jboss.test.visitor.TypeHierarchyTraversal;
38 import org.jboss.test.visitor.PropertiesVisitorImpl;
39
40 /**
41  * A Test PolicyPlugin.
42  *
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 56504 $
45  */

46 public class TestsPolicyPlugin extends PolicyPlugin
47 {
48    private HashSet JavaDoc<Permission JavaDoc> classPermissions = new HashSet JavaDoc<Permission JavaDoc>();
49
50    private static final URL JavaDoc codeSourceLocation;
51    
52    static
53    {
54       URL JavaDoc temp = null;
55       ProtectionDomain JavaDoc pd = TestsPolicyPlugin.class.getProtectionDomain();
56       if (pd != null)
57       {
58          CodeSource JavaDoc cs = pd.getCodeSource();
59          if (cs != null)
60          {
61             temp = cs.getLocation();
62          }
63       }
64       codeSourceLocation = temp;
65    }
66    
67    /**
68     * This ctor scans for properties using the TypeHierarchyTraversal and
69     * PropertiesVisitorImpl to pickup testclass specific permissions. Only
70     * class/interfaces properties are currently consulted for properties of
71     * the form 'test.Permission.N' where N=[0-9]+
72     * The value format of the test.Permission.N property is:
73     * perm-class, name [, actions]
74     * which conforms to the BasicPermission(String name, String actions) and
75     * BasicPermission(String name) sigs.
76     * @param clazz
77     */

78    public TestsPolicyPlugin(Class JavaDoc clazz)
79    {
80       // Augment the policy with testcase clazz data
81
PropertiesVisitorImpl visitor = new PropertiesVisitorImpl();
82       TypeHierarchyTraversal.visit(clazz, visitor);
83       HashMap JavaDoc<Class JavaDoc, Properties JavaDoc> typeProperties = visitor.getTypeProperties();
84       Iterator JavaDoc<Properties JavaDoc> iter = typeProperties.values().iterator();
85       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
86       while( iter.hasNext() )
87       {
88          Properties JavaDoc props = iter.next();
89          Enumeration JavaDoc names = props.propertyNames();
90          while( names.hasMoreElements() )
91          {
92             String JavaDoc name = (String JavaDoc) names.nextElement();
93             // Any test.Permission.N is what we are looking for
94
if( name.matches("test.Permission.[0-9]+") )
95             {
96                // Permission value syntax is perm-class, name [, actions]
97
String JavaDoc value = props.getProperty(name);
98                String JavaDoc[] info = value.split(", ");
99                try
100                {
101                   // Create the permission based on the number of args
102
Class JavaDoc pc = loader.loadClass(info[0]);
103                   Permission JavaDoc p;
104                   if( info.length == 1 )
105                   {
106                      p = (Permission JavaDoc) pc.newInstance();
107                   }
108                   else if( info.length == 2 )
109                   {
110                      Class JavaDoc[] sig = {String JavaDoc.class};
111                      Object JavaDoc[] args = {info[1]};
112                      Constructor JavaDoc ctor = pc.getConstructor(sig);
113                      p = (Permission JavaDoc) ctor.newInstance(args);
114                   }
115                   else
116                   {
117                      Class JavaDoc[] sig = {String JavaDoc.class, String JavaDoc.class};
118                      Object JavaDoc[] args = {info[1], info[2]};
119                      Constructor JavaDoc ctor = pc.getConstructor(sig);
120                      p = (Permission JavaDoc) ctor.newInstance(args);
121                   }
122                   classPermissions.add(p);
123                }
124                catch(ClassNotFoundException JavaDoc e)
125                {
126                   // We could break out ClassNotFoundException to create lazy loaded...
127
}
128                catch(Exception JavaDoc e)
129                {
130                   e.printStackTrace();
131                }
132             }
133          }
134       }
135    }
136
137    public PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource)
138    {
139       URL JavaDoc url = codesource.getLocation();
140       if (url != null)
141       {
142          // Is this us?
143
if (url.equals(codeSourceLocation))
144             return allPermissions();
145          
146          // Is this a test location?
147
File JavaDoc file = new File JavaDoc(url.toString());
148          String JavaDoc name = file.getName();
149          if (name.indexOf("tests") != -1 || name.indexOf("-test.jar") != -1)
150          {
151             // @TODO: Make configurable
152
PermissionCollection JavaDoc pc = fileReadPermissions();
153             // Needed for the class loading tests
154
pc.add(new RuntimePermission JavaDoc("createClassLoader"));
155             pc.add(new RuntimePermission JavaDoc("getProtectionDomain"));
156             // Add any class declared permissions
157
Iterator JavaDoc iter = classPermissions.iterator();
158             while( iter.hasNext() )
159             {
160                Permission JavaDoc p = (Permission JavaDoc) iter.next();
161                pc.add(p);
162             }
163             return pc;
164          }
165       }
166       return allPermissions();
167    }
168 }
169
Popular Tags