Author: abroekhuis
Date: Mon May 23 15:45:24 2011
New Revision: 1126546

URL: http://svn.apache.org/viewvc?rev=1126546&view=rev
Log:
Merged bundle cache patch from CELIX-2

Modified:
    incubator/celix/trunk/framework/private/include/bundle_cache.h
    incubator/celix/trunk/framework/private/include/celix_errno.h
    incubator/celix/trunk/framework/private/src/bundle_cache.c
    incubator/celix/trunk/framework/private/src/framework.c

Modified: incubator/celix/trunk/framework/private/include/bundle_cache.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/framework/private/include/bundle_cache.h?rev=1126546&r1=1126545&r2=1126546&view=diff
==============================================================================
--- incubator/celix/trunk/framework/private/include/bundle_cache.h (original)
+++ incubator/celix/trunk/framework/private/include/bundle_cache.h Mon May 23 
15:45:24 2011
@@ -32,10 +32,10 @@
 
 typedef struct bundleCache * BUNDLE_CACHE;
 
-BUNDLE_CACHE bundleCache_create(PROPERTIES configurationMap, apr_pool_t *mp);
-ARRAY_LIST bundleCache_getArchives(BUNDLE_CACHE cache);
-BUNDLE_ARCHIVE bundleCache_createArchive(BUNDLE_CACHE cache, long id, char * 
location, apr_pool_t *bundlePool);
-void bundleCache_delete(BUNDLE_CACHE cache);
+celix_status_t bundleCache_create(PROPERTIES configurationMap, apr_pool_t *mp, 
BUNDLE_CACHE *bundle_cache);
+celix_status_t bundleCache_getArchives(BUNDLE_CACHE cache, ARRAY_LIST 
*archives);
+celix_status_t bundleCache_createArchive(BUNDLE_CACHE cache, long id, char * 
location, apr_pool_t *bundlePool, BUNDLE_ARCHIVE *archive);
+celix_status_t bundleCache_delete(BUNDLE_CACHE cache);
 
 
 #endif /* BUNDLE_CACHE_H_ */

Modified: incubator/celix/trunk/framework/private/include/celix_errno.h
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/framework/private/include/celix_errno.h?rev=1126546&r1=1126545&r2=1126546&view=diff
==============================================================================
--- incubator/celix/trunk/framework/private/include/celix_errno.h (original)
+++ incubator/celix/trunk/framework/private/include/celix_errno.h Mon May 23 
15:45:24 2011
@@ -23,6 +23,7 @@ typedef int celix_status_t;
 #define CELIX_FRAMEWORK_SHUTDOWN (CELIX_START_ERROR + 5)
 #define CELIX_ILLEGAL_STATE (CELIX_START_ERROR + 6)
 #define CELIX_FRAMEWORK_EXCEPTION (CELIX_START_ERROR + 7)
+#define CELIX_FILE_IO_EXCEPTION (CELIX_START_ERROR + 8)
 
 #define CELIX_ENOMEM ENOMEM
 

Modified: incubator/celix/trunk/framework/private/src/bundle_cache.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/framework/private/src/bundle_cache.c?rev=1126546&r1=1126545&r2=1126546&view=diff
==============================================================================
--- incubator/celix/trunk/framework/private/src/bundle_cache.c (original)
+++ incubator/celix/trunk/framework/private/src/bundle_cache.c Mon May 23 
15:45:24 2011
@@ -26,7 +26,6 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
-//#include <sys/stat.h>
 
 #include <apr_file_io.h>
 
@@ -41,48 +40,66 @@ struct bundleCache {
        apr_pool_t *mp;
 };
 
-void bundleCache_deleteTree(char * directory, apr_pool_t *mp);
+static celix_status_t bundleCache_deleteTree(char * directory, apr_pool_t *mp);
 
