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.apache.axis.providers.java.RMIProvider.java

/**
 * Return a object which implements the service.
 * /*  w ww  .j av a 2 s  .c  o  m*/
 * @param msgContext the message context
 * @param clsName The JNDI name of the EJB home class
 * @return an object that implements the service
 */
protected Object makeNewServiceObject(MessageContext msgContext, String clsName) throws Exception {
    // Read deployment descriptor options
    String namingLookup = getStrOption(OPTION_NAMING_LOOKUP, msgContext.getService());
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new RMISecurityManager());
    }
    Object targetObject = Naming.lookup(namingLookup);
    return targetObject;
}

From source file:org.apache.falcon.unit.FalconUnit.java

private static void cleanUpOozie() throws IOException, FalconException {
    LocalOozie.stop();/*from   w  ww . j ava  2 s  . c o m*/
    FileUtils.deleteDirectory(new File(OOZIE_HOME_DIR));
    resetSystemProperties();
    System.setSecurityManager(null);
}

From source file:org.apache.hadoop.hbase.mapreduce.TestCellCounter.java

/**
 * Test main method of CellCounter//  w  w w  . ja v a2  s.  c  o m
 */
@Test(timeout = 300000)
public void testCellCounterMain() throws Exception {

    PrintStream oldPrintStream = System.err;
    SecurityManager SECURITY_MANAGER = System.getSecurityManager();
    LauncherSecurityManager newSecurityManager = new LauncherSecurityManager();
    System.setSecurityManager(newSecurityManager);
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    String[] args = {};
    System.setErr(new PrintStream(data));
    try {
        System.setErr(new PrintStream(data));

        try {
            CellCounter.main(args);
            fail("should be SecurityException");
        } catch (SecurityException e) {
            assertEquals(-1, newSecurityManager.getExitCode());
            assertTrue(data.toString().contains("ERROR: Wrong number of parameters:"));
            // should be information about usage
            assertTrue(data.toString().contains("Usage:"));
        }

    } finally {
        System.setErr(oldPrintStream);
        System.setSecurityManager(SECURITY_MANAGER);
    }
}

From source file:org.apache.hadoop.hbase.mapreduce.TestImportExport.java

/**
 * test main method. Import should print help and call System.exit
 *///from  w  ww .ja v a  2s.  c  o  m
@Test
public void testImportMain() throws Exception {
    PrintStream oldPrintStream = System.err;
    SecurityManager SECURITY_MANAGER = System.getSecurityManager();
    LauncherSecurityManager newSecurityManager = new LauncherSecurityManager();
    System.setSecurityManager(newSecurityManager);
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    String[] args = {};
    System.setErr(new PrintStream(data));
    try {
        System.setErr(new PrintStream(data));
        Import.main(args);
        fail("should be SecurityException");
    } catch (SecurityException e) {
        assertEquals(-1, newSecurityManager.getExitCode());
        assertTrue(data.toString().contains("Wrong number of arguments:"));
        assertTrue(data.toString().contains("-Dimport.bulk.output=/path/for/output"));
        assertTrue(data.toString().contains("-Dimport.filter.class=<name of filter class>"));
        assertTrue(data.toString().contains("-Dimport.bulk.output=/path/for/output"));
        assertTrue(data.toString().contains("-Dmapreduce.reduce.speculative=false"));
    } finally {
        System.setErr(oldPrintStream);
        System.setSecurityManager(SECURITY_MANAGER);
    }
}

From source file:org.apache.hadoop.hbase.mapreduce.TestImportExport.java

/**
 * test main method. Export should print help and call System.exit
 *///w w w .j a v  a 2s. co m
@Test
public void testExportMain() throws Exception {
    PrintStream oldPrintStream = System.err;
    SecurityManager SECURITY_MANAGER = System.getSecurityManager();
    LauncherSecurityManager newSecurityManager = new LauncherSecurityManager();
    System.setSecurityManager(newSecurityManager);
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    String[] args = {};
    System.setErr(new PrintStream(data));
    try {
        System.setErr(new PrintStream(data));
        Export.main(args);
        fail("should be SecurityException");
    } catch (SecurityException e) {
        assertEquals(-1, newSecurityManager.getExitCode());
        assertTrue(data.toString().contains("Wrong number of arguments:"));
        assertTrue(data.toString()
                .contains("Usage: Export [-D <property=value>]* <tablename> <outputdir> [<versions> "
                        + "[<starttime> [<endtime>]] [^[regex pattern] or [Prefix] to filter]]"));
        assertTrue(data.toString().contains("-D hbase.mapreduce.scan.column.family=<familyName>"));
        assertTrue(data.toString().contains("-D hbase.mapreduce.include.deleted.rows=true"));
        assertTrue(data.toString().contains("-Dhbase.client.scanner.caching=100"));
        assertTrue(data.toString().contains("-Dmapreduce.map.speculative=false"));
        assertTrue(data.toString().contains("-Dmapreduce.reduce.speculative=false"));
        assertTrue(data.toString().contains("-Dhbase.export.scanner.batch=10"));
    } finally {
        System.setErr(oldPrintStream);
        System.setSecurityManager(SECURITY_MANAGER);
    }
}

