CELIX-351: Refactoring of shell to use service tracker instead of service 
listener.


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

Branch: refs/heads/release/celix-2.0.0
Commit: 90adc1f4ef56f75b64e8943f9848f9f51b26048b
Parents: e0812b0
Author: Pepijn Noltes <[email protected]>
Authored: Fri Feb 12 15:03:05 2016 +0100
Committer: Pepijn Noltes <[email protected]>
Committed: Fri Feb 12 15:03:05 2016 +0100

----------------------------------------------------------------------
 .../private/src/dm_shell_list_command.c         |  1 +
 framework/private/src/service_tracker.c         |  1 -
 shell/private/include/shell_private.h           |  5 +-
 shell/private/src/activator.c                   | 49 ++++++--------------
 shell/private/src/shell.c                       | 38 ++-------------
 5 files changed, 21 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/90adc1f4/dependency_manager/private/src/dm_shell_list_command.c
----------------------------------------------------------------------
diff --git a/dependency_manager/private/src/dm_shell_list_command.c 
b/dependency_manager/private/src/dm_shell_list_command.c
index 4862d42..6f95eb9 100644
--- a/dependency_manager/private/src/dm_shell_list_command.c
+++ b/dependency_manager/private/src/dm_shell_list_command.c
@@ -107,6 +107,7 @@ void dmListCommand_execute(bundle_context_pt context, char 
* line, FILE *out, FI
             infoServ->destroyInfo(infoServ->handle, info);
 
                bundleContext_ungetService(context,servRef,NULL);
+               bundleContext_ungetServiceReference(context,servRef);
 
         }
 

http://git-wip-us.apache.org/repos/asf/celix/blob/90adc1f4/framework/private/src/service_tracker.c
----------------------------------------------------------------------
diff --git a/framework/private/src/service_tracker.c 
b/framework/private/src/service_tracker.c
index 1fb9b37..546ee5d 100644
--- a/framework/private/src/service_tracker.c
+++ b/framework/private/src/service_tracker.c
@@ -132,7 +132,6 @@ celix_status_t serviceTracker_open(service_tracker_pt 
tracker) {
                        for (i = 0; i < arrayList_size(initial); i++) {
                                initial_reference = (service_reference_pt) 
arrayList_get(initial, i);
                                serviceTracker_track(tracker, 
initial_reference, NULL);
-                bundleContext_ungetServiceReference(tracker->context, 
initial_reference);
                        }
                        arrayList_clear(initial);
                        arrayList_destroy(initial);

http://git-wip-us.apache.org/repos/asf/celix/blob/90adc1f4/shell/private/include/shell_private.h
----------------------------------------------------------------------
diff --git a/shell/private/include/shell_private.h 
b/shell/private/include/shell_private.h
index c3a3386..7270fa2 100644
--- a/shell/private/include/shell_private.h
+++ b/shell/private/include/shell_private.h
@@ -42,11 +42,10 @@ struct shell {
 
 celix_status_t shell_create(bundle_context_pt context_ptr, shell_service_pt 
*shell_service_ptr);
 celix_status_t shell_destroy(shell_service_pt *shell_service_ptr);
-celix_status_t shell_addCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr);
-celix_status_t shell_removeCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr);
+celix_status_t shell_addCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr, void *svc);
+celix_status_t shell_removeCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr, void *svc);
 
 celix_status_t shell_getCommandReference(shell_pt shell_ptr, char 
*command_name_str, service_reference_pt *command_reference_ptr);
 celix_status_t shell_executeCommand(shell_pt shell_ptr, char 
*command_line_str, FILE *out, FILE *err);
-celix_status_t shell_serviceChanged(service_listener_pt listener_ptr, 
service_event_pt event_ptr);
 
 #endif /* SHELL_PRIVATE_H_ */

http://git-wip-us.apache.org/repos/asf/celix/blob/90adc1f4/shell/private/src/activator.c
----------------------------------------------------------------------
diff --git a/shell/private/src/activator.c b/shell/private/src/activator.c
index 69f656f..b98cb26 100644
--- a/shell/private/src/activator.c
+++ b/shell/private/src/activator.c
@@ -28,6 +28,7 @@
 #include "shell_private.h"
 #include "bundle_activator.h"
 #include "std_commands.h"
+#include "service_tracker.h"
 
 #define NUMBER_OF_COMMANDS 10
 
