incubator-celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From abroekh...@apache.org
Subject svn commit: r1543333 [2/2] - in /incubator/celix/trunk: framework/ framework/private/mock/ framework/private/src/ framework/public/include/ launcher/private/src/
Date Tue, 19 Nov 2013 07:40:02 GMT
Modified: incubator/celix/trunk/framework/private/src/framework.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/framework/private/src/framework.c?rev=1543333&r1=1543332&r2=1543333&view=diff
==============================================================================
--- incubator/celix/trunk/framework/private/src/framework.c (original)
+++ incubator/celix/trunk/framework/private/src/framework.c Tue Nov 19 07:40:01 2013
@@ -100,11 +100,16 @@ struct framework {
 	apr_thread_t *dispatcherThread;
 };
 
+typedef celix_status_t (*create_function_pt)(bundle_context_pt context, void **userData);
+typedef celix_status_t (*start_function_pt)(void * handle, bundle_context_pt context);
+typedef celix_status_t (*stop_function_pt)(void * handle, bundle_context_pt context);
+typedef celix_status_t (*destroy_function_pt)(void * handle, bundle_context_pt context);
+
 struct activator {
-	void * userData;
-	void (*start)(void * userData, bundle_context_pt context);
-	void (*stop)(void * userData, bundle_context_pt context);
-	void (*destroy)(void * userData, bundle_context_pt context);
+    void * userData;
+    start_function_pt start;
+    stop_function_pt stop;
+    destroy_function_pt destroy;
 };
 
 celix_status_t framework_setBundleStateAndNotify(framework_pt framework, bundle_pt bundle, int state);
