This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/585-celix-conditions
in repository https://gitbox.apache.org/repos/asf/celix.git

commit ba48303b694cd540ea6d21707be4f914679069bc
Author: Pepijn Noltes <[email protected]>
AuthorDate: Mon Jul 10 23:04:01 2023 +0200

    Cleanup framework_start
---
 libs/framework/include_deprecated/framework.h | 12 +++++++++++
 libs/framework/src/celix_framework_bundle.c   | 20 ++++++++---------
 libs/framework/src/framework.c                | 31 +++++++++++----------------
 3 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/libs/framework/include_deprecated/framework.h 
b/libs/framework/include_deprecated/framework.h
index d184eb2f..e3c45ada 100644
--- a/libs/framework/include_deprecated/framework.h
+++ b/libs/framework/include_deprecated/framework.h
@@ -32,8 +32,20 @@ extern "C" {
 
 CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t 
framework_create(celix_framework_t **framework, celix_properties_t *config);
 
+/**
+ * @brief Start the framework.
+ * @note Not thread safe.
+ * @param[in] framework The framework to start.
+ * @return CELIX_SUCCESS if the framework is started.
+ */
 CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t 
framework_start(celix_framework_t *framework);
 
+/**
+ * @brief Stop the framework.
+ * @note Not thread safe.
+ * @param[in] framework The framework to stop.
+ * @return CELIX_SUCCESS if the framework is stopped.
+ */
 CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t 
framework_stop(celix_framework_t *framework);
 
 CELIX_FRAMEWORK_DEPRECATED_EXPORT celix_status_t 
framework_destroy(celix_framework_t *framework);
diff --git a/libs/framework/src/celix_framework_bundle.c 
b/libs/framework/src/celix_framework_bundle.c
index 883d5c50..8eb2c06c 100644
--- a/libs/framework/src/celix_framework_bundle.c
+++ b/libs/framework/src/celix_framework_bundle.c
@@ -30,7 +30,7 @@
 /**
  * @brief Celix framework bundle activator struct.
  */
-typedef struct celix_framework_bundle_activator {
+typedef struct celix_framework_bundle {
     celix_bundle_context_t* ctx;
     celix_condition_t conditionInstance; /**< condition instance which can be 
used for multiple condition services.*/
     framework_listener_t listener;       /**< framework listener to check if 
the framework is ready. */
@@ -42,11 +42,11 @@ typedef struct celix_framework_bundle_activator {
     long checkComponentsScheduledEventId; /**< event id of the scheduled event 
to check if the framework is ready. */
     long componentsReadyConditionSvcId;   /**< service id of the condition 
service which is set when all components are
                                             ready. */
-} celix_framework_bundle_activator_t;
+} celix_framework_bundle_t;
 
 celix_status_t celix_frameworkBundle_create(celix_bundle_context_t* ctx, 
void** userData) {
     *userData = NULL;
-    celix_framework_bundle_activator_t* act = calloc(1, sizeof(*act));
+    celix_framework_bundle_t* act = calloc(1, sizeof(*act));
     if (!act) {
         return ENOMEM;
     }
@@ -70,7 +70,7 @@ celix_status_t 
celix_frameworkBundle_create(celix_bundle_context_t* ctx, void**
     return CELIX_SUCCESS;
 }
 
-static void 
celix_frameworkBundle_registerTrueCondition(celix_framework_bundle_activator_t* 
act) {
+static void 
celix_frameworkBundle_registerTrueCondition(celix_framework_bundle_t* act) {
     celix_service_registration_options_t opts = 
CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
     opts.serviceName = CELIX_CONDITION_SERVICE_NAME;
     opts.serviceVersion = CELIX_CONDITION_SERVICE_VERSION;
@@ -86,7 +86,7 @@ static void 
celix_frameworkBundle_registerTrueCondition(celix_framework_bundle_a
 
 celix_status_t celix_frameworkBundle_handleFrameworkEvent(void* handle, 
framework_event_t* event) {
     framework_listener_t* listener = handle;
-    celix_framework_bundle_activator_t* act = listener->handle;
+    celix_framework_bundle_t* act = listener->handle;
     if (event->type == OSGI_FRAMEWORK_EVENT_STARTED || event->type == 
OSGI_FRAMEWORK_EVENT_ERROR) {
         celixThreadMutex_lock(&act->mutex);
 
@@ -123,7 +123,7 @@ celix_status_t 
celix_frameworkBundle_handleFrameworkEvent(void* handle, framewor
 }
 
 void celix_frameworkBundle_componentsCheck(void* data) {
-    celix_framework_bundle_activator_t* act = data;
+    celix_framework_bundle_t* act = data;
     celix_dependency_manager_t* mng = 
celix_bundleContext_getDependencyManager(act->ctx);
 
     celixThreadMutex_lock(&act->mutex);
@@ -149,7 +149,7 @@ void celix_frameworkBundle_componentsCheck(void* data) {
     celixThreadMutex_unlock(&act->mutex);
 }
 
-static void 
celix_frameworkBundle_startComponentsCheck(celix_framework_bundle_activator_t* 
act) {
+static void 
celix_frameworkBundle_startComponentsCheck(celix_framework_bundle_t* act) {
     celix_scheduled_event_options_t opts = CELIX_EMPTY_SCHEDULED_EVENT_OPTIONS;
     opts.name = "celix_frameworkBundle_componentsCheck";
     opts.callback = celix_frameworkBundle_componentsCheck;
@@ -160,7 +160,7 @@ static void 
celix_frameworkBundle_startComponentsCheck(celix_framework_bundle_ac
 }
 
 celix_status_t celix_frameworkBundle_start(void* userData, 
celix_bundle_context_t* ctx) {
-    celix_framework_bundle_activator_t* act = userData;
+    celix_framework_bundle_t* act = userData;
 
     bool conditionsEnabled = celix_bundleContext_getPropertyAsBool(
         ctx, CELIX_FRAMEWORK_CONDITION_SERVICES_ENABLED, 
CELIX_FRAMEWORK_CONDITION_SERVICES_ENABLED_DEFAULT);
@@ -181,7 +181,7 @@ celix_status_t celix_frameworkBundle_start(void* userData, 
celix_bundle_context_
 }
 
 celix_status_t celix_frameworkBundle_stop(void* userData, 
celix_bundle_context_t* ctx) {
-    celix_framework_bundle_activator_t* act = userData;
+    celix_framework_bundle_t* act = userData;
     celix_framework_t* framework = celix_bundleContext_getFramework(ctx);
 
     // remove framework listener
@@ -214,7 +214,7 @@ celix_status_t celix_frameworkBundle_stop(void* userData, 
celix_bundle_context_t
 }
 
 celix_status_t celix_frameworkBundle_destroy(void* userData, 
celix_bundle_context_t* ctx __attribute__((unused))) {
-    celix_framework_bundle_activator_t* act = userData;
+    celix_framework_bundle_t* act = userData;
     if (act) {
         celixThreadMutex_destroy(&act->mutex);
         free(userData);
diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c
index a3295e1d..7fa890b0 100644
--- a/libs/framework/src/framework.c
+++ b/libs/framework/src/framework.c
@@ -446,20 +446,19 @@ celix_status_t fw_init(framework_pt framework) {
 
 celix_status_t framework_start(celix_framework_t* framework) {
     celix_status_t status = CELIX_SUCCESS;
-    bundle_state_e state = CELIX_BUNDLE_STATE_UNKNOWN;
+    bundle_state_e state = celix_bundle_getState(framework->bundle);
 
-    status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
-    if (status == CELIX_SUCCESS) {
-        if ((state == CELIX_BUNDLE_STATE_INSTALLED) || (state == 
CELIX_BUNDLE_STATE_RESOLVED)) {
-            status = CELIX_DO_IF(status, fw_init(framework));
-        }
-    }
+    //framework_start should be called when state is INSTALLED or RESOLVED
+    bool expectedState = state == CELIX_BUNDLE_STATE_INSTALLED || state == 
CELIX_BUNDLE_STATE_RESOLVED;
 
-    status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
-    if (status == CELIX_SUCCESS && state == CELIX_BUNDLE_STATE_STARTING) {
-        bundle_setState(framework->bundle, CELIX_BUNDLE_STATE_ACTIVE);
+    if (!expectedState) {
+        fw_log(framework->logger, CELIX_LOG_LEVEL_ERROR, "Could not start 
framework, unexpected state %i", state);
+        return CELIX_ILLEGAL_STATE;
     }
 
+    status = CELIX_DO_IF(status, fw_init(framework));
+    status = CELIX_DO_IF(status, bundle_setState(framework->bundle, 
CELIX_BUNDLE_STATE_ACTIVE));
+
     if (status != CELIX_SUCCESS) {
         fw_log(framework->logger, CELIX_LOG_LEVEL_ERROR, "Could not initialize 
framework");
         return status;
@@ -467,24 +466,18 @@ celix_status_t framework_start(celix_framework_t* 
framework) {
 
     celix_framework_bundle_entry_t* entry =
         
celix_framework_bundleEntry_getBundleEntryAndIncreaseUseCount(framework, 
framework->bundleId);
-    CELIX_DO_IF(status, fw_fireBundleEvent(framework, 
OSGI_FRAMEWORK_BUNDLE_EVENT_STARTED, entry));
+    fw_fireBundleEvent(framework, OSGI_FRAMEWORK_BUNDLE_EVENT_STARTED, entry);
     celix_framework_bundleEntry_decreaseUseCount(entry);
 
-    if (status != CELIX_SUCCESS) {
-        status = CELIX_BUNDLE_EXCEPTION;
-        fw_logCode(framework->logger, CELIX_LOG_LEVEL_ERROR, status, "Could 
not start framework bundle");
-        fw_fireFrameworkEvent(framework, OSGI_FRAMEWORK_EVENT_ERROR, status);
-        return status;
-    }
-
     celix_status_t startStatus = 
framework_autoStartConfiguredBundles(framework);
     celix_status_t installStatus = 
framework_autoInstallConfiguredBundles(framework);
+
     if (startStatus == CELIX_SUCCESS && installStatus == CELIX_SUCCESS) {
         //fire started event if all bundles are started/installed and the 
event queue is empty
         celix_framework_waitForEmptyEventQueue(framework);
         fw_fireFrameworkEvent(framework, OSGI_FRAMEWORK_EVENT_STARTED, 
CELIX_SUCCESS);
     } else {
-        //note not returning a error, because the framework is started, but 
not all bundles are started/installed
+        //note not returning an error, because the framework is started, but 
not all bundles are started/installed
         fw_logCode(framework->logger, CELIX_LOG_LEVEL_ERROR, status, "Could 
not auto start or install all configured bundles");
         fw_fireFrameworkEvent(framework, OSGI_FRAMEWORK_EVENT_ERROR, 
CELIX_BUNDLE_EXCEPTION);
     }

Reply via email to