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.beangle.model.persist.hibernate.internal.BundleDelegatingClassLoader.java

/**
 * Factory method for creating a class loader over the given bundle and with
 * a given class loader as fall-back. In case the bundle cannot find a class
 * or locate a resource, the given class loader will be used as fall back.
 * /* ww  w .  java  2 s .co m*/
 * @param bundle
 *            bundle used for class loading and resource acquisition
 * @param bridge
 *            class loader used as fall back in case the bundle cannot load
 *            a class or find a resource. Can be <code>null</code>
 * @return class loader adapter over the given bundle and class loader
 */
public static BundleDelegatingClassLoader createBundleClassLoaderFor(final Bundle bundle,
        final ClassLoader bridge) {
    return AccessController.doPrivileged(new PrivilegedAction<BundleDelegatingClassLoader>() {

        public BundleDelegatingClassLoader run() {
            return new BundleDelegatingClassLoader(bundle, bridge);
        }
    });
}

From source file:Main.java

/**
 * Get the Thread context class loader.// w w  w  .  j  a  va 2  s .c  om
 * <p/>
 *
 * @return the Thread context class loader
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return Thread.currentThread().getContextClassLoader();
        }
    });
}

From source file:edu.ku.brc.af.auth.SecurityMgr.java

/**
 * Returns the instance to the singleton
 * @return  the instance to the singleton
 *//*from   ww w  .j  a  v a 2 s  . c  o  m*/
public static SecurityMgr getInstance() {
    if (instance != null) {
        return instance;
    }

    // else
    String factoryNameStr = AccessController.doPrivileged(new java.security.PrivilegedAction<String>() {
        public String run() {
            return System.getProperty(factoryName);
        }
    });

    if (isNotEmpty(factoryNameStr)) {
        try {
            return instance = (SecurityMgr) Class.forName(factoryNameStr).newInstance();

        } catch (Exception e) {
            e.printStackTrace();
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(SecurityMgr.class, e);
            InternalError error = new InternalError("Can't instantiate SecurityMgr factory " + factoryNameStr); //$NON-NLS-1$
            error.initCause(e);
            throw error;
        }
    }
    return null;
}

From source file:SecuritySupport.java

static ClassLoader getParentClassLoader(final ClassLoader cl) {
    return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            ClassLoader parent = null;
            try {
                parent = cl.getParent();
            } catch (SecurityException ex) {
            }/*  w w w .  j ava  2 s  .c  o m*/

            // eliminate loops in case of the boot
            // ClassLoader returning itself as a parent
            return (parent == cl) ? null : parent;
        }
    });
}

From source file:org.apache.openjpa.lib.util.Files.java

/**
 * Backup the given file to a new file called &lt;file-name&gt;~. If
 * the file does not exist or a backup could not be created, returns null.
 *///from  www  .java  2 s  .  c om
public static File backup(File file, boolean copy) {
    if (file == null || !(AccessController.doPrivileged(J2DoPrivHelper.existsAction(file))).booleanValue())
        return null;

    // create new file object copy so we don't modify the original
    String aPath = AccessController.doPrivileged(J2DoPrivHelper.getAbsolutePathAction(file));
    File clone = new File(aPath);
    File bk = new File(aPath + "~");
    if (!(AccessController.doPrivileged(J2DoPrivHelper.renameToAction(clone, bk))).booleanValue())
        return null;
    if (copy) {
        try {
            copy(bk, file);
        } catch (IOException ioe) {
            throw new NestableRuntimeException(ioe);
        }
    }
    return bk;
}

From source file:SecurityActions.java

static void setTCL(final ClassLoader tcl) {
    if (System.getSecurityManager() == null) {
        Thread.currentThread().setContextClassLoader(tcl);
    } else {/*  www .  ja  va  2  s  . c o  m*/
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                Thread.currentThread().setContextClassLoader(tcl);
                return null;
            }
        });
    }
}

From source file:edu.ku.brc.af.core.expresssearch.QueryAdjusterForDomain.java

/**
 * Returns the instance to the singleton
 * @return  the instance to the singleton
 */// www. jav  a 2  s  .c  om
public static QueryAdjusterForDomain getInstance() {
    if (instance != null) {
        return instance;
    }

    // else
    String factoryNameStr = AccessController.doPrivileged(new java.security.PrivilegedAction<String>() {
        public String run() {
            return System.getProperty(factoryName);
        }
    });

    if (isNotEmpty(factoryNameStr)) {
        try {
            return instance = (QueryAdjusterForDomain) Class.forName(factoryNameStr).newInstance();

        } catch (Exception e) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(QueryAdjusterForDomain.class, e);
            InternalError error = new InternalError(
                    "Can't instantiate ExpressSearchSQLAdjuster factory " + factoryNameStr); //$NON-NLS-1$
            error.initCause(e);
            throw error;
        }
    }
    // if not factory than pass an instance of this in
    // and this does nothing to the SQL.
    return instance = new QueryAdjusterForDomain();
}

