Example usage for java.lang SecurityManager checkPackageDefinition

List of usage examples for java.lang SecurityManager checkPackageDefinition

Introduction

In this page you can find the example usage for java.lang SecurityManager checkPackageDefinition.

Prototype

public void checkPackageDefinition(String pkg) 

Source Link

Document

Throws a SecurityException if the calling thread is not allowed to define classes in the specified package.

Usage

From source file:Main.java

public static void main(String[] args) {
    System.setProperty("java.security.policy", "file:/C:/java.policy");

    SecurityManager sm = new Main();

    System.setSecurityManager(sm);

    sm.checkPackageDefinition("java2s.com");

    System.out.println("Allowed!");
}

From source file:com.sshtools.j2ssh.util.DynamicClassLoader.java

private boolean securityAllowsClass(String className) {
    try {//from  w  ww . j  av a  2 s .  co m
        SecurityManager security = System.getSecurityManager();

        if (security == null) {
            // if there's no security manager then all classes
            // are allowed to be loaded
            return true;
        }

        int lastDot = className.lastIndexOf('.');

        // Check if we are allowed to load the class' package
        security.checkPackageDefinition((lastDot > -1) ? className.substring(0, lastDot) : "");

        // Throws if not allowed
        return true;
    } catch (SecurityException e) {
        return false;
    }
}