KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > security > PolicyImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source 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, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.security;
30
31 import com.caucho.loader.EnvironmentClassLoader;
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import java.security.*;
36 import java.util.ArrayList JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Defines the policy for the current security context.
41  */

42 public class PolicyImpl extends Policy {
43   private static Logger JavaDoc _log;
44   private static L10N _L;
45
46   private static final PolicyImpl _policy = new PolicyImpl();
47
48   private ClassLoader JavaDoc _systemClassLoader;
49   private Policy _parent;
50
51   private PolicyImpl()
52   {
53     _parent = Policy.getPolicy();
54     _systemClassLoader = ClassLoader.getSystemClassLoader();
55   }
56
57   public static PolicyImpl getPolicy()
58   {
59     return _policy;
60   }
61
62   public static void init()
63   {
64     Policy.setPolicy(_policy);
65   }
66
67   public PermissionCollection getPermissions(CodeSource codesource)
68   {
69     PermissionCollection perms = new Permissions();
70     perms.add(new AllPermission());
71     
72     return perms;
73   }
74
75   public PermissionCollection getPermissions(ProtectionDomain domain)
76   {
77     PermissionCollection perms = new Permissions();
78     perms.add(new AllPermission());
79     
80     return perms;
81   }
82
83   public boolean implies(ProtectionDomain domain, Permission permission)
84   {
85     /*
86     if (domain == null)
87       return true; // handle null value passed from RMI
88     */

89     
90     ClassLoader JavaDoc loader = domain.getClassLoader();
91
92     if (loader == _systemClassLoader)
93       return true;
94
95     // XXX: temporary to restore default security-manager
96
if (true)
97       return true;
98     if (true && _parent != null)
99       return _parent.implies(domain, permission);
100     else if (true)
101       return true;
102
103     for (; loader != null; loader = loader.getParent()) {
104       if (loader instanceof EnvironmentClassLoader) {
105     EnvironmentClassLoader envLoader;
106     envLoader = (EnvironmentClassLoader) loader;
107
108     ArrayList JavaDoc<Permission> perms = envLoader.getPermissions();
109
110     if (perms == null)
111       return _parent.implies(domain, permission);
112     
113     for (int i = perms.size() - 1; i >= 0; i--) {
114       Permission perm = perms.get(i);
115
116       if (permission.implies(perm))
117         return true;
118     }
119     
120     return _parent.implies(domain, permission);
121       }
122     }
123
124     if (loader == null)
125       return true;
126     
127     return true;
128   }
129
130   public void refresh()
131   {
132   }
133
134   private Logger JavaDoc log()
135   {
136     if (_log == null)
137       _log = Log.open(PolicyImpl.class);
138
139     return _log;
140   }
141
142   private L10N L()
143   {
144     if (_L == null)
145       _L = new L10N(PolicyImpl.class);
146
147     return _L;
148   }
149
150   public String JavaDoc toString()
151   {
152     return "PolicyImpl[]";
153   }
154 }
155
Popular Tags