@@ -44,7 +45,7 @@ struct command {
 struct bundle_instance {
        shell_service_pt shellService;
        service_registration_pt registration;
-       service_listener_pt listener;
+    service_tracker_pt tracker;
 
     struct command std_commands[NUMBER_OF_COMMANDS];
 };
@@ -179,8 +180,6 @@ celix_status_t bundleActivator_start(void *_ptr, 
bundle_context_pt context_ptr)
        celix_status_t status = CELIX_SUCCESS;
 
        bundle_instance_pt instance_ptr  = (bundle_instance_pt) _ptr;
-    array_list_pt references_ptr     = NULL;
-    service_listener_pt listener_ptr = NULL;
 
     if (!instance_ptr || !context_ptr) {
         status = CELIX_ILLEGAL_ARGUMENT;
@@ -191,29 +190,12 @@ celix_status_t bundleActivator_start(void *_ptr, 
bundle_context_pt context_ptr)
     }
 
        if (status == CELIX_SUCCESS) {
-        listener_ptr = (service_listener_pt) calloc(1, sizeof(*listener_ptr));
-        if (!listener_ptr) {
-            status = CELIX_ENOMEM;
-        }
-    }
-
-    if (status == CELIX_SUCCESS) {
-        instance_ptr->listener = listener_ptr;
-        listener_ptr->handle = instance_ptr->shellService->shell;
-        listener_ptr->serviceChanged = (void *) shell_serviceChanged;
-
-        status = bundleContext_addServiceListener(context_ptr, listener_ptr, 
"(objectClass=commandService)");
+        service_tracker_customizer_pt cust = NULL;
+        serviceTrackerCustomizer_create(instance_ptr->shellService->shell, 
NULL, (void *)shell_addCommand, NULL, (void *)shell_removeCommand, &cust);
+        serviceTracker_create(context_ptr, (char 
*)OSGI_SHELL_COMMAND_SERVICE_NAME, cust, &instance_ptr->tracker);
+        serviceTracker_open(instance_ptr->tracker);
     }
 
-    if (status == CELIX_SUCCESS) {
-        status = bundleContext_getServiceReferences(context_ptr, 
"commandService", NULL, &references_ptr);
-    }
-
-    if (status == CELIX_SUCCESS) {
-        for (unsigned int i = 0; i < arrayList_size(references_ptr); i++) {
-            shell_addCommand(instance_ptr->shellService->shell, 
arrayList_get(references_ptr, i));
-        }
-    }
 
     if (status == CELIX_SUCCESS) {
         for (unsigned int i = 0; instance_ptr->std_commands[i].exec != NULL; 
i++) {
@@ -226,19 +208,13 @@ celix_status_t bundleActivator_start(void *_ptr, 
bundle_context_pt context_ptr)
             }
 
         }
-               arrayList_destroy(references_ptr);
        }
 
-    if (status != CELIX_SUCCESS) {
-        bundleActivator_stop(_ptr, context_ptr);
-    }
-
        return status;
 }
 
 celix_status_t bundleActivator_stop(void *_ptr, bundle_context_pt context_ptr) 
{
     celix_status_t status = CELIX_SUCCESS;
-    celix_status_t sub_status;
 
     bundle_instance_pt instance_ptr = (bundle_instance_pt) _ptr;
 
@@ -251,9 +227,8 @@ celix_status_t bundleActivator_stop(void *_ptr, 
bundle_context_pt context_ptr) {
             }
         }
 
