java.security.AccessController.doPrivileged()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(328)

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

AccessController.doPrivileged介绍

[英]Calls action.run().
[中]呼吁采取行动。运行()。

代码示例

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

public static InetAddress[] allAddressesByName(final String hostname) throws UnknownHostException {
  try {
    return AccessController.doPrivileged(new PrivilegedExceptionAction<InetAddress[]>() {
      @Override
      public InetAddress[] run() throws UnknownHostException {
        return InetAddress.getAllByName(hostname);
      }
    });
  } catch (PrivilegedActionException e) {
    throw (UnknownHostException) e.getCause();
  }
}

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

/**
  * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple call
  * to Unsafe.getUnsafe when integrating into a jdk.
  *
  * @return a sun.misc.Unsafe
  */
 private static sun.misc.Unsafe getUnsafe() {
  try {
   return sun.misc.Unsafe.getUnsafe();
  } catch (SecurityException tryReflectionInstead) {
  }
  try {
   return java.security.AccessController.doPrivileged(
     new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
      public sun.misc.Unsafe run() throws Exception {
       Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
       for (java.lang.reflect.Field f : k.getDeclaredFields()) {
        f.setAccessible(true);
        Object x = f.get(null);
        if (k.isInstance(x)) return k.cast(x);
       }
       throw new NoSuchFieldError("the Unsafe");
      }
     });
  } catch (java.security.PrivilegedActionException e) {
   throw new RuntimeException("Could not initialize intrinsics", e.getCause());
  }
 }
}

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

public T call() throws Exception {
    try {
      return AccessController.doPrivileged(
        new PrivilegedExceptionAction<T>() {
          public T run() throws Exception {
            return task.call();
          }
        }, acc);
    } catch (PrivilegedActionException e) {
      throw e.getException();
    }
  }
}

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

FileInputStream getFileInputStream(final File file)
  throws FileNotFoundException
{
  try {
    return (FileInputStream)
      AccessController.doPrivileged(new PrivilegedExceptionAction() {
        public Object run() throws FileNotFoundException {
          return new FileInputStream(file);
        }
      });
  } catch (PrivilegedActionException e) {
    throw (FileNotFoundException)e.getException();
  }
}

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

public static InetAddress addressByName(final String hostname) throws UnknownHostException {
  try {
    return AccessController.doPrivileged(new PrivilegedExceptionAction<InetAddress>() {
      @Override
      public InetAddress run() throws UnknownHostException {
        return InetAddress.getByName(hostname);
      }
    });
  } catch (PrivilegedActionException e) {
    throw (UnknownHostException) e.getCause();
  }
}

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

/**
  * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple call
  * to Unsafe.getUnsafe when integrating into a jdk.
  *
  * @return a sun.misc.Unsafe
  */
 private static sun.misc.Unsafe getUnsafe() {
  try {
   return sun.misc.Unsafe.getUnsafe();
  } catch (SecurityException tryReflectionInstead) {
  }
  try {
   return java.security.AccessController.doPrivileged(
     new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
      public sun.misc.Unsafe run() throws Exception {
       Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
       for (java.lang.reflect.Field f : k.getDeclaredFields()) {
        f.setAccessible(true);
        Object x = f.get(null);
        if (k.isInstance(x)) return k.cast(x);
       }
       throw new NoSuchFieldError("the Unsafe");
      }
     });
  } catch (java.security.PrivilegedActionException e) {
   throw new RuntimeException("Could not initialize intrinsics", e.getCause());
  }
 }
}

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

FileInputStream getFileInputStream(final File file)
  throws FileNotFoundException
{
  try {
    return (FileInputStream)
      AccessController.doPrivileged(new PrivilegedExceptionAction() {
        public Object run() throws FileNotFoundException {
          return new FileInputStream(file);
        }
      });
  } catch (PrivilegedActionException e) {
    throw (FileNotFoundException)e.getException();
  }
}

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

public static SocketChannel accept(final ServerSocketChannel serverSocketChannel) throws IOException {
  try {
    return AccessController.doPrivileged(new PrivilegedExceptionAction<SocketChannel>() {
      @Override
      public SocketChannel run() throws IOException {
        return serverSocketChannel.accept();
      }
    });
  } catch (PrivilegedActionException e) {
    throw (IOException) e.getCause();
  }
}

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

