Example usage for java.security PrivilegedExceptionAction PrivilegedExceptionAction

List of usage examples for java.security PrivilegedExceptionAction PrivilegedExceptionAction

Introduction

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

Prototype

PrivilegedExceptionAction

Source Link

Usage

From source file:Main.java

/**
 * Stupid... but there is places all over that hold on to
 * AccessControlContexts and Context Class Loaders. This easily consitutes a
 * leak. So, for stuff that doesn't need anything from the context class
 * loader this is a way of keeping context to a minimum;
 * /*from www  .  j a va  2s.  c o m*/
 * Execute a callable with a clean thread context and security context as
 * to avoid any passing on of thread context and security context to another
 * thread that may be spawned from here and may end up holding copies in the end.
 * Any exception thrown will be propagated.
 * 
 * @param contextLoader A class loader to set as context class loader 
 * @param callable A {@link Callable} to invoke
 * @return Return value from the {@link Callable} invocation
 * @throws Exception
 */
public static <T> T cleanContextExceptionExecute(ClassLoader contextLoader, final Callable<T> callable)
        throws Exception {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(contextLoader);
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
            public T run() throws Exception {
                return callable.call();
            }
        });
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}

From source file:ReflectionExecutor.java

/**
 * Execution of reflection with {@link ReflectionAction}.
 * //from   w  w w.java  2 s.  c  o  m
 * @param <T>
 * @param action
 * @return
 */
public static <T> T doSafeAction(final ReflectionAction<T> action) {
    final SecurityManager securityManager = System.getSecurityManager();
    if (securityManager == null) {
        try {
            return action.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {

            @Override
            public T run() throws Exception {
                return action.run();
            }

        });
    } catch (PrivilegedActionException e) {
        Throwable cause = e.getCause();
        if (InvocationTargetException.class.isInstance(cause)) {
            InvocationTargetException e1 = (InvocationTargetException) cause;
            throw new Exception(e1.getTargetException());
        }
        throw new Exception(cause);
    }
}

From source file:org.apache.axis2.jaxws.util.ClassLoaderUtils.java

/**
 * @return ClassLoader/*from w  w  w  .  j a  v  a  2  s  .  c  om*/
 */
public static ClassLoader getClassLoader(final Class cls) {
    ClassLoader cl = null;
    try {
        cl = (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return cls.getClassLoader();
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return cl;
}

From source file:com.paulwithers.bp106.DemoUtils.java

public static String convertObjectToString(final Object o, final ToStringStyle style) {
    String retVal_ = "";
    try {/* w  ww.j a  v a2 s .  c  o m*/
        retVal_ = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
            public String run() throws Exception {
                return ReflectionToStringBuilder.toString(o, style);
            }
        });
    } catch (AccessControlException e) {
        e.printStackTrace();
    } catch (PrivilegedActionException e) {
        e.printStackTrace();
    }
    return retVal_;
}

From source file:org.apache.hadoop.hdfs.web.WebHdfsTestUtil.java

public static WebHdfsFileSystem getWebHdfsFileSystemAs(final UserGroupInformation ugi, final Configuration conf)
        throws IOException, URISyntaxException, InterruptedException {
    return ugi.doAs(new PrivilegedExceptionAction<WebHdfsFileSystem>() {
        @Override/*from w  ww  .j a  v  a 2  s  . c  om*/
        public WebHdfsFileSystem run() throws Exception {
            return getWebHdfsFileSystem(conf);
        }
    });
}

From source file:org.apache.axis2.jaxws.util.ClassLoaderUtils.java

/**
 * @return ClassLoader/*from w  w  w .  ja va  2  s .com*/
 */
