Java Code Examples for java.security.PrivilegedActionException.getCause()

The following are Jave code examples for showing how to use getCause() of the java.security.PrivilegedActionException class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: hadoop   File: MRApps.java   Source Code and License Vote up 6 votes
private static ClassLoader createJobClassLoader(final String appClasspath,
    final String[] systemClasses) throws IOException {
  try {
    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);
  }
}
 
Example 2
Project: smart-testing   File: SecurityUtils.java   Source Code and License Vote up 6 votes
/**
 * Obtains the Constructor specified from the given Class and argument types
 */
private static <T> Constructor<T> getConstructor(final Class<T> clazz, final Class<?>... argumentTypes)
    throws NoSuchMethodException {
    try {
        return AccessController.doPrivileged(
            (PrivilegedExceptionAction<Constructor<T>>) () -> clazz.getDeclaredConstructor(argumentTypes));
    }
    // Unwrap
    catch (final PrivilegedActionException pae) {
        final Throwable t = pae.getCause();
        // Rethrow
        if (t instanceof NoSuchMethodException) {
            throw (NoSuchMethodException) t;
        } else {
            // No other checked Exception thrown by Class.getConstructor
            try {
                throw (RuntimeException) t;
            }
            // Just in case we've really messed up
            catch (final ClassCastException cce) {
                throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
            }
        }
    }
}
 
Example 3
Project: jdk8u-jdk   File: RMIIIOPServerImpl.java   Source Code and License Vote up 6 votes
@Override
RMIConnection doNewClient(final Object credentials) throws IOException {
    if (callerACC == null) {
        throw new SecurityException("AccessControlContext cannot be null");
    }
    try {
        return AccessController.doPrivileged(
            new PrivilegedExceptionAction<RMIConnection>() {
                public RMIConnection run() throws IOException {
                    return superDoNewClient(credentials);
                }
        }, callerACC);
    } catch (PrivilegedActionException pae) {
        throw (IOException) pae.getCause();
    }
}
 
Example 4
Project: elasticsearch_my   File: TikaImpl.java   Source Code and License Vote up 6 votes
/**
 * parses with tika, throwing any exception hit while parsing the document
 */
// only package private for testing!
static String parse(final byte content[], final Metadata metadata, final int limit) throws TikaException, IOException {
    // check that its not unprivileged code like a script
    SpecialPermission.check();

    try {
        return AccessController.doPrivileged((PrivilegedExceptionAction<String>)
            () -> TIKA_INSTANCE.parseToString(new ByteArrayInputStream(content), metadata, limit), RESTRICTED_CONTEXT);
    } catch (PrivilegedActionException e) {
        // checked exception from tika: unbox it
        Throwable cause = e.getCause();
        if (cause instanceof TikaException) {
            throw (TikaException) cause;
        } else if (cause instanceof IOException) {
            throw (IOException) cause;
        } else {
            throw new AssertionError(cause);
        }
    }
}
 
Example 5
Project: apache-tomcat-7.0.73-with-comment   File: DefaultInstanceManager.java   Source Code and License Vote up 6 votes
protected Class<?> loadClassMaybePrivileged(final String className, final ClassLoader classLoader) throws ClassNotFoundException {
    Class<?> clazz;
    if (SecurityUtil.isPackageProtectionEnabled()) {
        try {
            clazz = AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {

                @Override
                public Class<?> run() throws Exception {
                    return loadClass(className, classLoader);
                }
            });
        } catch (PrivilegedActionException e) {
            Throwable t = e.getCause();
            if (t instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) t;
            }
            throw new RuntimeException(t);
        }
    } else {
        clazz = loadClass(className, classLoader);
    }
    checkAccess(clazz);
    return clazz;
}
 
Example 6
Project: arquillian-reporter   File: SecurityActions.java   Source Code and License Vote up 5 votes
/**
 * Obtains the Constructor specified from the given Class and argument types
 * @param clazz
 * @param argumentTypes
 * @return
 * @throws NoSuchMethodException
 */