@@ -129,7 +134,7 @@ celix_status_t fw_refreshBundle(framewor
 celix_status_t fw_populateDependentGraph(framework_pt framework, bundle_pt exporter, hash_map_pt *map);
 
 celix_status_t fw_fireBundleEvent(framework_pt framework, bundle_event_type_e, bundle_pt bundle);
-celix_status_t fw_fireFrameworkEvent(framework_pt framework, framework_event_type_e eventType, bundle_pt bundle, celix_status_t errorCode, char *error);
+celix_status_t fw_fireFrameworkEvent(framework_pt framework, framework_event_type_e eventType, bundle_pt bundle, celix_status_t errorCode);
 static void *APR_THREAD_FUNC fw_eventDispatcher(apr_thread_t *thd, void *fw);
 
 celix_status_t fw_invokeBundleListener(framework_pt framework, bundle_listener_pt listener, bundle_event_pt event, bundle_pt bundle);
@@ -192,95 +197,54 @@ typedef struct request *request_pt;
 celix_status_t framework_create(framework_pt *framework, apr_pool_t *memoryPool, properties_pt config) {
     celix_status_t status = CELIX_SUCCESS;
 
-    // framework_log(NULL, FW_LOG_INFO, __func__, __FILE__, __LINE__, "Log this message %s\n", "now!");
-    // fw_log(NULL, FW_LOG_INFO, "Log this message %s\n", "now!");
-
-	*framework = (framework_pt) apr_palloc(memoryPool, sizeof(**framework));
-	if (*framework == NULL) {
-		status = CELIX_ENOMEM;
-	} else {
-	    apr_status_t apr_status = apr_pool_create(&(*framework)->mp, memoryPool);
-        if (apr_status != APR_SUCCESS) {
-            status = CELIX_FRAMEWORK_EXCEPTION;
-            fw_log(NULL, FW_LOG_ERROR,  "Could not create framework memory pool");
-        } else {
-            bundle_pt bundle = NULL;
-            apr_pool_t *bundlePool;
-//            apr_status_t apr_status = apr_pool_create(&bundlePool, (*framework)->mp);
-//            if (apr_status != APR_SUCCESS) {
-//                status = CELIX_FRAMEWORK_EXCEPTION;
-//            } else {
-                apr_status_t apr_status = bundle_create(&bundle, (*framework)->mp);
-                if (apr_status != CELIX_SUCCESS) {
-                    status = CELIX_FRAMEWORK_EXCEPTION;
-                    fw_log(NULL, FW_LOG_ERROR,  "Could not create framework bundle");
-                } else {
-                    apr_status_t apr_status = apr_thread_cond_create(&(*framework)->condition, (*framework)->mp);
-                    if (apr_status != APR_SUCCESS) {
-                        status = CELIX_FRAMEWORK_EXCEPTION;
-                        fw_log(NULL, FW_LOG_ERROR,  "Could not create framework condition");
-                    } else {
-                        apr_status_t apr_status = apr_thread_mutex_create(&(*framework)->mutex, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp);
-                        if (apr_status != APR_SUCCESS) {
-                            status = CELIX_FRAMEWORK_EXCEPTION;
-                            fw_log(NULL, FW_LOG_ERROR,  "Could not create framework mutex");
-                        } else {
-                            apr_status_t apr_status = apr_thread_mutex_create(&(*framework)->bundleLock, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp);
-                            if (apr_status != APR_SUCCESS) {
-                                status = CELIX_FRAMEWORK_EXCEPTION;
-                                fw_log(NULL, FW_LOG_ERROR,  "Could not create framework bundle lock mutex");
-                            } else {
-                                apr_status_t apr_status = apr_thread_mutex_create(&(*framework)->installRequestLock, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp);
-                                if (apr_status != APR_SUCCESS) {
-                                    status = CELIX_FRAMEWORK_EXCEPTION;
-                                    fw_log(NULL, FW_LOG_ERROR,  "Could not create framework install request lock mutex");
-                                } else {
-                                	apr_status_t apr_status = apr_thread_mutex_create(&(*framework)->dispatcherLock, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp);
-                                	if (apr_status != CELIX_SUCCESS) {
-										status = CELIX_FRAMEWORK_EXCEPTION;
-										fw_log(NULL, FW_LOG_ERROR,  "Could not create framework dispatcher lock mutex");
-									} else {
-										apr_status_t apr_status = apr_thread_cond_create(&(*framework)->dispatcher, (*framework)->mp);
-										if (apr_status != APR_SUCCESS) {
-											status = CELIX_FRAMEWORK_EXCEPTION;
-											fw_log(NULL, FW_LOG_ERROR,  "Could not create framework dispatcher condition");
-										} else {
-											(*framework)->bundle = bundle;
-
-											(*framework)->installedBundleMap = NULL;
-											(*framework)->registry = NULL;
-
-											(*framework)->interrupted = false;
-											(*framework)->shutdown = false;
-
-											(*framework)->globalLockWaitersList = NULL;
-											arrayList_create((*framework)->mp, &(*framework)->globalLockWaitersList);
-											(*framework)->globalLockCount = 0;
-											(*framework)->globalLockThread = 0;
-											(*framework)->nextBundleId = 1l;
-											(*framework)->cache = NULL;
-
-											(*framework)->installRequestMap = hashMap_create(utils_stringHash, utils_stringHash, utils_stringEquals, utils_stringEquals);
-											(*framework)->serviceListeners = NULL;
-											(*framework)->bundleListeners = NULL;
-											(*framework)->frameworkListeners = NULL;
-											(*framework)->requests = NULL;
-											(*framework)->shutdownGate = NULL;
-											(*framework)->configurationMap = config;
-
-											status = bundle_setFramework((*framework)->bundle, (*framework));
-										}
-									}
-                                }
-                            }
-//                        }
-                    }
-                }
+    *framework = (framework_pt) apr_palloc(memoryPool, sizeof(**framework));
+    if (*framework != NULL) {
+        apr_status_t apr_status = APR_SUCCESS;
+        apr_status = CELIX_DO_IF(apr_status, apr_pool_create(&(*framework)->mp, memoryPool));
+        apr_status = CELIX_DO_IF(apr_status, apr_thread_cond_create(&(*framework)->condition, (*framework)->mp));
+        apr_status = CELIX_DO_IF(apr_status, apr_thread_mutex_create(&(*framework)->mutex, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp));
+        apr_status = CELIX_DO_IF(apr_status, apr_thread_mutex_create(&(*framework)->bundleLock, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp));
+        apr_status = CELIX_DO_IF(apr_status, apr_thread_mutex_create(&(*framework)->installRequestLock, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp));
+        apr_status = CELIX_DO_IF(apr_status, apr_thread_mutex_create(&(*framework)->dispatcherLock, APR_THREAD_MUTEX_UNNESTED, (*framework)->mp));
+        apr_status = CELIX_DO_IF(apr_status, apr_thread_cond_create(&(*framework)->dispatcher, (*framework)->mp));
+        if (apr_status == APR_SUCCESS) {
+            (*framework)->bundle = NULL;
+            (*framework)->installedBundleMap = NULL;
+            (*framework)->registry = NULL;
+            (*framework)->interrupted = false;
+            (*framework)->shutdown = false;
+            (*framework)->globalLockWaitersList = NULL;
+            (*framework)->globalLockCount = 0;
+            (*framework)->globalLockThread = 0;
+            (*framework)->nextBundleId = 1l;
+            (*framework)->cache = NULL;
+            (*framework)->installRequestMap = hashMap_create(utils_stringHash, utils_stringHash, utils_stringEquals, utils_stringEquals);
+            (*framework)->serviceListeners = NULL;
+            (*framework)->bundleListeners = NULL;
+            (*framework)->frameworkListeners = NULL;
+            (*framework)->requests = NULL;
+            (*framework)->shutdownGate = NULL;
+            (*framework)->configurationMap = config;
+
+            status = CELIX_DO_IF(status, bundle_create(&(*framework)->bundle, (*framework)->mp));
+            status = CELIX_DO_IF(status, arrayList_create((*framework)->mp, &(*framework)->globalLockWaitersList));
+            status = CELIX_DO_IF(status, bundle_setFramework((*framework)->bundle, (*framework)));
+            if (status == CELIX_SUCCESS) {
+                //
+            } else {
+                status = CELIX_FRAMEWORK_EXCEPTION;
+                fw_logCode(FW_LOG_ERROR, status, "Could not create framework");
             }
+        } else {
+            status = CELIX_FRAMEWORK_EXCEPTION;
+            fw_logCode(FW_LOG_ERROR, status, "Could not create framework");
         }
-	}
+    } else {
+        status = CELIX_FRAMEWORK_EXCEPTION;
+        fw_logCode(FW_LOG_ERROR, CELIX_ENOMEM, "Could not create framework");
+    }
 
-	return status;
+    return status;
 }
 
 celix_status_t framework_destroy(framework_pt framework) {
@@ -332,21 +296,31 @@ celix_status_t framework_destroy(framewo
 	return status;
 }
 
-typedef void *(*create_function_pt)(bundle_context_pt context, void **userData);
-typedef void (*start_function_pt)(void * handle, bundle_context_pt context);
-typedef void (*stop_function_pt)(void * handle, bundle_context_pt context);
-typedef void (*destroy_function_pt)(void * handle, bundle_context_pt context);
-
 #ifdef _WIN32
-HMODULE fw_getCurrentModule() {
-	HMODULE hModule = NULL;
-	GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)fw_getCurrentModule, &hModule);
-	return hModule;
-}
+    #define handle_t HMODULE
+    #define fw_getSystemLibrary() fw_getCurrentModule()
+    #define fw_openLibrary(path) LoadLibrary(path)
+    #define fw_closeLibrary(handle) FreeLibrary(handle)
+
+    #define fw_getSymbol(handle, name) GetProcAddress(handle, name)
+
+    #define fw_getLastError() GetLastError()
+
+    HMODULE fw_getCurrentModule() {
+        HMODULE hModule = NULL;
+        GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)fw_getCurrentModule, &hModule);
+        return hModule;
+    }
+#else
+    #define handle_t void *
+    #define fw_getSystemLibrary() dlopen(NULL, RTLD_LAZY|RTLD_LOCAL)
+    #define fw_openLibrary(path) dlopen(path, RTLD_LAZY|RTLD_LOCAL)
+    #define fw_closeLibrary(handle) dlclose(handle)
+    #define fw_getSymbol(handle, name) dlsym(handle, name)
+    #define fw_getLastError() dlerror()
 #endif
 
 celix_status_t fw_init(framework_pt framework) {
-	celix_status_t status = framework_acquireBundleLock(framework, framework->bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE);
 	bundle_state_e state;
 	char *location;
 	module_pt module = NULL;
@@ -355,203 +329,171 @@ celix_status_t fw_init(framework_pt fram
 	bundle_archive_pt archive = NULL;
 	char uuid[APR_UUID_FORMATTED_LENGTH+1];
 
-	/*create and store framework uuid*/
-	apr_uuid_t aprUuid;
-	apr_uuid_get(&aprUuid);
-	apr_uuid_format(uuid, &aprUuid);
-	setenv(FRAMEWORK_UUID, uuid, true);
-
-
-	if (status != CELIX_SUCCESS) {
-		fw_log(NULL, FW_LOG_ERROR,  "Could not lock framework bundle");
-		framework_releaseBundleLock(framework, framework->bundle);
-		return status;
-	}
-
-	arrayList_create(framework->mp, &framework->serviceListeners);
-	arrayList_create(framework->mp, &framework->bundleListeners);
-	arrayList_create(framework->mp, &framework->frameworkListeners);
-	arrayList_create(framework->mp, &framework->requests);
-
-	if (apr_thread_create(&framework->dispatcherThread, NULL, fw_eventDispatcher, framework, framework->mp) != APR_SUCCESS) {
-		fw_log(NULL, FW_LOG_ERROR,  "Could not create framework dispatcher thread");
-		return CELIX_FRAMEWORK_EXCEPTION;
-	}
-
-	bundle_getState(framework->bundle, &state);
-	if ((state == BUNDLE_INSTALLED) || (state == BUNDLE_RESOLVED)) {
-	    properties_pt props = properties_create();
-		properties_set(props, (char *) FRAMEWORK_STORAGE, ".cache");
-		status = bundleCache_create(props, framework->mp, &framework->cache);
-		if (status == CELIX_SUCCESS) {
-			bundle_state_e state;
-			bundle_getState(framework->bundle, &state);
-            if (state == BUNDLE_INSTALLED) {
-                // clean cache
-                // bundleCache_delete(framework->cache);
+	celix_status_t status = CELIX_SUCCESS;
+	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, framework->bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE));
+	status = CELIX_DO_IF(status, arrayList_create(framework->mp, &framework->serviceListeners));
+	status = CELIX_DO_IF(status, arrayList_create(framework->mp, &framework->bundleListeners));
+	status = CELIX_DO_IF(status, arrayList_create(framework->mp, &framework->frameworkListeners));
+	status = CELIX_DO_IF(status, arrayList_create(framework->mp, &framework->requests));
+	status = CELIX_DO_IF(status, apr_thread_create(&framework->dispatcherThread, NULL, fw_eventDispatcher, framework, framework->mp));
+	status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
+	if (status == CELIX_SUCCESS) {
+	    if ((state == BUNDLE_INSTALLED) || (state == BUNDLE_RESOLVED)) {
+	        bundle_state_e state;
+	        properties_pt props = properties_create();
+	        properties_set(props, (char *) FRAMEWORK_STORAGE, ".cache");
+
+	        status = CELIX_DO_IF(status, bundleCache_create(props, framework->mp, &framework->cache));
+	        status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
+	        if (status == CELIX_SUCCESS) {
+	            if (state == BUNDLE_INSTALLED) {
+	                // clean cache
+	                // bundleCache_delete(framework->cache);
+	            }
             }
-		} else {
-			fw_log(NULL, FW_LOG_ERROR,  "Could not create bundle cache");
-		    return status;
-		}
+        }
 	}
 
-	framework->installedBundleMap = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
-
-	status = bundle_getArchive(framework->bundle, &archive);
-	status = bundleArchive_getLocation(archive, &location);
-	hashMap_put(framework->installedBundleMap, location, framework->bundle);
-
-	status = bundle_getCurrentModule(framework->bundle, &module);
-	if (status != CELIX_SUCCESS) {
-		fw_log(NULL, FW_LOG_ERROR,  "Could not get framework module");
-		return status;
-	}
-	wires = resolver_resolve(module);
-	if (wires == NULL) {
-		fw_log(NULL, FW_LOG_ERROR,  "Unresolved constraints in System Bundle");
-		framework_releaseBundleLock(framework, framework->bundle);
-		return CELIX_BUNDLE_EXCEPTION;
-	} else {
-		framework_markResolvedModules(framework, wires);
-		hashMap_destroy(wires, false, false);
+	if (status == CELIX_SUCCESS) {
+        /*create and store framework uuid*/
+        apr_uuid_t aprUuid;
+        apr_uuid_get(&aprUuid);
+        apr_uuid_format(uuid, &aprUuid);
+        setenv(FRAMEWORK_UUID, uuid, true);
+
+        framework->installedBundleMap = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
 	}
 
-	// reload archives from cache
-	status = bundleCache_getArchives(framework->cache, framework->mp, &archives);
-	if (status != CELIX_SUCCESS) {
-		fw_log(NULL, FW_LOG_ERROR,  "Could not get framework archive");
-	    return status;
-	} else {
-#ifdef _WIN32
-		HMODULE handle = NULL;
-#else
-		void * handle = NULL;
-#endif
-        unsigned int arcIdx;
-		bundle_context_pt context = NULL;
-		activator_pt activator;
+    status = CELIX_DO_IF(status, bundle_getArchive(framework->bundle, &archive));
+    status = CELIX_DO_IF(status, bundleArchive_getLocation(archive, &location));
+    if (status == CELIX_SUCCESS) {
+        hashMap_put(framework->installedBundleMap, location, framework->bundle);
+    }
+    status = CELIX_DO_IF(status, bundle_getCurrentModule(framework->bundle, &module));
+    if (status == CELIX_SUCCESS) {
+        wires = resolver_resolve(module);
+        if (wires != NULL) {
+            framework_markResolvedModules(framework, wires);
+            hashMap_destroy(wires, false, false);
+        } else {
+            status = CELIX_BUNDLE_EXCEPTION;
+            fw_logCode(FW_LOG_ERROR, status, "Unresolved constraints in System Bundle");
+        }
+    }
 
+    status = CELIX_DO_IF(status, bundleCache_getArchives(framework->cache, framework->mp, &archives));
+    if (status == CELIX_SUCCESS) {
+        unsigned int arcIdx;
         for (arcIdx = 0; arcIdx < arrayList_size(archives); arcIdx++) {
             bundle_archive_pt archive1 = (bundle_archive_pt) arrayList_get(archives, arcIdx);
             long id;
-			bundle_state_e bundleState;
+            bundle_state_e bundleState;
             bundleArchive_getId(archive1, &id);
-    		framework->nextBundleId = framework->nextBundleId > id + 1 ? framework->nextBundleId : id + 1;
-    		
-    		bundleArchive_getPersistentState(archive1, &bundleState);
+            framework->nextBundleId = framework->nextBundleId > id + 1 ? framework->nextBundleId : id + 1;
+
+            bundleArchive_getPersistentState(archive1, &bundleState);
             if (bundleState == BUNDLE_UNINSTALLED) {
                 bundleArchive_closeAndDelete(archive1);
             } else {
                 bundle_pt bundle = NULL;
                 char *location1 = NULL;
-				status = bundleArchive_getLocation(archive1, &location1);
+                status = bundleArchive_getLocation(archive1, &location1);
                 fw_installBundle2(framework, &bundle, id, location1, NULL, archive1);
             }
         }
         arrayList_destroy(archives);
-        framework->registry = NULL;
-		serviceRegistry_create(framework->mp, framework, fw_serviceChanged, &framework->registry);
-
-        framework_setBundleStateAndNotify(framework, framework->bundle, BUNDLE_STARTING);
-
-        apr_thread_cond_create(&framework->shutdownGate, framework->mp);
+    }
 
-#ifdef _WIN32
-        handle = fw_getCurrentModule();
-#else
-        handle = dlopen(NULL, RTLD_LAZY|RTLD_LOCAL);
-#endif
-        if (handle == NULL) {
-#ifdef _WIN32
-			printf ("%s\n", GetLastError());
-#else
-            printf ("%s\n", dlerror());
-#endif
-            fw_log(NULL, FW_LOG_ERROR,  "Could not get handle to framework library");
-            framework_releaseBundleLock(framework, framework->bundle);
-            return CELIX_START_ERROR;
-        }
-        
-        if (bundleContext_create(framework, framework->bundle, &context) != CELIX_SUCCESS) {
-        	fw_log(NULL, FW_LOG_ERROR,  "Could not create framework bundle context");
-            return CELIX_START_ERROR;
+    status = CELIX_DO_IF(status, serviceRegistry_create(framework->mp, framework, fw_serviceChanged, &framework->registry));
+    status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, framework->bundle, BUNDLE_STARTING));
+    status = CELIX_DO_IF(status, apr_thread_cond_create(&framework->shutdownGate, framework->mp));
+    if (status == CELIX_SUCCESS) {
+        handle_t handle = NULL;
+        handle = fw_getSystemLibrary();
+        if (handle != NULL) {
+            bundle_setHandle(framework->bundle, handle);
+        } else {
+            status = CELIX_FRAMEWORK_EXCEPTION;
+            fw_logCode(FW_LOG_ERROR,  status, "Could not get handle to framework library");
         }
-        bundle_setContext(framework->bundle, context);
-
-        bundle_setHandle(framework->bundle, handle);
+    }
 
+    bundle_context_pt context = NULL;
+    status = CELIX_DO_IF(status, bundleContext_create(framework, framework->bundle, &context));
+    status = CELIX_DO_IF(status, bundle_setContext(framework->bundle, context));
+    if (status == CELIX_SUCCESS) {
+        activator_pt activator;
         activator = (activator_pt) apr_palloc(framework->mp, (sizeof(*activator)));
-        if (activator == NULL) {
-            status = CELIX_ENOMEM;
-        	fw_log(NULL, FW_LOG_ERROR,  "Could not allocate memory");
-        }  else {
-			bundle_context_pt context = NULL;
-			void * userData = NULL;
-#ifdef _WIN32
-			create_function_pt create = (create_function_pt) GetProcAddress((HMODULE) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_CREATE);
-			start_function_pt start = (start_function_pt) GetProcAddress((HMODULE) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_START);
-			stop_function_pt stop = (stop_function_pt) GetProcAddress((HMODULE) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_STOP);
-			destroy_function_pt destroy = (destroy_function_pt) GetProcAddress((HMODULE) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_DESTROY);
-#else
-            create_function_pt create = dlsym(bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_CREATE);
-            start_function_pt start = dlsym(bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_START);
-            stop_function_pt stop = dlsym(bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_STOP);
-            destroy_function_pt destroy = dlsym(bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_DESTROY);
-#endif
+        if (activator != NULL) {
+            bundle_context_pt context = NULL;
+            void * userData = NULL;
+
+            create_function_pt create = (create_function_pt) fw_getSymbol((handle_t) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_CREATE);
+            start_function_pt start = (start_function_pt) fw_getSymbol((handle_t) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_START);
+            stop_function_pt stop = (stop_function_pt) fw_getSymbol((handle_t) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_STOP);
+            destroy_function_pt destroy = (destroy_function_pt) fw_getSymbol((handle_t) bundle_getHandle(framework->bundle), BUNDLE_ACTIVATOR_DESTROY);
+
             activator->start = start;
             activator->stop = stop;
             activator->destroy = destroy;
-            bundle_setActivator(framework->bundle, activator);
-            
-			bundle_getContext(framework->bundle, &context);
-            
-			if (create != NULL) {
-                create(context, &userData);
-            }
-            activator->userData = userData;
+            status = CELIX_DO_IF(status, bundle_setActivator(framework->bundle, activator));
+            status = CELIX_DO_IF(status, bundle_getContext(framework->bundle, &context));
 
-            if (start != NULL) {
-                start(userData, context);
+            if (status == CELIX_SUCCESS) {
+                if (create != NULL) {
+                    create(context, &userData);
+                }
+                activator->userData = userData;
+
+                if (start != NULL) {
+                    start(userData, context);
+                }
             }
+        } else {
+            status = CELIX_ENOMEM;
+        }
+    }
 
-            framework_releaseBundleLock(framework, framework->bundle);
+    if (status != CELIX_SUCCESS) {
+       fw_logCode(FW_LOG_ERROR, status, "Could not init framework");
+    }
+
+    framework_releaseBundleLock(framework, framework->bundle);
 
-            status = CELIX_SUCCESS;
-        }
-	}
 	return status;
 }
 
 celix_status_t framework_start(framework_pt framework) {
-	celix_status_t lock = framework_acquireBundleLock(framework, framework->bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE);
+	celix_status_t status = CELIX_SUCCESS;
 	bundle_state_e state;
 
-	if (lock != CELIX_SUCCESS) {
-		fw_log(NULL, FW_LOG_ERROR,  "Could not lock framework bundle");
-//		fw_fireFrameworkEvent(framework, FRAMEWORK_EVENT_ERROR, framework->bundle, lock, "Could not lock framework bundle");
-		framework_releaseBundleLock(framework, framework->bundle);
-		return lock;
+	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, framework->bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE));
+	status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
+	if (status == CELIX_SUCCESS) {
+	    if ((state == BUNDLE_INSTALLED) || (state == BUNDLE_RESOLVED)) {
+	        status = CELIX_DO_IF(status, fw_init(framework));
+        }
 	}
 
+	status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
+	if (status == CELIX_SUCCESS) {
+	    if (state == BUNDLE_STARTING) {
+	        status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, framework->bundle, BUNDLE_ACTIVE));
+	    }
 
-	bundle_getState(framework->bundle, &state);
-
-	if ((state == BUNDLE_INSTALLED) || (state == BUNDLE_RESOLVED)) {
-		fw_init(framework);
-	}
-
-	bundle_getState(framework->bundle, &state);
-	if (state == BUNDLE_STARTING) {
-		framework_setBundleStateAndNotify(framework, framework->bundle, BUNDLE_ACTIVE);
+	    framework_releaseBundleLock(framework, framework->bundle);
 	}
 
-	framework_releaseBundleLock(framework, framework->bundle);
+	status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_STARTED, framework->bundle));
+	status = CELIX_DO_IF(status, fw_fireFrameworkEvent(framework, FRAMEWORK_EVENT_STARTED, framework->bundle, 0));
 