From source file:Main.java

public static IIOMetadataFormat instantiateMetadataFormat(String formatName, boolean standardFormatSupported,
        String nativeMetadataFormatName, String nativeMetadataFormatClassName,
        String[] extraMetadataFormatNames, String[] extraMetadataFormatClassNames) {
    if (formatName == null) {
        throw new IllegalArgumentException("formatName == null!");
    }//from  ww  w .  j  a  v a2  s  .  co m
    if (formatName.equals(IIOMetadataFormatImpl.standardMetadataFormatName)) {
        if (standardFormatSupported) {
            return IIOMetadataFormatImpl.getStandardFormatInstance();
        }
    }

    String className = null;

    if (formatName.equals(nativeMetadataFormatName)) {
        className = nativeMetadataFormatClassName;
    } else if (extraMetadataFormatNames != null) {
        for (int i = 0; i < extraMetadataFormatNames.length; i++) {
            if (formatName.equals(extraMetadataFormatNames[i])) {
                className = extraMetadataFormatClassNames[i];
                break;
            }
        }
    }

    if (className == null) {
        throw new IllegalArgumentException("Unsupported format name");
    }

    // Get the context class loader and try to use it first
    ClassLoader contextClassloader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        public ClassLoader run() {
            return Thread.currentThread().getContextClassLoader();
        }
    });

    Class cls;

    try {
        cls = Class.forName(className, true, contextClassloader);
    } catch (ClassNotFoundException e) {
        try {
            // Use current class loader
            cls = Class.forName(className);
        } catch (ClassNotFoundException e1) {
            throw new IllegalStateException("Can't obtain format");
        }
    }

    try {
        //???AWT:
        //Method getInstance = cls.getMethod("getInstance");
        //return (IIOMetadataFormat) getInstance.invoke(null);
        return null;
    } catch (Exception e) {
        IllegalStateException e1 = new IllegalStateException("Can't obtain format");
        e1.initCause(e); // Add some details to the message
        throw e1;
    }
}

From source file:org.codice.ddf.configuration.migration.AccessUtils.java

/**
 * Performs the specified action with privileges enabled. The action is performed with <i>all</i>
 * of the permissions possessed by this class' (or by the migration framework's) protection
 * domain.//  ww w. j av  a  2  s  .c  om
 *
 * <p>If the action's {@link ThrowingSupplier#get} method throws an <i>unchecked</i> exception, it
 * will propagate through this method.
 *
 * <p><i>Note:</i> Any DomainCombiner associated with the current AccessControlContext will be
 * ignored while the action is performed.
 *
 * @param <T> the type of the value returned by the action
 * @param <E> the type of exceptions thrown by the action
 * @param action the action to be performed
 * @return the value returned by the action's {@code run} method
 * @throws E if the specified action's threw the exception
 * @throws IllegalArgumentException if <code>action</code> is <code>null</code>
 */
public static <T, E extends Exception> T doPrivileged(ThrowingSupplier<T, E> action) throws E {
    Validate.notNull(action, AccessUtils.INVALID_NULL_ACTION);
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
            @Override
            public T run() throws Exception {
                return action.get();
            }
        });
    } catch (PrivilegedActionException pe) {
        final Exception e = pe.getException();

        if (e instanceof RuntimeException) { // should never happen but just to be safe!
            throw (RuntimeException) e;
        } else { // by design, the action is declared to only throw E
            throw (E) e;
        }
    }
}

From source file:Main.java

/**
 * Changes the locale of the messages.//from   w ww. j  ava  2s  . c  o  m
 * 
 * @param locale
 *            Locale the locale to change to.
 * @param resource
 *            the name of the bundle resource
 */
static public ResourceBundle setLocale(final Locale locale, final String resource) {
    try {
        // BEGIN android-removed
        // final ClassLoader loader = VM.bootCallerClassLoader();
        // END android-removed
        return (ResourceBundle) AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                // BEGIN android-changed
                return ResourceBundle.getBundle(resource, locale, ClassLoader.getSystemClassLoader());
                // END android-changed
            }
        });
    } catch (MissingResourceException e) {
    }
    return null;
}