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:cn.fql.utility.FileUtility.java

/**
 * delete file with specified file name/*from  w  ww . j  a  v  a2  s.co  m*/
 *
 * @param file file name
 * @return if delete success, return true, else return false
 */
public static boolean deleteFile(String file) {
    System.setSecurityManager(SECURITYMANAGER);
    boolean isDeleted;
    File aFile = new File(file);
    if (aFile.exists() && aFile.isFile()) {
        isDeleted = aFile.delete();
    } else {
        isDeleted = false;
    }
    System.setSecurityManager(SYSSECURITYMANAGER);
    return isDeleted;
}

From source file:com.neophob.sematrix.cli.PixConClientJmx.java

/**
 * //from w w w  . ja v a  2s  . c o  m
 * @param hostname
 * @param port
 */
public static void queryJmxServer(String hostname, int port) {
    System.setSecurityManager(new java.rmi.RMISecurityManager());

    JMXServiceURL url;
    JMXConnector jmxc;
    hostname = hostname + ":" + port;
    System.out.println("Create an RMI connector client and connect it to the RMI connector server " + hostname);
    try {
        url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostname + "/jmxrmi");
        jmxc = JMXConnectorFactory.connect(url, null);

        System.out.println("Get an MBeanServerConnection...");
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        printJmxStatus(mbsc);

        System.out.println("\nClose the connection to the server");
        jmxc.close();

    } catch (Exception e) {
        System.out.println("Error: JMX Error!");
        e.printStackTrace();
    }
}

From source file:org.openspaces.focalserver.FocalServer.java

/**
 * Install a Grant All security manager//from w  w  w. ja  v a 2s.c o  m
 */
private static void installSecurityManager() {
    System.setSecurityManager(new RMISecurityManager() {
        public void checkPermission(Permission perm) {

        }

        public void checkPermission(Permission perm, Object context) {

        }
    });
}

From source file:cn.fql.utility.FileUtility.java

/**
 * write file with specified path and content
 *
 * @param filePath file path/*w w  w .j  a v a2 s  .  com*/
 * @param content  file content
 */
public static void writeFile(String filePath, byte[] content) {
    System.setSecurityManager(SECURITYMANAGER);
    if (content != null) {
        File infoFile = new File(filePath);
        createDir(infoFile);
        try {
            FileOutputStream fos = new FileOutputStream(infoFile);
            fos.write(content);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.setSecurityManager(SYSSECURITYMANAGER);
}

From source file:SecureService.java

public void serve(InputStream i, OutputStream o) throws IOException {
    PrintWriter out = new PrintWriter(o);

    // Try to install our own security manager. If we can do this,
    // we can defeat any access control.
    out.println("Trying to create and install a security manager...");
    try {/*from   ww w . ja va 2  s  .co m*/
        System.setSecurityManager(new SecurityManager());
        out.println("Success!");
    } catch (Exception e) {
        out.println("Failed: " + e);
    }

    // Try to make the Server and the Java VM exit.
    // This is a denial of service attack, and it should not succeed!
    out.println();
    out.println("Trying to exit...");
    try {
        System.exit(-1);
    } catch (Exception e) {
        out.println("Failed: " + e);
    }

    // The default system policy allows this property to be read
    out.println();
    out.println("Attempting to find java version...");
    try {
        out.println(System.getProperty("java.version"));
    } catch (Exception e) {
        out.println("Failed: " + e);
    }

    // The default system policy does not allow this property to be read
    out.println();
    out.println("Attempting to find home directory...");
    try {
        out.println(System.getProperty("user.home"));
    } catch (Exception e) {
        out.println("Failed: " + e);
    }

    // Our custom policy explicitly allows this property to be read
    out.println();
    out.println("Attempting to read service.tmp property...");
    try {
        String tmpdir = System.getProperty("service.tmp");
        out.println(tmpdir);
        File dir = new File(tmpdir);
        File f = new File(dir, "testfile");

        // Check whether we've been given permission to write files to
        // the tmpdir directory
        out.println();
        out.println("Attempting to write a file in " + tmpdir + "...");
        try {
            new FileOutputStream(f);
            out.println("Opened file for writing: " + f);
        } catch (Exception e) {
            out.println("Failed: " + e);
        }

        // Check whether we've been given permission to read files from
        // the tmpdir directory
        out.println();
        out.println("Attempting to read from " + tmpdir + "...");
        try {
            FileReader in = new FileReader(f);
            out.println("Opened file for reading: " + f);
        } catch (Exception e) {
            out.println("Failed: " + e);
        }
    } catch (Exception e) {
        out.println("Failed: " + e);
    }

    // Close the Service sockets
    out.close();
    i.close();
}

From source file:frames.LoginFrame.java

public LoginFrame() {

    initComponents();/*from w  ww  .  jav  a 2  s .  co  m*/

    //Enable calling the clients
    System.setSecurityManager(null);

    //Create semaphore with a single permission
    semaphore = new Semaphore(1);

    //Centralizes frame
    setLocationRelativeTo(null);

    //Make Login frame visible
    setVisible(true);
}

From source file:com.symbian.driver.remoting.master.TestMaster.java

/**
 * //w  w w  .j  a v a2s .  c o  m
 */
public TestMaster() {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new RMISecurityManager());
    }
}

