You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2020/03/10 20:33:25 UTC

[celix] 01/01: Changes auto start bundle handling to first installing and then starting a bundle. In some cases this is preferred.

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

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

commit 9621e29fbf96713fe4040426a77af1adf18c93d7
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Tue Mar 10 21:33:11 2020 +0100

    Changes auto start bundle handling to first installing and then starting a bundle. In some cases this is preferred.
---
 cmake/cmake_celix/ContainerPackaging.cmake        |  4 +--
 libs/framework/include/celix_constants.h          |  1 +
 libs/framework/private/mock/bundle_context_mock.c |  8 +++++
 libs/framework/src/framework.c                    | 42 ++++++++++++-----------
 4 files changed, 33 insertions(+), 22 deletions(-)

diff --git a/cmake/cmake_celix/ContainerPackaging.cmake b/cmake/cmake_celix/ContainerPackaging.cmake
index 27fa7bc..7d643b7 100644
--- a/cmake/cmake_celix/ContainerPackaging.cmake
+++ b/cmake/cmake_celix/ContainerPackaging.cmake
@@ -408,7 +408,7 @@ endfunction()
 Add the selected bundles to the Celix container. These bundles are (if configured) copied to the container build dir and
 are added to the configuration properties so that they are installed and started when the Celix container executed.
 
-The Celix framework support 7 (0 - 6) run levels. Run levels can be used to control the start and stop order of bundles.
+The Celix framework supports 7 (0 - 6) run levels. Run levels can be used to control the start and stop order of bundles.
 Bundles in run level 0 are started first and bundles in run level 6 are started last.
 When stopping bundles in run level 6 are stopped first and bundles in run level 0 are stopped last.
 Within a run level the order of configured decides the start order; bundles added earlier are started first.
