Example usage for java.lang SecurityManager SecurityManager

List of usage examples for java.lang SecurityManager SecurityManager

Introduction

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

Prototype

public SecurityManager() 

Source Link

Document

Constructs a new SecurityManager.

Usage

From source file:AuthorizedFileWriter.java

public static void main(String[] args) {
    System.setSecurityManager(new SecurityManager());
    String file = "authorized.txt";
    String fileBody = "test";
    try {// ww w. j a  v a  2 s .c  o m
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(fileBody);
        fileWriter.close();
        System.exit(0);
    } catch (IOException ioException) {
        ioException.printStackTrace();
        System.exit(1);
    }
}

From source file:Main.java

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

    SecurityManager sm = new SecurityManager();

    System.setSecurityManager(sm);

    // perform the check
    sm.checkCreateClassLoader();/*from  ww w  .j  a v a 2 s .  c o  m*/
    System.out.println("Allowed!");
}

From source file:Main.java

public static void main(String[] args) {
    AccessControlContext acc = AccessController.getContext();

    System.setProperty("java.security.policy", "file:/C:/java.policy");

    SecurityManager sm = new SecurityManager();

    System.setSecurityManager(sm);

    // perform the check
    sm.checkConnect("www.java2s.com", 8080, acc);

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

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();
    }/*from w  w w .j  a  v a2s . c  om*/
}

From source file:security.SecurityExample.java

public static void main(String[] args) {
    SecurityManager manager = new SecurityManager();
    SecureBean bean = getSecureBean();/* www  .  j a  v a  2s. c o m*/
    manager.login("clarence", "pwd");
    bean.writeSecureMessage();
    manager.logout();
    try {
        manager.login("bilbo", "mylittletreasure");
        bean.writeSecureMessage();
    } catch (SecurityException ex) {
        System.out.println("Exception caught: " + ex.getMessage());
    } finally {
        manager.logout();
    }
    try {
        bean.writeSecureMessage();
    } catch (SecurityException ex) {
        System.out.println("Exception caught: " + ex.getMessage());
    }
}

From source file:client.ComputePi.java

public static void main(String args[]) {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }// ww w . j a  va  2s.  com
    try {
        String name = "Compute";
        Registry registry = LocateRegistry.getRegistry(args[0]);
        Compute comp = (Compute) registry.lookup(name);
        Pi task = new Pi(Integer.parseInt(args[1]));
        BigDecimal pi = comp.executeTask(task);
        System.out.println(pi);
    } catch (Exception e) {
        System.err.println("ComputePi exception:");
        e.printStackTrace();
    }
}

From source file:engine.ComputeEngine.java

public static void main(String[] args) {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }//from   www . ja v a  2 s.com
    try {
        String name = "Compute";
        Compute engine = new ComputeEngine();
        Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind(name, stub);
        System.out.println("ComputeEngine bound");
    } catch (Exception e) {
        System.err.println("ComputeEngine exception:");
        e.printStackTrace();
    }
}

From source file:azkaban.soloserver.AzkabanSingleServer.java

public static void main(String[] args) throws Exception {
    log.info("Starting Azkaban Server");

    if (System.getSecurityManager() == null) {
        Policy.setPolicy(new Policy() {
            @Override//from w ww  .  j a  v a2  s . c  o m
            public boolean implies(final ProtectionDomain domain, final Permission permission) {
                return true; // allow all
            }
        });
        System.setSecurityManager(new SecurityManager());
    }

    if (args.length == 0) {
        args = prepareDefaultConf();
    }

    final Props props = AzkabanServer.loadProps(args);
    if (props == null) {
        log.error("Properties not found. Need it to connect to the db.");
        log.error("Exiting...");
        return;
    }

    if (props.getBoolean(AzkabanDatabaseSetup.DATABASE_CHECK_VERSION, true)) {
        final boolean updateDB = props.getBoolean(AzkabanDatabaseSetup.DATABASE_AUTO_UPDATE_TABLES, true);
        final String scriptDir = props.getString(AzkabanDatabaseSetup.DATABASE_SQL_SCRIPT_DIR, "sql");
        AzkabanDatabaseUpdater.runDatabaseUpdater(props, scriptDir, updateDB);
    }

    /* Initialize Guice Injector */
    final Injector injector = Guice.createInjector(new AzkabanCommonModule(props), new AzkabanWebServerModule(),
            new AzkabanExecServerModule());
    SERVICE_PROVIDER.setInjector(injector);

    /* Launch server */
    injector.getInstance(AzkabanSingleServer.class).launch();
}

From source file:edu.cornell.med.icb.R.RConfigurationServer.java

public static void main(final String[] args) throws RemoteException {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }/*from w w  w. j a v  a  2 s  .  c  om*/

    final String name = "RConfiguration";
    final RConfiguration engine = new RConfigurationServer();
    final RConfiguration stub = (RConfiguration) UnicastRemoteObject.exportObject(engine, 0);
    final Registry registry = LocateRegistry.getRegistry();
    System.out.println(ArrayUtils.toString(registry.list()));
    registry.rebind(name, stub);
    System.out.println("RConfiguration bound");
}

From source file:engine.Pi.java

public static void main(String args[]) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }//from  ww w .j  av a  2s . co m
        try {
            String name = "Compute";
            Registry registry = LocateRegistry.getRegistry(args[0]);
            Compute comp = (Compute) registry.lookup(name);
            Pi task = new Pi(Integer.parseInt(args[1]));
            BigDecimal pi = comp.executeTask(task);
            System.out.println(pi);
        } catch (Exception e) {
            System.err.println("ComputePi exception:");
            e.printStackTrace();
        }
    }