Search in sources :

Example 96 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project ecf by eclipse.

the class RemoteServiceRegistrationImpl method callService.

public Object callService(IRemoteCall call) throws Exception {
    Object[] callArgs = call.getParameters();
    Object[] args = (callArgs == null) ? NULL_ARGS : callArgs;
    final Method method = ClassUtil.getMethod(service.getClass(), call.getMethod(), getTypesForParameters(args));
    AccessController.doPrivileged(new PrivilegedExceptionAction() {

        public Object run() throws Exception {
            if (!method.isAccessible())
                method.setAccessible(true);
            return null;
        }
    });
    return method.invoke(service, args);
}
Also used : Method(java.lang.reflect.Method) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 97 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project ecf by eclipse.

the class RemoteServiceAdmin method exportService.

// RemoteServiceAdmin service interface impl methods
public Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> exportService(final ServiceReference<?> serviceReference, Map<String, ?> op) {
    trace("exportService", // $NON-NLS-1$ //$NON-NLS-2$
    "serviceReference=" + serviceReference + ",properties=" + // $NON-NLS-1$
    op);
    final Map<String, ?> overridingProperties = PropertiesUtil.mergeProperties(serviceReference, op == null ? Collections.EMPTY_MAP : op);
    // get exported interfaces
    final String[] exportedInterfaces = PropertiesUtil.getExportedInterfaces(serviceReference, overridingProperties);
    if (exportedInterfaces == null)
        throw new IllegalArgumentException(// $NON-NLS-1$
        org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_INTERFACES + " not set");
    // verifyExportedInterfaces
    if (!validExportedInterfaces(serviceReference, exportedInterfaces))
        return Collections.EMPTY_LIST;
    // Get optional exported configs
    String[] ecs = PropertiesUtil.getStringArrayFromPropertyValue(overridingProperties.get(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_CONFIGS));
    if (ecs == null) {
        ecs = PropertiesUtil.getStringArrayFromPropertyValue(serviceReference.getProperty(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_EXPORTED_CONFIGS));
    }
    final String[] exportedConfigs = ecs;
    // Get all intents (service.intents, service.exported.intents,
    // service.exported.intents.extra)
    final String[] serviceIntents = PropertiesUtil.getServiceIntents(serviceReference, overridingProperties);
    // Create result registrations. This collection will be returned
    Collection<ExportRegistration> resultRegistrations = new ArrayList<ExportRegistration>();
    // check for previously exported registration for the serviceReference
    synchronized (exportedRegistrations) {
        ExportEndpoint exportEndpoint = findExistingExportEndpoint(serviceReference, null);
        // If found then create a second ExportRegistration from endpoint
        if (exportEndpoint != null) {
            trace("exportService", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            "serviceReference=" + serviceReference + " export endpoint already exists=" + exportEndpoint + // $NON-NLS-1$
            ".  Returning new ExportRegistration for existing endpoint");
            ExportRegistration reg = new ExportRegistration(exportEndpoint);
            addExportRegistration(reg);
            resultRegistrations.add(reg);
        }
    }
    // If the serviceReference hasn't already been exported before (above)
    if (resultRegistrations.size() == 0) {
        // Get a host container selector
        final IHostContainerSelector hostContainerSelector = getHostContainerSelector();
        // and use it to select ECF remote service containers that match given exported
        // interfaces, configs, and intents
        IRemoteServiceContainer[] rsContainers = null;
        try {
            rsContainers = AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws SelectContainerException {
                    return hostContainerSelector.selectHostContainers(serviceReference, (Map<String, Object>) overridingProperties, exportedInterfaces, exportedConfigs, serviceIntents);
                }
            });
        } catch (PrivilegedActionException e) {
            Exception except = e.getException();
            // see discussion on osgi bug
            // https://www.osgi.org/members/bugzilla/show_bug.cgi?id=2591
            // $NON-NLS-1$
            String errorMessage = "Failed to select host container";
            if (except instanceof SelectContainerException) {
                SelectContainerException sce = (SelectContainerException) except;
                Throwable sceCause = sce.getCause();
                if (sceCause instanceof ContainerCreateException) {
                    // Some dummy props need to be set to allow the creation of a dummy export
                    // registration
                    Map<String, Object> props = new HashMap<String, Object>(overridingProperties);
                    // $NON-NLS-1$
                    props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID, "0");
                    props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS, // $NON-NLS-1$
                    "import.error.config");
                    // $NON-NLS-1$
                    props.put(RemoteConstants.ENDPOINT_ID, "export.error.id");
                    props.put(RemoteConstants.ENDPOINT_CONTAINER_ID_NAMESPACE, StringID.class.getName());
                    ExportRegistration errorRegistration = new RemoteServiceAdmin.ExportRegistration(sceCause, new EndpointDescription(serviceReference, props));
                    addExportRegistration(errorRegistration);
                    resultRegistrations.add(errorRegistration);
                } else
                    throw new IllegalArgumentException(errorMessage, except);
            } else
                throw new IllegalArgumentException(errorMessage, except);
        }
        // If no registration exist (no errorRegistration added above)
        if (resultRegistrations.size() == 0) {
            // If no containers found above, log warning and return
            if (rsContainers == null || rsContainers.length == 0) {
                String errorMessage = // $NON-NLS-1$
                "No containers found for serviceReference=" + serviceReference + " properties=" + // $NON-NLS-1$
                overridingProperties + // $NON-NLS-1$
                ". Remote service NOT EXPORTED";
                // $NON-NLS-1$
                logWarning("exportService", errorMessage);
                return Collections.EMPTY_LIST;
            }
            // actually do the export
            synchronized (exportedRegistrations) {
                // For all selected containers
                for (int i = 0; i < rsContainers.length; i++) {
                    Map endpointDescriptionProperties = createExportEndpointDescriptionProperties(serviceReference, (Map<String, Object>) overridingProperties, exportedInterfaces, serviceIntents, rsContainers[i]);
                    // otherwise, actually export the service to create
                    // a new ExportEndpoint and use it to create a new
                    // ExportRegistration
                    EndpointDescription endpointDescription = new EndpointDescription(endpointDescriptionProperties);
                    checkEndpointPermission(endpointDescription, EndpointPermission.EXPORT);
                    ExportRegistration exportRegistration = null;
                    try {
                        // Actually do the export and return export
                        // registration
                        exportRegistration = exportService(serviceReference, overridingProperties, exportedInterfaces, rsContainers[i], endpointDescriptionProperties);
                    } catch (Exception e) {
                        exportRegistration = new ExportRegistration(e, endpointDescription);
                    }
                    addExportRegistration(exportRegistration);
                    // We add it to the results in either success or error case
                    resultRegistrations.add(exportRegistration);
                }
            }
        }
    }
    // publish all activeExportRegistrations
    for (ExportRegistration exportReg : resultRegistrations) publishExportEvent(exportReg);
    // $NON-NLS-1$ //$NON-NLS-2$
    trace("exportService", "exported registrations=" + resultRegistrations);
    // and return
    return new ArrayList<org.osgi.service.remoteserviceadmin.ExportRegistration>(resultRegistrations);
}
Also used : ArrayList(java.util.ArrayList) ContainerCreateException(org.eclipse.ecf.core.ContainerCreateException) IRemoteServiceContainer(org.eclipse.ecf.remoteservice.IRemoteServiceContainer) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) ECFException(org.eclipse.ecf.core.util.ECFException) ServiceException(org.osgi.framework.ServiceException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) PrivilegedActionException(java.security.PrivilegedActionException) ContainerCreateException(org.eclipse.ecf.core.ContainerCreateException) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 98 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project directory-ldap-api by apache.