static <T> Constructor<T> getConstructor(final Class<T> clazz, final Class<?>... argumentTypes)
      throws NoSuchMethodException
{
   try
   {
      return AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<T>>()
      {
         public Constructor<T> run() throws NoSuchMethodException
         {
            return clazz.getDeclaredConstructor(argumentTypes);
         }
      });
   }
   // Unwrap
   catch (final PrivilegedActionException pae)
   {
      final Throwable t = pae.getCause();
      // Rethrow
      if (t instanceof NoSuchMethodException)
      {
         throw (NoSuchMethodException) t;
      }
      else
      {
         // No other checked Exception thrown by Class.getConstructor
         try
         {
            throw (RuntimeException) t;
         }
         // Just in case we've really messed up
         catch (final ClassCastException cce)
         {
            throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
         }
      }
   }
}
 
Example 7
Project: openjdk-jdk10   File: ServiceLoader.java   Source Code and License Vote up 5 votes
/**
 * Invokes the provider's "provider" method to instantiate a provider.
 * When running with a security manager then the method runs with
 * permissions that are restricted by the security context of whatever
 * created this loader.
 */
private S invokeFactoryMethod() {
    Object result = null;
    Throwable exc = null;
    if (acc == null) {
        try {
            result = factoryMethod.invoke(null);
        } catch (Throwable x) {
            exc = x;
        }
    } else {
        PrivilegedExceptionAction<?> pa = new PrivilegedExceptionAction<>() {
            @Override
            public Object run() throws Exception {
                return factoryMethod.invoke(null);
            }
        };
        // invoke factory method with permissions restricted by acc
        try {
            result = AccessController.doPrivileged(pa, acc);
        } catch (PrivilegedActionException pae) {
            exc = pae.getCause();
        }
    }
    if (exc != null) {
        if (exc instanceof InvocationTargetException)
            exc = exc.getCause();
        fail(service, factoryMethod + " failed", exc);
    }
    if (result == null) {
        fail(service, factoryMethod + " returned null");
    }
    @SuppressWarnings("unchecked")
    S p = (S) result;
    return p;
}
 
Example 8
Project: jdk8u-jdk   File: ClassLoader.java   Source Code and License Vote up 5 votes
private static synchronized void initSystemClassLoader() {
    if (!sclSet) {
        if (scl != null)
            throw new IllegalStateException("recursive invocation");
        sun.misc.Launcher l = sun.misc.Launcher.getLauncher();
        if (l != null) {
            Throwable oops = null;
            scl = l.getClassLoader();
            try {
                scl = AccessController.doPrivileged(
                    new SystemClassLoaderAction(scl));
            } catch (PrivilegedActionException pae) {
                oops = pae.getCause();
                if (oops instanceof InvocationTargetException) {
                    oops = oops.getCause();
                }
            }
            if (oops != null) {
                if (oops instanceof Error) {
                    throw (Error) oops;
                } else {
                    // wrap the exception
                    throw new Error(oops);
                }
            }
        }
        sclSet = true;
    }
}
 
Example 9
Project: elasticsearch_my   File: AzureComputeServiceImpl.java   Source Code and License Vote up 5 votes
@Override
public HostedServiceGetDetailedResponse getServiceDetails() {
    SpecialPermission.check();
    try {
        return AccessController.doPrivileged((PrivilegedExceptionAction<HostedServiceGetDetailedResponse>)
            () -> client.getHostedServicesOperations().getDetailed(serviceName));
    } catch (PrivilegedActionException e) {
        throw new AzureServiceRemoteException("can not get list of azure nodes", e.getCause());
    }
}
 
Example 10
Project: elasticsearch_my   File: SocketAccess.java   Source Code and License Vote up 5 votes
public static <T> T doPrivilegedException(PrivilegedExceptionAction<T> operation) throws StorageException, URISyntaxException {
    SpecialPermission.check();
    try {
        return AccessController.doPrivileged(operation);
    } catch (PrivilegedActionException e) {
        throw (StorageException) e.getCause();
    }
}
 
Example 11
Project: jdk8u-jdk   File: PipeImpl.java   Source Code and License Vote up 5 votes
PipeImpl(final SelectorProvider sp) throws IOException {
    try {
        AccessController.doPrivileged(new Initializer(sp));
    } catch (PrivilegedActionException x) {
        throw (IOException)x.getCause();
    }
}
 