-	fw_fireBundleEvent(framework, BUNDLE_EVENT_STARTED, framework->bundle);
-	fw_fireFrameworkEvent(framework, FRAMEWORK_EVENT_STARTED, framework->bundle, 0, NULL);
+	if (status != CELIX_SUCCESS) {
+       status = CELIX_BUNDLE_EXCEPTION;
+       fw_logCode(FW_LOG_ERROR, status, "Could not start framework");
+       fw_fireFrameworkEvent(framework, FRAMEWORK_EVENT_ERROR, framework->bundle, status);
+    }
 
-	return CELIX_SUCCESS;
+	return status;
 }
 
 void framework_stop(framework_pt framework) {
@@ -562,10 +504,8 @@ celix_status_t fw_getProperty(framework_
 	celix_status_t status = CELIX_SUCCESS;
 
 	if (framework == NULL || name == NULL || *value != NULL) {
-		char errMsg[256];
 		status = CELIX_ILLEGAL_ARGUMENT;
-		celix_strerror(status, errMsg, 256);
-		fw_log(NULL, FW_LOG_ERROR,  errMsg);
+		fw_logCode(FW_LOG_ERROR, status, "Missing required arguments");
 	} else {
 		if (framework->configurationMap != NULL) {
 			*value = properties_get(framework->configurationMap, (char *) name);
@@ -583,68 +523,64 @@ celix_status_t fw_installBundle(framewor
 }
 
 celix_status_t fw_installBundle2(framework_pt framework, bundle_pt * bundle, long id, char * location, char *inputFile, bundle_archive_pt archive) {
-    bundle_archive_pt bundle_archive = NULL;
+    celix_status_t status = CELIX_SUCCESS;
+//    bundle_archive_pt bundle_archive = NULL;
     bundle_state_e state;
 	apr_pool_t *bundlePool;
   	bool locked;
 
-	celix_status_t status = framework_acquireInstallLock(framework, location);
-    if (status != CELIX_SUCCESS) {
-    	fw_log(NULL, FW_LOG_ERROR,  "Could not acquire install lock");
-        return status;
-    }
-
-    bundle_getState(framework->bundle, &state);
-
-	if (state == BUNDLE_STOPPING || state == BUNDLE_UNINSTALLED) {
-		fw_log(NULL, FW_LOG_INFO,  "The framework is being shutdown");
-		framework_releaseInstallLock(framework, location);
-		return CELIX_FRAMEWORK_SHUTDOWN;
-	}
+  	status = CELIX_DO_IF(status, framework_acquireInstallLock(framework, location));
+  	status = CELIX_DO_IF(status, bundle_getState(framework->bundle, &state));
+  	if (status == CELIX_SUCCESS) {
+        if (state == BUNDLE_STOPPING || state == BUNDLE_UNINSTALLED) {
+            fw_log(FW_LOG_INFO,  "The framework is being shutdown");
+            status = CELIX_DO_IF(status, framework_releaseInstallLock(framework, location));
+            status = CELIX_FRAMEWORK_SHUTDOWN;
+        }
+  	}
 
-	*bundle = framework_getBundle(framework, location);
-	if (*bundle != NULL) {
-		framework_releaseInstallLock(framework, location);
-		return CELIX_SUCCESS;
-	}
+  	if (status == CELIX_SUCCESS) {
+        *bundle = framework_getBundle(framework, location);
+        if (*bundle != NULL) {
+            framework_releaseInstallLock(framework, location);
+            return CELIX_SUCCESS;
+        }
 
-	apr_pool_create(&bundlePool, framework->mp);
-	if (archive == NULL) {
-		id = framework_getNextBundleId(framework);
-		status = bundleCache_createArchive(framework->cache, bundlePool, id, location, inputFile, &bundle_archive);
-		if (status != CELIX_SUCCESS) {
-		    framework_releaseInstallLock(framework, location);
-		    fw_log(NULL, FW_LOG_ERROR,  "Could not create bundle archive");
-		    return status;
-		} else {
-		    archive = bundle_archive;
-		}
-	} else {
-		// purge revision
-		// multiple revisions not yet implemented
-	}
+        apr_pool_create(&bundlePool, framework->mp);
+        if (archive == NULL) {
+            id = framework_getNextBundleId(framework);
+            status = CELIX_DO_IF(status, bundleCache_createArchive(framework->cache, bundlePool, id, location, inputFile, &archive));
+        } else {
+            // purge revision
+            // multiple revisions not yet implemented
+        }
 
-	locked = framework_acquireGlobalLock(framework);
-	if (!locked) {
-		status = CELIX_BUNDLE_EXCEPTION;
-		fw_log(NULL, FW_LOG_ERROR,  "Unable to acquire the global lock to install the bundle");
-	} else {
-	    status = bundle_createFromArchive(bundle, framework, archive, bundlePool);
-	}
+        if (status == CELIX_SUCCESS) {
+            locked = framework_acquireGlobalLock(framework);
+            if (locked) {
+                status = CELIX_DO_IF(status, bundle_createFromArchive(bundle, framework, archive, bundlePool));
+            } else {
+                status = CELIX_BUNDLE_EXCEPTION;
+            }
 
-    framework_releaseGlobalLock(framework);
-	if (status == CELIX_SUCCESS) {
-        hashMap_put(framework->installedBundleMap, location, *bundle);
-	} else {
-		char msg[256];
-		sprintf(msg, "Unable to install bundle: %s", location);
-	    fw_log(NULL, FW_LOG_ERROR,  msg);
-	    bundleArchive_closeAndDelete(bundle_archive);
-	    apr_pool_destroy(bundlePool);
-	}
-    framework_releaseInstallLock(framework, location);
+            status = CELIX_DO_IF(status, framework_releaseGlobalLock(framework));
+            if (status == CELIX_SUCCESS) {
+                hashMap_put(framework->installedBundleMap, location, *bundle);
+            } else {
+                status = CELIX_BUNDLE_EXCEPTION;
+                status = CELIX_DO_IF(status, bundleArchive_closeAndDelete(archive));
+                apr_pool_destroy(bundlePool);
+            }
+            status = CELIX_DO_IF(status, framework_releaseInstallLock(framework, location));
+        }
+  	}
 
-    fw_fireBundleEvent(framework, BUNDLE_EVENT_INSTALLED, *bundle);
+    if (status != CELIX_SUCCESS) {
+        status = CELIX_DO_IF(status, framework_releaseInstallLock(framework, location));
+    	fw_logCode(FW_LOG_ERROR, status, "Could not install bundle");
+    } else {
+        status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_INSTALLED, *bundle));
+    }
 
   	return status;
 }
@@ -654,41 +590,35 @@ celix_status_t framework_getBundleEntry(
 
 	bundle_revision_pt revision;
 	bundle_archive_pt archive = NULL;
+    char *root;
 
-	status = bundle_getArchive(bundle, &archive);
-	if (status == CELIX_SUCCESS) {
-		status = bundleArchive_getCurrentRevision(archive, &revision);
-		if (status == CELIX_SUCCESS) {
-			char *root;
-			if ((strlen(name) > 0) && (name[0] == '/')) {
-				name++;
-			}
-
-
-			status = bundleRevision_getRoot(revision, &root);
-			if (status == CELIX_SUCCESS) {
-				char *e = NULL;
-				apr_status_t ret;
-				apr_finfo_t info;
-				apr_filepath_merge(&e, root, name, APR_FILEPATH_NOTABOVEROOT, framework->mp);
-				ret = apr_stat(&info, e, APR_FINFO_DIRENT|APR_FINFO_TYPE, framework->mp);
-				if (ret == APR_ENOENT) {
-					(*entry) = NULL;
-				} else if (ret == APR_SUCCESS || ret == APR_INCOMPLETE) {
-					(*entry) = apr_pstrdup(pool, e);
-				}
-			}
-		}
-	}
+	status = CELIX_DO_IF(status, bundle_getArchive(bundle, &archive));
+    status = CELIX_DO_IF(status, bundleArchive_getCurrentRevision(archive, &revision));
+    status = CELIX_DO_IF(status, bundleRevision_getRoot(revision, &root));
+    if (status == CELIX_SUCCESS) {
+        char *e = NULL;
+        apr_status_t ret;
+        apr_finfo_t info;
+        if ((strlen(name) > 0) && (name[0] == '/')) {
+            name++;
+        }
+        apr_filepath_merge(&e, root, name, APR_FILEPATH_NOTABOVEROOT, framework->mp);
+        ret = apr_stat(&info, e, APR_FINFO_DIRENT|APR_FINFO_TYPE, framework->mp);
+        if (ret == APR_ENOENT) {
+            (*entry) = NULL;
+        } else if (ret == APR_SUCCESS || ret == APR_INCOMPLETE) {
+            (*entry) = apr_pstrdup(pool, e);
+        }
+    }
 
 	return status;
 }
 
 celix_status_t fw_startBundle(framework_pt framework, bundle_pt bundle, int options) {
-	celix_status_t lock = framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE);
+	celix_status_t status = CELIX_SUCCESS;
 
 	hash_map_pt wires = NULL;
-	void * handle;
+	handle_t handle;
 	bundle_context_pt context = NULL;
 	bundle_state_e state;
 	module_pt module = NULL;
@@ -712,342 +642,368 @@ celix_status_t fw_startBundle(framework_
 	activator_pt activator;
 	bundle_archive_pt archive = NULL;
 	apr_pool_t *bundlePool = NULL;
+	char *error = NULL;
 
-	if (lock != CELIX_SUCCESS) {
-		printf("Unable to start\n");
-		framework_releaseBundleLock(framework, bundle);
-		return lock;
-	}
 
-	bundle_getState(bundle, &state);
+	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE));
+	status = CELIX_DO_IF(status, bundle_getState(bundle, &state));
 
-	switch (state) {
-		case BUNDLE_UNKNOWN:
-			printf("Cannot start bundle since its state is unknown.\n");
-			framework_releaseBundleLock(framework, bundle);
-			return CELIX_ILLEGAL_STATE;
-		case BUNDLE_UNINSTALLED:
-			printf("Cannot start bundle since it is uninstalled.\n");
-			framework_releaseBundleLock(framework, bundle);
-			return CELIX_ILLEGAL_STATE;
-		case BUNDLE_STARTING:
-			printf("Cannot start bundle since it is starting.\n");
-			framework_releaseBundleLock(framework, bundle);
-			return CELIX_BUNDLE_EXCEPTION;
-		case BUNDLE_STOPPING:
-			printf("Cannot start bundle since it is stopping.\n");
-			framework_releaseBundleLock(framework, bundle);
-			return CELIX_ILLEGAL_STATE;
-		case BUNDLE_ACTIVE:
-			printf("Cannot start bundle since it is already active.\n");
-			framework_releaseBundleLock(framework, bundle);
-			return CELIX_SUCCESS;
-		case BUNDLE_INSTALLED:
-			bundle_getCurrentModule(bundle, &module);
-			if (!module_isResolved(module)) {
-                wires = resolver_resolve(module);
-                if (wires == NULL) {
-                    framework_releaseBundleLock(framework, bundle);
-                    return CELIX_BUNDLE_EXCEPTION;
+	if (status == CELIX_SUCCESS) {
+	    switch (state) {
+            case BUNDLE_UNKNOWN:
+                error = "state is unknown";
+                status = CELIX_ILLEGAL_STATE;
+                break;
+            case BUNDLE_UNINSTALLED:
+                error = "bundle is uninstalled";
+                status = CELIX_ILLEGAL_STATE;
+                break;
+            case BUNDLE_STARTING:
+                error = "bundle is starting";
+                status = CELIX_BUNDLE_EXCEPTION;
+                break;
+            case BUNDLE_STOPPING:
+                error = "bundle is stopping";
+                status = CELIX_BUNDLE_EXCEPTION;
+                break;
+            case BUNDLE_ACTIVE:
+                break;
+            case BUNDLE_INSTALLED:
+                bundle_getCurrentModule(bundle, &module);
+                if (!module_isResolved(module)) {
+                    wires = resolver_resolve(module);
+                    if (wires == NULL) {
+                        framework_releaseBundleLock(framework, bundle);
+                        return CELIX_BUNDLE_EXCEPTION;
+                    }
+                    framework_markResolvedModules(framework, wires);
+                }
+                if (wires != NULL) {
+                    hashMap_destroy(wires, false, false);
+                }
+                /* no break */
+            case BUNDLE_RESOLVED:
+                status = CELIX_DO_IF(status, bundleContext_create(framework, bundle, &context));
+                status = CELIX_DO_IF(status, bundle_setContext(bundle, context));
+
+                status = CELIX_DO_IF(status, bundle_getArchive(bundle, &archive));
+                status = CELIX_DO_IF(status, bundle_getMemoryPool(bundle, &bundlePool));
+                status = CELIX_DO_IF(status, getManifest(archive, bundlePool, &manifest));
+                status = CELIX_DO_IF(status, bundle_setManifest(bundle, manifest));
+                if (status == CELIX_SUCCESS) {
+                    library = manifest_getValue(manifest, HEADER_LIBRARY);
                 }
-                framework_markResolvedModules(framework, wires);
-		    }
-			if (wires != NULL) {
-				hashMap_destroy(wires, false, false);
-			}
-			/* no break */
-		case BUNDLE_RESOLVED:
-			if (bundleContext_create(framework, bundle, &context) != CELIX_SUCCESS) {
-				return CELIX_ENOMEM;
-			}
-			bundle_setContext(bundle, context);
-
-			bundle_getArchive(bundle, &archive);
-			bundle_getMemoryPool(bundle, &bundlePool);
-
-			if (getManifest(archive, bundlePool, &manifest) == CELIX_SUCCESS) {
-                bundle_setManifest(bundle, manifest);
-                library = manifest_getValue(manifest, HEADER_LIBRARY);
-			}
-
-			bundleArchive_getRefreshCount(archive, &refreshCount);
-			bundleArchive_getArchiveRoot(archive, &archiveRoot);
-			bundleArchive_getCurrentRevisionNumber(archive, &revisionNumber);
-
-			sprintf(libraryPath, "%s/version%ld.%ld/%s%s%s",
-					archiveRoot,
-					refreshCount,
-					revisionNumber,
-					library_prefix,
-					library,
-					library_extension
-					);
-
-			// BUG: Can't use apr_dso_load, apr assumes RTLD_GLOBAL for loading libraries.
-//			apr_dso_handle_t *handle;
-//			apr_dso_load(&handle, libraryPath, bundle->memoryPool);
-#ifdef _WIN32
-			handle = LoadLibrary(libraryPath);
-#else
-			handle = dlopen(libraryPath, RTLD_LAZY|RTLD_LOCAL);
-#endif
-			if (handle == NULL) {
-#ifdef _WIN32
-				printf ("%s\n", GetLastError());
-#else
-				printf ("%s\n", dlerror());
-#endif
-				framework_releaseBundleLock(framework, bundle);
-				return CELIX_BUNDLE_EXCEPTION;
-			}
-
-			bundle_setHandle(bundle, handle);
-
-			activator = (activator_pt) apr_palloc(bundlePool, (sizeof(*activator)));
-			if (activator == NULL) {
-			    return CELIX_ENOMEM;
-			} else {
-				void * userData = NULL;
-                bundle_context_pt context;
-#ifdef _WIN32
-                
-				create_function_pt create = (create_function_pt) GetProcAddress((HMODULE) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_CREATE);
-				start_function_pt start = (start_function_pt) GetProcAddress((HMODULE) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_START);
-				stop_function_pt stop = (stop_function_pt) GetProcAddress((HMODULE) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_STOP);
-				destroy_function_pt destroy = (destroy_function_pt) GetProcAddress((HMODULE) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_DESTROY);
-#else
-				create_function_pt create = dlsym(bundle_getHandle(bundle), BUNDLE_ACTIVATOR_CREATE);
-                start_function_pt start = dlsym(bundle_getHandle(bundle), BUNDLE_ACTIVATOR_START);
-                stop_function_pt stop = dlsym(bundle_getHandle(bundle), BUNDLE_ACTIVATOR_STOP);
-                destroy_function_pt destroy = dlsym(bundle_getHandle(bundle), BUNDLE_ACTIVATOR_DESTROY);
-#endif
-
-                activator->start = start;
-                activator->stop = stop;
-                activator->destroy = destroy;
-                bundle_setActivator(bundle, activator);
-
-                framework_setBundleStateAndNotify(framework, bundle, BUNDLE_STARTING);
 
-                fw_fireBundleEvent(framework, BUNDLE_EVENT_STARTING, bundle);
+                status = CELIX_DO_IF(status, bundleArchive_getRefreshCount(archive, &refreshCount));
+                status = CELIX_DO_IF(status, bundleArchive_getArchiveRoot(archive, &archiveRoot));
+                status = CELIX_DO_IF(status, bundleArchive_getCurrentRevisionNumber(archive, &revisionNumber));
+
+                if (status == CELIX_SUCCESS) {
+                    sprintf(libraryPath, "%s/version%ld.%ld/%s%s%s", archiveRoot, refreshCount, revisionNumber, library_prefix, library, library_extension);
+
+                    // BUG: Can't use apr_dso_load, apr assumes RTLD_GLOBAL for loading libraries.
+                    // apr_dso_handle_t *handle;
+                    // apr_dso_load(&handle, libraryPath, bundle->memoryPool);
+                    handle = fw_openLibrary(libraryPath);
+                    if (handle == NULL) {
+                        char err[256];
+                        sprintf(err, "library could not be opened: %s", fw_getLastError());
+                        error = err;
+                        status =  CELIX_BUNDLE_EXCEPTION;
+                    }
 
-                bundle_getContext(bundle, &context);
+                    if (status == CELIX_SUCCESS) {
+                        bundle_setHandle(bundle, handle);
 
-                if (create != NULL) {
-                    create(context, &userData);
-                }
-                activator->userData = userData;
+                        activator = (activator_pt) apr_palloc(bundlePool, (sizeof(*activator)));
+                        if (activator == NULL) {
+                            status = CELIX_ENOMEM;
+                        } else {
+                            void * userData = NULL;
+                            bundle_context_pt context;
+                            create_function_pt create = (create_function_pt) fw_getSymbol((handle_t) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_CREATE);
+                            start_function_pt start = (start_function_pt) fw_getSymbol((handle_t) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_START);
+                            stop_function_pt stop = (stop_function_pt) fw_getSymbol((handle_t) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_STOP);
+                            destroy_function_pt destroy = (destroy_function_pt) fw_getSymbol((handle_t) bundle_getHandle(bundle), BUNDLE_ACTIVATOR_DESTROY);
+
+                            activator->start = start;
+                            activator->stop = stop;
+                            activator->destroy = destroy;
+                            status = CELIX_DO_IF(status, bundle_setActivator(bundle, activator));
+
+                            status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, BUNDLE_STARTING));
+                            status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_STARTING, bundle));
+
+                            status = CELIX_DO_IF(status, bundle_getContext(bundle, &context));
+
+                            if (status == CELIX_SUCCESS) {
+                                if (create != NULL) {
+                                    status = CELIX_DO_IF(status, create(context, &userData));
+                                    if (status == CELIX_SUCCESS) {
+                                        activator->userData = userData;
+                                    }
+                                }
+                            }
+                            if (status == CELIX_SUCCESS) {
+                                if (start != NULL) {
+                                    status = CELIX_DO_IF(status, start(userData, context));
+                                }
+                            }
 
-                if (start != NULL) {
-                    start(userData, context);
+                            status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, BUNDLE_ACTIVE));
+                            status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_STARTED, bundle));
+                        }
+                    }
                 }
 