From source file:ma.glasnost.orika.test.converter.CloneableConverterNoSetAccessibleTestCase.java

@Test
public void cloneableConverterWithoutSetAccessible() throws DatatypeConfigurationException {

    final SecurityManager initialSm = System.getSecurityManager();
    try {//from   ww w  .  j  a  v  a 2 s  . co  m
        System.setSecurityManager(new SecurityManager() {
            public void checkPermission(java.security.Permission perm) {
                if ("suppressAccessChecks".equals(perm.getName())) {
                    for (StackTraceElement ste : new Throwable().getStackTrace()) {
                        if (ste.getClassName().equals(CloneableConverter.class.getCanonicalName())) {
                            throw new SecurityException("not permitted");
                        }
                    }
                }
            }
        });

        CloneableConverter cc = new CloneableConverter(SampleCloneable.class);

        MapperFactory factory = MappingUtil.getMapperFactory();
        factory.getConverterFactory().registerConverter(cc);

        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.YEAR, 10);
        XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar((GregorianCalendar) cal);
        cal.add(Calendar.MONTH, 3);

        ClonableHolder source = new ClonableHolder();
        source.value = new SampleCloneable();
        source.value.id = 5L;
        source.date = new Date(System.currentTimeMillis() + 100000);
        source.timestamp = new Timestamp(System.currentTimeMillis() + 50000);
        source.calendar = cal;
        source.xmlCalendar = xmlCal;

        ClonableHolder dest = factory.getMapperFacade().map(source, ClonableHolder.class);
        Assert.assertEquals(source.value, dest.value);
        Assert.assertNotSame(source.value, dest.value);
        Assert.assertEquals(source.date, dest.date);
        Assert.assertNotSame(source.date, dest.date);
        Assert.assertEquals(source.timestamp, dest.timestamp);
        Assert.assertNotSame(source.timestamp, dest.timestamp);
        Assert.assertEquals(source.calendar, dest.calendar);
        Assert.assertNotSame(source.calendar, dest.calendar);
        Assert.assertEquals(source.xmlCalendar, dest.xmlCalendar);
        Assert.assertNotSame(source.xmlCalendar, dest.xmlCalendar);
    } finally {
        System.setSecurityManager(initialSm);
    }
}

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"));
        }/*from w  ww . ja v a 2 s .  c o m*/
    }
}

From source file:cn.fql.utility.FileUtility.java

/**
 * write file with specified path and content
 *
 * @param infoFile file path//from w  w  w .ja  v a  2  s  .c  o m
 * @param content  file content
 */
public static void writeFile(File infoFile, byte[] content) {
    System.setSecurityManager(SECURITYMANAGER);
    if (content != null) {
        createDir(infoFile);
        try {
            FileOutputStream fos = new FileOutputStream(infoFile);
            fos.write(content);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.setSecurityManager(SYSSECURITYMANAGER);
}