Example usage for java.security AccessController doPrivileged

List of usage examples for java.security AccessController doPrivileged

Introduction

In this page you can find the example usage for java.security AccessController doPrivileged.

Prototype

@CallerSensitive
public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws PrivilegedActionException 

Source Link

Document

Performs the specified PrivilegedExceptionAction with privileges enabled.

Usage

From source file:Main.java

static long getLastModified(final File f) {
    return ((Long) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return new Long(f.lastModified());
        }//from ww w. jav a2  s .  co  m
    })).longValue();
}

From source file:Main.java

private static String getPrivilegedProperty(final String property_name) {
    return AccessController.doPrivileged(new PrivilegedAction<String>() {
        public String run() {
            return System.getProperty(property_name);
        }/*from w ww . j  a v  a 2s .c  o  m*/
    });
}

From source file:Main.java

/**
 * Gets a boolean property as a privileged action.
 *//*www  .ja  v  a 2s .com*/
public static boolean getPrivilegedBoolean(final String property_name) {
    return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
        public Boolean run() {
            return Boolean.getBoolean(property_name);
        }
    });
}

From source file:Main.java

static void execPrivileged(final String[] cmd_array) throws Exception {
    try {/*  ww w. java  2 s . com*/
        Process process = AccessController.doPrivileged(new PrivilegedExceptionAction<Process>() {
            public Process run() throws Exception {
                return Runtime.getRuntime().exec(cmd_array);
            }
        });
        // Close unused streams to make sure the child process won't hang
        process.getInputStream().close();
        process.getOutputStream().close();
        process.getErrorStream().close();
    } catch (PrivilegedActionException e) {
        throw (Exception) e.getCause();
    }
}

From source file:Utils.java

public static void makeAccessible(final AccessibleObject object) {
    if (!object.isAccessible()) {
        if (System.getSecurityManager() == null) {
            object.setAccessible(true);//from   w w  w. j  a  v a2 s  . co m
        } else {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {

                public Object run() {
                    object.setAccessible(true);
                    return null;
                }
            });
        }
    }
}

From source file:Main.java

/**
 * Gets an integer property as a privileged action.
 *
 * @param property_name the integer property name
 *
 * @return the property value/*  ww w .j av a 2s.c om*/
 */
public static Integer getPrivilegedInteger(final String property_name) {
    return AccessController.doPrivileged(new PrivilegedAction<Integer>() {
        public Integer run() {
            return Integer.getInteger(property_name);
        }
    });
}

From source file:Main.java

/**
 * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this platform.
 */// www .  j ava 2s.  c o m
private static sun.misc.Unsafe getUnsafe() {
    sun.misc.Unsafe unsafe = null;
    try {
        unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() {
            @Override
            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;

                for (Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x)) {
                        return k.cast(x);
                    }
                }
                // The sun.misc.Unsafe field does not exist.
                return null;
            }
        });
    } catch (Throwable e) {
        // Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError
        // for Unsafe.
    }
    return unsafe;
}

From source file:Main.java

private static EventQueue getEventQueue() {
    return AccessController.doPrivileged(
            (PrivilegedAction<EventQueue>) () -> java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue());
}

From source file:Main.java

private static void setField(final Object bean, final Field field, final Object value) throws Exception {
    if (!field.isAccessible()) {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                field.setAccessible(true);
                return null;
            }/*from ww  w .j a va  2 s. co  m*/
        });
    }
    field.set(bean, value);
}

From source file:Main.java

/**
 * Gets an integer property as a privileged action.
 *
 * @param property_name the integer property name
 * @param default_val   the default value to use if the property is not defined
 *
 * @return the property value// w w  w .  ja va  2s  .  c o m
 */
public static Integer getPrivilegedInteger(final String property_name, final int default_val) {
    return AccessController.doPrivileged(new PrivilegedAction<Integer>() {
        public Integer run() {
            return Integer.getInteger(property_name, default_val);
        }
    });
}