@@ -419,7 +419,7 @@ Optional Arguments:
 
 ```CMake
 celix_container_bundles(<celix_container_target_name>
-    [LEVEL (0..5)]
+    [LEVEL (0..6)]
     bundle1
     bundle2
     ...
diff --git a/libs/framework/include/celix_constants.h b/libs/framework/include/celix_constants.h
index eea20c7..e202550 100644
--- a/libs/framework/include/celix_constants.h
+++ b/libs/framework/include/celix_constants.h
@@ -85,6 +85,7 @@ static const char *const CELIX_LOAD_BUNDLES_WITH_NODELETE = "CELIX_LOAD_BUNDLES_
 #define CELIX_AUTO_START_3 "CELIX_AUTO_START_3"
 #define CELIX_AUTO_START_4 "CELIX_AUTO_START_4"
 #define CELIX_AUTO_START_5 "CELIX_AUTO_START_5"
+#define CELIX_AUTO_START_6 "CELIX_AUTO_START_6"
 
 
 #ifdef __cplusplus
diff --git a/libs/framework/private/mock/bundle_context_mock.c b/libs/framework/private/mock/bundle_context_mock.c
index 61642c2..0600508 100644
--- a/libs/framework/private/mock/bundle_context_mock.c
+++ b/libs/framework/private/mock/bundle_context_mock.c
@@ -371,3 +371,11 @@ bool celix_bundleContext_getPropertyAsBool(celix_bundle_context_t *ctx, const ch
 			->withBoolParameters("defaultValue", defaultValue);
 	return mock_c()->returnValue().value.boolValue;
 }
+
+const char* celix_bundleContext_getProperty(celix_bundle_context_t *ctx, const char *key, const char* defaultValue) {
+    mock_c()->actualCall("celix_bundleContext_getProperty")
+            ->withPointerParameters("ctx", ctx)
+            ->withStringParameters("key", key)
+            ->withStringParameters("defaultValue", defaultValue);
+    return mock_c()->returnValue().value.stringValue;
+}
diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c
index c3aef4d..f5d5a68 100644
--- a/libs/framework/src/framework.c
+++ b/libs/framework/src/framework.c
@@ -173,7 +173,8 @@ static celix_status_t frameworkActivator_stop(void * userData, bundle_context_t
 static celix_status_t frameworkActivator_destroy(void * userData, bundle_context_t *context);
 
 static void framework_autoStartConfiguredBundles(bundle_context_t *fwCtx);
-static void framework_autoStartConfiguredBundlesForList(bundle_context_t *fwCtx, const char *autoStart);
+static void framework_autoInstallConfiguredBundlesForList(bundle_context_t *fwCtx, const char *autoStart, celix_array_list_t *installedBundles);
+static void framework_autoStartConfiguredBundlesForList(bundle_context_t *fwCtx, const celix_array_list_t *installedBundles);
 
 struct fw_refreshHelper {
     framework_pt framework;
@@ -634,27 +635,30 @@ celix_status_t framework_start(framework_pt framework) {
 }
 
 static void framework_autoStartConfiguredBundles(bundle_context_t *fwCtx) {
-    const char* autoStart = NULL;
-    const char* cosgiKeys[] = {"cosgi.auto.start.0","cosgi.auto.start.1","cosgi.auto.start.2","cosgi.auto.start.3","cosgi.auto.start.4","cosgi.auto.start.5"};
-    const char* celixKeys[] = {CELIX_AUTO_START_0, CELIX_AUTO_START_1, CELIX_AUTO_START_2, CELIX_AUTO_START_3, CELIX_AUTO_START_4, CELIX_AUTO_START_5};
-    size_t len = 6;
+    const char* cosgiKeys[] = {"cosgi.auto.start.0","cosgi.auto.start.1","cosgi.auto.start.2","cosgi.auto.start.3","cosgi.auto.start.4","cosgi.auto.start.5","cosgi.auto.start.6"};
+    const char* celixKeys[] = {CELIX_AUTO_START_0, CELIX_AUTO_START_1, CELIX_AUTO_START_2, CELIX_AUTO_START_3, CELIX_AUTO_START_4, CELIX_AUTO_START_5, CELIX_AUTO_START_6};
+    celix_array_list_t *installedBundles = celix_arrayList_create();
+    size_t len = 7;
     for (int i = 0; i < len; ++i) {
-        bundleContext_getProperty(fwCtx, celixKeys[i], &autoStart);
+        const char *autoStart = celix_bundleContext_getProperty(fwCtx, celixKeys[i], NULL);
         if (autoStart == NULL) {
-            bundleContext_getProperty(fwCtx, cosgiKeys[i], &autoStart);
+            autoStart = celix_bundleContext_getProperty(fwCtx, cosgiKeys[i], NULL);
         }
         if (autoStart != NULL) {
-            framework_autoStartConfiguredBundlesForList(fwCtx, autoStart);
+            framework_autoInstallConfiguredBundlesForList(fwCtx, autoStart, installedBundles);
         }
     }
+    for (int i = 0; i < len; ++i) {
+        framework_autoStartConfiguredBundlesForList(fwCtx, installedBundles);
+    }
+    celix_arrayList_destroy(installedBundles);
 }
 
-static void framework_autoStartConfiguredBundlesForList(bundle_context_t *fwCtx, const char *autoStartIn)  {
+
+static void framework_autoInstallConfiguredBundlesForList(bundle_context_t *fwCtx, const char *autoStartIn, celix_array_list_t *installedBundles) {
     char delims[] = " ";
     char *save_ptr = NULL;
-    array_list_pt installed = NULL;
-    char *autoStart = strndup(autoStartIn, 1024*1024*10);
-    arrayList_create(&installed);
+    char *autoStart = celix_utils_strdup(autoStartIn);
 
     if (autoStart != NULL) {
         char *location = strtok_r(autoStart, delims, &save_ptr);
@@ -663,30 +667,28 @@ static void framework_autoStartConfiguredBundlesForList(bundle_context_t *fwCtx,
             bundle_t *bnd = NULL;
             celix_status_t  rc = bundleContext_installBundle(fwCtx, location, &bnd);
             if (rc == CELIX_SUCCESS) {
-                arrayList_add(installed, bnd);
+                celix_arrayList_add(installedBundles, bnd);
             } else {
                 printf("Could not install bundle '%s'\n", location);
             }
             location = strtok_r(NULL, delims, &save_ptr);
         }
     }
+    free(autoStart);
+}
 
-    unsigned int i;
-    for (i = 0; i < arrayList_size(installed); ++i) {
+static void framework_autoStartConfiguredBundlesForList(bundle_context_t *fwCtx, const celix_array_list_t *installedBundles) {
+    for (int i = 0; i < celix_arrayList_size(installedBundles); ++i) {
         long bndId = -1;
-        bundle_t *bnd = arrayList_get(installed, i);
+        bundle_t *bnd = celix_arrayList_get(installedBundles, i);
         bundle_getBundleId(bnd, &bndId);
         celix_status_t rc = bundle_startWithOptions(bnd, 0);
         if (rc != CELIX_SUCCESS) {
             printf("Could not start bundle %li\n", bndId);
         }
     }
-
-    free(autoStart);
-    arrayList_destroy(installed);
 }
 
-
 celix_status_t framework_stop(framework_pt framework) {
 	return fw_stopBundle(framework, framework->bundleId, true);
 }