java.lang.System.getSecurityManager()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(597)

本文整理了Java中java.lang.System.getSecurityManager()方法的一些代码示例,展示了System.getSecurityManager()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。System.getSecurityManager()方法的具体详情如下:
包路径:java.lang.System
类名称:System
方法名:getSecurityManager

System.getSecurityManager介绍

[英]Returns null. Android does not use SecurityManager. This method is only provided for source compatibility.
[中]返回null。Android不使用SecurityManager。此方法仅用于源代码兼容性。

代码示例

代码示例来源:origin: spring-projects/spring-framework

protected boolean isSecurityManagerPresent() {
  return (System.getSecurityManager() != null);
}

代码示例来源:origin: netty/netty

public DefaultThreadFactory(String poolName, boolean daemon, int priority) {
  this(poolName, daemon, priority, System.getSecurityManager() == null ?
      Thread.currentThread().getThreadGroup() : System.getSecurityManager().getThreadGroup());
}

代码示例来源:origin: netty/netty

static ClassLoader getSystemClassLoader() {
  if (System.getSecurityManager() == null) {
    return ClassLoader.getSystemClassLoader();
  } else {
    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
      @Override
      public ClassLoader run() {
        return ClassLoader.getSystemClassLoader();
      }
    });
  }
}

代码示例来源:origin: netty/netty

static ClassLoader getClassLoader(final Class<?> clazz) {
  if (System.getSecurityManager() == null) {
    return clazz.getClassLoader();
  } else {
    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
      @Override
      public ClassLoader run() {
        return clazz.getClassLoader();
      }
    });
  }
}

代码示例来源:origin: apache/incubator-dubbo

public NamedThreadFactory(String prefix, boolean daemon) {
  mPrefix = prefix + "-thread-";
  mDaemon = daemon;
  SecurityManager s = System.getSecurityManager();
  mGroup = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
}

代码示例来源:origin: apache/incubator-dubbo

public NamedThreadFactory(String prefix, boolean daemon) {
  mPrefix = prefix + "-thread-";
  mDaemon = daemon;
  SecurityManager s = System.getSecurityManager();
  mGroup = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
}

代码示例来源:origin: netty/netty

static ClassLoader getContextClassLoader() {
  if (System.getSecurityManager() == null) {
    return Thread.currentThread().getContextClassLoader();
  } else {
    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
      @Override
      public ClassLoader run() {
        return Thread.currentThread().getContextClassLoader();
      }
    });
  }
}

代码示例来源:origin: prestodb/presto

/**
 * Checks whether the user has permission 'ConverterManager.alterPartialConverters'.
 * 
 * @throws SecurityException if the user does not have the permission
 */
private void checkAlterPartialConverters() throws SecurityException {
  SecurityManager sm = System.getSecurityManager();
  if (sm != null) {
    sm.checkPermission(new JodaTimePermission("ConverterManager.alterPartialConverters"));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
    final Constructor<?> ctor, Object... args) {
  if (!bd.hasMethodOverrides()) {
    if (System.getSecurityManager() != null) {
      // use own privileged to change accessibility (when security is on)
      AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
        ReflectionUtils.makeAccessible(ctor);
        return null;
      });
    }
    return BeanUtils.instantiateClass(ctor, args);
  }
  else {
    return instantiateWithMethodInjection(bd, beanName, owner, ctor, args);
  }
}

代码示例来源:origin: netty/netty