-BUNDLE_CACHE bundleCache_create(PROPERTIES configurationMap, apr_pool_t *mp) {
+celix_status_t bundleCache_create(PROPERTIES configurationMap, apr_pool_t *mp, 
BUNDLE_CACHE *bundle_cache) {
+    celix_status_t status = CELIX_SUCCESS;
        BUNDLE_CACHE cache = (BUNDLE_CACHE) malloc(sizeof(*cache));
 
-       cache->configurationMap = configurationMap;
-       char * cacheDir = properties_get(configurationMap, (char *) 
FRAMEWORK_STORAGE);
-       if (cacheDir == NULL) {
-               cacheDir = ".cache";
+       if (configurationMap != NULL && mp != NULL && *bundle_cache == NULL) {
+        cache->configurationMap = configurationMap;
+        char * cacheDir = properties_get(configurationMap, (char *) 
FRAMEWORK_STORAGE);
+        if (cacheDir == NULL) {
+            cacheDir = ".cache";
+        }
+        cache->cacheDir = cacheDir;
+        cache->mp = mp;
+
+        *bundle_cache = cache;
+       } else {
+        status = CELIX_ILLEGAL_ARGUMENT;
        }
-       cache->cacheDir = cacheDir;
-       cache->mp = mp;
 
-       return cache;
+       return status;
 }
 
-void bundleCache_delete(BUNDLE_CACHE cache) {
-       bundleCache_deleteTree(cache->cacheDir, cache->mp);
+celix_status_t bundleCache_delete(BUNDLE_CACHE cache) {
+       return bundleCache_deleteTree(cache->cacheDir, cache->mp);
 }
 
-void bundleCache_deleteTree(char * directory, apr_pool_t *mp) {
+static celix_status_t bundleCache_deleteTree(char * directory, apr_pool_t *mp) 
{
+    celix_status_t status = CELIX_SUCCESS;
        apr_dir_t *dir;
-       apr_dir_open(&dir, directory, mp);
-       apr_finfo_t dp;
-       while ((apr_dir_read(&dp, APR_FINFO_DIRENT|APR_FINFO_TYPE, dir)) == 
APR_SUCCESS) {
-               if ((strcmp((dp.name), ".") != 0) && (strcmp((dp.name), "..") 
!= 0)) {
-                       char subdir[strlen(directory) + strlen(dp.name) + 2];
-                       strcpy(subdir, directory);
-                       strcat(subdir, "/");
-                       strcat(subdir, dp.name);
-
-                       if (dp.filetype == APR_DIR) {
-                               bundleCache_deleteTree(subdir, mp);
-                       } else {
-                               remove(subdir);
-                       }
-               }
+
+       if (directory && mp) {
+        if (apr_dir_open(&dir, directory, mp) == APR_SUCCESS) {
+            apr_finfo_t dp;
+            while ((apr_dir_read(&dp, APR_FINFO_DIRENT|APR_FINFO_TYPE, dir)) 
== APR_SUCCESS) {
+                if ((strcmp((dp.name), ".") != 0) && (strcmp((dp.name), "..") 
!= 0)) {
+                    char subdir[strlen(directory) + strlen(dp.name) + 2];
+                    strcpy(subdir, directory);
+                    strcat(subdir, "/");
+                    strcat(subdir, dp.name);
+
+                    if (dp.filetype == APR_DIR) {
+                        bundleCache_deleteTree(subdir, mp);
+                    } else {
+                        remove(subdir);
+                    }
+                }
+            }
+            remove(directory);
+        } else {
+            status = CELIX_FILE_IO_EXCEPTION;
+        }
+       } else {
+           status = CELIX_ILLEGAL_ARGUMENT;
        }
-       remove(directory);
+
+       return status;
 }
 
-ARRAY_LIST bundleCache_getArchives(BUNDLE_CACHE cache) {
+celix_status_t bundleCache_getArchives(BUNDLE_CACHE cache, ARRAY_LIST 
*archives) {
        apr_dir_t *dir;
        apr_status_t status = apr_dir_open(&dir, cache->cacheDir, cache->mp);
 
@@ -91,33 +108,51 @@ ARRAY_LIST bundleCache_getArchives(BUNDL
                status = apr_dir_open(&dir, cache->cacheDir, cache->mp);
        }
 
-       ARRAY_LIST list = arrayList_create();
-       apr_finfo_t dp;
-       while ((apr_dir_read(&dp, APR_FINFO_DIRENT|APR_FINFO_TYPE, dir)) == 
APR_SUCCESS) {
-               char archiveRoot[strlen(cache->cacheDir) + strlen(dp.name) + 2];
-               strcpy(archiveRoot, cache->cacheDir);
-               strcat(archiveRoot, "/");
-               strcat(archiveRoot, dp.name);
-
-               if (dp.filetype == APR_DIR
-                               && (strcmp((dp.name), ".") != 0)
-                               && (strcmp((dp.name), "..") != 0)
-                               && (strncmp(dp.name, "bundle", 6) == 0)
-                               && (strcmp(dp.name, "bundle0") != 0)) {
-
-                       BUNDLE_ARCHIVE archive = 
bundleArchive_recreate(strdup(archiveRoot), cache->mp);
-                       arrayList_add(list, archive);
-               }
+       if (status == APR_SUCCESS) {
+        ARRAY_LIST list = arrayList_create();
+        apr_finfo_t dp;
+        while ((apr_dir_read(&dp, APR_FINFO_DIRENT|APR_FINFO_TYPE, dir)) == 
APR_SUCCESS) {
+            char archiveRoot[strlen(cache->cacheDir) + strlen(dp.name) + 2];
+            strcpy(archiveRoot, cache->cacheDir);
+            strcat(archiveRoot, "/");
+            strcat(archiveRoot, dp.name);
+
+            if (dp.filetype == APR_DIR
+                    && (strcmp((dp.name), ".") != 0)
+                    && (strcmp((dp.name), "..") != 0)
+                    && (strncmp(dp.name, "bundle", 6) == 0)
+                    && (strcmp(dp.name, "bundle0") != 0)) {
+
+                BUNDLE_ARCHIVE archive = 
bundleArchive_recreate(strdup(archiveRoot), cache->mp);
+                arrayList_add(list, archive);
+            }
+        }
+
+        apr_dir_close(dir);
+
+        *archives = list;
+
+        status = CELIX_SUCCESS;
+       } else {
+           status = CELIX_FILE_IO_EXCEPTION;
        }
 
-       apr_dir_close(dir);
-
-       return list;
+       return status;
 }
 
-BUNDLE_ARCHIVE bundleCache_createArchive(BUNDLE_CACHE cache, long id, char * 
location, apr_pool_t *bundlePool) {
+celix_status_t bundleCache_createArchive(BUNDLE_CACHE cache, long id, char * 
location, apr_pool_t *bundlePool,
+        BUNDLE_ARCHIVE *bundle_archive) {
+    celix_status_t status;
        char archiveRoot[256];
-       sprintf(archiveRoot, "%s/bundle%ld",  cache->cacheDir, id);
+    BUNDLE_ARCHIVE archive;
+
+       if (cache && location && bundlePool) {
+        sprintf(archiveRoot, "%s/bundle%ld",  cache->cacheDir, id);
+
+        *bundle_archive = bundleArchive_create(strdup(archiveRoot), id, 
location, bundlePool);
+
+        status = CELIX_SUCCESS;
+       }
 
-       return bundleArchive_create(strdup(archiveRoot), id, location, 
bundlePool);
+       return status;
 }

Modified: incubator/celix/trunk/framework/private/src/framework.c
URL: 
http://svn.apache.org/viewvc/incubator/celix/trunk/framework/private/src/framework.c?rev=1126546&r1=1126545&r2=1126546&view=diff
==============================================================================
--- incubator/celix/trunk/framework/private/src/framework.c (original)
+++ incubator/celix/trunk/framework/private/src/framework.c Mon May 23 15:45:24 
2011
@@ -167,20 +167,23 @@ celix_status_t framework_destroy(FRAMEWO
 }
 
 celix_status_t fw_init(FRAMEWORK framework) {
-       celix_status_t lock = framework_acquireBundleLock(framework, 
framework->bundle, 
BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE);
-       if (lock != CELIX_SUCCESS) {
+       celix_status_t status = framework_acquireBundleLock(framework, 
framework->bundle, 
BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE);
+       if (status != CELIX_SUCCESS) {
                framework_releaseBundleLock(framework, framework->bundle);
-               return lock;
+               return status;
        }
 
        if ((bundle_getState(framework->bundle) == BUNDLE_INSTALLED) || 
(bundle_getState(framework->bundle) == BUNDLE_RESOLVED)) {
-               PROPERTIES props = properties_create();
+           PROPERTIES props = properties_create();
                properties_set(props, (char *) FRAMEWORK_STORAGE, ".cache");
-               framework->cache = bundleCache_create(props, framework->mp);
-
-               if (bundle_getState(framework->bundle) == BUNDLE_INSTALLED) {
-                       // clean cache
-                       // bundleCache_delete(framework->cache);
+               status = bundleCache_create(props, framework->mp, 
&framework->cache);
+               if (status == CELIX_SUCCESS) {
+            if (bundle_getState(framework->bundle) == BUNDLE_INSTALLED) {
+                // clean cache
+                // bundleCache_delete(framework->cache);
+            }
+               } else {
+                   return status;
                }
        }
 
@@ -199,70 +202,75 @@ celix_status_t fw_init(FRAMEWORK framewo
        }
 
        // reload archives from cache
-       ARRAY_LIST archives = bundleCache_getArchives(framework->cache);
-       int arcIdx;
-       for (arcIdx = 0; arcIdx < arrayList_size(archives); arcIdx++) {
-               BUNDLE_ARCHIVE archive = (BUNDLE_ARCHIVE) 
arrayList_get(archives, arcIdx);
-               framework->nextBundleId = fmaxl(framework->nextBundleId, 
bundleArchive_getId(archive) + 1);
+       ARRAY_LIST archives;
+       status = bundleCache_getArchives(framework->cache, &archives);
+       if (status != CELIX_SUCCESS) {
+           return status;
+       } else {
+        int arcIdx;
+        for (arcIdx = 0; arcIdx < arrayList_size(archives); arcIdx++) {
+            BUNDLE_ARCHIVE archive = (BUNDLE_ARCHIVE) arrayList_get(archives, 
arcIdx);
+               framework->nextBundleId = fmaxl(framework->nextBundleId, 
bundleArchive_getId(archive) + 1);
+
+            if (bundleArchive_getPersistentState(archive) == 
BUNDLE_UNINSTALLED) {
+                bundleArchive_closeAndDelete(archive);
+            } else {
+                BUNDLE bundle;
+                fw_installBundle2(framework, &bundle, 
bundleArchive_getId(archive), bundleArchive_getLocation(archive), archive);
+            }
+        }
+        arrayList_destroy(archives);
+        framework->registry = serviceRegistry_create(fw_serviceChanged);
 
-               if (bundleArchive_getPersistentState(archive) == 
BUNDLE_UNINSTALLED) {
-                       bundleArchive_closeAndDelete(archive);
-               } else {
-                       BUNDLE bundle;
-                       fw_installBundle2(framework, &bundle, 
bundleArchive_getId(archive), bundleArchive_getLocation(archive), archive);
-               }
-       }
-       arrayList_destroy(archives);
-       framework->registry = serviceRegistry_create(fw_serviceChanged);
+        framework_setBundleStateAndNotify(framework, framework->bundle, 
BUNDLE_STARTING);
 
-       framework_setBundleStateAndNotify(framework, framework->bundle, 
BUNDLE_STARTING);
+        pthread_cond_init(&framework->shutdownGate, NULL);
 
-       pthread_cond_init(&framework->shutdownGate, NULL);
+        void * handle = dlopen(NULL, RTLD_LAZY|RTLD_LOCAL);
+        if (handle == NULL) {
+            printf ("%s\n", dlerror());
+            framework_releaseBundleLock(framework, framework->bundle);
+            return CELIX_START_ERROR;
+        }
 
-       void * handle = dlopen(NULL, RTLD_LAZY|RTLD_LOCAL);
-       if (handle == NULL) {
-               printf ("%s\n", dlerror());
-               framework_releaseBundleLock(framework, framework->bundle);
-               return CELIX_START_ERROR;
-       }
+        BUNDLE_CONTEXT context = NULL;
+        if (bundleContext_create(framework, framework->bundle, &context) != 
CELIX_SUCCESS) {
+            return CELIX_START_ERROR;
+        }
+        bundle_setContext(framework->bundle, context);
 
-       BUNDLE_CONTEXT context = NULL;
-       if (bundleContext_create(framework, framework->bundle, &context) != 
CELIX_SUCCESS) {
-               return CELIX_START_ERROR;
-       }
-       bundle_setContext(framework->bundle, context);
+        bundle_setHandle(framework->bundle, handle);
 
-       bundle_setHandle(framework->bundle, handle);
+        ACTIVATOR activator = (ACTIVATOR) malloc(sizeof(*activator));
+        void * (*create)(BUNDLE_CONTEXT context);
+        void (*start)(void * handle, BUNDLE_CONTEXT context);
+        void (*stop)(void * handle, BUNDLE_CONTEXT context);
+        void (*destroy)(void * handle, BUNDLE_CONTEXT context);
+        create = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_CREATE);
+        start = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_START);
+        stop = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_STOP);
+        destroy = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_DESTROY);
+        activator->start = start;
+        activator->stop = stop;
+        activator->destroy = destroy;
+        bundle_setActivator(framework->bundle, activator);
+
+        void * userData = NULL;
+        if (create != NULL) {
+            userData = create(bundle_getContext(framework->bundle));
+        }
+        activator->userData = userData;
 
-       ACTIVATOR activator = (ACTIVATOR) malloc(sizeof(*activator));
-       void * (*create)(BUNDLE_CONTEXT context);
-       void (*start)(void * handle, BUNDLE_CONTEXT context);
-       void (*stop)(void * handle, BUNDLE_CONTEXT context);
-       void (*destroy)(void * handle, BUNDLE_CONTEXT context);
-       create = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_CREATE);
-       start = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_START);
-       stop = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_STOP);
-       destroy = dlsym(bundle_getHandle(framework->bundle), 
BUNDLE_ACTIVATOR_DESTROY);
-       activator->start = start;
-       activator->stop = stop;
-       activator->destroy = destroy;
-       bundle_setActivator(framework->bundle, activator);
+        if (start != NULL) {
+            start(userData, bundle_getContext(framework->bundle));
+        }
 
-       void * userData = NULL;
-       if (create != NULL) {
-               userData = create(bundle_getContext(framework->bundle));
-       }
-       activator->userData = userData;
+        m_serviceListeners = arrayList_create();
+        framework_releaseBundleLock(framework, framework->bundle);
 
-       if (start != NULL) {
-               start(userData, bundle_getContext(framework->bundle));
+        status = CELIX_SUCCESS;
        }
-
-
-       m_serviceListeners = arrayList_create();
-       framework_releaseBundleLock(framework, framework->bundle);
-
-       return CELIX_SUCCESS;
+       return status;
 }
 
 celix_status_t framework_start(FRAMEWORK framework) {
@@ -293,7 +301,11 @@ celix_status_t fw_installBundle(FRAMEWOR
 }
 
 celix_status_t fw_installBundle2(FRAMEWORK framework, BUNDLE * bundle, long 
id, char * location, BUNDLE_ARCHIVE archive) {
-       framework_acquireInstallLock(framework, location);
+    BUNDLE_ARCHIVE bundle_archive = NULL;
+       celix_status_t status = framework_acquireInstallLock(framework, 
location);
+    if (status != CELIX_SUCCESS) {
+        return status;
+    }
 
        if (bundle_getState(framework->bundle) == BUNDLE_STOPPING || 
bundle_getState(framework->bundle) == BUNDLE_UNINSTALLED) {
                printf("The framework has been shutdown.\n");
@@ -311,7 +323,12 @@ celix_status_t fw_installBundle2(FRAMEWO
        apr_pool_create(&bundlePool, framework->mp);
        if (archive == NULL) {
                id = framework_getNextBundleId(framework);
-               archive = bundleCache_createArchive(framework->cache, id, 
location, bundlePool); //fw_createArchive(id, location);
+               status = bundleCache_createArchive(framework->cache, id, 
location, bundlePool, &bundle_archive);
+               if (status != CELIX_SUCCESS) {
+                   return status;
+               } else {
+                   archive = bundle_archive;
+               }
        } else {
                // purge revision
                // multiple revisions not yet implemented
@@ -388,7 +405,7 @@ celix_status_t fw_startBundle(FRAMEWORK 
                        framework_releaseBundleLock(framework, bundle);
                        return CELIX_SUCCESS;
                case BUNDLE_INSTALLED:
-                   if (!module_isResolved(bundle_getCurrentModule(bundle))) {
+                       if 
(!module_isResolved(bundle_getCurrentModule(bundle))) {
                 wires = resolver_resolve(bundle_getCurrentModule(bundle));
                 if (wires == NULL) {
                     framework_releaseBundleLock(framework, bundle);
@@ -662,7 +679,7 @@ celix_status_t fw_refreshBundles(FRAMEWO
         BUNDLE *newTargets;
         int nrofvalues;
         hashMapValues_toArray(values, (void *) &newTargets, &nrofvalues);
-
+    
         bool restart = false;
         if (newTargets != NULL) {
             int i = 0;


Reply via email to