KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > authorization > policy > JGuardPolicy


1 /*
2  jGuard is a security framework based on top of jaas (java authentication and authorization security).
3  it is written for web applications, to resolve simply, access control problems.
4  version $Name$
5  http://sourceforge.net/projects/jguard/
6
7  Copyright (C) 2004 Charles GAY
8
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26
27  */

28 package net.sf.jguard.core.authorization.policy;
29
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 import java.security.CodeSource JavaDoc;
36 import java.security.PermissionCollection JavaDoc;
37 import java.security.Policy JavaDoc;
38 import java.security.ProtectionDomain JavaDoc;
39
40 import java.util.Properties JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44
45 /**
46  * JGuard Policy abstract implementation.
47  * @see net.sf.jguard.core.authorization.policy.AbstractMultipleAppPolicy
48  * @see net.sf.jguard.core.authorization.policy.MultipleAppPolicy
49  * @see net.sf.jguard.ext.authorization.policy.classic.SingleAppPolicy
50  * @author <a HREF="mailto:diabolo512@users.sourceforge.net ">Charles Gay</a>
51  * @author <a HREF="mailto:vberetti@users.sourceforge.net">Vincent Beretti</a>
52  */

53 public abstract class JGuardPolicy extends java.security.Policy JavaDoc {
54
55     private static final String JavaDoc LIB = "lib";
56     private static final String JavaDoc SECURITY = "security";
57     private static final String JavaDoc J_GUARD_POLICY = "jGuard.policy";
58     private static final String JavaDoc JGUARD_POLICY_LOCATION = File.separator + JGuardPolicy.LIB + File.separator + JGuardPolicy.SECURITY + File.separator + JGuardPolicy.J_GUARD_POLICY;
59     private static final String JavaDoc DEFAULT_POLICY = "defaultPolicy";
60     private static final String JavaDoc JAVA_HOME = "java.home";
61     //old Policy instance replaced by JGuardPolicy
62
protected static Policy JavaDoc defaultPolicy;
63     //old Policy instance Class replaced by JGuardPolicy
64
private static Class JavaDoc policyClass;
65     private static Logger JavaDoc logger = Logger.getLogger(JGuardPolicy.class.getName());
66     protected final static String JavaDoc version = "1.0.0";
67
68
69     /**
70      * load the default Policy implementation class.
71      */

72     protected void loadDefaultPolicy() {
73         //the securityManager is not set
74
if (System.getSecurityManager() == null) {
75             String JavaDoc javaHome = System.getProperty(JGuardPolicy.JAVA_HOME);
76             Properties JavaDoc props = new Properties JavaDoc();
77             String JavaDoc defPolicy = null;
78
79             try {
80                 props.load(new FileInputStream JavaDoc(new File JavaDoc(javaHome + JGuardPolicy.JGUARD_POLICY_LOCATION)));
81                 defPolicy = props.getProperty(JGuardPolicy.DEFAULT_POLICY);
82             } catch (FileNotFoundException JavaDoc e) {
83                 logger.log(Level.CONFIG, "loadDefaultPolicy() - jGuard.policy is not found " + e.getMessage());
84             } catch (IOException JavaDoc e) {
85                 logger.log(Level.CONFIG, "loadDefaultPolicy() - jGuard.policy is not reachable " + e.getMessage());
86             }
87
88             try {
89
90                 if(defPolicy==null){
91                     logger.log(Level.CONFIG,"loadDefaultPolicy() - 'defaultPolicy' field in the jGuard.Policy file is not defined ");
92                     logger.log(Level.CONFIG,"loadDefaultPolicy() - jGuard try to discover the default one ");
93                     // we search the default policy class
94
policyClass = PolicyHelper.findDefaultPolicy();
95                 } else {
96                     // we use the defined default policy class
97
policyClass = Class.forName(defPolicy);
98                 }
99             } catch (ClassNotFoundException JavaDoc e1) {
100                 logger.log(Level.CONFIG, "loadDefaultPolicy() - the default policy class cannot be found " + e1.getMessage());
101             }
102
103             //the securityManager is set
104
}else{
105             policyClass = PolicyHelper.findDefaultPolicy();
106         }
107
108         try {
109             defaultPolicy = (Policy JavaDoc)policyClass.newInstance();
110         } catch (InstantiationException JavaDoc e2) {
111             logger.log(Level.CONFIG,"loadDefaultPolicy() - the default policy class cannot be instantiated"
112                     + e2.getMessage());
113         } catch (IllegalAccessException JavaDoc e2) {
114             logger.log(Level.CONFIG,"loadDefaultPolicy() - the default policy class cannot be accessed "
115                     + e2.getMessage());
116         }
117     }
118
119     /**
120      * JGuard Policy act as a wrapper for this method.
121      * it delegates to default's Policy implementation defined in Jguard.policy file, this method.
122      * @see java.security.Policy#getPermissions(java.security.CodeSource)
123      * @param codesource
124      * @return all the permissions own by the CodeSource
125      */

126     public PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource) {
127         PermissionCollection JavaDoc permColl = defaultPolicy.getPermissions(codesource);
128         return permColl;
129     }
130
131     public abstract PermissionCollection JavaDoc getPermissions(ProtectionDomain JavaDoc protectionDomain);
132
133     public abstract void refresh();
134 }
135
Popular Tags