@Override
public void freeDirectBuffer(ByteBuffer buffer) {
  // Try to minimize overhead when there is no SecurityManager present.
  // See https://bugs.openjdk.java.net/browse/JDK-8191053.
  if (System.getSecurityManager() == null) {
    try {
      INVOKE_CLEANER.invoke(PlatformDependent0.UNSAFE, buffer);
    } catch (Throwable cause) {
      PlatformDependent0.throwException(cause);
    }
  } else {
    freeDirectBufferPrivileged(buffer);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private Method determineDestroyMethod(String name) {
  try {
    if (System.getSecurityManager() != null) {
      return AccessController.doPrivileged((PrivilegedAction<Method>) () -> findDestroyMethod(name));
    }
    else {
      return findDestroyMethod(name);
    }
  }
  catch (IllegalArgumentException ex) {
    throw new BeanDefinitionValidationException("Could not find unique destroy method on bean with name '" +
        this.beanName + ": " + ex.getMessage());
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Retrieve all candidate methods for the given class, considering
 * the {@link RootBeanDefinition#isNonPublicAccessAllowed()} flag.
 * Called as the starting point for factory method determination.
 */
private Method[] getCandidateMethods(Class<?> factoryClass, RootBeanDefinition mbd) {
  if (System.getSecurityManager() != null) {
    return AccessController.doPrivileged((PrivilegedAction<Method[]>) () ->
        (mbd.isNonPublicAccessAllowed() ?
          ReflectionUtils.getAllDeclaredMethods(factoryClass) : factoryClass.getMethods()));
  }
  else {
    return (mbd.isNonPublicAccessAllowed() ?
        ReflectionUtils.getAllDeclaredMethods(factoryClass) : factoryClass.getMethods());
  }
}

代码示例来源:origin: google/guava

public void testExistsThrowsSecurityException() throws IOException, URISyntaxException {
 SecurityManager oldSecurityManager = System.getSecurityManager();
 try {
  doTestExistsThrowsSecurityException();
 } finally {
  System.setSecurityManager(oldSecurityManager);
 }
}

代码示例来源:origin: netty/netty

@Override
public void freeDirectBuffer(ByteBuffer buffer) {
  if (!buffer.isDirect()) {
    return;
  }
  if (System.getSecurityManager() == null) {
    try {
      freeDirectBuffer0(buffer);
    } catch (Throwable cause) {
      PlatformDependent0.throwException(cause);
    }
  } else {
    freeDirectBufferPrivileged(buffer);
  }
}

代码示例来源:origin: spring-projects/spring-framework

private Object instantiate(
    String beanName, RootBeanDefinition mbd, Constructor constructorToUse, Object[] argsToUse) {
  try {
    InstantiationStrategy strategy = this.beanFactory.getInstantiationStrategy();
    if (System.getSecurityManager() != null) {
      return AccessController.doPrivileged((PrivilegedAction<Object>) () ->
          strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse),
          this.beanFactory.getAccessControlContext());
    }
    else {
      return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
    }
  }
  catch (Throwable ex) {
    throw new BeanCreationException(mbd.getResourceDescription(), beanName,
        "Bean instantiation via constructor failed", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

private Object instantiate(String beanName, RootBeanDefinition mbd,
    @Nullable Object factoryBean, Method factoryMethod, Object[] args) {
  try {
    if (System.getSecurityManager() != null) {
      return AccessController.doPrivileged((PrivilegedAction<Object>) () ->
          this.beanFactory.getInstantiationStrategy().instantiate(
              mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args),
          this.beanFactory.getAccessControlContext());
    }
    else {
      return this.beanFactory.getInstantiationStrategy().instantiate(
          mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args);
    }
  }
  catch (Throwable ex) {
    throw new BeanCreationException(mbd.getResourceDescription(), beanName,
        "Bean instantiation via factory method failed", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

public CallbacksSecurityTests() {
  // setup security
  if (System.getSecurityManager() == null) {
    Policy policy = Policy.getPolicy();
    URL policyURL = getClass()
        .getResource(
            "/org/springframework/beans/factory/support/security/policy.all");
    System.setProperty("java.security.policy", policyURL.toString());
    System.setProperty("policy.allowSystemProperty", "true");
    policy.refresh();
    System.setSecurityManager(new SecurityManager());
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setUp() {
  originalSecurityManager = System.getSecurityManager();
  env = StandardEnvironmentTests.getModifiableSystemEnvironment();
  env.put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "p1");
}

代码示例来源:origin: google/guava

/**
 * Tests that the use of a {@link FinalizableReferenceQueue} does not subsequently prevent the
 * loader of that class from being garbage-collected.
 */
public void testUnloadableWithoutSecurityManager() throws Exception {
 if (isJdk9OrHigher()) {
  return;
 }
 SecurityManager oldSecurityManager = System.getSecurityManager();
 try {
  System.setSecurityManager(null);
  doTestUnloadable();
 } finally {
  System.setSecurityManager(oldSecurityManager);
 }
}

代码示例来源:origin: google/guava

public void testUnloadableInStaticFieldIfClosed() throws Exception {
 if (isJdk9OrHigher()) {
  return;
 }
 Policy oldPolicy = Policy.getPolicy();
 SecurityManager oldSecurityManager = System.getSecurityManager();
 try {
  Policy.setPolicy(new PermissivePolicy());
  System.setSecurityManager(new SecurityManager());
  WeakReference<ClassLoader> loaderRef = doTestUnloadableInStaticFieldIfClosed();
  GcFinalization.awaitClear(loaderRef);
 } finally {
  System.setSecurityManager(oldSecurityManager);
  Policy.setPolicy(oldPolicy);
 }
}

相关文章