-        sub_status = bundleContext_removeServiceListener(context_ptr, 
instance_ptr->listener);
-        if (status == CELIX_SUCCESS && sub_status != CELIX_SUCCESS) {
-            status = sub_status;
+        if (instance_ptr->tracker != NULL) {
+            serviceTracker_close(instance_ptr->tracker);
         }
     } else {
         status = CELIX_ILLEGAL_ARGUMENT;
@@ -268,14 +243,18 @@ celix_status_t bundleActivator_destroy(void *_ptr, 
bundle_context_pt __attribute
     bundle_instance_pt instance_ptr = (bundle_instance_pt) _ptr;
 
     if (instance_ptr) {
+        serviceRegistration_unregister(instance_ptr->registration);
+
         for (unsigned int i = 0; instance_ptr->std_commands[i].exec != NULL; 
i++) {
             free(instance_ptr->std_commands[i].service);
         }
 
-        serviceRegistration_unregister(instance_ptr->registration);
         shell_destroy(&instance_ptr->shellService);
 
-        free(instance_ptr->listener);
+        if (instance_ptr->tracker != NULL) {
+            serviceTracker_destroy(instance_ptr->tracker);
+        }
+
         free(instance_ptr);
     } else {
         status = CELIX_ILLEGAL_ARGUMENT;

http://git-wip-us.apache.org/repos/asf/celix/blob/90adc1f4/shell/private/src/shell.c
----------------------------------------------------------------------
diff --git a/shell/private/src/shell.c b/shell/private/src/shell.c
index c48c265..8f8241e 100644
--- a/shell/private/src/shell.c
+++ b/shell/private/src/shell.c
@@ -107,7 +107,7 @@ celix_status_t shell_destroy(shell_service_pt 
*shell_service_ptr) {
        return status;
 }
 
-celix_status_t shell_addCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr) {
+celix_status_t shell_addCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr, void *svc) {
     celix_status_t status = CELIX_SUCCESS;
     command_service_pt command_ptr = NULL;
     char *name_str = NULL;
@@ -117,10 +117,7 @@ celix_status_t shell_addCommand(shell_pt shell_ptr, 
service_reference_pt referen
     }
 
     if (status == CELIX_SUCCESS) {
-        status = bundleContext_getService(shell_ptr->bundle_context_ptr, 
reference_ptr, (void **) &command_ptr);
-        if (!command_ptr) {
-            status = CELIX_BUNDLE_EXCEPTION;
-        }
+        command_ptr = svc;
     }
 
     if (status == CELIX_SUCCESS) {
@@ -137,7 +134,7 @@ celix_status_t shell_addCommand(shell_pt shell_ptr, 
service_reference_pt referen
     }
 
     if (status != CELIX_SUCCESS) {
-        shell_removeCommand(shell_ptr, reference_ptr);
+        shell_removeCommand(shell_ptr, reference_ptr, svc);
         char err[32];
         celix_strerror(status, err, 32);
         logHelper_log(shell_ptr->logHelper, OSGI_LOGSERVICE_ERROR, "Could not 
add command, got error %s\n", err);
@@ -146,7 +143,7 @@ celix_status_t shell_addCommand(shell_pt shell_ptr, 
service_reference_pt referen
     return status;
 }
 
-celix_status_t shell_removeCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr) {
+celix_status_t shell_removeCommand(shell_pt shell_ptr, service_reference_pt 
reference_ptr, void *svc) {
     celix_status_t status = CELIX_SUCCESS;
 
     command_service_pt command_ptr = NULL;
@@ -174,13 +171,6 @@ celix_status_t shell_removeCommand(shell_pt shell_ptr, 
service_reference_pt refe
         hashMap_remove(shell_ptr->command_name_map_ptr, name_str);
     }
 
-    if (status == CELIX_SUCCESS) {
-        celix_status_t sub_status = 
bundleContext_ungetService(shell_ptr->bundle_context_ptr, reference_ptr, NULL);
-        if (sub_status != CELIX_SUCCESS && status == CELIX_SUCCESS) {
-            status = sub_status;
-        }
-    }
-
     return status;
 }
 
@@ -312,23 +302,3 @@ celix_status_t shell_executeCommand(shell_pt shell_ptr, 
char *command_line_str,
 
        return status;
 }
-
-celix_status_t shell_serviceChanged(service_listener_pt listener_ptr, 
service_event_pt event_ptr) {
-       celix_status_t status = CELIX_SUCCESS;
-
-       if (!listener_ptr || !event_ptr || !listener_ptr->handle || 
!event_ptr->type || !event_ptr->reference) {
-               status = CELIX_ILLEGAL_ARGUMENT;
-       }
-
-       if (status == CELIX_SUCCESS) {
-               shell_pt shell_ptr = (shell_pt) listener_ptr->handle;
-               if (event_ptr->type == OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED) 
{
-                       status = shell_addCommand(shell_ptr, 
event_ptr->reference);
-               } else if (event_ptr->type == 
OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING) {
-                       status = shell_removeCommand(shell_ptr, 
event_ptr->reference);
-               }
-       }
-
-       return status;
-}
-

Reply via email to