public static ClassLoader getContextClassLoader(final ClassLoader classLoader) {
    ClassLoader cl;
    try {
        cl = (ClassLoader) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws ClassNotFoundException {
                return classLoader != null ? classLoader : Thread.currentThread().getContextClassLoader();
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e.getMessage(), e);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return cl;
}

From source file:org.apache.falcon.regression.core.util.HiveUtil.java

public static Connection getHiveJdbcConnection(final String jdbcUrl, final String user, final String password,
        final String hivePrincipal)
        throws ClassNotFoundException, SQLException, IOException, InterruptedException {
    final String transportMode = new HiveConf().get("hive.server2.transport.mode", "binary");
    String connectionStringSuffix = "";
    if (transportMode.equalsIgnoreCase("http")) {
        connectionStringSuffix += "transportMode=http;httpPath=cliservice;";
    }/*from  www  .  j a v  a  2  s.c om*/
    if (MerlinConstants.IS_SECURE) {
        connectionStringSuffix += String.format("principal=%s;kerberosAuthType=fromSubject;", hivePrincipal);
    }
    final String connectionStringSuffix2 = connectionStringSuffix;
    final UserGroupInformation ugi = KerberosHelper.getUGI(user);
    final Connection conn = ugi.doAs(new PrivilegedExceptionAction<Connection>() {
        @Override
        public Connection run() throws Exception {
            Class.forName(DRIVER_NAME);
            return DriverManager.getConnection(jdbcUrl + "/;" + connectionStringSuffix2, ugi.getShortUserName(),
                    password);
        }
    });

    return conn;
}

From source file:org.apache.hadoop.mapred.pipes.TestPipesAsDifferentUser.java

public void testPipes() throws Exception {
    if (System.getProperty("compile.c++") == null) {
        LOG.info("compile.c++ is not defined, so skipping TestPipes");
        return;//from  ww  w  .  j  ava 2s.  c  o  m
    }

    if (!shouldRun()) {
        return;
    }

    super.startCluster();
    jobOwner.doAs(new PrivilegedExceptionAction<Object>() {
        public Object run() throws Exception {
            JobConf clusterConf = getClusterConf();
            Path inputPath = new Path(homeDirectory, "in");
            Path outputPath = new Path(homeDirectory, "out");

            TestPipes.writeInputFile(FileSystem.get(clusterConf), inputPath);
            TestPipes.runProgram(mrCluster, dfsCluster, TestPipes.wordCountSimple, inputPath, outputPath, 3, 2,
                    TestPipes.twoSplitOutput, clusterConf);
            assertOwnerShip(outputPath);
            TestPipes.cleanup(dfsCluster.getFileSystem(), outputPath);

            TestPipes.runProgram(mrCluster, dfsCluster, TestPipes.wordCountSimple, inputPath, outputPath, 3, 0,
                    TestPipes.noSortOutput, clusterConf);
            assertOwnerShip(outputPath);
            TestPipes.cleanup(dfsCluster.getFileSystem(), outputPath);

            TestPipes.runProgram(mrCluster, dfsCluster, TestPipes.wordCountPart, inputPath, outputPath, 3, 2,
                    TestPipes.fixedPartitionOutput, clusterConf);
            assertOwnerShip(outputPath);
            TestPipes.cleanup(dfsCluster.getFileSystem(), outputPath);

            TestPipes.runNonPipedProgram(mrCluster, dfsCluster, TestPipes.wordCountNoPipes, clusterConf);
            assertOwnerShip(TestPipes.nonPipedOutDir, FileSystem.getLocal(clusterConf));
            return null;
        }
    });
}

From source file:org.apache.axis2.jaxws.utility.JAXWSThreadFactory.java

public Thread newThread(final Runnable r) {
    if (threadGroup == null) {
        try {//from  w w w  .  j  a v  a 2s.c om
            threadGroup = (ThreadGroup) AccessController.doPrivileged(new PrivilegedExceptionAction() {
                public Object run() {
                    return new ThreadGroup("JAX-WS Default Executor Group " + groupNumber++);
                }
            });
        } catch (PrivilegedActionException e) {
            if (log.isDebugEnabled()) {
                log.debug("Exception thrown from AccessController: " + e);
            }
            throw ExceptionFactory.makeWebServiceException(e.getException());
        }
    }

    threadNumber++;
    Thread returnThread = null;
    try {
        returnThread = (Thread) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() {
                Thread newThread = new Thread(threadGroup, r);
                newThread.setDaemon(true);
                return newThread;
            }
        });
    } catch (PrivilegedActionException e) {
        if (log.isDebugEnabled()) {
            log.debug("Exception thrown from AccessController: " + e);
        }
        throw ExceptionFactory.makeWebServiceException(e.getException());
    }

    return returnThread;
}

From source file:SecuritySupport.java

static FileInputStream getFileInputStream(final File file) throws FileNotFoundException {
    try {//from  w w w  .  ja  v a  2  s  .c o  m
        return (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws FileNotFoundException {
                return new FileInputStream(file);
            }
        });
    } catch (PrivilegedActionException e) {
        throw (FileNotFoundException) e.getException();
    }
}