Example usage for java.lang System setSecurityManager

List of usage examples for java.lang System setSecurityManager

Introduction

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

Prototype

public static void setSecurityManager(SecurityManager sm) 

Source Link

Document

Sets the system-wide security manager.

Usage

From source file:org.cruxframework.crux.tools.compile.AbstractCruxCompiler.java

/**
 * /*from   w  w w  . j av a 2  s  . c  o m*/
 */
private void restoreSecurityManager() {
    AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
        public Boolean run() {
            System.setSecurityManager(originalSecurityManager);
            return true;
        }
    });
}

From source file:org.cruxframework.crux.tools.compile.AbstractCruxCompiler.java

/**
 * // ww  w.  j a  v a  2s. co  m
 */
private void setSecurityManagerToAvoidSystemExit() {
    AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
        public Boolean run() {
            originalSecurityManager = System.getSecurityManager();
            System.setSecurityManager(new SecurityManager() {

                @Override
                public void checkExit(int status) {
                    if (status == 0) {
                        throw new DoNotExitException();
                    }
                    super.checkExit(status);
                }

                @Override
                public void checkPermission(Permission perm) {
                }

                @Override
                public void checkPermission(Permission perm, Object context) {
                }
            });
            return true;
        }
    });
}

From source file:org.digidoc4j.main.DigiDoc4JTest.java

private static void forbidSystemExitCall() {
    final SecurityManager preventExitSecurityManager = new SecurityManager() {
        public void checkPermission(Permission permission) {
        }//from  w ww  . ja v a2s.c om

        @Override
        public void checkExit(int status) {
            super.checkExit(status);
            throw new DigiDoc4JUtilityException(status, "preventing system exist");
        }
    };
    System.setSecurityManager(preventExitSecurityManager);
}

From source file:org.digidoc4j.main.DigiDoc4JTest.java

void callMainWithoutSystemExit(String[] params) {
    SecurityManager securityManager = System.getSecurityManager();
    forbidSystemExitCall();/*w ww  . j  a v a2 s  .c  o  m*/
    try {
        DigiDoc4J.main(params);
    } catch (DigiDoc4JUtilityException ignore) {
    }
    System.setSecurityManager(securityManager);
}

From source file:org.digidoc4j.testutils.RestrictedFileWritingRule.java

@Override
protected void before() throws Throwable {
    super.before();
    System.setSecurityManager(new SecurityManager() {

        @Override/*from  w  w w  .j ava  2s  . c o  m*/
        public void checkWrite(String file) {
            if (!isAllowedToWrite(file)) {
                throw new FileWritingRestrictedException();
            }
        }

        @Override
        public void checkPermission(Permission perm) {
            return;
        }
    });
}

From source file:org.digidoc4j.testutils.RestrictedFileWritingRule.java

@Override
protected void after() {
    System.setSecurityManager(null); // or save and restore original
    super.after();
}

From source file:org.docx4all.ui.main.WordMLEditor.java

public static void main(String[] args) {

    //      // Whenever we flush Preferences on Linux, we need *not* to use
    //      // our Xalan jar (which we need 
    //      javax.xml.transform.TransformerFactory tfactory = javax.xml.transform.TransformerFactory.newInstance();         
    //      TRANSFORMER_FACTORY_DEFAULT = tfactory.getClass().getName();
    //      log.debug("Set TRANSFORMER_FACTORY_DEFAULT to " + TRANSFORMER_FACTORY_DEFAULT);

    // If we launch via JNLP with Java 6 JAXB (as opposed to the JAXB RI),
    // we trip up with access denied on RuntimePermission accessDeclaredMembers
    // (is this because NamespacePrefixMapperSunInternal extends com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper?).
    // This is despite the JNLP file seeking all-permissions.
    // Workaround:
    if (System.getSecurityManager() == null) {
        System.out.println("Initial SecurityManager: null");
    } else {/*from   ww w.  j a  va  2s  . c  om*/
        System.out.println("Initial SecurityManager: " + System.getSecurityManager().getClass().getName());
        System.setSecurityManager(null);
    }

    launch(WordMLEditor.class, args);
}

