Java Code Examples for java.security.PrivilegedActionException#getException()

The following examples show how to use java.security.PrivilegedActionException#getException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public synchronized OutputStream getOutputStream() throws IOException {
    connecting = true;
    SocketPermission p = URLtoSocketPermission(this.url);

    if (p != null) {
        try {
            return AccessController.doPrivileged(
                new PrivilegedExceptionAction<OutputStream>() {
                    public OutputStream run() throws IOException {
                        return getOutputStream0();
                    }
                }, null, p
            );
        } catch (PrivilegedActionException e) {
            throw (IOException) e.getException();
        }
    } else {
        return getOutputStream0();
    }
}
 
Example 2
/**
 * Remove this Session from the active Sessions for this Manager,
 * and from the Store.
 *
 * @param id Session's id to be removed
 */    
protected void removeSession(String id){
    try {
        if (SecurityUtil.isPackageProtectionEnabled()){
            try{
                AccessController.doPrivileged(new PrivilegedStoreRemove(id));
            }catch(PrivilegedActionException ex){
                Exception exception = ex.getException();
                log.error("Exception in the Store during removeSession: "
                          + exception, exception);
            }
        } else {
             store.remove(id);
        }               
    } catch (IOException e) {
        log.error("Exception removing session  " + e.getMessage(), e);
    }        
}
 
Example 3
NamingEnumeration<InputStream> getResources(final ClassLoader cl,
        final String name) throws IOException {
    Enumeration<URL> urls;
    try {
        urls = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Enumeration<URL>>() {
                public Enumeration<URL> run() throws IOException {
                    return (cl == null)
                        ? ClassLoader.getSystemResources(name)
                        : cl.getResources(name);
                }
            }
        );
    } catch (PrivilegedActionException e) {
        throw (IOException)e.getException();
    }
    return new InputStreamEnumeration(urls);
}
 
Example 4
public T call() throws Exception {
    try {
        return AccessController.doPrivileged(
            new PrivilegedExceptionAction<T>() {
                public T run() throws Exception {
                    Thread t = Thread.currentThread();
                    ClassLoader cl = t.getContextClassLoader();
                    if (ccl == cl) {
                        return task.call();
                    } else {
                        t.setContextClassLoader(ccl);
                        try {
                            return task.call();
                        } finally {
                            t.setContextClassLoader(cl);
                        }
                    }
                }
            }, acc);
    } catch (PrivilegedActionException e) {
        throw e.getException();
    }
}
 
Example 5
/**
 * Activate the object for this id.
 *
 * @param force if true, forces the activator to contact the group
 * when activating the object (instead of returning a cached reference);
 * if false, returning a cached value is acceptable.
 * @return the reference to the active remote object
 * @exception ActivationException if activation fails
 * @exception UnknownObjectException if the object is unknown
 * @exception RemoteException if remote call fails
 * @since 1.2
 */
public Remote activate(boolean force)
    throws ActivationException, UnknownObjectException, RemoteException
{
    try {
        MarshalledObject<? extends Remote> mobj =
            activator.activate(this, force);
        return AccessController.doPrivileged(
            new PrivilegedExceptionAction<Remote>() {
                public Remote run() throws IOException, ClassNotFoundException {
                    return mobj.get();
                }
            }, NOPERMS_ACC);
    } catch (PrivilegedActionException pae) {
        Exception ex = pae.getException();
        if (ex instanceof RemoteException) {
            throw (RemoteException) ex;
        } else {
            throw new UnmarshalException("activation failed", ex);
        }
    }

}
 
Example 6
static FileInputStream getFileInputStream(final File file)
throws FileNotFoundException
{
    try {
        return (FileInputStream)
        AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws FileNotFoundException {
                return new FileInputStream(file);
            }
        });
    } catch (PrivilegedActionException e) {
        throw (FileNotFoundException)e.getException();
    }
}
 
