From 697e77ae751e2cc892f412f50cf4dc699643709d Mon Sep 17 00:00:00 2001 From: xuzhenbao Date: Thu, 25 Jul 2024 14:51:55 +0800 Subject: [PATCH] Make the framework UUID configurable --- .../gtest/src/CelixFrameworkTestSuite.cc | 9 +++++++++ libs/framework/include/celix/Constants.h | 2 +- libs/framework/include/celix_constants.h | 4 ++-- libs/framework/src/framework.c | 16 ++++++++++------ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/libs/framework/gtest/src/CelixFrameworkTestSuite.cc b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc index 7dc84bdb8..7a5f5dc08 100644 --- a/libs/framework/gtest/src/CelixFrameworkTestSuite.cc +++ b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc @@ -224,6 +224,15 @@ TEST_F(FrameworkFactoryTestSuite, FactoryCreateTest) { celix_frameworkFactory_destroyFramework(fw); } +TEST_F(FrameworkFactoryTestSuite, ConfigureFrameworkUUIDTest) { + celix_properties_t* config = celix_properties_create(); + celix_properties_set(config, CELIX_FRAMEWORK_UUID, "test-framework"); + framework_t* fw = celix_frameworkFactory_createFramework(config); + ASSERT_TRUE(fw != nullptr); + EXPECT_STREQ("test-framework", celix_framework_getUUID(fw)); + celix_frameworkFactory_destroyFramework(fw); +} + TEST_F(FrameworkFactoryTestSuite, FactoryCreateAndToManyStartAndStopsTest) { framework_t* fw = celix_frameworkFactory_createFramework(nullptr); ASSERT_TRUE(fw != nullptr); diff --git a/libs/framework/include/celix/Constants.h b/libs/framework/include/celix/Constants.h index b00fe8059..1b838bdaa 100644 --- a/libs/framework/include/celix/Constants.h +++ b/libs/framework/include/celix/Constants.h @@ -114,7 +114,7 @@ namespace celix { constexpr const char * const FRAMEWORK_CACHE_DIR = CELIX_FRAMEWORK_CACHE_DIR; /** - * @brief Celix framework environment property (named "org.osgi.framework.uuid") specifying the UUID for the + * @brief Celix framework environment property (named "CELIX_FRAMEWORK_UUID") specifying the UUID for the * framework UUID. * * The framework UUID is used to uniquely identify a single framework. If no framework uuid is provided diff --git a/libs/framework/include/celix_constants.h b/libs/framework/include/celix_constants.h index ffc0abcfb..9de3f332e 100644 --- a/libs/framework/include/celix_constants.h +++ b/libs/framework/include/celix_constants.h @@ -164,7 +164,7 @@ extern "C" { #define CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE "CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE" /** - * @brief Celix framework environment property (named "org.osgi.framework.uuid") specifying the UUID for the + * @brief Celix framework environment property (named "CELIX_FRAMEWORK_UUID") specifying the UUID for the * framework UUID. * * The framework UUID is used to uniquely identify a single framework. If no framework uuid is provided @@ -172,7 +172,7 @@ extern "C" { * * @note The Celix framework expects framework UUIDs to be unique per process. */ -#define CELIX_FRAMEWORK_UUID "org.osgi.framework.uuid" +#define CELIX_FRAMEWORK_UUID "CELIX_FRAMEWORK_UUID" /** * @brief Celix framework environment property (named "CELIX_BUNDLES_PATH") which specified a `:` separated diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c index 4706a4ae6..4848a7bcc 100644 --- a/libs/framework/src/framework.c +++ b/libs/framework/src/framework.c @@ -217,6 +217,15 @@ struct fw_frameworkListener { typedef struct fw_frameworkListener * fw_framework_listener_pt; +static void celix_framework_createAndStoreFrameworkUUID(celix_framework_t* fw) { + if (celix_properties_get(fw->configurationMap, CELIX_FRAMEWORK_UUID, NULL) == NULL) { + char uuid[37]; + uuid_t uid; + uuid_generate(uid); + uuid_unparse(uid, uuid); + celix_properties_set(fw->configurationMap, CELIX_FRAMEWORK_UUID, uuid); + } +} celix_status_t framework_create(framework_pt *out, celix_properties_t* config) { celix_framework_t* framework = calloc(1, sizeof(*framework)); @@ -244,12 +253,7 @@ celix_status_t framework_create(framework_pt *out, celix_properties_t* config) { framework->dispatcher.dynamicEventQueue = celix_arrayList_create(); framework->dispatcher.scheduledEvents = celix_longHashMap_create(); - //create and store framework uuid - char uuid[37]; - uuid_t uid; - uuid_generate(uid); - uuid_unparse(uid, uuid); - celix_properties_set(framework->configurationMap, CELIX_FRAMEWORK_UUID, uuid); + celix_framework_createAndStoreFrameworkUUID(framework); //setup framework logger const char* logStr = celix_framework_getConfigProperty(framework, CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL_CONFIG_NAME, CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL_DEFAULT_VALUE, NULL);