Example usage for java.security Policy getPolicy

List of usage examples for java.security Policy getPolicy

Introduction

In this page you can find the example usage for java.security Policy getPolicy.

Prototype

public static Policy getPolicy() 

Source Link

Document

Returns the installed Policy object.

Usage

From source file:Main.java

public static void main(String args[]) {
    ProtectionDomain domain = "".getClass().getProtectionDomain();

    PermissionCollection pcoll = Policy.getPolicy().getPermissions(domain);

    Enumeration e = pcoll.elements();
    for (; e.hasMoreElements();) {
        Permission p = (Permission) e.nextElement();
        System.out.println(p);/*from w w w  .  j  a va 2 s.c o  m*/

    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ProtectionDomain domain = String.class.getProtectionDomain();
    PermissionCollection pcoll = Policy.getPolicy().getPermissions(domain);
    Enumeration e = pcoll.elements();
    for (; e.hasMoreElements();) {
        Permission p = (Permission) e.nextElement();
    }/* www .jav  a  2 s.  co m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    SecurityManager sm = new SecurityManager();
    System.setSecurityManager(sm);
    URL codebase = new URL("http://java.sun.com/");

    //codebase = new File("c:\\java\\").toURI().toURL();
    //codebase = new File(System.getProperty("user.home")).toURI().toURL();

    CodeSource cs = new CodeSource(codebase, (Certificate[]) null);

    PermissionCollection pcoll = Policy.getPolicy().getPermissions(cs);

    Enumeration e = pcoll.elements();
    for (; e.hasMoreElements();) {
        Permission p = (Permission) e.nextElement();
    }//w  w  w . j a  v  a2 s  .  c om
}

From source file:com.thoughtworks.acceptance.SecurityManagerTest.java

protected void setUp() throws Exception {
    super.setUp();
    System.setSecurityManager(null);
    source = new CodeSource(new File("target").toURI().toURL(), (Certificate[]) null);

    sm = new DynamicSecurityManager();
    Policy policy = Policy.getPolicy();
    sm.setPermissions(source, policy.getPermissions(source));
    sm.addPermission(source, new RuntimePermission("setSecurityManager"));

    File mainClasses = new File(System.getProperty("user.dir"), "target/classes/-");
    File testClasses = new File(System.getProperty("user.dir"), "target/test-classes/-");
    String[] javaClassPath = StringUtils.split(System.getProperty("java.class.path"), File.pathSeparatorChar);
    File javaHome = new File(System.getProperty("java.home"), "-");

    // necessary permission start here
    sm.addPermission(source, new FilePermission(mainClasses.toString(), "read"));
    sm.addPermission(source, new FilePermission(testClasses.toString(), "read"));
    sm.addPermission(source, new FilePermission(javaHome.toString(), "read"));
    for (int i = 0; i < javaClassPath.length; ++i) {
        if (javaClassPath[i].endsWith(".jar")) {
            sm.addPermission(source, new FilePermission(javaClassPath[i], "read"));
        }//  w w  w.ja  va 2  s. c o  m
    }
}

From source file:functionaltests.AbstractRestFuncTestCase.java

private static void configureSecurityManager() {
    if (System.getProperty("java.security.policy") == null) {
        System.setProperty("java.security.policy",
                System.getProperty(PAResourceManagerProperties.RM_HOME.getKey())
                        + "/config/security.java.policy-server");

        Policy.getPolicy().refresh();
    }//  w  w w. j a v  a2  s.co  m
}

From source file:com.stratuscom.harvester.Utils.java

public static void logGrantsToClass(final Logger log, final Level level, final Class c) {
    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        public Object run() {
            ClassLoader cl = c.getClassLoader();
            DynamicPolicyProvider dpp = (DynamicPolicyProvider) Policy.getPolicy();
            Permission[] perms = dpp.getGrants(c, null);
            log.log(level, MessageNames.GRANTS_TO_CLASS_ARE, new Object[] { c.getName(), Utils.format(perms) });
            return null;
        }/*ww  w  .j  a  va2 s  .  c o  m*/
    });
}

From source file:net.sourceforge.safr.jaas.policy.InstancePolicyUtil.java

