Example usage for java.lang System getSecurityManager

List of usage examples for java.lang System getSecurityManager

Introduction

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

Prototype

public static SecurityManager getSecurityManager() 

Source Link

Document

Gets the system-wide security manager.

Usage

From source file:org.apache.accumulo.server.security.SystemCredentials.java

public static SystemCredentials get() {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(SYSTEM_CREDENTIALS_PERMISSION);
    }/*from   w ww. ja  v a2s  .c  o m*/
    if (SYSTEM_CREDS == null) {
        SYSTEM_CREDS = new SystemCredentials();
    }
    return SYSTEM_CREDS;
}

From source file:org.pepstock.jem.junit.test.springbatch.java.TrySecurityRunnable.java

@Override
public void run() {

    String what = null;/*  w w  w.  ja  v a2s.  com*/
    try {
        what = "CHANGE SECURITY MANAGER!";
        // gry to change security manager
        System.setSecurityManager(null);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            what = "CHANGE FIELD OF SECURITY MANAGER!";
            SecurityManager sm = System.getSecurityManager();
            Field f = sm.getClass().getField("isAdministrator");
            System.err.println(FieldUtils.readField(f, sm, true));
        } catch (Exception e1) {
            e1.printStackTrace();
            return;
        }
    }
    throw new SecurityException("Securitymanager is not secure: " + what);
}

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

