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);
}
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);
}
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);
}
}
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;
}
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());
}
}
}
Aggregations