Search in sources :

Example 56 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project jdk8u_jdk by JetBrains.

the class RuleBasedBreakIterator method readFile.

protected byte[] readFile(final String datafile) throws IOException, MissingResourceException {
    BufferedInputStream is;
    try {
        is = AccessController.doPrivileged(new PrivilegedExceptionAction<BufferedInputStream>() {

            @Override
            public BufferedInputStream run() throws Exception {
                return new BufferedInputStream(getClass().getResourceAsStream("/sun/text/resources/" + datafile));
            }
        });
    } catch (PrivilegedActionException e) {
        throw new InternalError(e.toString(), e);
    }
    int offset = 0;
    /* First, read magic, version, and header_info. */
    int len = LABEL_LENGTH + 5;
    byte[] buf = new byte[len];
    if (is.read(buf) != len) {
        throw new MissingResourceException("Wrong header length", datafile, "");
    }
    /* Validate the magic number. */
    for (int i = 0; i < LABEL_LENGTH; i++, offset++) {
        if (buf[offset] != LABEL[offset]) {
            throw new MissingResourceException("Wrong magic number", datafile, "");
        }
    }
    /* Validate the version number. */
    if (buf[offset] != supportedVersion) {
        throw new MissingResourceException("Unsupported version(" + buf[offset] + ")", datafile, "");
    }
    /* Read data: totalDataSize + 8(for checksum) */
    len = getInt(buf, ++offset);
    buf = new byte[len];
    if (is.read(buf) != len) {
        throw new MissingResourceException("Wrong data length", datafile, "");
    }
    is.close();
    return buf;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) PrivilegedActionException(java.security.PrivilegedActionException) MissingResourceException(java.util.MissingResourceException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 57 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project jdk8u_jdk by JetBrains.

the class UnixPrintServiceLookup method execCmd.

static String[] execCmd(final String command) {
    ArrayList results = null;
    try {
        final String[] cmd = new String[3];
        if (isSysV() || isAIX()) {
            cmd[0] = "/usr/bin/sh";
            cmd[1] = "-c";
            cmd[2] = "env LC_ALL=C " + command;
        } else {
            cmd[0] = "/bin/sh";
            cmd[1] = "-c";
            cmd[2] = "LC_ALL=C " + command;
        }
        results = (ArrayList) AccessController.doPrivileged(new PrivilegedExceptionAction() {

            public Object run() throws IOException {
                Process proc;
                BufferedReader bufferedReader = null;
                File f = Files.createTempFile("prn", "xc").toFile();
                cmd[2] = cmd[2] + ">" + f.getAbsolutePath();
                proc = Runtime.getRuntime().exec(cmd);
                try {
                    // in case of interrupt.
                    boolean done = false;
                    while (!done) {
                        try {
                            proc.waitFor();
                            done = true;
                        } catch (InterruptedException e) {
                        }
                    }
                    if (proc.exitValue() == 0) {
                        FileReader reader = new FileReader(f);
                        bufferedReader = new BufferedReader(reader);
                        String line;
                        ArrayList results = new ArrayList();
                        while ((line = bufferedReader.readLine()) != null) {
                            results.add(line);
                        }
                        return results;
                    }
                } finally {
                    f.delete();
                    // promptly close all streams.
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    proc.getInputStream().close();
                    proc.getErrorStream().close();
                    proc.getOutputStream().close();
                }
                return null;
            }
        });
    } catch (PrivilegedActionException e) {
    }
    if (results == null) {
        return new String[0];
    } else {
        return (String[]) results.toArray(new String[results.size()]);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) File(java.io.File)

Example 58 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project jdk8u_jdk by JetBrains.

the class ActivationGroupImpl method newInstance.

/**
     * Creates a new instance of an activatable remote object. The
     * <code>Activator</code> calls this method to create an activatable
     * object in this group. This method should be idempotent; a call to
     * activate an already active object should return the previously
     * activated object.
     *
     * Note: this method assumes that the Activator will only invoke
     * newInstance for the same object in a serial fashion (i.e.,
     * the activator will not allow the group to see concurrent requests
     * to activate the same object.
     *
     * @param id the object's activation identifier
     * @param desc the object's activation descriptor
     * @return a marshalled object containing the activated object's stub
     */
public MarshalledObject<? extends Remote> newInstance(final ActivationID id, final ActivationDesc desc) throws ActivationException, RemoteException {
    RegistryImpl.checkAccess("ActivationInstantiator.newInstance");
    if (!groupID.equals(desc.getGroupID()))
        throw new ActivationException("newInstance in wrong group");
    try {
        acquireLock(id);
        synchronized (this) {
            if (groupInactive == true)
                throw new InactiveGroupException("group is inactive");
        }
        ActiveEntry entry = active.get(id);
        if (entry != null)
            return entry.mobj;
        String className = desc.getClassName();
        final Class<? extends Remote> cl = RMIClassLoader.loadClass(desc.getLocation(), className).asSubclass(Remote.class);
        Remote impl = null;
        final Thread t = Thread.currentThread();
        final ClassLoader savedCcl = t.getContextClassLoader();
        ClassLoader objcl = cl.getClassLoader();
        final ClassLoader ccl = covers(objcl, savedCcl) ? objcl : savedCcl;
        /*
             * Fix for 4164971: allow non-public activatable class
             * and/or constructor, create the activatable object in a
             * privileged block
             */
        try {
            /*
                 * The code below is in a doPrivileged block to
                 * protect against user code which code might have set
                 * a global socket factory (in which case application
                 * code would be on the stack).
                 */
            impl = AccessController.doPrivileged(new PrivilegedExceptionAction<Remote>() {

                public Remote run() throws InstantiationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
                    Constructor<? extends Remote> constructor = cl.getDeclaredConstructor(ActivationID.class, MarshalledObject.class);
                    constructor.setAccessible(true);
                    try {
                        /*
                               * Fix for 4289544: make sure to set the
                               * context class loader to be the class
                               * loader of the impl class before
                               * constructing that class.
                               */
                        t.setContextClassLoader(ccl);
                        return constructor.newInstance(id, desc.getData());
                    } finally {
                        t.setContextClassLoader(savedCcl);
                    }
                }
            });
        } catch (PrivilegedActionException pae) {
            Throwable e = pae.getException();
            // narrow the exception's type and rethrow it
            if (e instanceof InstantiationException) {
                throw (InstantiationException) e;
            } else if (e instanceof NoSuchMethodException) {
                throw (NoSuchMethodException) e;
            } else if (e instanceof IllegalAccessException) {
                throw (IllegalAccessException) e;
            } else if (e instanceof InvocationTargetException) {
                throw (InvocationTargetException) e;
            } else if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else if (e instanceof Error) {
                throw (Error) e;
            }
        }
        entry = new ActiveEntry(impl);
        active.put(id, entry);
        return entry.mobj;
    } catch (NoSuchMethodException | NoSuchMethodError e) {
        /* user forgot to provide activatable constructor?
             * or code recompiled and user forgot to provide
             *  activatable constructor?
             */
        throw new ActivationException("Activatable object must provide an activation" + " constructor", e);
    } catch (InvocationTargetException e) {
        throw new ActivationException("exception in object constructor", e.getTargetException());
    } catch (Exception e) {
        throw new ActivationException("unable to activate object", e);
    } finally {
        releaseLock(id);
        checkInactiveGroup();
    }
}
Also used : ActivationException(java.rmi.activation.ActivationException) PrivilegedActionException(java.security.PrivilegedActionException) Remote(java.rmi.Remote) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchObjectException(java.rmi.NoSuchObjectException) ActivationException(java.rmi.activation.ActivationException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) UnknownObjectException(java.rmi.activation.UnknownObjectException) RMIClassLoader(java.rmi.server.RMIClassLoader)

Example 59 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project jdk8u_jdk by JetBrains.

the class Krb5AcceptCredential method getInstance.

static Krb5AcceptCredential getInstance(final GSSCaller caller, Krb5NameElement name) throws GSSException {
    final String serverPrinc = (name == null ? null : name.getKrb5PrincipalName().getName());
    final AccessControlContext acc = AccessController.getContext();
    ServiceCreds creds = null;
    try {
        creds = AccessController.doPrivileged(new PrivilegedExceptionAction<ServiceCreds>() {

            public ServiceCreds run() throws Exception {
                return Krb5Util.getServiceCreds(caller == GSSCaller.CALLER_UNKNOWN ? GSSCaller.CALLER_ACCEPT : caller, serverPrinc, acc);
            }
        });
    } catch (PrivilegedActionException e) {
        GSSException ge = new GSSException(GSSException.NO_CRED, -1, "Attempt to obtain new ACCEPT credentials failed!");
        ge.initCause(e.getException());
        throw ge;
    }
    if (creds == null)
        throw new GSSException(GSSException.NO_CRED, -1, "Failed to find any Kerberos credentails");
    if (name == null) {
        String fullName = creds.getName();
        if (fullName != null) {
            name = Krb5NameElement.getInstance(fullName, Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
        }
    }
    return new Krb5AcceptCredential(name, creds);
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 60 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project jdk8u_jdk by JetBrains.

the class AppContextCreator method findClass.

/*
     * Finds the applet class with the specified name. First searches
     * loaded JAR files then the applet code base for the class.
     */
protected Class findClass(String name) throws ClassNotFoundException {
    int index = name.indexOf(";");
    String cookie = "";
    if (index != -1) {
        cookie = name.substring(index, name.length());
        name = name.substring(0, index);
    }
    // check loaded JAR files
    try {
        return super.findClass(name);
    } catch (ClassNotFoundException e) {
    }
    // during resource requests. [stanley.ho]
    if (codebaseLookup == false)
        throw new ClassNotFoundException(name);
    //      final String path = name.replace('.', '/').concat(".class").concat(cookie);
    String encodedName = ParseUtil.encodePath(name.replace('.', '/'), false);
    final String path = (new StringBuffer(encodedName)).append(".class").append(cookie).toString();
    try {
        byte[] b = (byte[]) AccessController.doPrivileged(new PrivilegedExceptionAction() {

            public Object run() throws IOException {
                try {
                    URL finalURL = new URL(base, path);
                    // Make sure the codebase won't be modified
                    if (base.getProtocol().equals(finalURL.getProtocol()) && base.getHost().equals(finalURL.getHost()) && base.getPort() == finalURL.getPort()) {
                        return getBytes(finalURL);
                    } else {
                        return null;
                    }
                } catch (Exception e) {
                    return null;
                }
            }
        }, acc);
        if (b != null) {
            return defineClass(name, b, 0, b.length, codesource);
        } else {
            throw new ClassNotFoundException(name);
        }
    } catch (PrivilegedActionException e) {
        throw new ClassNotFoundException(name, e.getException());
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) URL(java.net.URL) NullPointerException(java.lang.NullPointerException) NoSuchElementException(java.util.NoSuchElementException) PrivilegedActionException(java.security.PrivilegedActionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)387 IOException (java.io.IOException)199 PrivilegedActionException (java.security.PrivilegedActionException)135 Test (org.junit.Test)104 Connection (org.apache.hadoop.hbase.client.Connection)81 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)76 Table (org.apache.hadoop.hbase.client.Table)62 TableName (org.apache.hadoop.hbase.TableName)57 Result (org.apache.hadoop.hbase.client.Result)56 Scan (org.apache.hadoop.hbase.client.Scan)55 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)53 Delete (org.apache.hadoop.hbase.client.Delete)48 InterruptedIOException (java.io.InterruptedIOException)47 Cell (org.apache.hadoop.hbase.Cell)38 CellScanner (org.apache.hadoop.hbase.CellScanner)38 Configuration (org.apache.hadoop.conf.Configuration)36 File (java.io.File)33 AuthorizationException (org.apache.hadoop.security.authorize.AuthorizationException)33 Path (org.apache.hadoop.fs.Path)23 ArrayList (java.util.ArrayList)22