@Test
public void cloneableConverterWithoutSetAccessible() throws DatatypeConfigurationException {

    final SecurityManager initialSm = System.getSecurityManager();
    try {//from   w ww .  j a v  a2  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:Main.java

/**
 * see RFE 5012841 [Per AppContect security permissions] for the
 * details//from   ww w. j av  a  2  s. co  m
 *
 */
private static boolean isTrustedContext() {
    return (System.getSecurityManager() == null)
            || (AppContext.getAppContext().get(UntrustedClipboardAccess) == null);
}

From source file:com.newlandframework.rpc.parallel.NamedThreadFactory.java

public NamedThreadFactory(String prefix, boolean daemo) {
    this.prefix = StringUtils.isNotEmpty(prefix) ? prefix + "-thread-" : "";
    daemoThread = daemo;/*from   ww w  . j a v a  2 s. c  o  m*/
    SecurityManager s = System.getSecurityManager();
    threadGroup = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
}

From source file:org.wso2.carbon.micro.integrator.core.internal.Activator.java

@Override
public void start(BundleContext bundleContext) throws Exception {
    try {/*from w  w  w.  ja v  a 2s  .  c  o m*/
        // Need permissions in order to activate Carbon Core
        SecurityManager secMan = System.getSecurityManager();
        if (secMan != null) {
            secMan.checkPermission(new ManagementPermission("control"));
        }
        // We assume it's super tenant during the deployment time
        PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        privilegedCarbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        privilegedCarbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        log.info("Starting WSO2 Micro Integrator ...");
        log.info("Operating System : " + System.getProperty("os.name") + " " + System.getProperty("os.version")
                + ", " + System.getProperty("os.arch"));
        if (log.isDebugEnabled()) {
            log.debug("Java Home        : " + System.getProperty("java.home"));
        }
        log.info("Java Version     : " + System.getProperty("java.version"));
        log.info("Java VM          : " + System.getProperty("java.vm.name") + " "
                + System.getProperty("java.vm.version") + "," + System.getProperty("java.vendor"));

        String carbonHome;
        if ((carbonHome = System.getProperty("carbon.home")).equals(".")) {
            carbonHome = new File(".").getAbsolutePath();
        }
        log.info("Micro Integrator Home      : " + carbonHome);

        if (log.isDebugEnabled()) {
            log.info("Java Temp Dir    : " + System.getProperty("java.io.tmpdir"));
            log.info("User             : " + System.getProperty("user.name") + ", "
                    + System.getProperty("user.language") + "-" + System.getProperty("user.country") + ", "
                    + System.getProperty("user.timezone"));
        }
        Security.addProvider(new BouncyCastleProvider());
        if (log.isDebugEnabled()) {
            log.debug("BouncyCastle security provider is successfully registered in JVM.");
        }
        bundleContext.registerService(CarbonCoreInitializedEvent.class.getName(),
                new CarbonCoreInitializedEventImpl(), null);
        GhostServiceMetaArtifactsLoader serviceMetaArtifactsLoader = new GhostServiceMetaArtifactsLoader();
        bundleContext.registerService(GhostMetaArtifactsLoader.class.getName(), serviceMetaArtifactsLoader,
                null);
        CarbonCoreDataHolder.getInstance().setBundleContext(bundleContext);
    } catch (Throwable e) {
        throw new Exception(e);
    }
}

From source file:org.wso2.carbon.user.core.internal.Activator.java

public void startDeploy(BundleContext bundleContext) throws Exception {
    // Need permissions in order to instantiate user core
    SecurityManager secMan = System.getSecurityManager();

    /*//w w  w .  ja  v  a2  s  .  c  o m
       * Read the SSL trust store configurations from the Security.TrustStore
     * element of the Carbon.xml
     */
    ServerConfiguration config = ServerConfiguration.getInstance();
    String type = config.getFirstProperty("Security.TrustStore.Type");
    String password = config.getFirstProperty("Security.TrustStore.Password");
    String storeFile = new File(config.getFirstProperty("Security.TrustStore.Location")).getAbsolutePath();
    // set the SSL trust store System Properties
    System.setProperty("javax.net.ssl.trustStore", storeFile);
    System.setProperty("javax.net.ssl.trustStoreType", type);
    System.setProperty("javax.net.ssl.trustStorePassword", password);

    if (secMan != null) {
        secMan.checkPermission(new ManagementPermission("control"));
    }
    try {
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        RealmService realmService = new DefaultRealmService(bundleContext);
        bundleContext.registerService(
                new String[] { RealmService.class.getName(), UserRealmService.class.getName() }, realmService,
                null);
        UserCoreUtil.setRealmService(realmService);
    } catch (Throwable e) {
        String msg = "Cannot start User Manager Core bundle";
        log.error(msg, e);
        // do not throw exceptions;
    }
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.CustomListenerAdapterUtils.java

/**
 * Specialised reflection utility that determines all methods that accept two parameters such:
 * //from   ww  w.j  a  v a2  s  .co  m
 * <pre> methodName(Type serviceType, Type1 arg)
 * 
 * methodName(Type serviceType, Type2 arg)
 * 
 * methodName(AnotherType serviceType, Type1 arg)
 * 
 * methodName(Type serviceType) </pre>
 * 
 * It will return a map which has the serviceType (first argument) as type and contains as list the variants of
 * methods using the second argument. This method is normally used by listeners when determining custom methods.
 * 
 * @param target
 * @param methodName
 * @param possibleArgumentTypes
 * @param modifier
 * @return
 */
static Map<Class<?>, List<Method>> determineCustomMethods(final Class<?> target, final String methodName,
        final Class<?>[] possibleArgumentTypes, final boolean onlyPublic) {

    if (!StringUtils.hasText(methodName)) {
        return Collections.emptyMap();
    }

    Assert.notEmpty(possibleArgumentTypes);

    if (System.getSecurityManager() != null) {
        return AccessController.doPrivileged(new PrivilegedAction<Map<Class<?>, List<Method>>>() {
            public Map<Class<?>, List<Method>> run() {
                return doDetermineCustomMethods(target, methodName, possibleArgumentTypes, onlyPublic);
            }
        });
    } else {
        return doDetermineCustomMethods(target, methodName, possibleArgumentTypes, onlyPublic);
    }
}

From source file:org.apache.hadoop.tools.TestExternalCall.java

@Before
public void setup() {

    securityManager = System.getSecurityManager();
    System.setSecurityManager(new NoExitSecurityManager());
    try {/* ww  w. jav  a  2s .co  m*/
        fs = FileSystem.get(getConf());
        root = new Path("target/tmp").makeQualified(fs.getUri(), fs.getWorkingDirectory()).toString();
        TestDistCpUtils.delete(fs, root);
    } catch (IOException e) {
        LOG.error("Exception encountered ", e);
    }
}

From source file:org.apache.axis.providers.java.RMIProvider.java

/**
 * Return a object which implements the service.
 * /* www .j  a  va  2s. 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;
}