From source file:org.apache.hadoop.hbase.mapreduce.TestRowCounter.java

/**
 * test main method. Import should print help and call System.exit
 *///from w  ww.  ja v  a  2 s  .  com
@Test
public void testImportMain() throws Exception {
    PrintStream oldPrintStream = System.err;
    SecurityManager SECURITY_MANAGER = System.getSecurityManager();
    LauncherSecurityManager newSecurityManager = new LauncherSecurityManager();
    System.setSecurityManager(newSecurityManager);
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    String[] args = {};
    System.setErr(new PrintStream(data));
    try {
        System.setErr(new PrintStream(data));

        try {
            RowCounter.main(args);
            fail("should be SecurityException");
        } catch (SecurityException e) {
            assertEquals(-1, newSecurityManager.getExitCode());
            assertTrue(data.toString().contains("Wrong number of parameters:"));
            assertTrue(data.toString()
                    .contains("Usage: RowCounter [options] <tablename> [--range=[startKey],[endKey]] "
                            + "[<column1> <column2>...]"));
            assertTrue(data.toString().contains("-Dhbase.client.scanner.caching=100"));
            assertTrue(data.toString().contains("-Dmapreduce.map.speculative=false"));
        }
        data.reset();
        try {
            args = new String[2];
            args[0] = "table";
            args[1] = "--range=1";
            RowCounter.main(args);
            fail("should be SecurityException");
        } catch (SecurityException e) {
            assertEquals(-1, newSecurityManager.getExitCode());
            assertTrue(data.toString().contains(
                    "Please specify range in such format as \"--range=a,b\" or, with only one boundary,"
                            + " \"--range=,b\" or \"--range=a,\""));
            assertTrue(data.toString()
                    .contains("Usage: RowCounter [options] <tablename> [--range=[startKey],[endKey]] "
                            + "[<column1> <column2>...]"));
        }

    } finally {
        System.setErr(oldPrintStream);
        System.setSecurityManager(SECURITY_MANAGER);
    }

}

From source file:org.apache.hadoop.hdfs.DnsMonitorSecurityManager.java

/**
 * Update's the system's security manager to an instance of this class if
 * system property dns_call_stack_logging is set to true.
 *///  w ww  .  j av a2 s.co  m
public static void setTheManager() {
    if ("true".equalsIgnoreCase(System.getProperty(ACTIVATE_FLAG))) {
        if (!(System.getSecurityManager() instanceof DnsMonitorSecurityManager)) {
            System.setSecurityManager(theManager);
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestNameNodeFormat.java

@Before
public void setUp() throws IOException {
    System.setSecurityManager(new NoExitSecurityManager());

    baseDir = System.getProperty("test.build.data", "build/test/data");

    hdfsDir = new File(baseDir, "dfs/name");
    if (hdfsDir.exists() && !FileUtil.fullyDelete(hdfsDir)) {
        throw new IOException("Could not delete test directory '" + hdfsDir + "'");
    }/*from   w w  w  . j  a  v a  2s .  c o m*/
    LOG.info("hdfsdir is " + hdfsDir.getAbsolutePath());

    // set the defaults before every test to make sure we have a clean state
    StartupOption.FORMAT.setConfirmationNeeded(true);
    StartupOption.FORMAT.setInteractive(true);

    config = new Configuration();
    config.set("dfs.name.dir", hdfsDir.getPath());
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestNameNodeFormat.java

@After
public void tearDown() throws IOException {
    System.setSecurityManager(null); // or save and restore original
    if (hdfsDir.exists() && !FileUtil.fullyDelete(hdfsDir)) {
        throw new IOException("Could not tearDown test directory '" + hdfsDir + "'");
    }/*from  w  ww  .  jav a 2s.co m*/
}

From source file:org.apache.hadoop.hdfs.TestDatanodeRegistration.java

/**
 * Ensure the datanode manager does not do host lookup after registration,
 * especially for node reports./*from w  w w  .j a v a  2s. c  o m*/
 *
 * @throws Exception
 */
@Test
public void testDNSLookups() throws Exception {
    MonitorDNS sm = new MonitorDNS();
    System.setSecurityManager(sm);

    MiniDFSCluster cluster = null;
    try {
        HdfsConfiguration conf = new HdfsConfiguration();
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(8).build();
        cluster.waitActive();

        int initialLookups = sm.lookups;
        assertTrue("dns security manager is active", initialLookups != 0);

        DatanodeManager dm = cluster.getNamesystem().getBlockManager().getDatanodeManager();

        // make sure no lookups occur
        dm.refreshNodes(conf);
        assertEquals(initialLookups, sm.lookups);

        dm.refreshNodes(conf);
        assertEquals(initialLookups, sm.lookups);

        // ensure none of the reports trigger lookups
        dm.getDatanodeListForReport(DatanodeReportType.ALL);
        assertEquals(initialLookups, sm.lookups);

        dm.getDatanodeListForReport(DatanodeReportType.LIVE);
        assertEquals(initialLookups, sm.lookups);

        dm.getDatanodeListForReport(DatanodeReportType.DEAD);
        assertEquals(initialLookups, sm.lookups);
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
        System.setSecurityManager(null);
    }
}