Debian Codegraph @

rhino/src/org/mozilla/javascript/SecurityUtilities.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.security.AccessController;
10 import java.security.PrivilegedAction;
11 import java.security.ProtectionDomain;
12
13 /**
14 * @author Attila Szegedi
15 */
16 public class SecurityUtilities
17 {
18 /**
19 * Retrieves a system property within a privileged block. Use it only when
20 * the property is used from within Rhino code and is not passed out of it.
21 * @param name the name of the system property
22 * @return the value of the system property
23 */
24 public static String getSystemProperty(final String name)
25 {
26 return AccessController.doPrivileged(
27 new PrivilegedAction<String>()
28 {
29 public String run()
30 {
31 return System.getProperty(name);
32 }
33 });
34 }
35
36 public static ProtectionDomain getProtectionDomain(final Class<?> clazz)
37 {
38 return AccessController.doPrivileged(
39 new PrivilegedAction<ProtectionDomain>()
40 {
41 public ProtectionDomain run()
42 {
43 return clazz.getProtectionDomain();
44 }
45 });
46 }
47
48 /**
49 * Look up the top-most element in the current stack representing a
50 * script and return its protection domain. This relies on the system-wide
51 * SecurityManager being an instance of {@link RhinoSecurityManager},
52 * otherwise it returns <code>null</code>.
53 * @return The protection of the top-most script in the current stack, or null
54 */
55 public static ProtectionDomain getScriptProtectionDomain() {
56 final SecurityManager securityManager = System.getSecurityManager();
57 if (securityManager instanceof RhinoSecurityManager) {
58 return AccessController.doPrivileged(
59 new PrivilegedAction<ProtectionDomain>() {
60 public ProtectionDomain run() {
61 Class<?> c = ((RhinoSecurityManager) securityManager)
62 .getCurrentScriptClass();
63 return c == null ? null : c.getProtectionDomain();
64 }
65 }
66 );
67 }
68 return null;
69 }
70 }