-                framework_setBundleStateAndNotify(framework, bundle, BUNDLE_ACTIVE);
+            break;
+        }
+	}
 
-                fw_fireBundleEvent(framework, BUNDLE_EVENT_STARTED, bundle);
-			}
+	framework_releaseBundleLock(framework, bundle);
 
-			break;
+	if (status != CELIX_SUCCESS) {
+	    module_pt module = NULL;
+	    char *symbolicName = NULL;
+	    long id = 0;
+	    bundle_getCurrentModule(bundle, &module);
+	    module_getSymbolicName(module, &symbolicName);
+	    bundle_getBundleId(bundle, &id);
+	    if (error != NULL) {
+	        fw_logCode(FW_LOG_ERROR, status, "Could not start bundle: %s [%ld]; cause: %s", symbolicName, id, error);
+	    } else {
+	        fw_logCode(FW_LOG_ERROR, status, "Could not start bundle: %s [%ld]", symbolicName, id);
+	    }
 	}
 
-	framework_releaseBundleLock(framework, bundle);
-	return CELIX_SUCCESS;
+	return status;
 }
 
 celix_status_t framework_updateBundle(framework_pt framework, bundle_pt bundle, char *inputFile) {
-	celix_status_t status;
-	celix_status_t lock = framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_ACTIVE);
+	celix_status_t status = CELIX_SUCCESS;
 	bundle_state_e oldState;
 	char *location;
