Search in sources :

Example 21 with PrivilegedAction

use of java.security.PrivilegedAction in project robovm by robovm.

the class Subject method doAs_PrivilegedExceptionAction.

// instantiates a new context and passes it to AccessController
@SuppressWarnings("unchecked")
private static <T> T doAs_PrivilegedExceptionAction(Subject subject, PrivilegedExceptionAction<T> action, final AccessControlContext context) throws PrivilegedActionException {
    AccessControlContext newContext;
    final SubjectDomainCombiner combiner;
    if (subject == null) {
        // performance optimization
        // if subject is null there is nothing to combine
        combiner = null;
    } else {
        combiner = new SubjectDomainCombiner(subject);
    }
    PrivilegedAction<AccessControlContext> dccAction = new PrivilegedAction<AccessControlContext>() {

        public AccessControlContext run() {
            return new AccessControlContext(context, combiner);
        }
    };
    newContext = AccessController.doPrivileged(dccAction);
    return AccessController.doPrivileged(action, newContext);
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction)

Example 22 with PrivilegedAction

use of java.security.PrivilegedAction in project hazelcast by hazelcast.

the class UnsafeHelper method findUnsafe.

private static Unsafe findUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {

            @Override
            public Unsafe run() {
                try {
                    Class<Unsafe> type = Unsafe.class;
                    try {
                        Field field = type.getDeclaredField("theUnsafe");
                        field.setAccessible(true);
                        return type.cast(field.get(type));
                    } catch (Exception e) {
                        for (Field field : type.getDeclaredFields()) {
                            if (type.isAssignableFrom(field.getType())) {
                                field.setAccessible(true);
                                return type.cast(field.get(type));
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException("Unsafe unavailable", e);
                }
                throw new RuntimeException("Unsafe unavailable");
            }
        });
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Unsafe(sun.misc.Unsafe)

Example 23 with PrivilegedAction

use of java.security.PrivilegedAction in project XobotOS by xamarin.

the class LogFactory method getProperties.

/**
     * Given a URL that refers to a .properties file, load that file.
     * This is done under an AccessController so that this method will
     * succeed when this jarfile is privileged but the caller is not.
     * This method must therefore remain private to avoid security issues.
     * <p>
     * Null is returned if the URL cannot be opened.
     */
private static Properties getProperties(final URL url) {
    PrivilegedAction action = new PrivilegedAction() {

        public Object run() {
            try {
                InputStream stream = url.openStream();
                if (stream != null) {
                    Properties props = new Properties();
                    props.load(stream);
                    stream.close();
                    return props;
                }
            } catch (IOException e) {
                if (isDiagnosticsEnabled()) {
                    logDiagnostic("Unable to read URL " + url);
                }
            }
            return null;
        }
    };
    return (Properties) AccessController.doPrivileged(action);
}
Also used : PrivilegedAction(java.security.PrivilegedAction) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties)

Example 24 with PrivilegedAction

use of java.security.PrivilegedAction in project XobotOS by xamarin.

the class LogFactory method getResources.

/**
     * Given a filename, return an enumeration of URLs pointing to
     * all the occurrences of that filename in the classpath.
     * <p>
     * This is just like ClassLoader.getResources except that the
     * operation is done under an AccessController so that this method will
     * succeed when this jarfile is privileged but the caller is not.
     * This method must therefore remain private to avoid security issues.
     * <p>
     * If no instances are found, an Enumeration is returned whose
     * hasMoreElements method returns false (ie an "empty" enumeration).
     * If resources could not be listed for some reason, null is returned.
     */
private static Enumeration getResources(final ClassLoader loader, final String name) {
    PrivilegedAction action = new PrivilegedAction() {

        public Object run() {
            try {
                if (loader != null) {
                    return loader.getResources(name);
                } else {
                    return ClassLoader.getSystemResources(name);
                }
            } catch (IOException e) {
                if (isDiagnosticsEnabled()) {
                    logDiagnostic("Exception while trying to find configuration file " + name + ":" + e.getMessage());
                }
                return null;
            } catch (NoSuchMethodError e) {
                // this case.
                return null;
            }
        }
    };
    Object result = AccessController.doPrivileged(action);
    return (Enumeration) result;
}
Also used : Enumeration(java.util.Enumeration) PrivilegedAction(java.security.PrivilegedAction) IOException(java.io.IOException)

Example 25 with PrivilegedAction

use of java.security.PrivilegedAction in project XobotOS by xamarin.

the class Subject method doAs_PrivilegedExceptionAction.

// instantiates a new context and passes it to AccessController
@SuppressWarnings("unchecked")
private static <T> T doAs_PrivilegedExceptionAction(Subject subject, PrivilegedExceptionAction<T> action, final AccessControlContext context) throws PrivilegedActionException {
    AccessControlContext newContext;
    final SubjectDomainCombiner combiner;
    if (subject == null) {
        // performance optimization
        // if subject is null there is nothing to combine
        combiner = null;
    } else {
        combiner = new SubjectDomainCombiner(subject);
    }
    PrivilegedAction<AccessControlContext> dccAction = new PrivilegedAction<AccessControlContext>() {

        public AccessControlContext run() {
            return new AccessControlContext(context, combiner);
        }
    };
    newContext = AccessController.doPrivileged(dccAction);
    return AccessController.doPrivileged(action, newContext);
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction)

Aggregations

PrivilegedAction (java.security.PrivilegedAction)359 IOException (java.io.IOException)85 Subject (javax.security.auth.Subject)61 AccessControlContext (java.security.AccessControlContext)31 File (java.io.File)29 HashMap (java.util.HashMap)29 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)29 Method (java.lang.reflect.Method)24 ArrayList (java.util.ArrayList)23 ClientResponse (com.sun.jersey.api.client.ClientResponse)21 InputStream (java.io.InputStream)21 URL (java.net.URL)21 FileNotFoundException (java.io.FileNotFoundException)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)18 Iterator (java.util.Iterator)18 MalformedURLException (java.net.MalformedURLException)17 List (java.util.List)17 UnknownHostException (java.net.UnknownHostException)16 Principal (java.security.Principal)15 PrivilegedActionException (java.security.PrivilegedActionException)15