Example 12
Project: elasticsearch_my   File: Access.java   Source Code and License Vote up 5 votes
public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operation) throws IOException {
    SpecialPermission.check();
    try {
        return AccessController.doPrivileged(operation);
    } catch (PrivilegedActionException e) {
        throw (IOException) e.getCause();
    }
}
 
Example 13
Project: arquillian-reporter   File: Reporter.java   Source Code and License Vote up 5 votes
/**
 * Obtains the Constructor specified from the given Class and argument types
 *
 * @param clazz
 * @param argumentTypes
 * @return
 * @throws NoSuchMethodException
 */
private static <T> Constructor<T> getConstructor(final Class<T> clazz, final Class<?>... argumentTypes)
    throws NoSuchMethodException {
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<T>>() {
            public Constructor<T> run() throws NoSuchMethodException {
                return clazz.getDeclaredConstructor(argumentTypes);
            }
        });
    }
    // Unwrap
    catch (final PrivilegedActionException pae) {
        final Throwable t = pae.getCause();
        // Rethrow
        if (t instanceof NoSuchMethodException) {
            throw (NoSuchMethodException) t;
        } else {
            // No other checked Exception thrown by Class.getConstructor
            try {
                throw (RuntimeException) t;
            }
            // Just in case we've really messed up
            catch (final ClassCastException cce) {
                throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
            }
        }
    }
}
 
Example 14
Project: gitea-plugin   File: DefaultGiteaConnection.java   Source Code and License Vote up 5 votes
/**
 * Workaround for a bug in {@code HttpURLConnection.setRequestMethod(String)}
 * The implementation of Sun/Oracle is throwing a {@code ProtocolException}
 * when the method is other than the HTTP/1.1 default methods. So to use {@code PROPFIND}
 * and others, we must apply this workaround.
 */
private static void setRequestMethodViaJreBugWorkaround(final HttpURLConnection httpURLConnection,
                                                        final String method) {
    try {
        httpURLConnection.setRequestMethod(method); // Check whether we are running on a buggy JRE
    } catch (final ProtocolException pe) {
        try {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                @Override
                public Object run() throws NoSuchFieldException, IllegalAccessException {
                    final Object target;
                    if (httpURLConnection instanceof HttpsURLConnection) {
                        final Field delegate = httpURLConnection.getClass().getDeclaredField("delegate");
                        delegate.setAccessible(true);
                        target = delegate.get(httpURLConnection);
                    } else {
                        target = httpURLConnection;
                    }
                    final Field methodField = HttpURLConnection.class.getDeclaredField("method");
                    methodField.setAccessible(true);
                    methodField.set(target, method);
                    return null;
                }
            });
        } catch (final PrivilegedActionException e) {
            final Throwable cause = e.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else {
                throw new RuntimeException(cause);
            }
        }
    }
}
 
Example 15
Project: jdk8u-jdk   File: CalendarSystem.java   Source Code and License Vote up 5 votes
/**
 * Returns a {@link Properties} loaded from lib/calendars.properties.
 *
 * @return a {@link Properties} loaded from lib/calendars.properties
 * @throws IOException if an error occurred when reading from the input stream
 * @throws IllegalArgumentException if the input stream contains any malformed
 *                                  Unicode escape sequences
 */
public static Properties getCalendarProperties() throws IOException {
    Properties calendarProps = null;
    try {
        String homeDir = AccessController.doPrivileged(
            new sun.security.action.GetPropertyAction("java.home"));
        final String fname = homeDir + File.separator + "lib" + File.separator
                             + "calendars.properties";
        calendarProps = AccessController.doPrivileged(new PrivilegedExceptionAction<Properties>() {
            @Override
            public Properties run() throws IOException {
                Properties props = new Properties();
                try (FileInputStream fis = new FileInputStream(fname)) {
                    props.load(fis);
                }
                return props;
            }
        });
    } catch (PrivilegedActionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else if (cause instanceof IllegalArgumentException) {
            throw (IllegalArgumentException) cause;
        }
        // Should not happen
        throw new InternalError(cause);
    }
    return calendarProps;
}
 
