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:org.batoo.common.reflect.ReflectHelper.java

/**
 * Sets the member's accessibility status.
 * // ww w .  ja  v a  2 s  .c  o  m
 * @param member
 *            the member of which to set accessibility status
 * @param accessible
 *            true to set accessible, false to make it not accessible
 * 
 * @since 2.0.1
 */
public static void setAccessible(final Member member, final boolean accessible) {
    AccessController.doPrivileged(new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            if (member instanceof Field) {
                ((Field) member).setAccessible(accessible);
            }

            else if (member instanceof Method) {
                ((Method) member).setAccessible(accessible);
            }

            else {
                ((Constructor<?>) member).setAccessible(accessible);
            }

            return null;
        }
    });
}

From source file:org.apache.axiom.om.util.StAXUtils.java

public static XMLStreamWriter createXMLStreamWriter(StAXWriterConfiguration configuration,
        final OutputStream out, final String encoding) throws XMLStreamException {
    final XMLOutputFactory outputFactory = getXMLOutputFactory(configuration);
    try {/*from w ww  .j  a v a  2  s.com*/
        XMLStreamWriter writer = (XMLStreamWriter) AccessController
                .doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws XMLStreamException {
                        return outputFactory.createXMLStreamWriter(out, encoding);
                    }
                });

        if (isDebugEnabled) {
            log.debug("XMLStreamWriter is " + writer.getClass().getName());
        }
        return writer;
    } catch (PrivilegedActionException pae) {
        throw (XMLStreamException) pae.getException();
    }
}

From source file:org.apache.struts2.jasper.runtime.PageContextImpl.java

public void removeAttribute(final String name) {

    if (name == null) {
        throw new NullPointerException(Localizer.getMessage("jsp.error.attribute.null_name"));
    }//  w ww . j av  a2  s  .  c  o  m

    if (SecurityUtil.isPackageProtectionEnabled()) {
        AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                doRemoveAttribute(name);
                return null;
            }
        });
    } else {
        doRemoveAttribute(name);
    }
}

From source file:org.apache.jasper.runtime.PageContextImpl.java

public Object findAttribute(final String name) {
    if (System.getSecurityManager() != null) {
        return AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                if (name == null) {
                    throw new NullPointerException(Localizer.getMessage("jsp.error.attribute.null_name"));
                }/*from  www . ja v a2s  .  c  o  m*/

                return doFindAttribute(name);
            }
        });
    } else {
        if (name == null) {
            throw new NullPointerException(Localizer.getMessage("jsp.error.attribute.null_name"));
        }

        return doFindAttribute(name);
    }
}

From source file:org.apache.catalina.session.ManagerBase.java

/** Use /dev/random-type special device. This is new code, but may reduce the
*  big delay in generating the random./*from w  w  w  .ja  va2 s . co m*/
*
*  You must specify a path to a random generator file. Use /dev/urandom
*  for linux ( or similar ) systems. Use /dev/random for maximum security
*  ( it may block if not enough "random" exist ). You can also use
*  a pipe that generates random.
*
*  The code will check if the file exists, and default to java Random
*  if not found. There is a significant performance difference, very
*  visible on the first call to getSession ( like in the first JSP )
*  - so use it if available.
*/
public void setRandomFile(String s) {
    // as a hack, you can use a static file - and genarate the same
    // session ids ( good for strange debugging )
    if (System.getSecurityManager() != null) {
        randomIS = (DataInputStream) AccessController.doPrivileged(new PrivilegedSetRandomFile());
    } else {
        try {
            devRandomSource = s;
            File f = new File(devRandomSource);
            if (!f.exists())
                return;
            randomIS = new DataInputStream(new FileInputStream(f));
            randomIS.readLong();
            if (log.isDebugEnabled())
                log.debug("Opening " + devRandomSource);
        } catch (IOException ex) {
            randomIS = null;
        }
    }
}

From source file:org.apache.hadoop.mapreduce.v2.util.MRApps.java

private static ClassLoader createJobClassLoader(final String appClasspath, final String[] systemClasses)
        throws IOException {
    try {/*from w  w w .j  av  a  2  s  .  com*/
        return AccessController.doPrivileged(new PrivilegedExceptionAction<ClassLoader>() {
            @Override
            public ClassLoader run() throws MalformedURLException {
                return new ApplicationClassLoader(appClasspath, MRApps.class.getClassLoader(),
                        Arrays.asList(systemClasses));
            }
        });
    } catch (PrivilegedActionException e) {
        Throwable t = e.getCause();
        if (t instanceof MalformedURLException) {
            throw (MalformedURLException) t;
        }
        throw new IOException(e);
    }
}

