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 2018/10/12 09:03:36 UTC

[19/34] celix git commit: CELIX-434: Updates cache behaviour.

CELIX-434: Updates cache behaviour.

If no cache dir is specified a tmp dir in /tmp/celix-cache-<progname>-<fwUUID> will be created
and used for the bundle caches. This directory is also removed if the program exists normally.


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/d311fa7e
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/d311fa7e
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/d311fa7e

Branch: refs/heads/develop
Commit: d311fa7edfc39eed52807b49e32573a21ea4b20c
Parents: b635e27
Author: Pepijn Noltes <pe...@gmail.com>
Authored: Fri Sep 28 19:31:45 2018 +0200
Committer: Pepijn Noltes <pe...@gmail.com>
Committed: Fri Sep 28 19:31:45 2018 +0200

----------------------------------------------------------------------
 libs/framework/private/mock/bundle_cache_mock.c |  2 +-
 .../private/test/bundle_cache_test.cpp          |  2 +-
 libs/framework/src/bundle_cache.c               | 44 +++++++++++++++-----
 libs/framework/src/bundle_cache.h               |  3 +-
 libs/framework/src/bundle_cache_private.h       |  1 +
 libs/framework/src/framework.c                  | 20 ++++-----
 6 files changed, 47 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/d311fa7e/libs/framework/private/mock/bundle_cache_mock.c
----------------------------------------------------------------------
diff --git a/libs/framework/private/mock/bundle_cache_mock.c b/libs/framework/private/mock/bundle_cache_mock.c
index 169a256..94ae731 100644
--- a/libs/framework/private/mock/bundle_cache_mock.c
+++ b/libs/framework/private/mock/bundle_cache_mock.c
@@ -27,7 +27,7 @@
 
 #include "bundle_cache.h"
 
