Skip to content

Commit

Permalink
Added service-listener-hook example
Browse files Browse the repository at this point in the history
  • Loading branch information
Erjan Altena committed Feb 22, 2017
1 parent cd0164c commit ef3ef52
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ if (EXAMPLES)
endif()
add_subdirectory(whiteboard)
add_subdirectory(embedding)
add_subdirectory(service_hook_example)

endif(EXAMPLES)
37 changes: 37 additions & 0 deletions examples/service_hook_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

if(NOT APPLE)
#Importing and exporting libraries not (yet) work under OSX.

include_directories("${PROJECT_SOURCE_DIR}/utils/public/include")
include_directories("public/include")

add_bundle(hook_example
BUNDLE_SYMBOLICNAME "Hook_Example"
VERSION "1.0.0"
SOURCES
private/src/activator
)

add_deploy("hook_service_example"
BUNDLES
shell
shell_tui
hook_example
)
endif()
116 changes: 116 additions & 0 deletions examples/service_hook_example/private/src/activator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
*Licensed to the Apache Software Foundation (ASF) under one
*or more contributor license agreements. See the NOTICE file
*distributed with this work for additional information
*regarding copyright ownership. The ASF licenses this file
*to you under the Apache License, Version 2.0 (the
*"License"); you may not use this file except in compliance
*with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing,
*software distributed under the License is distributed on an
*"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
*specific language governing permissions and limitations
*under the License.
*/
/*
* activator.c
*
* \date Aug 20, 2010
* \author <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
* \copyright Apache License, Version 2.0
*/
#include <stdlib.h>
#include <stdio.h>

#include "bundle_activator.h"
#include "service_tracker_customizer.h"
#include "service_tracker.h"
#include "bundle_context.h"
#include "listener_hook_service.h"
#include "service_registry.h"

struct userData {
service_registration_pt hookReg;
service_tracker_pt trackerBefore;
service_tracker_pt trackerAfter;
listener_hook_service_pt hookService;
};


celix_status_t tracker_added(void*hook, array_list_pt listeners) {
for(unsigned int i = 0; i < arrayList_size(listeners); i++) {
listener_hook_info_pt info = arrayList_get(listeners, i);
printf("Added tracker for service %s\n", info->filter);
}

return CELIX_SUCCESS;
}

celix_status_t tracker_removed(void*hook, array_list_pt listeners) {
for(unsigned int i = 0; i < arrayList_size(listeners); i++) {
listener_hook_info_pt info = arrayList_get(listeners, i);
printf("Removed tracker for service %s\n", info->filter);
}

return CELIX_SUCCESS;
}

celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
celix_status_t status = CELIX_SUCCESS;
*userData = malloc(sizeof(struct userData));
if (*userData != NULL) {

} else {
status = CELIX_START_ERROR;
}
return status;
}

celix_status_t bundleActivator_start(void * handle, bundle_context_pt context) {
printf("Starting hook example bundle\n");
struct userData *userData = (struct userData*)handle;

userData->trackerBefore = 0;
serviceTracker_create(context, "MY_SERVICE_BEFORE_REGISTERING_HOOK", NULL, &userData->trackerBefore);
serviceTracker_open(userData->trackerBefore);

listener_hook_service_pt hookService = calloc(1, sizeof(*hookService));
hookService->handle = userData;
hookService->added = tracker_added;
hookService->removed = tracker_removed;

userData->hookService = hookService;
userData->hookReg = NULL;

bundleContext_registerService(context, OSGI_FRAMEWORK_LISTENER_HOOK_SERVICE_NAME, hookService, NULL, &userData->hookReg);

userData->trackerAfter = 0;
serviceTracker_create(context, "MY_SERVICE_AFTER_REGISTERING_HOOK", NULL, &userData->trackerAfter);
serviceTracker_open(userData->trackerAfter);

return CELIX_SUCCESS;
}

celix_status_t bundleActivator_stop(void * handle, bundle_context_pt context) {
printf("Stopping hook example bundle\n");
struct userData *userData = (struct userData*)handle;

serviceTracker_close(userData->trackerAfter);
serviceTracker_close(userData->trackerBefore);
serviceTracker_destroy(userData->trackerAfter);
serviceTracker_destroy(userData->trackerBefore);

serviceRegistration_unregister(userData->hookReg);
free(userData->hookService);

return CELIX_SUCCESS;
}

celix_status_t bundleActivator_destroy(void * handle, bundle_context_pt context) {
free(handle);
return CELIX_SUCCESS;
}

0 comments on commit ef3ef52

Please sign in to comment.