Example 16
Project: openjdk-jdk10   File: ORBUtility.java   Source Code and License Vote up 5 votes
/**
 * Return default ValueHandler
 */
public static ValueHandler createValueHandler() {
    ValueHandler vh;
    try {
        vh = AccessController.doPrivileged(new PrivilegedExceptionAction<ValueHandler>() {
            public ValueHandler run() throws Exception {
    return Util.createValueHandler();
}
        });
    } catch (PrivilegedActionException e) {
        throw new InternalError(e.getCause());
    }
    return vh;
}
 
Example 17
Project: gemini.blueprint   File: PrivilegedUtils.java   Source Code and License Vote up 5 votes
/**
 * Temporarily changes the TCCL to the given one for the duration of the
 * given execution. All actions except the execution are executed with
 * privileged access.
 * 
 * Consider checking if there is a security manager in place before calling
 * this method.
 * 
 * @param customClassLoader
 * @param execution
 * @return
 * @throws Throwable
 */
public static <T> T executeWithCustomTCCL(final ClassLoader customClassLoader,
		final UnprivilegedThrowableExecution<T> execution) throws Throwable {
	final Thread currentThread = Thread.currentThread();
	final ClassLoader oldTCCL = getTCCLAction.getTCCL();

	try {
		AccessController.doPrivileged(new PrivilegedAction<Object>() {

			public Object run() {
				currentThread.setContextClassLoader(customClassLoader);
				return null;
			}
		});
		return execution.run();
	}
	catch (PrivilegedActionException pae) {
		throw pae.getCause();
	}
	finally {
		AccessController.doPrivileged(new PrivilegedAction<Object>() {

			public Object run() {
				currentThread.setContextClassLoader(oldTCCL);
				return null;
			}
		});
	}
}
 
Example 18
Project: whackpad   File: SecureCaller.java   Source Code and License Vote up 4 votes
/**
 * Call the specified callable using a protection domain belonging to the 
 * specified code source. 
 */
static Object callSecurely(final CodeSource codeSource, Callable callable, 
        Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
{
    final Thread thread = Thread.currentThread();
    // Run in doPrivileged as we might be checked for "getClassLoader" 
    // runtime permission
    final ClassLoader classLoader = (ClassLoader)AccessController.doPrivileged(
        new PrivilegedAction<Object>() {
            public Object run() {
                return thread.getContextClassLoader();
            }
        });
    Map<ClassLoader,SoftReference<SecureCaller>> classLoaderMap;
    synchronized(callers)
    {
        classLoaderMap = callers.get(codeSource);
        if(classLoaderMap == null)
        {
            classLoaderMap = new WeakHashMap<ClassLoader,SoftReference<SecureCaller>>();
            callers.put(codeSource, classLoaderMap);
        }
    }
    SecureCaller caller;
    synchronized(classLoaderMap)
    {
        SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader);
        if (ref != null) {
            caller = ref.get();
        } else {
            caller = null;
        }
        if (caller == null) {
            try
            {
                // Run in doPrivileged as we'll be checked for 
                // "createClassLoader" runtime permission
                caller = (SecureCaller)AccessController.doPrivileged(
                        new PrivilegedExceptionAction<Object>()
                {
                    public Object run() throws Exception
                    {
                        ClassLoader effectiveClassLoader;
                        Class<?> thisClass = getClass();
                        if(classLoader.loadClass(thisClass.getName()) != thisClass) {
                            effectiveClassLoader = thisClass.getClassLoader();
                        } else {
                            effectiveClassLoader = classLoader;
                        }  
                        SecureClassLoaderImpl secCl = 
                            new SecureClassLoaderImpl(effectiveClassLoader);
                        Class<?> c = secCl.defineAndLinkClass(
                                SecureCaller.class.getName() + "Impl", 
                                secureCallerImplBytecode, codeSource);
                        return c.newInstance();
                    }
                });
                classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller));
            }
            catch(PrivilegedActionException ex)
            {
                throw new UndeclaredThrowableException(ex.getCause());
            }
        }
    }
    return caller.call(callable, cx, scope, thisObj, args);
}
 