-	bool locked;
 	bundle_archive_pt archive = NULL;
+	char *error = NULL;
 
-	if (lock != CELIX_SUCCESS) {
-		printf("Cannot update bundle, in wrong state\n");
-		framework_releaseBundleLock(framework, bundle);
-		return lock;
+	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_ACTIVE));
+	status = CELIX_DO_IF(status, bundle_getState(bundle, &oldState));
+	if (status == CELIX_SUCCESS) {
+        if (oldState == BUNDLE_ACTIVE) {
+            fw_stopBundle(framework, bundle, false);
+        }
 	}
-	
-	bundle_getState(bundle, &oldState);
-	bundle_getArchive(bundle, &archive);
-	
-	bundleArchive_getLocation(archive, &location);
+	status = CELIX_DO_IF(status, bundle_getArchive(bundle, &archive));
+	status = CELIX_DO_IF(status, bundleArchive_getLocation(archive, &location));
 
-	if (oldState == BUNDLE_ACTIVE) {
-		fw_stopBundle(framework, bundle, false);
+	if (status == CELIX_SUCCESS) {
+	    bool locked = framework_acquireGlobalLock(framework);
+	    if (!locked) {
+	        status = CELIX_BUNDLE_EXCEPTION;
+	        error = "Unable to acquire the global lock to update the bundle";
+	    }
 	}
+	
+	status = CELIX_DO_IF(status, bundle_revise(bundle, location, inputFile));
+	status = CELIX_DO_IF(status, framework_releaseGlobalLock(framework));
 
-	locked = framework_acquireGlobalLock(framework);
-	if (!locked) {
-		printf("Unable to acquire the global lock to update the bundle\n");
-		framework_releaseInstallLock(framework, location);
-		return CELIX_BUNDLE_EXCEPTION;
-	}
+	status = CELIX_DO_IF(status, bundleArchive_setLastModified(archive, time(NULL)));
+	status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, BUNDLE_INSTALLED));
 
-	bundle_revise(bundle, location, inputFile);
-	framework_releaseGlobalLock(framework);
+	status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_UNRESOLVED, bundle));
+	status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_UPDATED, bundle));
 
-	status = bundleArchive_setLastModified(archive, time(NULL));
-	framework_setBundleStateAndNotify(framework, bundle, BUNDLE_INSTALLED);
+    // Refresh packages?
 
-	fw_fireBundleEvent(framework, BUNDLE_EVENT_UNRESOLVED, bundle);
-	fw_fireBundleEvent(framework, BUNDLE_EVENT_UPDATED, bundle);
+	if (status == CELIX_SUCCESS) {
+	    if (oldState == BUNDLE_ACTIVE) {
+	        status = CELIX_DO_IF(status, fw_startBundle(framework, bundle, 1));
+	    }
+	}
 
-	// Refresh packages?
+	framework_releaseBundleLock(framework, bundle);
 
-	if (oldState == BUNDLE_ACTIVE) {
-		fw_startBundle(framework, bundle, 1);
+	if (status != CELIX_SUCCESS) {
+	    module_pt module = NULL;
+        char *symbolicName = NULL;
+        long id = 0;
+        bundle_getCurrentModule(bundle, &module);
+        module_getSymbolicName(module, &symbolicName);
+        bundle_getBundleId(bundle, &id);
+        if (error != NULL) {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot update bundle: %s [%ld]; cause: %s", symbolicName, id, error);
+        } else {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot update bundle: %s [%ld]", symbolicName, id);
+        }
 	}
-	framework_releaseBundleLock(framework, bundle);
 
-	return CELIX_SUCCESS;
+	return status;
 }
 
 celix_status_t fw_stopBundle(framework_pt framework, bundle_pt bundle, bool record) {
 	celix_status_t status = CELIX_SUCCESS;
+	bundle_state_e state;
+    activator_pt activator;
+    bundle_context_pt context;
+    module_pt module = NULL;
+    manifest_pt manifest = NULL;
+    bool wasActive = false;
+    long id = 0;
+    char *error = NULL;
 
-	status = framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE);
-	if (status != CELIX_SUCCESS) {
-		printf("Cannot stop bundle\n");
-		framework_releaseBundleLock(framework, bundle);
-	} else {
-		bundle_state_e state;
-		activator_pt activator;
-		bundle_context_pt context;
-		module_pt module = NULL;
-		manifest_pt manifest = NULL;
-		if (record) {
-			bundle_setPersistentStateInactive(bundle);
-		}
-
-		//if (!fw_isBundlePersistentlyStarted(framework, bundle)) {
-		//}
-		
-		bundle_getState(bundle, &state);
-
-		switch (state) {
-			case BUNDLE_UNKNOWN:
-				printf("Cannot stop bundle since its state is unknown.\n");
-				framework_releaseBundleLock(framework, bundle);
-				return CELIX_ILLEGAL_STATE;
-			case BUNDLE_UNINSTALLED:
-				printf("Cannot stop bundle since it is uninstalled.\n");
-				framework_releaseBundleLock(framework, bundle);
-				return status;
-			case BUNDLE_STARTING:
-				printf("Cannot stop bundle since it is starting.\n");
-				framework_releaseBundleLock(framework, bundle);
-				return status;
-			case BUNDLE_STOPPING:
-				printf("Cannot stop bundle since it is stopping.\n");
-				framework_releaseBundleLock(framework, bundle);
-				return status;
-			case BUNDLE_INSTALLED:
-			case BUNDLE_RESOLVED:
-				framework_releaseBundleLock(framework, bundle);
-				return status;
-			case BUNDLE_ACTIVE:
-				// only valid state
-				break;
-		}
+	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE));
 
-		framework_setBundleStateAndNotify(framework, bundle, BUNDLE_STOPPING);
-		fw_fireBundleEvent(framework, BUNDLE_EVENT_STOPPING, bundle);
+	if (record) {
+	    status = CELIX_DO_IF(status, bundle_setPersistentStateInactive(bundle));
+    }
 
-		activator = bundle_getActivator(bundle);
-		
-		bundle_getContext(bundle, &context);
-		if (activator->stop != NULL) {
-			activator->stop(activator->userData, context);
-		}
+	status = CELIX_DO_IF(status, bundle_getState(bundle, &state));
+	if (status == CELIX_SUCCESS) {
+	    switch (state) {
+            case BUNDLE_UNKNOWN:
+                status = CELIX_ILLEGAL_STATE;
+                error = "state is unknown";
+                break;
+            case BUNDLE_UNINSTALLED:
+                status = CELIX_ILLEGAL_STATE;
+                error = "bundle is uninstalled";
+                break;
+            case BUNDLE_STARTING:
+                status = CELIX_BUNDLE_EXCEPTION;
+                error = "bundle is starting";
+                break;
+            case BUNDLE_STOPPING:
+                status = CELIX_BUNDLE_EXCEPTION;
+                error = "bundle is stopping";
+                break;
+            case BUNDLE_INSTALLED:
+            case BUNDLE_RESOLVED:
+                break;
+            case BUNDLE_ACTIVE:
+                wasActive = true;
+                break;
+        }
+	}
 
-		if (activator->destroy != NULL) {
-			activator->destroy(activator->userData, context);
-		}
 
-		bundle_getCurrentModule(bundle, &module);
-		if (strcmp(module_getId(module), "0") != 0) {
-			activator->start = NULL;
-			activator->stop = NULL;
-			activator->userData = NULL;
-			//free(activator);
-			bundle_setActivator(bundle, NULL);
+	status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, BUNDLE_STOPPING));
+	status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_STOPPING, bundle));
+    status = CELIX_DO_IF(status, bundle_getBundleId(bundle, &id));
+	if (status == CELIX_SUCCESS) {
+	    if (wasActive || (id == 0)) {
+	        activator = bundle_getActivator(bundle);
 
-			serviceRegistry_unregisterServices(framework->registry, bundle);
-			serviceRegistry_ungetServices(framework->registry, bundle);
+	        status = CELIX_DO_IF(status, bundle_getContext(bundle, &context));
+	        if (status == CELIX_SUCCESS) {
+                if (activator->stop != NULL) {
+                    status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
+                }
+	        }
+            if (status == CELIX_SUCCESS) {
+                if (activator->destroy != NULL) {
+                    status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
+                }
+	        }
+	    }
 
-//			dlclose(bundle_getHandle(bundle));
-		}
+	    status = CELIX_DO_IF(status, bundle_getCurrentModule(bundle, &module));
+	    if (status == CELIX_SUCCESS) {
+            if (strcmp(module_getId(module), "0") != 0) {
+                activator->start = NULL;
+                activator->stop = NULL;
+                activator->userData = NULL;
+                //free(activator);
+                status = CELIX_DO_IF(status, bundle_setActivator(bundle, NULL));
+
+                status = CELIX_DO_IF(status, serviceRegistry_unregisterServices(framework->registry, bundle));
+                if (status == CELIX_SUCCESS) {
+                    serviceRegistry_ungetServices(framework->registry, bundle);
+                }
+                // #TODO remove listeners for bundle
 
-		bundleContext_destroy(context);
-		bundle_setContext(bundle, NULL);
+                // #TODO enable dlclose call
+//              dlclose(bundle_getHandle(bundle));
 
-		bundle_getManifest(bundle, &manifest);
+                status = CELIX_DO_IF(status, bundleContext_destroy(context));
+                status = CELIX_DO_IF(status, bundle_setContext(bundle, NULL));
 
-		framework_setBundleStateAndNotify(framework, bundle, BUNDLE_RESOLVED);
+                status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, BUNDLE_RESOLVED));
+            }
+	    }
+	}
 