Example 7
@Override
public Configuration[] listConfigurations(final String filterString)
    throws IOException, InvalidSyntaxException
{
  Configuration[] configurations = null;

  try {
    configurations =
      AccessController
          .doPrivileged(new PrivilegedExceptionAction<Configuration[]>() {
            @Override
            public Configuration[] run()
                throws IOException, InvalidSyntaxException
            {
              return ConfigurationAdminFactory.this
                  .listConfigurations(filterString,
                                      ConfigurationAdminImpl.this.callingBundle,
                                      true);
            }
          });
  } catch (final PrivilegedActionException e) {
    final Exception ee = e.getException();
    // Android don't supply nested exception
    if (ee != null) {
      if (ee instanceof InvalidSyntaxException) {
        throw (InvalidSyntaxException) ee;
      }
      throw (IOException) ee;
    } else {
      throw new IOException("Failed to handle CM data");
    }
  }
  return configurations;
}
 
Example 8
EC(PublicKey key) throws KeyException {
    super(key);
    ECPublicKey ecKey = (ECPublicKey)key;
    ECPoint ecPoint = ecKey.getW();
    ecParams = ecKey.getParams();
    try {
        AccessController.doPrivileged(
            new PrivilegedExceptionAction<Void>() {
                public Void run() throws
                    ClassNotFoundException, NoSuchMethodException
                {
                    getMethods();
                    return null;
                }
            }
        );
    } catch (PrivilegedActionException pae) {
        throw new KeyException("ECKeyValue not supported",
                                pae.getException());
    }
    Object[] args = new Object[] { ecPoint, ecParams.getCurve() };
    try {
        ecPublicKey = (byte[])encodePoint.invoke(null, args);
    } catch (IllegalAccessException iae) {
        throw new KeyException(iae);
    } catch (InvocationTargetException ite) {
        throw new KeyException(ite);
    }
}
 
Example 9
FileInputStream getFileInputStream(final File file)
throws FileNotFoundException
{
    try {
        return (FileInputStream)
        AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws FileNotFoundException {
                return new FileInputStream(file);
            }
        });
    } catch (PrivilegedActionException e) {
        throw (FileNotFoundException)e.getException();
    }
}
 
Example 10
/**
 * Does something using the Subject inside
 * @param action the action
 * @param in the input byte
 * @return the output byte
 * @throws java.lang.Exception
 */
public byte[] doAs(final Action action, final byte[] in) throws Exception {
    try {
        return Subject.doAs(s, new PrivilegedExceptionAction<byte[]>() {

            @Override
            public byte[] run() throws Exception {
                return action.run(Context.this, in);
            }
        });
    } catch (PrivilegedActionException pae) {
        throw pae.getException();
    }
}
 
Example 11
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
                              final String fieldName,
                              final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    Class<?> fieldt = field.getType();
    if (fieldt != int.class)
        throw new IllegalArgumentException("Must be integer type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 12
Source Project: rome   File: SecuritySupport.java    License: Apache License 2.0 5 votes vote down vote up
FileInputStream getFileInputStream(final File file) throws FileNotFoundException {
    try {
        final PrivilegedExceptionAction<FileInputStream> action = new PrivilegedExceptionAction<FileInputStream>() {
            @Override
            public FileInputStream run() throws FileNotFoundException {
                return new FileInputStream(file);
            }
        };
        return AccessController.doPrivileged(action);
    } catch (final PrivilegedActionException e) {
        throw (FileNotFoundException) e.getException();
    }
}
 
Example 13
Source Project: cxf   File: JAXBEncoderDecoder.java    License: Apache License 2.0 5 votes vote down vote up
public static Object unmarshall(final Unmarshaller u,
                                final Object source,
                                final QName elName,
                                final Class<?> clazz,
                                final boolean unwrap) {
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
            public Object run() throws Exception {
                return doUnmarshal(u, source, elName, clazz, unwrap);
            }
        });
    } catch (PrivilegedActionException e) {
        Exception ex = e.getException();
        if (ex instanceof Fault) {
            throw (Fault)ex;
        }
        if (ex instanceof javax.xml.bind.UnmarshalException) {
            javax.xml.bind.UnmarshalException unmarshalEx = (javax.xml.bind.UnmarshalException)ex;
            if (unmarshalEx.getLinkedException() != null) {
                throw new Fault(new Message("UNMARSHAL_ERROR", LOG,
                                        unmarshalEx.getLinkedException().getMessage()), ex);
            }
            throw new Fault(new Message("UNMARSHAL_ERROR", LOG,
                                        unmarshalEx.getMessage()), ex);
        }
        throw new Fault(new Message("UNMARSHAL_ERROR", LOG, ex.getMessage()), ex);
    }
}
 