Example 19
Project: HL4A   File: PolicySecurityController.java   Source Code and License Vote up 4 votes
@Override
public Object callWithDomain(final Object securityDomain, final Context cx,
        Callable callable, Scriptable scope, Scriptable thisObj,
        Object[] args)
{
    // Run in doPrivileged as we might be checked for "getClassLoader"
    // runtime permission
    final ClassLoader classLoader = (ClassLoader)AccessController.doPrivileged(
        new PrivilegedAction<Object>() {
            public Object run() {
                return cx.getApplicationClassLoader();
            }
        });
    final CodeSource codeSource = (CodeSource)securityDomain;
    Map<ClassLoader,SoftReference<SecureCaller>> classLoaderMap;
    synchronized (callers) {
        classLoaderMap = callers.get(codeSource);
        if(classLoaderMap == null) {
            classLoaderMap = new WeakHashMap<ClassLoader,SoftReference<SecureCaller>>();
            callers.put(codeSource, classLoaderMap);
        }
    }
    SecureCaller caller;
    synchronized (classLoaderMap) {
        SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader);
        if (ref != null) {
            caller = ref.get();
        } else {
            caller = null;
        }
        if (caller == null)
        {
            try
            {
                // Run in doPrivileged as we'll be checked for
                // "createClassLoader" runtime permission
                caller = (SecureCaller)AccessController.doPrivileged(
                        new PrivilegedExceptionAction<Object>()
                {
                    public Object run() throws Exception
                    {
                        Loader loader = new Loader(classLoader,
                                codeSource);
                        Class<?> c = loader.defineClass(
                                SecureCaller.class.getName() + "Impl",
                                secureCallerImplBytecode);
                        return c.newInstance();
                    }
                });
                classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller));
            }
            catch(PrivilegedActionException ex)
            {
                throw new UndeclaredThrowableException(ex.getCause());
            }
        }
    }
    return caller.call(callable, cx, scope, thisObj, args);
}
 
Example 20
Project: HL4A   File: SecureCaller.java   Source Code and License Vote up 4 votes
/**
 * Call the specified callable using a protection domain belonging to the
 * specified code source.
 */
static Object callSecurely(final CodeSource codeSource, Callable callable,
        Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
{
    final Thread thread = Thread.currentThread();
    // Run in doPrivileged as we might be checked for "getClassLoader"
    // runtime permission
    final ClassLoader classLoader = (ClassLoader)AccessController.doPrivileged(
        new PrivilegedAction<Object>() {
            public Object run() {
                return thread.getContextClassLoader();
            }
        });
    Map<ClassLoader,SoftReference<SecureCaller>> classLoaderMap;
    synchronized(callers)
    {
        classLoaderMap = callers.get(codeSource);
        if(classLoaderMap == null)
        {
            classLoaderMap = new WeakHashMap<ClassLoader,SoftReference<SecureCaller>>();
            callers.put(codeSource, classLoaderMap);
        }
    }
    SecureCaller caller;
    synchronized(classLoaderMap)
    {
        SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader);
        if (ref != null) {
            caller = ref.get();
        } else {
            caller = null;
        }
        if (caller == null) {
            try
            {
                // Run in doPrivileged as we'll be checked for
                // "createClassLoader" runtime permission
                caller = (SecureCaller)AccessController.doPrivileged(
                        new PrivilegedExceptionAction<Object>()
                {
                    public Object run() throws Exception
                    {
                        ClassLoader effectiveClassLoader;
                        Class<?> thisClass = getClass();
                        if(classLoader.loadClass(thisClass.getName()) != thisClass) {
                            effectiveClassLoader = thisClass.getClassLoader();
                        } else {
                            effectiveClassLoader = classLoader;
                        }
                        SecureClassLoaderImpl secCl =
                            new SecureClassLoaderImpl(effectiveClassLoader);
                        Class<?> c = secCl.defineAndLinkClass(
                                SecureCaller.class.getName() + "Impl",
                                secureCallerImplBytecode, codeSource);
                        return c.newInstance();
                    }
                });
                classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller));
            }
            catch(PrivilegedActionException ex)
            {
                throw new UndeclaredThrowableException(ex.getCause());
            }
        }
    }
    return caller.call(callable, cx, scope, thisObj, args);
}