CELIX-237: Split launcher in a main.c and launcher.c/.h. Trying to reuse the launcher for cpputests
Project: http://git-wip-us.apache.org/repos/asf/celix/repo Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/2f56576a Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/2f56576a Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/2f56576a Branch: refs/heads/develop Commit: 2f56576ae7f4bb006b8c22a776812dd0907abe6f Parents: 3d7f764 Author: Pepijn Noltes <[email protected]> Authored: Fri Jul 31 16:57:09 2015 +0200 Committer: Pepijn Noltes <[email protected]> Committed: Fri Jul 31 16:57:09 2015 +0200 ---------------------------------------------------------------------- framework/private/src/properties.c | 13 +- framework/public/include/properties.h | 3 + launcher/CMakeLists.txt | 6 +- launcher/private/src/launcher.c | 225 ++++++++++++++--------------- launcher/private/src/main.c | 62 ++++++++ launcher/public/include/launcher.h | 35 +++++ 6 files changed, 220 insertions(+), 124 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/celix/blob/2f56576a/framework/private/src/properties.c ---------------------------------------------------------------------- diff --git a/framework/private/src/properties.c b/framework/private/src/properties.c index 38ed214..14455c4 100644 --- a/framework/private/src/properties.c +++ b/framework/private/src/properties.c @@ -47,9 +47,14 @@ void properties_destroy(properties_pt properties) { } properties_pt properties_load(char *filename) { - properties_pt props = NULL; - FILE *file = fopen ( filename, "r" ); + FILE *file = fopen(filename, "r"); + properties_pt props = properties_loadWithStream(file); + fclose(file); + return props; +} +FRAMEWORK_EXPORT properties_pt properties_loadWithStream(FILE *file) { + properties_pt props = NULL; char line[1024]; char key[1024]; char value[1024]; @@ -110,7 +115,7 @@ properties_pt properties_load(char *filename) { } } else if (line[linePos] == '\\') { if (precedingCharIsBackslash) { //double backslash -> backslash - output[outputPos++] = '\\'; + output[outputPos++] = '\\'; } precedingCharIsBackslash = true; } else { //normal character @@ -128,8 +133,6 @@ properties_pt properties_load(char *filename) { hashMap_put(props, strdup(utils_stringTrim(key)), strdup(utils_stringTrim(value))); } } - - fclose(file); } return props; http://git-wip-us.apache.org/repos/asf/celix/blob/2f56576a/framework/public/include/properties.h ---------------------------------------------------------------------- diff --git a/framework/public/include/properties.h b/framework/public/include/properties.h index 36297a5..c4803cb 100644 --- a/framework/public/include/properties.h +++ b/framework/public/include/properties.h @@ -27,6 +27,8 @@ #ifndef PROPERTIES_H_ #define PROPERTIES_H_ +#include <stdio.h> + #include "hash_map.h" #include "framework_exports.h" @@ -35,6 +37,7 @@ typedef hash_map_pt properties_pt; FRAMEWORK_EXPORT properties_pt properties_create(void); FRAMEWORK_EXPORT void properties_destroy(properties_pt properties); FRAMEWORK_EXPORT properties_pt properties_load(char * filename); +FRAMEWORK_EXPORT properties_pt properties_loadWithStream(FILE *stream); FRAMEWORK_EXPORT void properties_store(properties_pt properties, char * file, char * header); FRAMEWORK_EXPORT char * properties_get(properties_pt properties, char * key); http://git-wip-us.apache.org/repos/asf/celix/blob/2f56576a/launcher/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 27d1682..6001eaf 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -17,8 +17,12 @@ celix_subproject(LAUNCHER "Option to build the launcher" "ON" DEPS UTILS FRAMEWORK) if (LAUNCHER) find_package(CURL REQUIRED) + + include_directories(public/include) - add_executable(celix private/src/launcher) + add_executable(celix + private/src/main + private/src/launcher) target_link_libraries(celix celix_framework ${CURL_LIBRARIES}) include_directories("${PROJECT_SOURCE_DIR}/utils/public/include") http://git-wip-us.apache.org/repos/asf/celix/blob/2f56576a/launcher/private/src/launcher.c ---------------------------------------------------------------------- diff --git a/launcher/private/src/launcher.c b/launcher/private/src/launcher.c index f334057..6d09fc4 100644 --- a/launcher/private/src/launcher.c +++ b/launcher/private/src/launcher.c @@ -23,6 +23,9 @@ * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> * \copyright Apache License, Version 2.0 */ + +#include "launcher.h" + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -34,33 +37,48 @@ #include "framework.h" #include "linked_list_iterator.h" -#define DEFAULT_CONFIG_FILE "config.properties" - -void launcher_shutdown(int signal); +static void launcher_shutdown(int signal); -int running = 0; - -struct framework * framework; +static struct framework * framework; #ifdef WITH_APR -apr_pool_t *memoryPool; +static apr_pool_t *memoryPool; #endif -void show_usage(char* prog_name); +int celixLauncher_launch(const char *configFile) { + int status = 0; + FILE *config = fopen(configFile, "r"); + if (config != NULL) { + status = celixLauncher_launchWithStream(config); + } else { + fprintf(stderr, "Error: invalid or non-existing configuration file: '%s'.", configFile); + perror(""); + status = 1; + } + return status; +} + +int celixLauncher_launchWithStream(FILE *stream) { + int status = 0; + + properties_pt config = properties_loadWithStream(stream); + fclose(stream); + // Make sure we've read it and that nothing went wrong with the file access... + if (config == NULL) { + fprintf(stderr, "Error: invalid configuration file"); + perror(NULL); + status = 1; + } -int main(int argc, char *argv[]) { - // Set signal handler + // Set signal handler #ifdef WITH_APR apr_status_t rv; apr_status_t s; #endif - properties_pt config = NULL; - char *autoStart = NULL; - bundle_pt fwBundle = NULL; - (void) signal(SIGINT, launcher_shutdown); + (void) signal(SIGINT, launcher_shutdown); - // Before doing anything else, let's setup Curl - curl_global_init(CURL_GLOBAL_NOTHING); + // Before doing anything else, let's setup Curl + curl_global_init(CURL_GLOBAL_NOTHING); #ifdef WITH_APR rv = apr_initialize(); @@ -74,128 +92,99 @@ int main(int argc, char *argv[]) { } #endif - // Perform some minimal command-line option parsing... - char* opt = NULL; - if (argc > 1) { - opt = argv[1]; - } - - char* config_file = NULL; - - if (opt) { - // Check whether the user wants some help... - if (strcmp("-h", opt) == 0 || strcmp("-help", opt) == 0) { - show_usage(argv[0]); - return 0; - } else { - config_file = opt; - } - } else { - config_file = DEFAULT_CONFIG_FILE; - } - - config = properties_load(config_file); - // Make sure we've read it and that nothing went wrong with the file access... - if (config == NULL) { - printf("Error: invalid or non-existing configuration file: \"%s\"!\n", config_file); - perror(NULL); - show_usage(argv[0]); - return CELIX_START_ERROR; - } - - autoStart = properties_get(config, "cosgi.auto.start.1"); - framework = NULL; - celix_status_t status; + + if (status == 0) { + char *autoStart = properties_get(config, "cosgi.auto.start.1"); + framework = NULL; + celix_status_t status; #ifdef WITH_APR - status = framework_create(&framework, memoryPool, config); + status = framework_create(&framework, memoryPool, config); #else - status = framework_create(&framework, config); + status = framework_create(&framework, config); #endif - if (status == CELIX_SUCCESS) { - status = fw_init(framework); - if (status == CELIX_SUCCESS) { - // Start the system bundle - framework_getFrameworkBundle(framework, &fwBundle); - bundle_start(fwBundle); - - char delims[] = " "; - char *result = NULL; - char *save_ptr = 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(&bundles); - result = strtok_r(autoStart, delims, &save_ptr); - while (result != NULL) { - char * location = strdup(result); - linkedList_addElement(bundles, location); - result = strtok_r(NULL, delims, &save_ptr); - } - // First install all bundles - // Afterwards start them - arrayList_create(&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, ¤t) == CELIX_SUCCESS) { - // Only add bundle if it is installed correctly - arrayList_add(installed, current); - } else { - printf("Could not install bundle from %s\n", location); + bundle_pt fwBundle = NULL; + if (status == CELIX_SUCCESS) { + status = fw_init(framework); + if (status == CELIX_SUCCESS) { + // Start the system bundle + framework_getFrameworkBundle(framework, &fwBundle); + bundle_start(fwBundle); + + char delims[] = " "; + char *result = NULL; + char *save_ptr = 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(&bundles); + result = strtok_r(autoStart, delims, &save_ptr); + while (result != NULL) { + char *location = strdup(result); + linkedList_addElement(bundles, location); + result = strtok_r(NULL, delims, &save_ptr); } - linkedListIterator_remove(iter); - free(location); - } - linkedListIterator_destroy(iter); - linkedList_destroy(bundles); + // First install all bundles + // Afterwards start them + arrayList_create(&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, ¤t) == CELIX_SUCCESS) { + // Only add bundle if it is installed correctly + arrayList_add(installed, current); + } else { + printf("Could not install bundle from %s\n", location); + } + linkedListIterator_remove(iter); + free(location); + } + linkedListIterator_destroy(iter); + linkedList_destroy(bundles); - for (i = 0; i < arrayList_size(installed); i++) { - bundle_pt installedBundle = (bundle_pt) arrayList_get(installed, i); - bundle_startWithOptions(installedBundle, 0); - } + for (i = 0; i < arrayList_size(installed); i++) { + bundle_pt installedBundle = (bundle_pt) arrayList_get(installed, i); + bundle_startWithOptions(installedBundle, 0); + } - arrayList_destroy(installed); + arrayList_destroy(installed); - framework_waitForStop(framework); - framework_destroy(framework); - properties_destroy(config); - } - } + framework_waitForStop(framework); + framework_destroy(framework); + properties_destroy(config); + } + } - if (status != CELIX_SUCCESS) { - printf("Problem creating framework\n"); - } + if (status != CELIX_SUCCESS) { + printf("Problem creating framework\n"); + } #ifdef WITH_APR apr_pool_destroy(memoryPool); apr_terminate(); #endif - // Cleanup Curl - curl_global_cleanup(); + // Cleanup Curl + curl_global_cleanup(); - printf("Launcher: Exit\n"); + printf("Launcher: Exit\n"); + } - return 0; + return status; } -void launcher_shutdown(int signal) { - bundle_pt fwBundle = NULL; - framework_getFrameworkBundle(framework, &fwBundle); - bundle_stop(fwBundle); +static void launcher_shutdown(int signal) { + bundle_pt fwBundle = NULL; + framework_getFrameworkBundle(framework, &fwBundle); + bundle_stop(fwBundle); // if (framework_waitForStop(framework) != CELIX_SUCCESS) { // celix_log("Error waiting for stop."); // } // framework_destroy(framework); } - -void show_usage(char* prog_name) { - printf("Usage:\n %s [path/to/config.properties]\n\n", basename(prog_name)); -} http://git-wip-us.apache.org/repos/asf/celix/blob/2f56576a/launcher/private/src/main.c ---------------------------------------------------------------------- diff --git a/launcher/private/src/main.c b/launcher/private/src/main.c new file mode 100644 index 0000000..0a59bc4 --- /dev/null +++ b/launcher/private/src/main.c @@ -0,0 +1,62 @@ +/** + *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. + */ +/* + * main.c + * + * \date Jul 30, 2015 + * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ + +#include <string.h> +#include <curl/curl.h> +#include <signal.h> +#include "launcher.h" + +static void show_usage(char* prog_name); + +#define DEFAULT_CONFIG_FILE "config.properties" + +int main(int argc, char *argv[]) { + // Perform some minimal command-line option parsing... + char *opt = NULL; + if (argc > 1) { + opt = argv[1]; + } + + char *config_file = NULL; + + if (opt) { + // Check whether the user wants some help... + if (strcmp("-h", opt) == 0 || strcmp("-help", opt) == 0) { + show_usage(argv[0]); + return 0; + } else { + config_file = opt; + } + } else { + config_file = DEFAULT_CONFIG_FILE; + } + + celixLauncher_launch(config_file); +} + +static void show_usage(char* prog_name) { + printf("Usage:\n %s [path/to/config.properties]\n\n", basename(prog_name)); +} http://git-wip-us.apache.org/repos/asf/celix/blob/2f56576a/launcher/public/include/launcher.h ---------------------------------------------------------------------- diff --git a/launcher/public/include/launcher.h b/launcher/public/include/launcher.h new file mode 100644 index 0000000..296df7c --- /dev/null +++ b/launcher/public/include/launcher.h @@ -0,0 +1,35 @@ +/** + *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. + */ +/* + * launcher.h + * + * \date Jul 30, 2015 + * \author <a href="mailto:[email protected]">Apache Celix Project Team</a> + * \copyright Apache License, Version 2.0 + */ + +#ifndef CELIX_LAUNCHER_H +#define CELIX_LAUNCHER_H + +#include <stdio.h> + +int celixLauncher_launch(const char *configFile); +int celixLauncher_launchWithStream(FILE *config); + +#endif //CELIX_LAUNCHER_H