public static void installInstancePolicy(InstancePolicy ip) {
    Policy current = Policy.getPolicy();
    if (current instanceof InstancePolicy) {
        LOG.info("instance policy already installed");
    } else {//from   w  w  w  .j a v  a  2s.c  o  m
        ip.setDefaultPolicy(current);
        Policy.setPolicy(ip);
        LOG.info("instance policy installed");
    }
}

From source file:net.sourceforge.safr.jaas.policy.InstancePolicyUtil.java

public static void uninstallInstancePolicy() {
    Policy current = Policy.getPolicy();
    if (current instanceof InstancePolicy) {
        InstancePolicy ip = (InstancePolicy) current;
        Policy.setPolicy(ip.getDefaultPolicy());
        LOG.info("instance policy uninstalled");
    } else {/* w  w  w.j  av  a 2  s  .com*/
        LOG.info("instance policy not installed");
    }
}

From source file:org.apache.catalina.loader.WebappClassLoader.java

/**
 * Refresh the system policy file, to pick up eventual changes.
 *//*from   w  w w. j  a  va 2s  . c  om*/
protected void refreshPolicy() {

    try {
        // The policy file may have been modified to adjust 
        // permissions, so we're reloading it when loading or 
        // reloading a Context
        Policy policy = Policy.getPolicy();
        policy.refresh();
    } catch (AccessControlException e) {
        // Some policy files may restrict this, even for the core,
        // so this exception is ignored
    }

}

From source file:org.apache.jasper.compiler.JspRuntimeContext.java

/**
 * Method used to initialize SecurityManager data.
 *//*from   www .j  a  va 2  s .c  om*/
private void initSecurity() {

    // Setup the PermissionCollection for this web app context
    // based on the permissions configured for the root of the
    // web app context directory, then add a file read permission
    // for that directory.
    Policy policy = Policy.getPolicy();
    if (policy != null) {
        try {
            // Get the permissions for the web app context
            String docBase = context.getRealPath("/");
            if (docBase == null) {
                docBase = options.getScratchDir().toString();
            }
            String codeBase = docBase;
            if (!codeBase.endsWith(File.separator)) {
                codeBase = codeBase + File.separator;
            }
            File contextDir = new File(codeBase);
            URL url = contextDir.getCanonicalFile().toURL();
            codeSource = new CodeSource(url, null);
            permissionCollection = policy.getPermissions(codeSource);

            // Create a file read permission for web app context directory
            if (!docBase.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(docBase, "read"));
                docBase = docBase + File.separator;
            } else {
                permissionCollection
                        .add(new FilePermission(docBase.substring(0, docBase.length() - 1), "read"));
            }
            docBase = docBase + "-";
            permissionCollection.add(new FilePermission(docBase, "read"));

            // Create a file read permission for web app tempdir (work)
            // directory
            String workDir = options.getScratchDir().toString();
            if (!workDir.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(workDir, "read"));
                workDir = workDir + File.separator;
            }
            workDir = workDir + "-";
            permissionCollection.add(new FilePermission(workDir, "read"));

            // Allow the JSP to access org.apache.jasper.runtime.HttpJspBase
            permissionCollection.add(new RuntimePermission("accessClassInPackage.org.apache.jasper.runtime"));

            if (parentClassLoader instanceof URLClassLoader) {
                URL[] urls = parentClassLoader.getURLs();
                String jarUrl = null;
                String jndiUrl = null;
                for (int i = 0; i < urls.length; i++) {
                    if (jndiUrl == null && urls[i].toString().startsWith("jndi:")) {
                        jndiUrl = urls[i].toString() + "-";
                    }
                    if (jarUrl == null && urls[i].toString().startsWith("jar:jndi:")) {
                        jarUrl = urls[i].toString();
                        jarUrl = jarUrl.substring(0, jarUrl.length() - 2);
                        jarUrl = jarUrl.substring(0, jarUrl.lastIndexOf('/')) + "/-";
                    }
                }
                if (jarUrl != null) {
                    permissionCollection.add(new FilePermission(jarUrl, "read"));
                    permissionCollection.add(new FilePermission(jarUrl.substring(4), "read"));
                }
                if (jndiUrl != null)
                    permissionCollection.add(new FilePermission(jndiUrl, "read"));
            }
        } catch (Exception e) {
            context.log("Security Init for context failed", e);
        }
    }
}