the class LdapNetworkConnection method bindAsync.

/**
 * Do an asynchronous bind, based on a GssApiRequest.
 *
 * @param request The GssApiRequest POJO containing all the needed parameters
 * @return The bind operation's future
 * @throws LdapException if some error occurred
 */
public BindFuture bindAsync(SaslGssApiRequest request) throws LdapException {
    // Krb5.conf file
    if (request.getKrb5ConfFilePath() != null) {
        // Using the krb5.conf file provided by the user
        System.setProperty(KRB5_CONF, request.getKrb5ConfFilePath());
    } else if ((request.getRealmName() != null) && (request.getKdcHost() != null) && (request.getKdcPort() != 0)) {
        try {
            // Using a custom krb5.conf we create from the settings provided by the user
            String krb5ConfPath = createKrb5ConfFile(request.getRealmName(), request.getKdcHost(), request.getKdcPort());
            System.setProperty(KRB5_CONF, krb5ConfPath);
        } catch (IOException ioe) {
            throw new LdapException(ioe);
        }
    } else {
        // Using the system Kerberos configuration
        System.clearProperty(KRB5_CONF);
    }
    // Login Module configuration
    if (request.getLoginModuleConfiguration() != null) {
        // Using the configuration provided by the user
        Configuration.setConfiguration(request.getLoginModuleConfiguration());
    } else {
        // Using the default configuration
        Configuration.setConfiguration(new Krb5LoginConfiguration());
    }
    try {
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "true");
        LoginContext loginContext = new LoginContext(request.getLoginContextName(), new SaslCallbackHandler(request));
        loginContext.login();
        final SaslGssApiRequest requetFinal = request;
        return (BindFuture) Subject.doAs(loginContext.getSubject(), new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws Exception {
                return bindSasl(requetFinal);
            }
        });
    } catch (Exception e) {
        throw new LdapException(e);
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) SaslCallbackHandler(org.apache.directory.ldap.client.api.callback.SaslCallbackHandler) IOException(java.io.IOException) BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 99 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project ma-core-public by infiniteautomation.