From source file:org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor.java

/**
 * Fail creating the context. Figure out unsatisfied dependencies and provide a very nice log message before closing
 * the appContext./*from  w  ww. j  av a2 s .  c om*/
 * 
 * <p/> Normally this method is called when an exception is caught.
 * 
 * @param t - the offending Throwable which caused our demise
 */
private void fail(Throwable t, boolean skipEvent) {

    // this will not thrown any exceptions (it just logs them)
    close();

    StringBuilder buf = new StringBuilder();

    synchronized (monitor) {
        if (dependencyDetector == null || dependencyDetector.isSatisfied()) {
            buf.append("none");
        } else {
            for (Iterator<MandatoryServiceDependency> iterator = dependencyDetector.getUnsatisfiedDependencies()
                    .keySet().iterator(); iterator.hasNext();) {
                MandatoryServiceDependency dependency = iterator.next();
                buf.append(dependency.toString());
                if (iterator.hasNext()) {
                    buf.append(", ");
                }
            }
        }
    }

    final StringBuilder message = new StringBuilder();
    message.append("Unable to create application context for [");
    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                message.append(OsgiStringUtils.nullSafeSymbolicName(getBundle()));
                return null;
            }
        });
    } else {
        message.append(OsgiStringUtils.nullSafeSymbolicName(getBundle()));
    }

    message.append("], unsatisfied dependencies: ");
    message.append(buf.toString());

    log.error(message.toString(), t);

    // send notification
    if (!skipEvent) {
        delegatedMulticaster.multicastEvent(
                new OsgiBundleContextFailedEvent(delegateContext, delegateContext.getBundle(), t));
    }
}

From source file:org.apache.catalina.core.ApplicationContextFacade.java

/**
 * Executes the method of the specified <code>ApplicationContext</code>
 * @param method The method object to be invoked.
 * @param context The AppliationContext object on which the method
 *                   will be invoked//  w w w. j  a  v a2s . co m
 * @param params The arguments passed to the called method.
 */
private Object executeMethod(final Method method, final ApplicationContext context, final Object[] params)
        throws PrivilegedActionException, IllegalAccessException, InvocationTargetException {

    if (System.getSecurityManager() != null) {
        return AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws IllegalAccessException, InvocationTargetException {
                return method.invoke(context, params);
            }
        });
    } else {
        return method.invoke(context, params);
    }
}

From source file:org.forgerock.openam.authentication.modules.persistentcookie.PersistentCookieAuthModule.java

/**
 * Gets the Key alias for the realm.//from   w  w  w.j  a  v a2s  .  co  m
 *
 * @param orgName The organisation name for the realm.
 * @return The key alias.
 * @throws SSOException If there is a problem getting the key alias.
 * @throws SMSException If there is a problem getting the key alias.
 */
protected String getKeyAlias(String orgName) throws SSOException, SMSException {

    SSOToken token = AccessController.doPrivileged(AdminTokenAction.getInstance());

    ServiceConfigManager scm = new ServiceConfigManager(AUTH_SERVICE_NAME, token);

    ServiceConfig orgConfig = scm.getOrganizationConfig(orgName, null);
    String keyAlias = CollectionHelper.getMapAttr(orgConfig.getAttributes(), AUTH_KEY_ALIAS);

    return keyAlias;
}

From source file:org.apache.axiom.om.util.StAXUtils.java

public static XMLStreamWriter createXMLStreamWriter(StAXWriterConfiguration configuration, final Writer out)
        throws XMLStreamException {
    final XMLOutputFactory outputFactory = getXMLOutputFactory(configuration);
    try {/*from   w ww.j a v a2s.com*/
        XMLStreamWriter writer = (XMLStreamWriter) AccessController
                .doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws XMLStreamException {
                        return outputFactory.createXMLStreamWriter(out);
                    }
                });
        if (isDebugEnabled) {
            log.debug("XMLStreamWriter is " + writer.getClass().getName());
        }
        return writer;
    } catch (PrivilegedActionException pae) {
        throw (XMLStreamException) pae.getException();
    }
}