Example 14
private static void assertNotDirectory(final File f) throws IOException {
    try {
        AccessController.doPrivileged (new PrivilegedExceptionAction() {
            public Object run() throws IOException {
                assertFalse("assertNotDirectory failed: " +
                      f.getCanonicalPath(), f.isDirectory());
                return null;
            }
        });
    } catch (PrivilegedActionException e) {
        // e.getException() should be an instance of IOException.
        throw (IOException) e.getException();
    }
}
 
Example 15
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
                              final String fieldName,
                              final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != int.class)
        throw new IllegalArgumentException("Must be integer type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 16
/**
 * Given a URL, retrieves a JAR file, caches it to disk, and creates a
 * cached JAR file object.
 */
 private static JarFile retrieve(final URL url, final URLJarFileCloseController closeController) throws IOException {
    /*
     * See if interface is set, then call retrieve function of the class
     * that implements URLJarFileCallBack interface (sun.plugin - to
     * handle the cache failure for JARJAR file.)
     */
    if (callback != null)
    {
        return callback.retrieve(url);
    }

    else
    {

        JarFile result = null;

        /* get the stream before asserting privileges */
        try (final InputStream in = url.openConnection().getInputStream()) {
            result = AccessController.doPrivileged(
                new PrivilegedExceptionAction<JarFile>() {
                    public JarFile run() throws IOException {
                        Path tmpFile = Files.createTempFile("jar_cache", null);
                        try {
                            Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
                            JarFile jarFile = new URLJarFile(tmpFile.toFile(), closeController);
                            tmpFile.toFile().deleteOnExit();
                            return jarFile;
                        } catch (Throwable thr) {
                            try {
                                Files.delete(tmpFile);
                            } catch (IOException ioe) {
                                thr.addSuppressed(ioe);
                            }
                            throw thr;
                        }
                    }
                });
        } catch (PrivilegedActionException pae) {
            throw (IOException) pae.getException();
        }

        return result;
    }
}
 
Example 17
LockedUpdater(final Class<T> tclass, final String fieldName,
              final Class<?> caller) {
    Field field = null;
    int modifiers = 0;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != long.class)
        throw new IllegalArgumentException("Must be long type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 18
Source Project: jdk-1.7-annotated   File: Calendar.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Reconstitutes this object from a stream (i.e., deserialize it).
 */
private void readObject(ObjectInputStream stream)
     throws IOException, ClassNotFoundException
{
    final ObjectInputStream input = stream;
    input.defaultReadObject();

    stamp = new int[FIELD_COUNT];

    // Starting with version 2 (not implemented yet), we expect that
    // fields[], isSet[], isTimeSet, and areFieldsSet may not be
    // streamed out anymore.  We expect 'time' to be correct.
    if (serialVersionOnStream >= 2)
    {
        isTimeSet = true;
        if (fields == null) fields = new int[FIELD_COUNT];
        if (isSet == null) isSet = new boolean[FIELD_COUNT];
    }
    else if (serialVersionOnStream >= 0)
    {
        for (int i=0; i<FIELD_COUNT; ++i)
            stamp[i] = isSet[i] ? COMPUTED : UNSET;
    }

    serialVersionOnStream = currentSerialVersion;

    // If there's a ZoneInfo object, use it for zone.
    ZoneInfo zi = null;
    try {
        zi = AccessController.doPrivileged(
                new PrivilegedExceptionAction<ZoneInfo>() {
                    public ZoneInfo run() throws Exception {
                        return (ZoneInfo) input.readObject();
                    }
                },
                CalendarAccessControlContext.INSTANCE);
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (!(e instanceof OptionalDataException)) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else if (e instanceof IOException) {
                throw (IOException) e;
            } else if (e instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) e;
            }
            throw new RuntimeException(e);
        }
    }
    if (zi != null) {
        zone = zi;
    }

    // If the deserialized object has a SimpleTimeZone, try to
    // replace it with a ZoneInfo equivalent (as of 1.4) in order
    // to be compatible with the SimpleTimeZone-based
    // implementation as much as possible.
    if (zone instanceof SimpleTimeZone) {
        String id = zone.getID();
        TimeZone tz = TimeZone.getTimeZone(id);
        if (tz != null && tz.hasSameRules(zone) && tz.getID().equals(id)) {
            zone = tz;
        }
    }
}
 
