You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by er...@apache.org on 2017/02/22 13:50:53 UTC
celix git commit: Added service-listener-hook example
Repository: celix
Updated Branches:
refs/heads/develop cd0164c72 -> ef3ef52a8
Added service-listener-hook example
Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/ef3ef52a
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/ef3ef52a
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/ef3ef52a
Branch: refs/heads/develop
Commit: ef3ef52a88d860b03db5d517b99cd0f80b111ba3
Parents: cd0164c
Author: Erjan Altena <er...@nl.thalesgroup.com>
Authored: Wed Feb 22 14:49:37 2017 +0100
Committer: Erjan Altena <er...@nl.thalesgroup.com>
Committed: Wed Feb 22 14:49:37 2017 +0100
----------------------------------------------------------------------
examples/CMakeLists.txt | 1 +
examples/service_hook_example/CMakeLists.txt | 37 ++++++
.../private/src/activator.c | 116 +++++++++++++++++++
3 files changed, 154 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/celix/blob/ef3ef52a/examples/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 5192ee3..122f87c 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -30,5 +30,6 @@ if (EXAMPLES)
endif()
add_subdirectory(whiteboard)
add_subdirectory(embedding)
+ add_subdirectory(service_hook_example)
endif(EXAMPLES)
http://git-wip-us.apache.org/repos/asf/celix/blob/ef3ef52a/examples/service_hook_example/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/examples/service_hook_example/CMakeLists.txt b/examples/service_hook_example/CMakeLists.txt
new file mode 100644
index 0000000..8835a85
--- /dev/null
+++ b/examples/service_hook_example/CMakeLists.txt
@@ -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()
http://git-wip-us.apache.org/repos/asf/celix/blob/ef3ef52a/examples/service_hook_example/private/src/activator.c
----------------------------------------------------------------------
diff --git a/examples/service_hook_example/private/src/activator.c b/examples/service_hook_example/private/src/activator.c
new file mode 100644
index 0000000..77238aa
--- /dev/null
+++ b/examples/service_hook_example/private/src/activator.c
@@ -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;
+}