the class Utf8ResourceBundleControl method newBundle.

@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, final ClassLoader loader, final boolean reload) throws InstantiationException, IllegalAccessException, IOException {
    String bundleName = toBundleName(baseName, locale);
    ResourceBundle bundle = null;
    if (format.equals("java.properties")) {
        final String resourceName = toResourceName(bundleName, "properties");
        InputStream stream = null;
        try {
            stream = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() {

                public InputStream run() throws IOException {
                    InputStream is = null;
                    if (reload) {
                        URL url = loader.getResource(resourceName);
                        if (url != null) {
                            URLConnection connection = url.openConnection();
                            if (connection != null) {
                                // Disable caches to get fresh data for
                                // reloading.
                                connection.setUseCaches(false);
                                is = connection.getInputStream();
                            }
                        }
                    } else {
                        is = loader.getResourceAsStream(resourceName);
                    }
                    return is;
                }
            });
        } catch (PrivilegedActionException e) {
            throw (IOException) e.getException();
        }
        if (stream != null) {
            try {
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
    } else
        return super.newBundle(baseName, locale, format, loader, reload);
    return bundle;
}
Also used : InputStreamReader(java.io.InputStreamReader) PrivilegedActionException(java.security.PrivilegedActionException) InputStream(java.io.InputStream) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) URL(java.net.URL) URLConnection(java.net.URLConnection) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 100 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project eclipse-collections by eclipse.

the class ConcurrentHashMapUnsafe method getUnsafe.

private static Unsafe getUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException ignored) {
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() {

                public Unsafe run() throws Exception {
                    Field f = Unsafe.class.getDeclaredField("theUnsafe");
                    f.setAccessible(true);
                    return (Unsafe) f.get(null);
                }
            });
        } catch (PrivilegedActionException e) {
            throw new RuntimeException("Could not initialize intrinsics", e.getCause());
        }
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedActionException(java.security.PrivilegedActionException) Unsafe(sun.misc.Unsafe) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

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