Example 19
protected boolean checkExtensionAgainst(String extensionName,
                                        Attributes attr,
                                        final File file)
    throws IOException,
           FileNotFoundException,
           ExtensionInstallationException
{

    debug("Checking extension " + extensionName +
          " against " + file.getName());

    // Load the jar file ...
    Manifest man;
    try {
        man = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Manifest>() {
                public Manifest run()
                        throws IOException, FileNotFoundException {
                     if (!file.exists())
                         throw new FileNotFoundException(file.getName());
                     JarFile jarFile =  new JarFile(file);
                     return jarFile.getManifest();
                 }
             });
    } catch(PrivilegedActionException e) {
        if (e.getException() instanceof FileNotFoundException)
            throw (FileNotFoundException) e.getException();
        throw (IOException) e.getException();
    }

    // Construct the extension information object
    ExtensionInfo reqInfo = new ExtensionInfo(extensionName, attr);
    debug("Requested Extension : " + reqInfo);

    int isCompatible = ExtensionInfo.INCOMPATIBLE;
    ExtensionInfo instInfo = null;

    if (man != null) {
        Attributes instAttr = man.getMainAttributes();
        if (instAttr != null) {
            instInfo = new ExtensionInfo(null, instAttr);
            debug("Extension Installed " + instInfo);
            isCompatible = instInfo.isCompatibleWith(reqInfo);
            switch(isCompatible) {
            case ExtensionInfo.COMPATIBLE:
                debug("Extensions are compatible");
                return true;

            case ExtensionInfo.INCOMPATIBLE:
                debug("Extensions are incompatible");
                return false;

            default:
                // everything else
                debug("Extensions require an upgrade or vendor switch");
                return installExtension(reqInfo, instInfo);

            }
        }
    }
    return false;
}
 
Example 20
Source Project: JDKSourceCode1.8   File: Init.java    License: MIT License 4 votes vote down vote up
/**
 * Dynamically initialise the library by registering the default algorithms/implementations
 */
private static void dynamicInit() {
    //
    // Load the Resource Bundle - the default is the English resource bundle.
    // To load another resource bundle, call I18n.init(...) before calling this
    // method.
    //
    I18n.init("en", "US");

    if (log.isLoggable(java.util.logging.Level.FINE)) {
        log.log(java.util.logging.Level.FINE, "Registering default algorithms");
    }
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Void>(){
            @Override public Void run() throws XMLSecurityException {
                //
                // Bind the default prefixes
                //
                ElementProxy.registerDefaultPrefixes();

                //
                // Set the default Transforms
                //
                Transform.registerDefaultAlgorithms();

                //
                // Set the default signature algorithms
                //
                SignatureAlgorithm.registerDefaultAlgorithms();

                //
                // Set the default JCE algorithms
                //
                JCEMapper.registerDefaultAlgorithms();

                //
                // Set the default c14n algorithms
                //
                Canonicalizer.registerDefaultAlgorithms();

                //
                // Register the default resolvers
                //
                ResourceResolver.registerDefaultResolvers();

                //
                // Register the default key resolvers
                //
                KeyResolver.registerDefaultResolvers();

                return null;
            }
       });
    } catch (PrivilegedActionException ex) {
        XMLSecurityException xse = (XMLSecurityException)ex.getException();
        log.log(java.util.logging.Level.SEVERE, xse.getMessage(), xse);
        xse.printStackTrace();
    }
}