-		framework_releaseBundleLock(framework, bundle);
+	framework_releaseBundleLock(framework, bundle);
 
-		fw_fireBundleEvent(framework, BUNDLE_EVENT_STOPPED, bundle);
-	}
+	if (status != CELIX_SUCCESS) {
+	    module_pt module = NULL;
+        char *symbolicName = NULL;
+        long id = 0;
+        bundle_getCurrentModule(bundle, &module);
+        module_getSymbolicName(module, &symbolicName);
+        bundle_getBundleId(bundle, &id);
+        if (error != NULL) {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot stop bundle: %s [%ld]; cause: %s", symbolicName, id, error);
+        } else {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot stop bundle: %s [%ld]", symbolicName, id);
+        }
+ 	} else {
+        fw_fireBundleEvent(framework, BUNDLE_EVENT_STOPPED, bundle);
+ 	}
 
 	return status;
 }
 
 celix_status_t fw_uninstallBundle(framework_pt framework, bundle_pt bundle) {
     celix_status_t status = CELIX_SUCCESS;
-
     bundle_state_e state;
-
-    celix_status_t lock = framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE|BUNDLE_STOPPING);
-    if (lock != CELIX_SUCCESS) {
-        printf("Cannot stop bundle\n");
-        framework_releaseBundleLock(framework, bundle);
-        bundle_getState(bundle, &state);
-        if (state == BUNDLE_UNINSTALLED) {
-            status = CELIX_ILLEGAL_STATE;
-        } else {
-            status = CELIX_BUNDLE_EXCEPTION;
-        }
-    } else {
-		bool locked;
-        fw_stopBundle(framework, bundle, true);
-
+    bool locked;
+    bundle_archive_pt archive = NULL;
+    char * location;
+    bundle_pt target;
+    char *error = NULL;
+
+    status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, bundle, BUNDLE_INSTALLED|BUNDLE_RESOLVED|BUNDLE_STARTING|BUNDLE_ACTIVE|BUNDLE_STOPPING));
+    status = CELIX_DO_IF(status, fw_stopBundle(framework, bundle, true));
+    if (status == CELIX_SUCCESS) {
         locked = framework_acquireGlobalLock(framework);
         if (!locked) {
-            printf("Unable to acquire the global lock to install the bundle\n");
-            framework_releaseGlobalLock(framework);
             status = CELIX_ILLEGAL_STATE;
-        } else {
-            bundle_archive_pt archive = NULL;
-            char * location;
-			bundle_pt target;
-			status = bundle_getArchive(bundle, &archive);
-            status = bundleArchive_getLocation(archive, &location);
-            // TODO sync issues?
-            target = (bundle_pt) hashMap_remove(framework->installedBundleMap, location);
-
-            if (target != NULL) {
-                bundle_setPersistentStateUninstalled(target);
-                // fw_rememberUninstalledBundle(framework, target);
-            }
-
-            framework_releaseGlobalLock(framework);
+            error = "Unable to acquire the global lock to uninstall the bundle";
+        }
+    }
 
-            if (target == NULL) {
-                printf("Could not remove bundle from installed map");
-            }
+    status = CELIX_DO_IF(status, bundle_getArchive(bundle, &archive));
+    status = CELIX_DO_IF(status, bundleArchive_getLocation(archive, &location));
+    if (status == CELIX_SUCCESS) {
+        // TODO sync issues?
+        target = (bundle_pt) hashMap_remove(framework->installedBundleMap, location);
+
+        if (target != NULL) {
+            status = CELIX_DO_IF(status, bundle_setPersistentStateUninstalled(target));
+            // fw_rememberUninstalledBundle(framework, target);
+        }
+    }
 
-            framework_setBundleStateAndNotify(framework, bundle, BUNDLE_INSTALLED);
-            fw_fireBundleEvent(framework, BUNDLE_EVENT_UNRESOLVED, bundle);
+    framework_releaseGlobalLock(framework);
 
-            framework_setBundleStateAndNotify(framework, bundle, BUNDLE_UNINSTALLED);
-            status = bundleArchive_setLastModified(archive, time(NULL));
+    if (status == CELIX_SUCCESS) {
+        if (target == NULL) {
+            fw_log(FW_LOG_ERROR, "Could not remove bundle from installed map");
         }
-        framework_releaseBundleLock(framework, bundle);
+    }
+
+    status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, BUNDLE_INSTALLED));
+
+    status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_UNRESOLVED, bundle));
 
-        fw_fireBundleEvent(framework, BUNDLE_EVENT_UNINSTALLED, bundle);
+    status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, BUNDLE_UNINSTALLED));
+    status = CELIX_DO_IF(status, bundleArchive_setLastModified(archive, time(NULL)));
 
