From 502958b72ac0bcad889c519991d020a25a49ee7b Mon Sep 17 00:00:00 2001 From: J-N-K Date: Fri, 30 Dec 2022 16:32:34 +0100 Subject: [PATCH] Remove AccessController.doPrivileged calls (#3271) The `AccessController` and the `SecurityManager` is deprecated for removal in Java 17. We don't make use of the `SecurityManager` anyway, so we can safely remove it. Signed-off-by: Jan N. Klug GitOrigin-RevId: 98b4902c4ab9d764f2839a330d9a3901d38e05d6 --- .../oauth2client/internal/OAuthConnector.java | 43 ++-- .../internal/TriggerHandlerCallbackImpl.java | 7 +- .../common/AbstractInvocationHandler.java | 11 +- .../common/SafeCallerBuilderImpl.java | 38 ++- .../discovery/DiscoveryResultBuilder.java | 9 +- .../DiscoveryServiceRegistryImpl.java | 27 +- .../internal/events/OSGiEventPublisher.java | 31 +-- .../http/internal/WebClientFactoryImpl.java | 232 ++++++++---------- .../core/thing/internal/ThingManagerImpl.java | 38 ++- 9 files changed, 164 insertions(+), 272 deletions(-) diff --git a/bundles/org.opensmarthouse.core.auth.oauth2client.core/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthConnector.java b/bundles/org.opensmarthouse.core.auth.oauth2client.core/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthConnector.java index eec68d1a616..37fabbd8768 100644 --- a/bundles/org.opensmarthouse.core.auth.oauth2client.core/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthConnector.java +++ b/bundles/org.opensmarthouse.core.auth.oauth2client.core/src/main/java/org/openhab/core/auth/oauth2client/internal/OAuthConnector.java @@ -18,9 +18,6 @@ import java.lang.reflect.InvocationTargetException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; import java.time.Instant; import java.util.Base64; import java.util.concurrent.ExecutionException; @@ -137,7 +134,7 @@ public String getAuthorizationUrl(String authorizationEndpoint, String clientId, * @return Access Token * @throws IOException IO/ network exceptions * @throws OAuthException Other exceptions - * @throws OAuthErrorException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error + * @throws OAuthResponseException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error * Response */ public AccessTokenResponse grantTypePassword(String tokenUrl, String username, String password, @@ -171,7 +168,7 @@ public AccessTokenResponse grantTypePassword(String tokenUrl, String username, S * @return Access Token * @throws IOException IO/ network exceptions * @throws OAuthException Other exceptions - * @throws OAuthErrorException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error + * @throws OAuthResponseException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error * Response */ public AccessTokenResponse grantTypeRefreshToken(String tokenUrl, String refreshToken, @Nullable String clientId, @@ -206,7 +203,7 @@ public AccessTokenResponse grantTypeRefreshToken(String tokenUrl, String refresh * @return Access Token * @throws IOException IO/ network exceptions * @throws OAuthException Other exceptions - * @throws OAuthErrorException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error + * @throws OAuthResponseException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error * Response */ public AccessTokenResponse grantTypeAuthorizationCode(String tokenUrl, String authorizationCode, String clientId, @@ -240,7 +237,7 @@ public AccessTokenResponse grantTypeAuthorizationCode(String tokenUrl, String au * @return Access Token * @throws IOException IO/ network exceptions * @throws OAuthException Other exceptions - * @throws OAuthErrorException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error + * @throws OAuthResponseException Error codes given by authorization provider, as in RFC 6749 section 5.2 Error * Response */ public AccessTokenResponse grantTypeClientCredentials(String tokenUrl, String clientId, @@ -301,11 +298,8 @@ private AccessTokenResponse doRequest(final String grantType, HttpClient httpCli String content = ""; try { final FormContentProvider entity = new FormContentProvider(fields); - final ContentResponse response = AccessController - .doPrivileged((PrivilegedExceptionAction) () -> { - Request requestWithContent = request.content(entity); - return requestWithContent.send(); - }); + Request requestWithContent = request.content(entity); + final ContentResponse response = requestWithContent.send(); statusCode = response.getStatus(); content = response.getContentAsString(); @@ -326,18 +320,15 @@ private AccessTokenResponse doRequest(final String grantType, HttpClient httpCli statusCode); throw new OAuthException("Bad http response, http code " + statusCode); } - } catch (PrivilegedActionException pae) { - Exception underlyingException = pae.getException(); - if (underlyingException instanceof InterruptedException || underlyingException instanceof TimeoutException - || underlyingException instanceof ExecutionException) { - throw new IOException("Exception in oauth communication, grant type " + grantType, underlyingException); - } - // Dont know what exception it is, wrap it up and throw it out - throw new OAuthException("Exception in oauth communication, grant type " + grantType, underlyingException); + } catch (InterruptedException | TimeoutException | ExecutionException e) { + throw new IOException("Exception in oauth communication, grant type " + grantType, e); } catch (JsonSyntaxException e) { throw new OAuthException(String.format( - "Unable to deserialize json into AccessTokenResponse/ OAuthResponseException. httpCode: %i json: %s", + "Unable to deserialize json into AccessTokenResponse/ OAuthResponseException. httpCode: %d json: %s", statusCode, content), e); + } catch (Exception e) { + // Dont know what exception it is, wrap it up and throw it out + throw new OAuthException("Exception in oauth communication, grant type " + grantType, e); } } @@ -356,10 +347,7 @@ private HttpClient createHttpClient(String tokenUrl) throws OAuthException { HttpClient httpClient = httpClientFactory.createHttpClient(HTTP_CLIENT_CONSUMER_NAME); if (!httpClient.isStarted()) { try { - AccessController.doPrivileged((PrivilegedExceptionAction<@Nullable Void>) () -> { - httpClient.start(); - return null; - }); + httpClient.start(); } catch (Exception e) { throw new OAuthException("Exception while starting httpClient, tokenUrl: " + tokenUrl, e); } @@ -370,10 +358,7 @@ private HttpClient createHttpClient(String tokenUrl) throws OAuthException { private void shutdownQuietly(@Nullable HttpClient httpClient) { try { if (httpClient != null) { - AccessController.doPrivileged((PrivilegedExceptionAction<@Nullable Void>) () -> { - httpClient.stop(); - return null; - }); + httpClient.stop(); } } catch (Exception e) { // there is nothing we can do here diff --git a/bundles/org.opensmarthouse.core.automation/src/main/java/org/openhab/core/automation/internal/TriggerHandlerCallbackImpl.java b/bundles/org.opensmarthouse.core.automation/src/main/java/org/openhab/core/automation/internal/TriggerHandlerCallbackImpl.java index 4b0c78e2be7..74442195594 100644 --- a/bundles/org.opensmarthouse.core.automation/src/main/java/org/openhab/core/automation/internal/TriggerHandlerCallbackImpl.java +++ b/bundles/org.opensmarthouse.core.automation/src/main/java/org/openhab/core/automation/internal/TriggerHandlerCallbackImpl.java @@ -12,8 +12,6 @@ */ package org.openhab.core.automation.internal; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -92,10 +90,7 @@ public void run() { public void dispose() { synchronized (this) { - AccessController.doPrivileged((PrivilegedAction<@Nullable Void>) () -> { - executor.shutdownNow(); - return null; - }); + executor.shutdownNow(); } } diff --git a/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/AbstractInvocationHandler.java b/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/AbstractInvocationHandler.java index 18b3ff4cf91..0c90b67e2bb 100644 --- a/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/AbstractInvocationHandler.java +++ b/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/AbstractInvocationHandler.java @@ -14,8 +14,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.ExecutionException; @@ -130,13 +128,8 @@ private String toString(Collection invocationStack) { } private String getStacktrace(final Thread thread) { - StackTraceElement[] elements = AccessController.doPrivileged(new PrivilegedAction() { - @Override - public StackTraceElement[] run() { - return thread.getStackTrace(); - } - }); - return Arrays.stream(elements).map(element -> "\tat " + element.toString()).collect(Collectors.joining("\n")); + StackTraceElement[] elements = thread.getStackTrace(); + return Arrays.stream(elements).map(element -> "\tat " + element).collect(Collectors.joining("\n")); } String toString(Method method) { diff --git a/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java b/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java index 78d08778cc8..7b32098ca15 100644 --- a/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java +++ b/bundles/org.opensmarthouse.core.common/src/main/java/org/openhab/core/internal/common/SafeCallerBuilderImpl.java @@ -14,8 +14,6 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.Arrays; import java.util.function.Consumer; import java.util.stream.Stream; @@ -56,25 +54,23 @@ public SafeCallerBuilderImpl(T target, Class[] classes, SafeCallManager manag @SuppressWarnings("unchecked") @Override public T build() { - return AccessController.doPrivileged((PrivilegedAction) () -> { - InvocationHandler handler; - if (async) { - handler = new InvocationHandlerAsync<>(manager, target, identifier, timeout, exceptionHandler, - timeoutHandler); - } else { - handler = new InvocationHandlerSync<>(manager, target, identifier, timeout, exceptionHandler, - timeoutHandler); - } - ClassLoader classLoader = getClass().getClassLoader(); - if (classLoader == null) { - throw new IllegalStateException( - "Cannot create proxy because '" + getClass().getName() + "' class loader is null"); - } - return (T) Proxy.newProxyInstance( - CombinedClassLoader.fromClasses(classLoader, - Stream.concat(Stream.of(target.getClass()), Arrays.stream(interfaceTypes))), - interfaceTypes, handler); - }); + InvocationHandler handler; + if (async) { + handler = new InvocationHandlerAsync<>(manager, target, identifier, timeout, exceptionHandler, + timeoutHandler); + } else { + handler = new InvocationHandlerSync<>(manager, target, identifier, timeout, exceptionHandler, + timeoutHandler); + } + ClassLoader classLoader = getClass().getClassLoader(); + if (classLoader == null) { + throw new IllegalStateException( + "Cannot create proxy because '" + getClass().getName() + "' class loader is null"); + } + return (T) Proxy.newProxyInstance( + CombinedClassLoader.fromClasses(classLoader, + Stream.concat(Stream.of(target.getClass()), Arrays.stream(interfaceTypes))), + interfaceTypes, handler); } @Override diff --git a/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/DiscoveryResultBuilder.java b/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/DiscoveryResultBuilder.java index c011eba940d..54ba5df144b 100644 --- a/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/DiscoveryResultBuilder.java +++ b/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/DiscoveryResultBuilder.java @@ -12,8 +12,6 @@ */ package org.openhab.core.config.discovery; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -175,12 +173,7 @@ private void validateThingUID(@Nullable ThingUID bridgeUID) { } private String getStackTrace(final Thread thread) { - StackTraceElement[] elements = AccessController.doPrivileged(new PrivilegedAction() { - @Override - public StackTraceElement[] run() { - return thread.getStackTrace(); - } - }); + StackTraceElement[] elements = thread.getStackTrace(); return Arrays.stream(elements).map(element -> "\tat " + element.toString()).collect(Collectors.joining("\n")); } } diff --git a/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/DiscoveryServiceRegistryImpl.java b/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/DiscoveryServiceRegistryImpl.java index 769616f4cec..b9408017fbf 100644 --- a/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/DiscoveryServiceRegistryImpl.java +++ b/bundles/org.opensmarthouse.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/DiscoveryServiceRegistryImpl.java @@ -12,8 +12,6 @@ */ package org.openhab.core.config.discovery.internal; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -254,13 +252,7 @@ public synchronized void thingDiscovered(final DiscoveryService source, final Di } for (final DiscoveryListener listener : listeners) { try { - AccessController.doPrivileged(new PrivilegedAction<@Nullable Void>() { - @Override - public @Nullable Void run() { - listener.thingDiscovered(source, result); - return null; - } - }); + listener.thingDiscovered(source, result); } catch (Exception ex) { logger.error("Cannot notify the DiscoveryListener '{}' on Thing discovered event!", listener.getClass().getName(), ex); @@ -280,13 +272,7 @@ public synchronized void thingRemoved(final DiscoveryService source, final Thing } for (final DiscoveryListener listener : listeners) { try { - AccessController.doPrivileged(new PrivilegedAction<@Nullable Void>() { - @Override - public @Nullable Void run() { - listener.thingRemoved(source, thingUID); - return null; - } - }); + listener.thingRemoved(source, thingUID); } catch (Exception ex) { logger.error("Cannot notify the DiscoveryListener '{}' on Thing removed event!", listener.getClass().getName(), ex); @@ -300,13 +286,8 @@ public synchronized void thingRemoved(final DiscoveryService source, final Thing Set removedResults = new HashSet<>(); for (final DiscoveryListener listener : listeners) { try { - Collection olderResults = AccessController - .doPrivileged(new PrivilegedAction<@Nullable Collection>() { - @Override - public @Nullable Collection run() { - return listener.removeOlderResults(source, timestamp, thingTypeUIDs, bridgeUID); - } - }); + Collection olderResults = listener.removeOlderResults(source, timestamp, thingTypeUIDs, + bridgeUID); if (olderResults != null) { removedResults.addAll(olderResults); } diff --git a/bundles/org.opensmarthouse.core.event.core/src/main/java/org/openhab/core/internal/events/OSGiEventPublisher.java b/bundles/org.opensmarthouse.core.event.core/src/main/java/org/openhab/core/internal/events/OSGiEventPublisher.java index 14a732db06e..23425366ddc 100644 --- a/bundles/org.opensmarthouse.core.event.core/src/main/java/org/openhab/core/internal/events/OSGiEventPublisher.java +++ b/bundles/org.opensmarthouse.core.event.core/src/main/java/org/openhab/core/internal/events/OSGiEventPublisher.java @@ -12,9 +12,6 @@ */ package org.openhab.core.internal.events; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; import java.util.Dictionary; import java.util.Hashtable; @@ -54,23 +51,17 @@ public void post(final Event event) throws IllegalArgumentException, IllegalStat private void postAsOSGiEvent(final EventAdmin eventAdmin, final Event event) throws IllegalStateException { try { - AccessController.doPrivileged(new PrivilegedExceptionAction() { - @Override - public Void run() throws Exception { - Dictionary properties = new Hashtable<>(3); - properties.put("type", event.getType()); - properties.put("payload", event.getPayload()); - properties.put("topic", event.getTopic()); - String source = event.getSource(); - if (source != null) { - properties.put("source", source); - } - eventAdmin.postEvent(new org.osgi.service.event.Event("openhab", properties)); - return null; - } - }); - } catch (PrivilegedActionException pae) { - Exception e = pae.getException(); + Dictionary properties = new Hashtable<>(3); + properties.put("type", event.getType()); + properties.put("payload", event.getPayload()); + properties.put("topic", event.getTopic()); + String source = event.getSource(); + if (source != null) { + properties.put("source", source); + } + eventAdmin.postEvent(new org.osgi.service.event.Event("openhab", properties)); + + } catch (Exception e) { throw new IllegalStateException("Cannot post the event via the event bus. Error message: " + e.getMessage(), e); } diff --git a/bundles/org.opensmarthouse.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java b/bundles/org.opensmarthouse.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java index 9f39924283c..91673ffe5d5 100644 --- a/bundles/org.opensmarthouse.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java +++ b/bundles/org.opensmarthouse.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java @@ -12,11 +12,8 @@ */ package org.openhab.core.io.net.http.internal; -import java.security.AccessController; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; import java.util.Map; import java.util.Objects; import java.util.regex.Pattern; @@ -188,160 +185,129 @@ private int getConfigParameter(Map parameters, String parameter, } private synchronized void initialize() { - if (threadPool == null || commonHttpClient == null || commonWebSocketClient == null) { - try { - AccessController.doPrivileged(new PrivilegedExceptionAction<@Nullable Void>() { - @Override - public @Nullable Void run() { - if (threadPool == null) { - threadPool = createThreadPool("common", minThreadsShared, maxThreadsShared, - keepAliveTimeoutShared); - } - - if (commonHttpClient == null) { - commonHttpClient = createHttpClientInternal("common", true, threadPool); - // we need to set the stop timeout AFTER the client has been started, because - // otherwise the Jetty client sets it back to the default value. - // We need the stop timeout in order to prevent blocking the deactivation of this - // component, see /~https://github.com/eclipse/smarthome/issues/6632 - threadPool.setStopTimeout(0); - logger.debug("Jetty shared http client created"); - } - - if (commonWebSocketClient == null) { - commonWebSocketClient = createWebSocketClientInternal("common", true, threadPool); - logger.debug("Jetty shared web socket client created"); - } - - return null; - } - }); - } catch (PrivilegedActionException e) { - Throwable cause = e.getCause(); - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } else { - throw new HttpClientInitializationException( - "unexpected checked exception during initialization of the jetty client", cause); - } + try { + if (threadPool == null) { + threadPool = createThreadPool("common", minThreadsShared, maxThreadsShared, keepAliveTimeoutShared); } + + if (commonHttpClient == null) { + commonHttpClient = createHttpClientInternal("common", true, threadPool); + // we need to set the stop timeout AFTER the client has been started, because + // otherwise the Jetty client sets it back to the default value. + // We need the stop timeout in order to prevent blocking the deactivation of this + // component, see /~https://github.com/eclipse/smarthome/issues/6632 + threadPool.setStopTimeout(0); + logger.debug("Jetty shared http client created"); + } + + if (commonWebSocketClient == null) { + commonWebSocketClient = createWebSocketClientInternal("common", true, threadPool); + logger.debug("Jetty shared web socket client created"); + } + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new HttpClientInitializationException( + "unexpected checked exception during initialization of the jetty client", e); } } private HttpClient createHttpClientInternal(String consumerName, boolean startClient, @Nullable QueuedThreadPool threadPool) { try { - return AccessController.doPrivileged(new PrivilegedExceptionAction() { - @Override - public HttpClient run() { - logger.debug("creating http client for consumer {}", consumerName); - - HttpClient httpClient = new HttpClient(createSslContextFactory()); - - // If proxy is set as property (standard Java property), provide the proxy information to Jetty HTTP - // Client - String httpProxyHost = System.getProperty("http.proxyHost"); - String httpsProxyHost = System.getProperty("https.proxyHost"); - - if (httpProxyHost != null) { - String sProxyPort = System.getProperty("http.proxyPort"); - if (sProxyPort != null) { - try { - int port = Integer.parseInt(sProxyPort); - httpClient.getProxyConfiguration().getProxies().add(new HttpProxy(httpProxyHost, port)); - } catch (NumberFormatException ex) { - // this was not a correct port. Ignoring. - logger.debug( - "HTTP Proxy detected (http.proxyHost), but invalid proxyport. Ignoring proxy."); - } - } - } else if (httpsProxyHost != null) { - String sProxyPort = System.getProperty("https.proxyPort"); - if (sProxyPort != null) { - try { - int port = Integer.parseInt(sProxyPort); - httpClient.getProxyConfiguration().getProxies() - .add(new HttpProxy(httpsProxyHost, port)); - } catch (NumberFormatException ex) { - // this was not a correct port. Ignoring. - logger.debug( - "HTTP Proxy detected (https.proxyHost), but invalid proxyport. Ignoring proxy."); - } - } + logger.debug("creating http client for consumer {}", consumerName); + + HttpClient httpClient = new HttpClient(createSslContextFactory()); + + // If proxy is set as property (standard Java property), provide the proxy information to Jetty HTTP + // Client + String httpProxyHost = System.getProperty("http.proxyHost"); + String httpsProxyHost = System.getProperty("https.proxyHost"); + + if (httpProxyHost != null) { + String sProxyPort = System.getProperty("http.proxyPort"); + if (sProxyPort != null) { + try { + int port = Integer.parseInt(sProxyPort); + httpClient.getProxyConfiguration().getProxies().add(new HttpProxy(httpProxyHost, port)); + } catch (NumberFormatException ex) { + // this was not a correct port. Ignoring. + logger.debug("HTTP Proxy detected (http.proxyHost), but invalid proxyport. Ignoring proxy."); } - - httpClient.setMaxConnectionsPerDestination(2); - - if (threadPool != null) { - httpClient.setExecutor(threadPool); - } else { - final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom, - maxThreadsCustom, keepAliveTimeoutCustom); - httpClient.setExecutor(queuedThreadPool); + } + } else if (httpsProxyHost != null) { + String sProxyPort = System.getProperty("https.proxyPort"); + if (sProxyPort != null) { + try { + int port = Integer.parseInt(sProxyPort); + httpClient.getProxyConfiguration().getProxies().add(new HttpProxy(httpsProxyHost, port)); + } catch (NumberFormatException ex) { + // this was not a correct port. Ignoring. + logger.debug("HTTP Proxy detected (https.proxyHost), but invalid proxyport. Ignoring proxy."); } + } + } - if (startClient) { - try { - httpClient.start(); - } catch (Exception e) { - logger.error("Could not start Jetty http client", e); - throw new HttpClientInitializationException("Could not start Jetty http client", e); - } - } + httpClient.setMaxConnectionsPerDestination(2); - return httpClient; - } - }); - } catch (PrivilegedActionException e) { - Throwable cause = e.getCause(); - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; + if (threadPool != null) { + httpClient.setExecutor(threadPool); } else { - throw new HttpClientInitializationException( - "unexpected checked exception during initialization of the Jetty http client", cause); + final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom, + maxThreadsCustom, keepAliveTimeoutCustom); + httpClient.setExecutor(queuedThreadPool); } + + if (startClient) { + try { + httpClient.start(); + } catch (Exception e) { + logger.error("Could not start Jetty http client", e); + throw new HttpClientInitializationException("Could not start Jetty http client", e); + } + } + + return httpClient; + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new HttpClientInitializationException( + "unexpected checked exception during initialization of the Jetty http client", e); } } private WebSocketClient createWebSocketClientInternal(String consumerName, boolean startClient, @Nullable QueuedThreadPool threadPool) { try { - return AccessController.doPrivileged(new PrivilegedExceptionAction() { - @Override - public WebSocketClient run() { - logger.debug("creating web socket client for consumer {}", consumerName); - - HttpClient httpClient = new HttpClient(createSslContextFactory()); - if (threadPool != null) { - httpClient.setExecutor(threadPool); - } else { - final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom, - maxThreadsCustom, keepAliveTimeoutCustom); - httpClient.setExecutor(queuedThreadPool); - } + logger.debug("creating web socket client for consumer {}", consumerName); - WebSocketClient webSocketClient = new WebSocketClient(httpClient); + HttpClient httpClient = new HttpClient(createSslContextFactory()); + if (threadPool != null) { + httpClient.setExecutor(threadPool); + } else { + final QueuedThreadPool queuedThreadPool = createThreadPool(consumerName, minThreadsCustom, + maxThreadsCustom, keepAliveTimeoutCustom); + httpClient.setExecutor(queuedThreadPool); + } - if (startClient) { - try { - webSocketClient.start(); - } catch (Exception e) { - logger.error("Could not start Jetty web socket client", e); - throw new HttpClientInitializationException("Could not start Jetty web socket client", e); - } - } + WebSocketClient webSocketClient = new WebSocketClient(httpClient); - return webSocketClient; + if (startClient) { + try { + webSocketClient.start(); + } catch (Exception e) { + logger.error("Could not start Jetty web socket client", e); + throw new HttpClientInitializationException("Could not start Jetty web socket client", e); } - }); - } catch (PrivilegedActionException e) { - Throwable cause = e.getCause(); - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } else { - throw new HttpClientInitializationException( - "unexpected checked exception during initialization of the Jetty web socket client", cause); } + + return webSocketClient; + + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new HttpClientInitializationException( + "unexpected checked exception during initialization of the Jetty web socket client", e); } } diff --git a/bundles/org.opensmarthouse.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java b/bundles/org.opensmarthouse.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java index 9745cfc16b1..c40ad37db1e 100644 --- a/bundles/org.opensmarthouse.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java +++ b/bundles/org.opensmarthouse.core.thing/src/main/java/org/openhab/core/thing/internal/ThingManagerImpl.java @@ -13,8 +13,6 @@ package org.openhab.core.thing.internal; import java.net.URI; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collection; @@ -454,23 +452,20 @@ private void thingUpdated(final Thing thing) { throw new IllegalArgumentException(MessageFormat.format( "Cannot update thing {0} because it is not known to the registry", thing.getUID().getAsString())); } - AccessController.doPrivileged((PrivilegedAction<@Nullable Void>) () -> { - final Provider provider = thingRegistry.getProvider(thing); - if (provider == null) { - throw new IllegalArgumentException(MessageFormat.format( - "Provider for thing {0} cannot be determined because it is not known to the registry", - thing.getUID().getAsString())); - } - if (provider instanceof ManagedProvider) { - final ManagedProvider managedProvider = (ManagedProvider) provider; - managedProvider.update(thing); - } else { - logger.debug("Only updating thing {} in the registry because provider {} is not managed.", - thing.getUID().getAsString(), provider); - thingRegistry.updated(provider, oldThing, thing); - } - return null; - }); + final Provider provider = thingRegistry.getProvider(thing); + if (provider == null) { + throw new IllegalArgumentException(MessageFormat.format( + "Provider for thing {0} cannot be determined because it is not known to the registry", + thing.getUID().getAsString())); + } + if (provider instanceof ManagedProvider) { + final ManagedProvider managedProvider = (ManagedProvider) provider; + managedProvider.update(thing); + } else { + logger.debug("Only updating thing {} in the registry because provider {} is not managed.", + thing.getUID().getAsString(), provider); + thingRegistry.updated(provider, oldThing, thing); + } thingUpdatedLock.remove(thing.getUID()); } @@ -1119,10 +1114,7 @@ private void notifyRegistryAboutForceRemove(final Thing thing) { // call asynchronous to avoid deadlocks in thing handler ThreadPoolManager.getPool(FORCE_REMOVE_THREAD_POOL_NAME).execute(() -> { try { - AccessController.doPrivileged((PrivilegedAction<@Nullable Void>) () -> { - thingRegistry.forceRemove(thing.getUID()); - return null; - }); + thingRegistry.forceRemove(thing.getUID()); } catch (IllegalStateException ex) { logger.debug("Could not remove thing {}. Most likely because it is not managed.", thing.getUID(), ex); } catch (Exception ex) {