-celix_status_t bundleCache_create(properties_pt configurationMap, bundle_cache_pt *bundle_cache) {
+celix_status_t bundleCache_create(const char *fwUUID, properties_pt configurationMap, bundle_cache_pt *bundle_cache) {
 	mock_c()->actualCall("bundleCache_create")
 		->withPointerParameters("configurationMap", configurationMap)
 		->withOutputParameter("bundle_cache", bundle_cache);

http://git-wip-us.apache.org/repos/asf/celix/blob/d311fa7e/libs/framework/private/test/bundle_cache_test.cpp
----------------------------------------------------------------------
diff --git a/libs/framework/private/test/bundle_cache_test.cpp b/libs/framework/private/test/bundle_cache_test.cpp
index 13e0e80..bfdc6af 100644
--- a/libs/framework/private/test/bundle_cache_test.cpp
+++ b/libs/framework/private/test/bundle_cache_test.cpp
@@ -64,7 +64,7 @@ TEST(bundle_cache, create) {
 		.andReturnValue((char *) NULL);
 
 	bundle_cache_pt cache = NULL;
-	LONGS_EQUAL(CELIX_SUCCESS, bundleCache_create(configuration, &cache));
+	LONGS_EQUAL(CELIX_SUCCESS, bundleCache_create("test-uuid", configuration, &cache));
 
 	LONGS_EQUAL(CELIX_SUCCESS, bundleCache_destroy(&cache));
 }

http://git-wip-us.apache.org/repos/asf/celix/blob/d311fa7e/libs/framework/src/bundle_cache.c
----------------------------------------------------------------------
diff --git a/libs/framework/src/bundle_cache.c b/libs/framework/src/bundle_cache.c
index 6de8ab7..054aa80 100644
--- a/libs/framework/src/bundle_cache.c
+++ b/libs/framework/src/bundle_cache.c
@@ -16,28 +16,34 @@
  *specific language governing permissions and limitations
  *under the License.
  */
-/*
- * bundle_cache.c
- *
- *  \date       Aug 6, 2010
- *  \author    	<a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
- *  \copyright	Apache License, Version 2.0
- */
+
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #include "bundle_cache_private.h"
 #include "bundle_archive.h"
 #include "constants.h"
 #include "celix_log.h"
+#include "celix_properties.h"
 
 static celix_status_t bundleCache_deleteTree(bundle_cache_pt cache, char * directory);
 
-celix_status_t bundleCache_create(properties_pt configurationMap, bundle_cache_pt *bundle_cache) {
+static const char* bundleCache_progamName() {
+#if defined(__APPLE__) || defined(__FreeBSD__)
+	return getprogname();
+#elif defined(_GNU_SOURCE)
+	return program_invocation_short_name;
+#else
+	return "";
+#endif
+}
+
+celix_status_t bundleCache_create(const char *fwUUID, celix_properties_t *configurationMap, bundle_cache_pt *bundle_cache) {
 	celix_status_t status;
 	bundle_cache_pt cache;
 
@@ -52,9 +58,22 @@ celix_status_t bundleCache_create(properties_pt configurationMap, bundle_cache_p
 		char* cacheDir = (char*)properties_get(configurationMap, (char *) OSGI_FRAMEWORK_FRAMEWORK_STORAGE);
 		cache->configurationMap = configurationMap;
 		if (cacheDir == NULL) {
-			cacheDir = ".cache";
+			//Using /tmp dir for cache, so that multiple frameworks can be launched
+			//instead of cacheDir = ".cache";
+			const char *pg = bundleCache_progamName();
+			if (pg == NULL) {
+			    pg = "";
+			}
+			size_t len = (size_t)snprintf(NULL, 0, "/tmp/celix-cache-%s-%s",pg, fwUUID) + 1;
+			char *tmpdir = calloc(len, sizeof(char));
+			snprintf(tmpdir, len, "/tmp/celix-cache-%s-%s", pg, fwUUID);
+
+			cache->cacheDir = tmpdir;
+			cache->deleteOnDestroy = true;
+		} else {
+			cache->cacheDir = strdup(cacheDir);
+			cache->deleteOnDestroy = false;
 		}
-		cache->cacheDir = cacheDir;
 
 		*bundle_cache = cache;
 		status = CELIX_SUCCESS;
@@ -66,7 +85,10 @@ celix_status_t bundleCache_create(properties_pt configurationMap, bundle_cache_p
 }
 
 celix_status_t bundleCache_destroy(bundle_cache_pt *cache) {
-
+	if ((*cache)->deleteOnDestroy) {
+		bundleCache_delete(*cache);
+	}
+	free((*cache)->cacheDir);
 	free(*cache);
 	*cache = NULL;
 

http://git-wip-us.apache.org/repos/asf/celix/blob/d311fa7e/libs/framework/src/bundle_cache.h
----------------------------------------------------------------------
diff --git a/libs/framework/src/bundle_cache.h b/libs/framework/src/bundle_cache.h
index 350f33b..c9de996 100644
--- a/libs/framework/src/bundle_cache.h
+++ b/libs/framework/src/bundle_cache.h
@@ -47,14 +47,13 @@ typedef struct bundleCache *bundle_cache_pt;
  * Creates the bundle cache using the supplied configuration map.
  *
  * @param configurationMap Set with properties to use for this cache
- * @param mp The memory pool to use for allocation the cache
  * @param bundle_cache Output parameter for the created cache
  * @return Status code indication failure or success:
  * 		- CELIX_SUCCESS when no errors are encountered.
  * 		- CELIX_ILLEGAL_ARGUMENT If <code>bundle_cache</code> not is null.
  * 		- CELIX_ENOMEM If allocating memory for <code>bundle_cache</code> failed.
  */
-celix_status_t bundleCache_create(properties_pt configurationMap, bundle_cache_pt *bundle_cache);
+celix_status_t bundleCache_create(const char *fwUUID, properties_pt configurationMap, bundle_cache_pt *bundle_cache);
 
 /**
  * Frees the bundle_cache memory allocated in bundleCache_create

http://git-wip-us.apache.org/repos/asf/celix/blob/d311fa7e/libs/framework/src/bundle_cache_private.h
----------------------------------------------------------------------
diff --git a/libs/framework/src/bundle_cache_private.h b/libs/framework/src/bundle_cache_private.h
index 69e26ee..bb701d9 100644
--- a/libs/framework/src/bundle_cache_private.h
+++ b/libs/framework/src/bundle_cache_private.h
@@ -33,6 +33,7 @@
 struct bundleCache {
 	properties_pt configurationMap;
 	char * cacheDir;
+	bool deleteOnDestroy;
 };
 
 

http://git-wip-us.apache.org/repos/asf/celix/blob/d311fa7e/libs/framework/src/framework.c
----------------------------------------------------------------------
diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c
index b0d505d..4284f9a 100644
--- a/libs/framework/src/framework.c
+++ b/libs/framework/src/framework.c
@@ -433,6 +433,15 @@ celix_status_t fw_init(framework_pt framework) {
 	array_list_pt archives = NULL;
 	bundle_archive_pt archive = NULL;
 
+    /*create and store framework uuid*/
+    char uuid[37];
+
+    uuid_t uid;
+    uuid_generate(uid);
+    uuid_unparse(uid, uuid);
+
+    properties_set(framework->configurationMap, (char*) OSGI_FRAMEWORK_FRAMEWORK_UUID, uuid);
+
 	celix_status_t status = CELIX_SUCCESS;
 	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, framework->bundle, OSGI_FRAMEWORK_BUNDLE_INSTALLED|OSGI_FRAMEWORK_BUNDLE_RESOLVED|OSGI_FRAMEWORK_BUNDLE_STARTING|OSGI_FRAMEWORK_BUNDLE_ACTIVE));
 	status = CELIX_DO_IF(status, arrayList_create(&framework->serviceListeners)); //entry is celix_fw_service_listener_entry_t
@@ -444,7 +453,7 @@ celix_status_t fw_init(framework_pt framework) {
 	if (status == CELIX_SUCCESS) {
 	    if ((state == OSGI_FRAMEWORK_BUNDLE_INSTALLED) || (state == OSGI_FRAMEWORK_BUNDLE_RESOLVED)) {
 	        bundle_state_e state;
-	        status = CELIX_DO_IF(status, bundleCache_create(framework->configurationMap,&framework->cache));
+	        status = CELIX_DO_IF(status, bundleCache_create(uuid, framework->configurationMap,&framework->cache));
 	        status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
 	        if (status == CELIX_SUCCESS) {
 	            if (state == OSGI_FRAMEWORK_BUNDLE_INSTALLED) {
@@ -458,15 +467,6 @@ celix_status_t fw_init(framework_pt framework) {
 	}
 
 	if (status == CELIX_SUCCESS) {
-        /*create and store framework uuid*/
-        char uuid[37];
-
-	    uuid_t uid;
-        uuid_generate(uid);
-        uuid_unparse(uid, uuid);
-
-        properties_set(framework->configurationMap, (char*) OSGI_FRAMEWORK_FRAMEWORK_UUID, uuid);
-
         framework->installedBundleMap = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
 	}