+    framework_releaseBundleLock(framework, bundle);
+
+    status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, BUNDLE_EVENT_UNINSTALLED, bundle));
+
+    if (status == CELIX_SUCCESS) {
         locked = framework_acquireGlobalLock(framework);
         if (locked) {
             bundle_pt bundles[] = { bundle };
@@ -1055,10 +1011,25 @@ celix_status_t fw_uninstallBundle(framew
             if (refreshStatus != CELIX_SUCCESS) {
                 printf("Could not refresh bundle");
             } else {
-            	bundle_destroy(bundle);
+                status = CELIX_DO_IF(status, bundle_destroy(bundle));
             }
 
-            framework_releaseGlobalLock(framework);
+            status = CELIX_DO_IF(status, framework_releaseGlobalLock(framework));
+        }
+    }
+
+
+    if (status != CELIX_SUCCESS) {
+        module_pt module = NULL;
+        char *symbolicName = NULL;
+        long id = 0;
+        bundle_getCurrentModule(bundle, &module);
+        module_getSymbolicName(module, &symbolicName);
+        bundle_getBundleId(bundle, &id);
+        if (error != NULL) {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot uninstall bundle: %s [%ld]; cause: %s", symbolicName, id, error);
+        } else {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot uninstall bundle: %s [%ld]", symbolicName, id);
         }
     }
 
@@ -1238,123 +1209,160 @@ celix_status_t fw_populateDependentGraph
 }
 
 celix_status_t fw_registerService(framework_pt framework, service_registration_pt *registration, bundle_pt bundle, char * serviceName, void * svcObj, properties_pt properties) {
-	celix_status_t lock;
-	if (serviceName == NULL) {
-		printf("Service name cannot be null");
-		return CELIX_ILLEGAL_ARGUMENT;
-	} else if (svcObj == NULL) {
-		printf("Service object cannot be null");
-		return CELIX_ILLEGAL_ARGUMENT;
+	celix_status_t status = CELIX_SUCCESS;
+	char *error = NULL;
+	if (serviceName == NULL || svcObj == NULL) {
+	    status = CELIX_ILLEGAL_ARGUMENT;
+	    error = "ServiceName and SvcObj cannot be null";
 	}
 
-	lock = framework_acquireBundleLock(framework, bundle, BUNDLE_STARTING|BUNDLE_ACTIVE);
-	if (lock != CELIX_SUCCESS) {
-		printf("Can only register services while bundle is active or starting");
-		framework_releaseBundleLock(framework, bundle);
-		return CELIX_ILLEGAL_STATE;
+	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, bundle, BUNDLE_STARTING|BUNDLE_ACTIVE));
+	status = CELIX_DO_IF(status, serviceRegistry_registerService(framework->registry, bundle, serviceName, svcObj, properties, registration));
+	bool res = framework_releaseBundleLock(framework, bundle);
+	if (!res) {
+	    status = CELIX_ILLEGAL_STATE;
+	    error = "Could not release bundle lock";
 	}
-	serviceRegistry_registerService(framework->registry, bundle, serviceName, svcObj, properties, registration);
-	framework_releaseBundleLock(framework, bundle);
-
-	// If this is a listener hook, invoke the callback with all current listeners
-	if (strcmp(serviceName, listener_hook_service_name) == 0) {
-		unsigned int i;
-		array_list_pt infos = NULL;
-		apr_pool_t *subpool;
-		service_reference_pt ref = NULL;
-		listener_hook_service_pt hook;
-		apr_pool_t *pool = NULL;
 
-		bundle_getMemoryPool(bundle, &pool);
+	if (status == CELIX_SUCCESS) {
+	    // If this is a listener hook, invoke the callback with all current listeners
+        if (strcmp(serviceName, listener_hook_service_name) == 0) {
+            unsigned int i;
+            array_list_pt infos = NULL;
+            apr_pool_t *subpool;
+            service_reference_pt ref = NULL;
+            listener_hook_service_pt hook;
+            apr_pool_t *pool = NULL;
+
+            status = CELIX_DO_IF(status, bundle_getMemoryPool(bundle, &pool));
+            status = CELIX_DO_IF(status, arrayList_create(pool, &infos));
+
+            if (status == CELIX_SUCCESS) {
+                celix_status_t subs = CELIX_SUCCESS;
+                for (i = 0; i < arrayList_size(framework->serviceListeners); i++) {
+                    fw_service_listener_pt listener =(fw_service_listener_pt) arrayList_get(framework->serviceListeners, i);
+                    apr_pool_t *pool = NULL;
+                    bundle_context_pt context = NULL;
+                    listener_hook_info_pt info = NULL;
+                    bundle_context_pt lContext = NULL;
+
+                    subs = CELIX_DO_IF(subs, bundle_getContext(bundle, &context));
+                    subs = CELIX_DO_IF(subs, bundleContext_getMemoryPool(context, &pool));
+                    if (subs == CELIX_SUCCESS) {
+                        info = (listener_hook_info_pt) apr_palloc(pool, sizeof(*info));
+                        if (info == NULL) {
+                            subs = CELIX_ENOMEM;
+                        }
+                    }
 
-		arrayList_create(pool, &infos);
-		for (i = 0; i < arrayList_size(framework->serviceListeners); i++) {
-			fw_service_listener_pt listener =(fw_service_listener_pt) arrayList_get(framework->serviceListeners, i);
-			apr_pool_t *pool;
-			bundle_context_pt context;
-			listener_hook_info_pt info;
-			bundle_context_pt lContext = NULL;
+                    subs = CELIX_DO_IF(subs, bundle_getContext(listener->bundle, &lContext));
+                    if (subs == CELIX_SUCCESS) {
+                        info->context = lContext;
+                        info->removed = false;
+                    }
+                    subs = CELIX_DO_IF(subs, filter_getString(listener->filter, &info->filter));
 
-			bundle_getContext(bundle, &context);
-			bundleContext_getMemoryPool(context, &pool);
-			info = (listener_hook_info_pt) apr_palloc(pool, sizeof(*info));
+                    if (subs == CELIX_SUCCESS) {
+                        arrayList_add(infos, info);
+                    }
+                    if (subs != CELIX_SUCCESS) {
+                        fw_logCode(FW_LOG_ERROR, status, "Could not pass all listeners to the hook: %s", serviceName);
+                    }
+                }
 
-			bundle_getContext(listener->bundle, &lContext);
-			info->context = lContext;
-			info->removed = false;
-			filter_getString(listener->filter, &info->filter);
+                bool ungetResult = false;
 
-			arrayList_add(infos, info);
-		}
-		bool ungetResult = false;
+                status = CELIX_DO_IF(status, apr_pool_create(&subpool, pool));
 
-		apr_pool_create(&subpool, pool);
+                status = CELIX_DO_IF(status, serviceRegistry_createServiceReference(framework->registry, subpool, *registration, &ref));
+                status = CELIX_DO_IF(status, fw_getService(framework,framework->bundle, ref, (void **) &hook));
+                if (status == CELIX_SUCCESS) {
+                    hook->added(hook->handle, infos);
+                }
+                status = CELIX_DO_IF(status, serviceRegistry_ungetService(framework->registry, framework->bundle, ref, &ungetResult));
 
-		serviceRegistry_createServiceReference(framework->registry, subpool, *registration, &ref);
-		celix_status_t status = fw_getService(framework,framework->bundle, ref, (void **) &hook);
-		hook->added(hook->handle, infos);
-		serviceRegistry_ungetService(framework->registry, framework->bundle, ref, &ungetResult);
+                apr_pool_destroy(subpool);
+             }
+        }
+	}
 
-		apr_pool_destroy(subpool);
+	if (status != CELIX_SUCCESS) {
+		if (error != NULL) {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot register service: %s; cause: %s", serviceName, error);
+        } else {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot register service: %s", serviceName);
+        }
 	}
 
-	return CELIX_SUCCESS;
+	return status;
 }
 
 celix_status_t fw_registerServiceFactory(framework_pt framework, service_registration_pt *registration, bundle_pt bundle, char * serviceName, service_factory_pt factory, properties_pt properties) {
-    celix_status_t lock;
-	if (serviceName == NULL) {
-        printf("Service name cannot be null");
-        return CELIX_ILLEGAL_ARGUMENT;
-    } else if (factory == NULL) {
-        printf("Service factory cannot be null");
-        return CELIX_ILLEGAL_ARGUMENT;
+    celix_status_t status = CELIX_SUCCESS;
+    char *error = NULL;
+	if (serviceName == NULL || factory == NULL) {
+        status = CELIX_ILLEGAL_ARGUMENT;
+        error = "Service name and factory cannot be null";
     }
 
-    lock = framework_acquireBundleLock(framework, bundle, BUNDLE_STARTING|BUNDLE_ACTIVE);
-    if (lock != CELIX_SUCCESS) {
-        printf("Can only register services while bundle is active or starting");
-        framework_releaseBundleLock(framework, bundle);
-        return CELIX_ILLEGAL_STATE;
+	status = CELIX_DO_IF(status, framework_acquireBundleLock(framework, bundle, BUNDLE_STARTING|BUNDLE_ACTIVE));
+	status = CELIX_DO_IF(status, serviceRegistry_registerServiceFactory(framework->registry, bundle, serviceName, factory, properties, registration));
+    if (!framework_releaseBundleLock(framework, bundle)) {
+        status = CELIX_ILLEGAL_STATE;
+        error = "Could not release bundle lock";
+    }
+
+
+    if (status != CELIX_SUCCESS) {
+        if (error != NULL) {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot register service factory: %s; cause: %s", serviceName, error);
+        } else {
+            fw_logCode(FW_LOG_ERROR, status, "Cannot register service factory: %s", serviceName);
+        }
     }
-    serviceRegistry_registerServiceFactory(framework->registry, bundle, serviceName, factory, properties, registration);
-    framework_releaseBundleLock(framework, bundle);
 
     return CELIX_SUCCESS;
 }
 
 celix_status_t fw_getServiceReferences(framework_pt framework, array_list_pt *references, bundle_pt bundle, const char * serviceName, char * sfilter) {
+    celix_status_t status = CELIX_SUCCESS;
+
 	filter_pt filter = NULL;
 	unsigned int refIdx = 0;
 	apr_pool_t *pool = NULL;
 
-	bundle_getMemoryPool(bundle, &pool);
-
-	if (sfilter != NULL) {
-		filter = filter_create(sfilter, pool);
+	status = CELIX_DO_IF(status, bundle_getMemoryPool(bundle, &pool));
+	if (status == CELIX_SUCCESS) {
+        if (sfilter != NULL) {
+            filter = filter_create(sfilter, pool);
+        }
 	}
 
-	serviceRegistry_getServiceReferences(framework->registry, pool, serviceName, filter, references);
+	status = CELIX_DO_IF(status, serviceRegistry_getServiceReferences(framework->registry, pool, serviceName, filter, references));
 
 	if (filter != NULL) {
 		filter_destroy(filter);
 	}
 
-	for (refIdx = 0; (*references != NULL) && refIdx < arrayList_size(*references); refIdx++) {
-		service_reference_pt ref = (service_reference_pt) arrayList_get(*references, refIdx);
-		service_registration_pt reg = NULL;
-		char * serviceName;
-		properties_pt props = NULL;
-		serviceReference_getServiceRegistration(ref, &reg);
-		serviceRegistration_getProperties(reg, &props);
-		serviceName = properties_get(props, (char *) OBJECTCLASS);
-		if (!serviceReference_isAssignableTo(ref, bundle, serviceName)) {
-			arrayList_remove(*references, refIdx);
-			refIdx--;
-		}
+	if (status == CELIX_SUCCESS) {
+        for (refIdx = 0; (*references != NULL) && refIdx < arrayList_size(*references); refIdx++) {
+            service_reference_pt ref = (service_reference_pt) arrayList_get(*references, refIdx);
+            service_registration_pt reg = NULL;
+            char * serviceName;
+            properties_pt props = NULL;
+            status = CELIX_DO_IF(status, serviceReference_getServiceRegistration(ref, &reg));
+            status = CELIX_DO_IF(status, serviceRegistration_getProperties(reg, &props));
+            if (status == CELIX_SUCCESS) {
+                serviceName = properties_get(props, (char *) OBJECTCLASS);
+                if (!serviceReference_isAssignableTo(ref, bundle, serviceName)) {
+                    arrayList_remove(*references, refIdx);
+                    refIdx--;
+                }
+            }
+        }
 	}
 
-	return CELIX_SUCCESS;
+	return status;
 }
 
 celix_status_t fw_getService(framework_pt framework, bundle_pt bundle, service_reference_pt reference, void **service) {
@@ -1769,20 +1777,20 @@ celix_status_t framework_setBundleStateA
 
 	int err = apr_thread_mutex_lock(framework->bundleLock);
 	if (err != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Failed to lock");
+		fw_log(FW_LOG_ERROR,  "Failed to lock");
 		return CELIX_BUNDLE_EXCEPTION;
 	}
 
 	bundle_setState(bundle, state);
 	err = apr_thread_cond_broadcast(framework->condition);
 	if (err != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Failed to broadcast");
+		fw_log(FW_LOG_ERROR,  "Failed to broadcast");
 		ret = CELIX_BUNDLE_EXCEPTION;
 	}
 
 	err = apr_thread_mutex_unlock(framework->bundleLock);
 	if (err != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Failed to unlock");
+		fw_log(FW_LOG_ERROR,  "Failed to unlock");
 		return CELIX_BUNDLE_EXCEPTION;
 	}
 	return CELIX_SUCCESS;
@@ -1796,7 +1804,7 @@ celix_status_t framework_acquireBundleLo
 
 	int err = apr_thread_mutex_lock(framework->bundleLock);
 	if (err != APR_SUCCESS) {
-		fw_log(NULL, FW_LOG_ERROR,  "Failed to lock");
+		fw_log(FW_LOG_ERROR,  "Failed to lock");
 		status = CELIX_BUNDLE_EXCEPTION;
 	} else {
 		bool lockable = false;
@@ -1908,7 +1916,7 @@ bool framework_acquireGlobalLock(framewo
 celix_status_t framework_releaseGlobalLock(framework_pt framework) {
 	int ret = CELIX_SUCCESS;
 	if (apr_thread_mutex_lock(framework->bundleLock) != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Error locking framework bundle lock");
+		fw_log(FW_LOG_ERROR,  "Error locking framework bundle lock");
 		return CELIX_FRAMEWORK_EXCEPTION;
 	}
 
@@ -1917,7 +1925,7 @@ celix_status_t framework_releaseGlobalLo
 		if (framework->globalLockCount == 0) {
 			framework->globalLockThread = 0;
 			if (apr_thread_cond_broadcast(framework->condition) != 0) {
-				fw_log(NULL, FW_LOG_ERROR,  "Failed to broadcast global lock release.");
+				fw_log(FW_LOG_ERROR,  "Failed to broadcast global lock release.");
 				ret = CELIX_FRAMEWORK_EXCEPTION;
 				// still need to unlock before returning
 			}
@@ -1927,7 +1935,7 @@ celix_status_t framework_releaseGlobalLo
 	}
 
 	if (apr_thread_mutex_unlock(framework->bundleLock) != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Error unlocking framework bundle lock");
+		fw_log(FW_LOG_ERROR,  "Error unlocking framework bundle lock");
 		return CELIX_FRAMEWORK_EXCEPTION;
 	}
 	return ret;
@@ -1935,19 +1943,19 @@ celix_status_t framework_releaseGlobalLo
 
 celix_status_t framework_waitForStop(framework_pt framework) {
 	if (apr_thread_mutex_lock(framework->mutex) != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Error locking the framework, shutdown gate not set.");
+		fw_log(FW_LOG_ERROR,  "Error locking the framework, shutdown gate not set.");
 		return CELIX_FRAMEWORK_EXCEPTION;
 	}
 	while (!framework->shutdown) {
 		apr_status_t apr_status = apr_thread_cond_wait(framework->shutdownGate, framework->mutex);
 		printf("FRAMEWORK: Gate opened\n");
 		if (apr_status != 0) {
-			fw_log(NULL, FW_LOG_ERROR,  "Error waiting for shutdown gate.");
+			fw_log(FW_LOG_ERROR,  "Error waiting for shutdown gate.");
 			return CELIX_FRAMEWORK_EXCEPTION;
 		}
 	}
 	if (apr_thread_mutex_unlock(framework->mutex) != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Error unlocking the framework.");
+		fw_log(FW_LOG_ERROR,  "Error unlocking the framework.");
 		return CELIX_FRAMEWORK_EXCEPTION;
 	}
 	printf("FRAMEWORK: Successful shutdown\n");
@@ -1980,7 +1988,7 @@ static void *APR_THREAD_FUNC framework_s
 
 	err = apr_thread_mutex_lock(fw->mutex);
 	if (err != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Error locking the framework, cannot exit clean.");
+		fw_log(FW_LOG_ERROR,  "Error locking the framework, cannot exit clean.");
 		apr_thread_exit(thd, APR_ENOLOCK);
 		return NULL;
 	}
@@ -1991,10 +1999,10 @@ static void *APR_THREAD_FUNC framework_s
 	printf("FRAMEWORK: BC send\n");
 	if (err != 0) {
 		printf("FRAMEWORK: BC send\n");
-		fw_log(NULL, FW_LOG_ERROR,  "Error waking the shutdown gate, cannot exit clean.");
+		fw_log(FW_LOG_ERROR,  "Error waking the shutdown gate, cannot exit clean.");
 		err = apr_thread_mutex_unlock(fw->mutex);
 		if (err != 0) {
-			fw_log(NULL, FW_LOG_ERROR,  "Error unlocking the framework, cannot exit clean.");
+			fw_log(FW_LOG_ERROR,  "Error unlocking the framework, cannot exit clean.");
 		}
 
 		apr_thread_exit(thd, APR_ENOLOCK);
@@ -2003,7 +2011,7 @@ static void *APR_THREAD_FUNC framework_s
 	printf("FRAMEWORK: Unlock\n");
 	err = apr_thread_mutex_unlock(fw->mutex);
 	if (err != 0) {
-		fw_log(NULL, FW_LOG_ERROR,  "Error unlocking the framework, cannot exit clean.");
+		fw_log(FW_LOG_ERROR,  "Error unlocking the framework, cannot exit clean.");
 	}
 
 	printf("FRAMEWORK: Exit thread\n");
@@ -2061,7 +2069,7 @@ celix_status_t fw_fireBundleEvent(framew
 	return status;
 }
 
-celix_status_t fw_fireFrameworkEvent(framework_pt framework, framework_event_type_e eventType, bundle_pt bundle, celix_status_t errorCode, char *error) {
+celix_status_t fw_fireFrameworkEvent(framework_pt framework, framework_event_type_e eventType, bundle_pt bundle, celix_status_t errorCode) {
 	celix_status_t status = CELIX_SUCCESS;
 
 	request_pt request = (request_pt) malloc(sizeof(*request));
@@ -2074,7 +2082,12 @@ celix_status_t fw_fireFrameworkEvent(fra
 		request->listeners = framework->frameworkListeners;
 		request->type = FRAMEWORK_EVENT_TYPE;
 		request->errorCode = errorCode;
-		request->error = error;
+
+		if (errorCode != CELIX_SUCCESS) {
+		    char message[256];
+            celix_strerror(errorCode, message, 256);
+            request->error = message;
+		}
 
 		arrayList_add(framework->requests, request);
 		if (apr_thread_mutex_lock(framework->dispatcherLock) != APR_SUCCESS) {
@@ -2102,7 +2115,7 @@ static void *APR_THREAD_FUNC fw_eventDis
 		apr_status_t status;
 
 		if (apr_thread_mutex_lock(framework->dispatcherLock) != 0) {
-			fw_log(NULL, FW_LOG_ERROR,  "Error locking the dispatcher");
+			fw_log(FW_LOG_ERROR,  "Error locking the dispatcher");
 			return NULL;
 		}
 
@@ -2121,7 +2134,7 @@ static void *APR_THREAD_FUNC fw_eventDis
 		request = (request_pt) arrayList_remove(framework->requests, 0);
 
 		if ((status = apr_thread_mutex_unlock(framework->dispatcherLock)) != 0) {
-			fw_log(NULL, FW_LOG_ERROR,  "Error unlocking the dispatcher.");
+			fw_log(FW_LOG_ERROR,  "Error unlocking the dispatcher.");
 			apr_thread_exit(thd, status);
 			return NULL;
 		}
@@ -2196,7 +2209,7 @@ celix_status_t bundleActivator_stop(void
 	    if (apr_thread_create(&shutdownThread, NULL, framework_shutdown, framework, framework->mp) == APR_SUCCESS) {
             apr_thread_detach(shutdownThread);
 	    } else {
-            fw_log(NULL, FW_LOG_ERROR,  "Could not create shutdown thread, normal exit not possible.");
+            fw_log(FW_LOG_ERROR,  "Could not create shutdown thread, normal exit not possible.");
 	        status = CELIX_FRAMEWORK_EXCEPTION;
 	    }
 	} else {

Modified: incubator/celix/trunk/framework/public/include/celix_log.h
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/framework/public/include/celix_log.h?rev=1543333&r1=1543332&r2=1543333&view=diff
==============================================================================
--- incubator/celix/trunk/framework/public/include/celix_log.h (original)
+++ incubator/celix/trunk/framework/public/include/celix_log.h Tue Nov 19 07:40:01 2013
@@ -28,13 +28,13 @@
 #define CELIX_LOG_H_
 
 #include <stdio.h>
-#include "framework.h"
 
 #if defined(WIN32)
 #define celix_log(msg) printf("%s\n", msg);
 #else
 #define celix_log(msg) printf("%s\n\tat %s(%s:%d)\n", msg, __func__, __FILE__, __LINE__);
-#define fw_log(framework, level, fmsg, args...) framework_log(framework, level, __func__, __FILE__, __LINE__, fmsg, ## args)
+#define fw_log(level, fmsg, args...) framework_log(level, __func__, __FILE__, __LINE__, fmsg, ## args)
+#define fw_logCode(level, code, fmsg, args...) framework_logCode(level, __func__, __FILE__, __LINE__, code, fmsg, ## args)
 #endif
 
 enum framework_log_level
@@ -47,6 +47,7 @@ enum framework_log_level
 
 typedef enum framework_log_level framework_log_level_t;
 
-void framework_log(framework_pt framework, framework_log_level_t level, const char *func, const char *file, int line, char *fmsg, ...);
+void framework_log(framework_log_level_t level, const char *func, const char *file, int line, char *fmsg, ...);
+void framework_logCode(framework_log_level_t level, const char *func, const char *file, int line, celix_status_t code, char *fmsg, ...);
 
 #endif /* CELIX_LOG_H_ */

Modified: incubator/celix/trunk/launcher/private/src/launcher.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/launcher/private/src/launcher.c?rev=1543333&r1=1543332&r2=1543333&view=diff
==============================================================================
--- incubator/celix/trunk/launcher/private/src/launcher.c (original)
+++ incubator/celix/trunk/launcher/private/src/launcher.c Tue Nov 19 07:40:01 2013
@@ -70,64 +70,67 @@ int main(void) {
     framework = NULL;
     celix_status_t status = framework_create(&framework, memoryPool, config);
     if (status == CELIX_SUCCESS) {
-		fw_init(framework);
-
-		// Start the system bundle
-		framework_getFrameworkBundle(framework, &fwBundle);
-		bundle_start(fwBundle);
-
-		if (apr_pool_create(&pool, memoryPool) == APR_SUCCESS) {
-			char delims[] = " ";
-			char *result = NULL;
-			linked_list_pt bundles;
-			array_list_pt installed = NULL;
-			bundle_pt bundle = NULL;
-			bundle_context_pt context = NULL;
-			linked_list_iterator_pt iter = NULL;
-			unsigned int i;
-
-			linkedList_create(pool, &bundles);
-			result = strtok(autoStart, delims);
-			while (result != NULL) {
-				char * location = apr_pstrdup(memoryPool, result);
-				linkedList_addElement(bundles, location);
-				result = strtok(NULL, delims);
-			}
-			// First install all bundles
-			// Afterwards start them
-			arrayList_create(pool, &installed);
-			framework_getFrameworkBundle(framework, &bundle);
-			bundle_getContext(bundle, &context);
-			iter = linkedListIterator_create(bundles, 0);
-			while (linkedListIterator_hasNext(iter)) {
-				bundle_pt current = NULL;
-				char * location = (char *) linkedListIterator_next(iter);
-				if (bundleContext_installBundle(context, location, &current) == CELIX_SUCCESS) {
-					// Only add bundle if it is installed correctly
-					arrayList_add(installed, current);
-				} else {
-					char error[256];
-					sprintf(error, "Could not install bundle from %s", location);
-					celix_log(error);
-				}
-				linkedListIterator_remove(iter);
-			}
-			linkedListIterator_destroy(iter);
-
-			for (i = 0; i < arrayList_size(installed); i++) {
-				bundle_pt bundle = (bundle_pt) arrayList_get(installed, i);
-				bundle_startWithOptions(bundle, 0);
-			}
-
-			arrayList_destroy(installed);
-			apr_pool_destroy(pool);
+		status = fw_init(framework);
+		if (status == CELIX_SUCCESS) {
+            // Start the system bundle
+            framework_getFrameworkBundle(framework, &fwBundle);
+            bundle_start(fwBundle);
+
+            if (apr_pool_create(&pool, memoryPool) == APR_SUCCESS) {
+                char delims[] = " ";
+                char *result = NULL;
+                linked_list_pt bundles;
+                array_list_pt installed = NULL;
+                bundle_pt bundle = NULL;
+                bundle_context_pt context = NULL;
+                linked_list_iterator_pt iter = NULL;
+                unsigned int i;
+
+                linkedList_create(pool, &bundles);
+                result = strtok(autoStart, delims);
+                while (result != NULL) {
+                    char * location = apr_pstrdup(memoryPool, result);
+                    linkedList_addElement(bundles, location);
+                    result = strtok(NULL, delims);
+                }
+                // First install all bundles
+                // Afterwards start them
+                arrayList_create(pool, &installed);
+                framework_getFrameworkBundle(framework, &bundle);
+                bundle_getContext(bundle, &context);
+                iter = linkedListIterator_create(bundles, 0);
+                while (linkedListIterator_hasNext(iter)) {
+                    bundle_pt current = NULL;
+                    char * location = (char *) linkedListIterator_next(iter);
+                    if (bundleContext_installBundle(context, location, &current) == CELIX_SUCCESS) {
+                        // Only add bundle if it is installed correctly
+                        arrayList_add(installed, current);
+                    } else {
+                        char error[256];
+                        sprintf(error, "Could not install bundle from %s", location);
+                        celix_log(error);
+                    }
+                    linkedListIterator_remove(iter);
+                }
+                linkedListIterator_destroy(iter);
+
+                for (i = 0; i < arrayList_size(installed); i++) {
+                    bundle_pt bundle = (bundle_pt) arrayList_get(installed, i);
+                    bundle_startWithOptions(bundle, 0);
+                }
+
+                arrayList_destroy(installed);
+                apr_pool_destroy(pool);
+            }
+
+            framework_waitForStop(framework);
+            framework_destroy(framework);
+            properties_destroy(config);
 		}
+    }
 
-		framework_waitForStop(framework);
-		framework_destroy(framework);
-		properties_destroy(config);
-    } else {
-    	celix_log("Problem creating framework");
+    if (status != CELIX_SUCCESS) {
+        celix_log("Problem creating framework");
     }
 
 	apr_pool_destroy(memoryPool);



Mime
View raw message