KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Constructor JavaDoc;
25 import java.io.FilePermission JavaDoc;
26 import java.security.AllPermission JavaDoc;
27 import java.security.PermissionCollection JavaDoc;
28 import java.security.Permissions JavaDoc;
29 import java.security.Policy JavaDoc;
30
31 /**
32  * A Security Policy Plugin.
33  *
34  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 43809 $
37  */

38 public abstract class PolicyPlugin extends Policy JavaDoc
39 {
40    /** No permissions */
41    private static PermissionCollection JavaDoc none;
42
43    /** All file permissions */
44    private static PermissionCollection JavaDoc fileRead;
45
46    /** All permissions */
47    private static PermissionCollection JavaDoc all;
48
49    /**
50     * Get the security plugin. This queries for the sytem property
51     * org.jboss.test.security.PolicyPlugin to determine the PolicyPlugin
52     * implementation class. If no such property exist the default
53     * org.jboss.test.security.TestsPolicyPlugin implementation is used.
54     *
55     * @see PolicyPlugin
56     * @see TestsPolicyPlugin
57     *
58     * @param clazz - the unit testcase class
59     * @return the security policy plugin
60     * @throws Exception for any error
61     */

62    public static PolicyPlugin getInstance(Class JavaDoc clazz)
63       throws Exception JavaDoc
64    {
65       String JavaDoc policyClassName = System.getProperty("org.jboss.test.security.PolicyPlugin",
66          "org.jboss.test.security.TestsPolicyPlugin");
67       Class JavaDoc policyClass = Thread.currentThread().getContextClassLoader().loadClass(policyClassName);
68       Class JavaDoc[] sig = {Class JavaDoc.class};
69       Constructor JavaDoc ctor = policyClass.getConstructor(sig);
70       Object JavaDoc[] args = {clazz};
71       return (PolicyPlugin) ctor.newInstance(args);
72    }
73
74    /**
75     * No-op implementation
76     */

77    public void refresh()
78    {
79    }
80
81    /**
82     * The empty Permissions none.
83     * @return none class ivar
84     */

85    protected PermissionCollection JavaDoc noPermissions()
86    {
87       if (none == null)
88          none = new Permissions JavaDoc();
89       return none;
90    }
91
92    /**
93     * Create a PermissionCollection with read for all files permission
94     * FilePermission("<<ALL FILES>>", "read")
95     * @return the fileRead class ivar
96     */

97    protected PermissionCollection JavaDoc fileReadPermissions()
98    {
99       if (fileRead == null)
100       {
101          fileRead = new Permissions JavaDoc();
102          fileRead.add(new FilePermission JavaDoc("<<ALL FILES>>", "read"));
103       }
104       return fileRead;
105    }
106
107    /**
108     * A PermissionCollection with the special AllPermission that enables
109     * all access.
110     *
111     * @see AllPermission
112     * @return the all class ivar
113     */

114    protected PermissionCollection JavaDoc allPermissions()
115    {
116       if (all == null)
117       {
118          all = new Permissions JavaDoc();
119          all.add(new AllPermission JavaDoc());
120       }
121       return all;
122    }
123 }
124
Popular Tags