From 62fbec0192829709945501c44d902d4fde567366 Mon Sep 17 00:00:00 2001 From: MMaiero Date: Mon, 4 Nov 2024 17:54:07 +0100 Subject: [PATCH] fix: Prevent publishing birth on shutdown Signed-off-by: MMaiero --- .../cloud/CloudConnectionManagerImpl.java | 38 +++++++++++++++---- .../kura/core/cloud/CloudServiceImpl.java | 37 +++++++++--------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/kura/org.eclipse.kura.cloudconnection.eclipseiot.mqtt.provider/src/main/java/org/eclipse/kura/internal/cloudconnection/eclipseiot/mqtt/cloud/CloudConnectionManagerImpl.java b/kura/org.eclipse.kura.cloudconnection.eclipseiot.mqtt.provider/src/main/java/org/eclipse/kura/internal/cloudconnection/eclipseiot/mqtt/cloud/CloudConnectionManagerImpl.java index 26b948f21b1..59b85e00a43 100644 --- a/kura/org.eclipse.kura.cloudconnection.eclipseiot.mqtt.provider/src/main/java/org/eclipse/kura/internal/cloudconnection/eclipseiot/mqtt/cloud/CloudConnectionManagerImpl.java +++ b/kura/org.eclipse.kura.cloudconnection.eclipseiot.mqtt.provider/src/main/java/org/eclipse/kura/internal/cloudconnection/eclipseiot/mqtt/cloud/CloudConnectionManagerImpl.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright (c) 2011, 2024 Eurotech and/or its affiliates and others - * + * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ - * + * * SPDX-License-Identifier: EPL-2.0 - * + * * Contributors: * Eurotech *******************************************************************************/ @@ -82,6 +82,8 @@ import org.eclipse.kura.position.PositionService; import org.eclipse.kura.system.SystemAdminService; import org.eclipse.kura.system.SystemService; +import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; @@ -148,7 +150,7 @@ public class CloudConnectionManagerImpl private final Set registeredCloudDeliveryListeners; private ScheduledFuture scheduledBirthPublisherFuture; - private ScheduledExecutorService scheduledBirthPublisher = Executors.newScheduledThreadPool(1); + private final ScheduledExecutorService scheduledBirthPublisher = Executors.newScheduledThreadPool(1); public CloudConnectionManagerImpl() { this.messageId = new AtomicInteger(); @@ -283,7 +285,7 @@ public void unsetNetworkStatusService(NetworkStatusService networkStatusService) protected void activate(ComponentContext componentContext, Map properties) { this.ownPid = (String) properties.get(ConfigurationService.KURA_SERVICE_PID); - logger.info("activate {}...", ownPid); + logger.info("activate {}...", this.ownPid); // // save the bundle context and the properties @@ -368,8 +370,8 @@ public void handleEvent(Event event) { return; } - if ((EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL.equals(topic) - || EVENT_TOPIC_DEPLOYMENT_ADMIN_UNINSTALL.equals(topic)) && this.dataService.isConnected()) { + if ((EVENT_TOPIC_DEPLOYMENT_ADMIN_INSTALL.equals(topic) || EVENT_TOPIC_DEPLOYMENT_ADMIN_UNINSTALL.equals(topic)) + && this.dataService.isConnected()) { logger.debug("CloudConnectionManagerImpl: received install/uninstall event, publishing BIRTH."); tryPublishBirthCertificate(false); } @@ -655,6 +657,11 @@ private void setupDeviceSubscriptions() throws KuraException { } private void publishBirthCertificate(boolean isNewConnection) throws KuraException { + if (isFrameworkStopping()) { + logger.info("framework is stopping.. not republishing birth certificate"); + return; + } + readModemProfile(); LifecycleMessage birthToPublish = new LifecycleMessage(this.options, this).asBirthCertificateMessage(); @@ -819,7 +826,7 @@ public String publish(KuraMessage message) throws KuraException { } String getOwnPid() { - return ownPid; + return this.ownPid; } @Override @@ -861,6 +868,21 @@ public void unregisterCloudDeliveryListener(CloudDeliveryListener cloudDeliveryL this.registeredCloudDeliveryListeners.remove(cloudDeliveryListener); } + private boolean isFrameworkStopping() { + try { + final Bundle ownBundle = FrameworkUtil.getBundle(CloudConnectionManagerImpl.class); + + if (ownBundle == null) { + return false; // not running in an OSGi framework? e.g. unit test + } + + return ownBundle.getBundleContext().getBundle(0).getState() == Bundle.STOPPING; + } catch (final Exception e) { + logger.warn("unexpected exception while checking if framework is shutting down", e); + return false; + } + } + private void readModemProfile() { this.networkStatusService.ifPresent(statusService -> { List modemStatuses = getModemsStatuses(statusService); diff --git a/kura/org.eclipse.kura.core.cloud/src/main/java/org/eclipse/kura/core/cloud/CloudServiceImpl.java b/kura/org.eclipse.kura.core.cloud/src/main/java/org/eclipse/kura/core/cloud/CloudServiceImpl.java index cf15b2f2df3..2cd283df13e 100644 --- a/kura/org.eclipse.kura.core.cloud/src/main/java/org/eclipse/kura/core/cloud/CloudServiceImpl.java +++ b/kura/org.eclipse.kura.core.cloud/src/main/java/org/eclipse/kura/core/cloud/CloudServiceImpl.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright (c) 2011, 2024 Eurotech and/or its affiliates and others - * + * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ - * + * * SPDX-License-Identifier: EPL-2.0 - * + * * Contributors: * Eurotech *******************************************************************************/ @@ -171,12 +171,12 @@ public class CloudServiceImpl private ServiceRegistration notificationPublisherRegistration; private final CloudNotificationPublisher notificationPublisher; - private Set tamperDetectionServices = new HashSet<>(); + private final Set tamperDetectionServices = new HashSet<>(); private String ownPid; private ScheduledFuture scheduledBirthPublisherFuture; - private ScheduledExecutorService scheduledBirthPublisher = Executors.newScheduledThreadPool(1); + private final ScheduledExecutorService scheduledBirthPublisher = Executors.newScheduledThreadPool(1); private LifecycleMessage lastBirthMessage; private LifecycleMessage lastAppMessage; @@ -329,7 +329,7 @@ public void unsetNetworkStatusService(NetworkStatusService networkStatusService) protected void activate(ComponentContext componentContext, Map properties) { this.ownPid = (String) properties.get(ConfigurationService.KURA_SERVICE_PID); - logger.info("activate {}...", ownPid); + logger.info("activate {}...", this.ownPid); // // save the bundle context and the properties @@ -856,6 +856,11 @@ private void setupCloudConnection(boolean isNewConnection) throws KuraException } private void publishBirthCertificate(boolean isNewConnection) throws KuraException { + if (isFrameworkStopping()) { + logger.info("framework is stopping.. not republishing birth certificate"); + return; + } + readModemProfile(); LifecycleMessage birthToPublish = new LifecycleMessage(this.options, this).asBirthCertificateMessage(); @@ -871,6 +876,10 @@ private void publishDisconnectCertificate() throws KuraException { } private void publishAppCertificate() { + if (isFrameworkStopping()) { + logger.info("framework is stopping.. not republishing app certificate"); + return; + } publishWithDelay(new LifecycleMessage(this.options, this).asAppCertificateMessage()); } @@ -895,12 +904,12 @@ private void publishWithDelay(LifecycleMessage message) { if (Objects.nonNull(this.lastBirthMessage)) { logger.debug("CloudServiceImpl: publishing cached BIRTH message."); - publishLifeCycleMessage(lastBirthMessage); + publishLifeCycleMessage(this.lastBirthMessage); } if (Objects.nonNull(this.lastAppMessage)) { logger.debug("CloudServiceImpl: publishing cached APP message."); - publishLifeCycleMessage(lastAppMessage); + publishLifeCycleMessage(this.lastAppMessage); } } catch (KuraException e) { @@ -986,11 +995,6 @@ private void postConnectionStateChangeEvent(final boolean isConnected) { public void registerRequestHandler(String appId, RequestHandler requestHandler) { this.registeredRequestHandlers.put(appId, requestHandler); - if (isFrameworkStopping()) { - logger.info("framework is stopping.. not republishing app certificate"); - return; - } - if (isConnected()) { publishAppCertificate(); } @@ -1000,11 +1004,6 @@ public void registerRequestHandler(String appId, RequestHandler requestHandler) public void unregister(String appId) { this.registeredRequestHandlers.remove(appId); - if (isFrameworkStopping()) { - logger.info("framework is stopping.. not republishing app certificate"); - return; - } - if (isConnected()) { publishAppCertificate(); } @@ -1193,7 +1192,7 @@ public void unregisterCloudDeliveryListener(CloudDeliveryListener cloudDeliveryL } String getOwnPid() { - return ownPid; + return this.ownPid; } void withTamperDetectionServices(final Consumer> consumer) {