From source file:org.eclipse.wb.internal.core.DesignerPlugin.java

/**
 * We should not allow user code to terminate JVM.
 *///from  w  w  w  . j  av  a 2s .  co  m
public static void installSecurityManager() {
    System.setSecurityManager(new SecurityManager() {
        @Override
        public void checkPermission(java.security.Permission perm) {
            if (isExitVM(perm)) {
                StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
                for (StackTraceElement element : stackTrace) {
                    String className = element.getClassName();
                    String methodName = element.getMethodName();
                    // ignore this class, because it has our class name prefix
                    if (className.equals(getClass().getName())) {
                        continue;
                    }
                    // ignore JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    if (className.equals("javax.swing.JFrame")
                            && methodName.equals("setDefaultCloseOperation")) {
                        return;
                    }
                    // prevent exit() from user invoked by "designer"
                    if (className.startsWith("org.eclipse.wb.")
                            || className.startsWith("com.google.gdt.eclipse.designer.")
                            || className.startsWith("net.rim.ejde.designer.")
                            || className.startsWith("java.awt.EventQueue")) {
                        // we often use test_exit() method as point to stop tests, allow it
                        if (methodName.startsWith("test_") && methodName.endsWith("_exit")) {
                            return;
                        }
                        // prevent exit()
                        throw new SecurityException("Exit from within user-loaded code");
                    }
                }
            }
        }

        private boolean isExitVM(java.security.Permission perm) {
            return perm instanceof RuntimePermission && StringUtils.startsWith(perm.getName(), "exitVM");
        }
    });
}

From source file:org.eu.gasp.core.bootstrap.Bootstrap.java

public Bootstrap(final String homeDir, final String policyFilePath, final String log4jFilePath,
        final String propertyFilePath) {
    if (StringUtils.isBlank(homeDir)) {
        throw new IllegalArgumentException("homeDir");
    }//w w  w .  j  a  v  a2  s .  c om
    try {
        this.homeDir = new File(homeDir).getCanonicalPath();
    } catch (Exception e) {
        throw new IllegalStateException("Unexpected exception", e);
    }

    // the home dir is declared in the System properties
    System.setProperty("gasp.home", homeDir);

    try {
        this.propertyFilePath = new File(homeDir,
                propertyFilePath == null ? "conf/gasp.properties" : propertyFilePath).getCanonicalPath();
    } catch (Exception e) {
        throw new IllegalStateException("Unexpected exception", e);
    }
    this.log4jFilePath = log4jFilePath;

    if (!StringUtils.isBlank(policyFilePath)) {
        // installing security manager
        try {
            System.setProperty("java.security.policy",
                    new File(homeDir, policyFilePath).getAbsoluteFile().toURI().toURL().toExternalForm());
            System.setSecurityManager(new SecurityManager());
        } catch (Exception e) {
            throw new IllegalStateException("Error while setting the Java security policy", e);
        }
    }

    // adding a shutdown hook to properly close the application
    Runtime.getRuntime().addShutdownHook(new ShutdownHook(this));
}

From source file:org.firstopen.custom.view.EventMonitorBean.java

public EventMonitorBean(String name) throws InfrastructureException {
    if (System.getSecurityManager() == null) {

        System.setSecurityManager(new SecurityManager());
    }//w  ww .ja v  a 2s.c om

    JMSUtil.createQueue(MONITOR_NAME);
    connection = JMSUtil.getQueueConnection();
    try {
        queueSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageConsumer receiver = queueSession.createConsumer(JMSUtil.getQueue(MONITOR_NAME));

        receiver.setMessageListener(this);

        connection.start();
    } catch (JMSException e) {

        log.error("unable to start listener on Queue");
        throw new InfrastructureException(e);
    }
}