KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > js > SecurityControllerImpl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 package org.lobobrowser.html.js;
22
23 import org.mozilla.javascript.*;
24 import java.security.*;
25
26 public class SecurityControllerImpl extends SecurityController {
27     private final java.net.URL JavaDoc url;
28     private final java.security.Policy JavaDoc policy;
29     private final CodeSource codesource;
30     
31     public SecurityControllerImpl(java.net.URL JavaDoc url, Policy policy) {
32         this.url = url;
33         this.policy = policy;
34         this.codesource = new CodeSource(this.url, (java.security.cert.Certificate JavaDoc[]) null);
35     }
36     
37     public Object JavaDoc callWithDomain(Object JavaDoc securityDomain, final Context ctx, final Callable callable, final Scriptable scope, final Scriptable thisObj, final Object JavaDoc[] args) {
38         if(securityDomain == null) {
39             return callable.call(ctx, scope, thisObj, args);
40         }
41         else {
42             PrivilegedAction action = new PrivilegedAction() {
43                 public Object JavaDoc run() {
44                     return callable.call(ctx, scope, thisObj, args);
45                 }
46             };
47             AccessControlContext acctx = new AccessControlContext(new ProtectionDomain[] { (ProtectionDomain) securityDomain });
48             return AccessController.doPrivileged(action, acctx);
49         }
50     }
51
52     public GeneratedClassLoader createClassLoader(ClassLoader JavaDoc parent, Object JavaDoc staticDomain) {
53         return new LocalSecureClassLoader(parent);
54     }
55
56     public Object JavaDoc getDynamicSecurityDomain(Object JavaDoc securityDomain) {
57         Policy policy = this.policy;
58         if(policy == null) {
59             return null;
60         }
61         else {
62             PermissionCollection permissions = this.policy.getPermissions(codesource);
63             return new ProtectionDomain(codesource, permissions);
64         }
65     }
66     
67     private class LocalSecureClassLoader extends SecureClassLoader implements GeneratedClassLoader {
68         public LocalSecureClassLoader(ClassLoader JavaDoc parent) {
69             super(parent);
70         }
71
72         public Class JavaDoc defineClass(String JavaDoc name, byte[] b) {
73             return this.defineClass(name, b, 0, b.length, codesource);
74         }
75
76         public void linkClass(Class JavaDoc clazz) {
77             super.resolveClass(clazz);
78         }
79     }
80 }
81
Popular Tags