return sun.misc.Unsafe.getUnsafe();
} catch (SecurityException tryReflectionInstead) {
 return java.security.AccessController.doPrivileged(
   new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
    @Override
 throw new RuntimeException("Could not initialize intrinsics", e.getCause());

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

/**
 * Load class.
 *
 * @param className the class name
 * @return loaded class
 * @throws ClassNotFoundException for any error
 */
protected Class loadClass(final String className) throws ClassNotFoundException {
  try {
    return (Class)AccessController.doPrivileged(new PrivilegedExceptionAction(){
      public Object run() throws Exception{
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        return Class.forName(className, true, cl);
      }
    });
  }
  catch (PrivilegedActionException pae) {
    throw new RuntimeException("cannot load the class: " + className, pae.getException());
  }
}

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

public static boolean connect(final SocketChannel socketChannel, final SocketAddress remoteAddress)
    throws IOException {
  try {
    return AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
      @Override
      public Boolean run() throws IOException {
        return socketChannel.connect(remoteAddress);
      }
    });
  } catch (PrivilegedActionException e) {
    throw (IOException) e.getCause();
  }
}

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

return sun.misc.Unsafe.getUnsafe();
} catch (SecurityException e) {
 return java.security.AccessController.doPrivileged(
   new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
    @Override
 throw new RuntimeException("Could not initialize intrinsics", e.getCause());

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

FileInputStream getFileInputStream(final File file)
  throws FileNotFoundException
{
  try {
    return (FileInputStream)
      AccessController.doPrivileged(new PrivilegedExceptionAction() {
        public Object run() throws FileNotFoundException {
          return new FileInputStream(file);
        }
      });
  } catch (PrivilegedActionException e) {
    throw (FileNotFoundException)e.getException();
  }
}

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

public static void bind(final SocketChannel socketChannel, final SocketAddress address) throws IOException {
  try {
    AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws IOException {
        socketChannel.bind(address);
        return null;
      }
    });
  } catch (PrivilegedActionException e) {
    throw (IOException) e.getCause();
  }
}

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

/**
  * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple call
  * to Unsafe.getUnsafe when integrating into a jdk.
  *
  * @return a sun.misc.Unsafe
  */
 private static sun.misc.Unsafe getUnsafe() {
  try {
   return sun.misc.Unsafe.getUnsafe();
  } catch (SecurityException tryReflectionInstead) {
  }
  try {
   return java.security.AccessController.doPrivileged(
     new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
      public sun.misc.Unsafe run() throws Exception {
       Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
       for (java.lang.reflect.Field f : k.getDeclaredFields()) {
        f.setAccessible(true);
        Object x = f.get(null);
        if (k.isInstance(x)) return k.cast(x);
       }
       throw new NoSuchFieldError("the Unsafe");
      }
     });
  } catch (java.security.PrivilegedActionException e) {
   throw new RuntimeException("Could not initialize intrinsics", e.getCause());
  }
 }
}

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

FileInputStream getFileInputStream(final File file)
  throws FileNotFoundException
{
  try {
    return (FileInputStream)
      AccessController.doPrivileged(new PrivilegedExceptionAction() {
        public Object run() throws FileNotFoundException {
          return new FileInputStream(file);
        }
      });
  } catch (PrivilegedActionException e) {
    throw (FileNotFoundException)e.getException();
  }
}

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

public static void bind(final DatagramChannel networkChannel, final SocketAddress address) throws IOException {
  try {
    AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws IOException {
        networkChannel.bind(address);
        return null;
      }
    });
  } catch (PrivilegedActionException e) {
    throw (IOException) e.getCause();
  }
}

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

/**
  * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple call
  * to Unsafe.getUnsafe when integrating into a jdk.
  *
  * @return a sun.misc.Unsafe
  */
 private static sun.misc.Unsafe getUnsafe() {
  try {
   return sun.misc.Unsafe.getUnsafe();
  } catch (SecurityException tryReflectionInstead) {
  }
  try {
   return java.security.AccessController.doPrivileged(
     new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
      public sun.misc.Unsafe run() throws Exception {
       Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
       for (java.lang.reflect.Field f : k.getDeclaredFields()) {
        f.setAccessible(true);
        Object x = f.get(null);
        if (k.isInstance(x)) return k.cast(x);
       }
       throw new NoSuchFieldError("the Unsafe");
      }
     });
  } catch (java.security.PrivilegedActionException e) {
   throw new RuntimeException("Could not initialize intrinsics", e.getCause());
  }
 }
}

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

FileInputStream getFileInputStream(final File file)
  throws FileNotFoundException
{
  try {
    return (FileInputStream)
      AccessController.doPrivileged(new PrivilegedExceptionAction() {
        public Object run() throws FileNotFoundException {
          return new FileInputStream(file);
        }
      });
  } catch (PrivilegedActionException e) {
    throw (FileNotFoundException)e.getException();
  }
}

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

public static void bind(final Socket socket, final SocketAddress bindpoint) throws IOException {
  try {
    AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws IOException {
        socket.bind(bindpoint);
        return null;
      }
    });
  } catch (PrivilegedActionException e) {
    throw (IOException) e.getCause();
  }
}

相关文章

微信公众号

最新文章

更多