You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2015/11/03 10:09:37 UTC
[03/11] celix git commit: CELIX-230: * Updated all commands to use
new structure and service registration. * Moved static global field to shell
activator instance. * (Re)implemented getUsage and getDescription functions
of the shell.
CELIX-230:
* Updated all commands to use new structure and service registration.
* Moved static global field to shell activator instance.
* (Re)implemented getUsage and getDescription functions of the shell.
Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/4d52743e
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/4d52743e
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/4d52743e
Branch: refs/heads/develop
Commit: 4d52743eee05782afd0cb8b1b3f9d2de7808d1aa
Parents: 595f3e9
Author: Alexander Broekhuis <a....@gmail.com>
Authored: Fri Mar 27 22:01:30 2015 +0100
Committer: Alexander Broekhuis <a....@gmail.com>
Committed: Fri Mar 27 22:01:30 2015 +0100
----------------------------------------------------------------------
shell/CMakeLists.txt | 14 ++--
shell/private/include/shell_private.h | 2 +-
shell/private/src/activator.c | 114 ++++++++++-----------------
shell/private/src/help_command.c | 44 +++--------
shell/private/src/inspect_command.c | 121 ++++++++++-------------------
shell/private/src/install_command.c | 49 +++---------
shell/private/src/log_command.c | 47 +++--------
shell/private/src/ps_command.c | 5 +-
shell/private/src/shell.c | 55 ++++++++++---
shell/private/src/stop_command.c | 34 +++-----
shell/private/src/uninstall_command.c | 33 ++------
shell/private/src/update_command.c | 42 +++-------
shell/public/include/shell.h | 1 +
13 files changed, 194 insertions(+), 367 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/shell/CMakeLists.txt b/shell/CMakeLists.txt
index 09777d3..66194ce 100644
--- a/shell/CMakeLists.txt
+++ b/shell/CMakeLists.txt
@@ -28,13 +28,13 @@ if (SHELL)
private/src/shell
private/src/ps_command
private/src/start_command
- #private/src/stop_command
- #private/src/install_command
- #private/src/update_command
- #private/src/uninstall_command
- #private/src/log_command
- #private/src/inspect_command
- #private/src/help_command
+ private/src/stop_command
+ private/src/install_command
+ private/src/update_command
+ private/src/uninstall_command
+ private/src/log_command
+ private/src/inspect_command
+ private/src/help_command
)
install_bundle(shell
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/include/shell_private.h
----------------------------------------------------------------------
diff --git a/shell/private/include/shell_private.h b/shell/private/include/shell_private.h
index db609ca..9734ad5 100644
--- a/shell/private/include/shell_private.h
+++ b/shell/private/include/shell_private.h
@@ -45,7 +45,7 @@ celix_status_t shell_addCommand(shell_pt shell, service_reference_pt reference);
char * shell_getCommandUsage(shell_pt shell, char * commandName);
char * shell_getCommandDescription(shell_pt shell, char * commandName);
-service_reference_pt shell_getCommandReference(shell_pt shell, char * command);
+celix_status_t shell_getCommandReference(shell_pt shell_ptr, char *command_name_str, service_reference_pt *command_reference_ptr);
void shell_executeCommand(shell_pt shell, char * commandLine, void (*out)(char *), void (*error)(char *));
void shell_serviceChanged(service_listener_pt listener, service_event_pt event);
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/activator.c
----------------------------------------------------------------------
diff --git a/shell/private/src/activator.c b/shell/private/src/activator.c
index ce01b59..cfa8bb3 100644
--- a/shell/private/src/activator.c
+++ b/shell/private/src/activator.c
@@ -24,16 +24,12 @@
* \copyright Apache License, Version 2.0
*/
#include <stdlib.h>
-#include <string.h>
#include "shell_private.h"
#include "bundle_activator.h"
-#include "bundle_context.h"
-#include "service_registration.h"
-#include "service_listener.h"
-#include "utils.h"
#include "std_commands.h"
-#include "properties.h"
+
+#define NUMBER_OF_COMMANDS 10
struct command {
celix_status_t (*exec)(void *handle, char *commandLine, FILE *out, FILE *err);
@@ -45,55 +41,18 @@ struct command {
properties_pt props;
};
-static struct command std_commands[] = {
- {psCommand_execute, "ps", "list installed bundles.", "ps [-l | -s | -u]", NULL, NULL, NULL},
- {startCommand_execute, "start", "start bundle(s).", "start <id> [<id> ...]", NULL, NULL, NULL},
- {NULL, NULL, NULL, NULL, NULL, NULL, NULL} /*marker for last element*/
-};
-
-
struct bundle_instance {
shell_service_pt shellService;
service_registration_pt registration;
service_listener_pt listener;
- /*
- ps
-
- service_registration_pt stopCommand;
- command_pt stopCmd;
- command_service_pt stopCmdSrv;
-
- service_registration_pt installCommand;
- command_pt installCmd;
- command_service_pt installCmdSrv;
-
- service_registration_pt uninstallCommand;
- command_pt uninstallCmd;
- command_service_pt uninstallCmdSrv;
-
- service_registration_pt updateCommand;
- command_pt updateCmd;
- command_service_pt updateCmdSrv;
-
- service_registration_pt logCommand;
- command_pt logCmd;
- command_service_pt logCmdSrv;
-
- service_registration_pt inspectCommand;
- command_pt inspectCmd;
- command_service_pt inspectCmdSrv;
-
- service_registration_pt helpCommand;
- command_pt helpCmd;
- command_service_pt helpCmdSrv;
- */
+ struct command std_commands[NUMBER_OF_COMMANDS];
};
typedef struct bundle_instance *bundle_instance_pt;
celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
- celix_status_t status = CELIX_SUCCESS;
+ celix_status_t status;
bundle_instance_pt bi = (bundle_instance_pt) calloc(1, sizeof(struct bundle_instance));
@@ -107,18 +66,29 @@ celix_status_t bundleActivator_create(bundle_context_pt context, void **userData
else {
status = shell_create(context, &bi->shellService);
- int i = 0;
- while (std_commands[i].exec != NULL) {
- std_commands[i].props = properties_create();
- if (std_commands[i].props != NULL) {
- properties_set(std_commands[i].props, "command.name", std_commands[i].name);
- properties_set(std_commands[i].props, "command.usage", std_commands[i].usage);
- properties_set(std_commands[i].props, "command.description", std_commands[i].description);
-
- std_commands[i].service = calloc(1, sizeof(struct commandService));
- if (std_commands[i].service != NULL) {
- std_commands[i].service->handle = context;
- std_commands[i].service->executeCommand = std_commands[i].exec;
+ bi->std_commands[0] = (struct command) {.exec = psCommand_execute, .name = "ps", .description = "list installed bundles.", .usage = "ps [-l | -s | -u]"};
+ bi->std_commands[1] = (struct command) {.exec = startCommand_execute, .name = "start", .description = "start bundle(s).", .usage = "start <id> [<id> ...]"};
+ bi->std_commands[2] = (struct command) {.exec = stopCommand_execute, .name = "stop", .description = "stop bundle(s).", .usage = "stop <id> [<id> ...]"};
+ bi->std_commands[3] = (struct command) {.exec = installCommand_execute, .name = "install", .description = "install bundle(s).", .usage = "install <file> [<file> ...]"};
+ bi->std_commands[4] = (struct command) {.exec = uninstallCommand_execute, .name = "uninstall", .description = "uninstall bundle(s).", .usage = "uninstall <file> [<file> ...]"};
+ bi->std_commands[5] = (struct command) {.exec = updateCommand_execute, .name = "update", .description = "update bundle(s).", .usage = "update <id> [<URL>]"};
+ bi->std_commands[6] = (struct command) {.exec = helpCommand_execute, .name = "help", .description = "display available commands and description.", .usage = "help <command>]"};
+ bi->std_commands[7] = (struct command) {.exec = logCommand_execute, .name = "log", .description = "print log.", .usage = "log"};
+ bi->std_commands[8] = (struct command) {.exec = inspectCommand_execute, .name = "inspect", .description = "inspect services and components.", .usage = "inspect (service) (capability|requirement) [<id> ...]"};
+ bi->std_commands[9] = (struct command) {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; /*marker for last element*/
+
+ int i = 0;
+ while (bi->std_commands[i].exec != NULL) {
+ bi->std_commands[i].props = properties_create();
+ if (bi->std_commands[i].props != NULL) {
+ properties_set(bi->std_commands[i].props, "command.name", bi->std_commands[i].name);
+ properties_set(bi->std_commands[i].props, "command.usage", bi->std_commands[i].usage);
+ properties_set(bi->std_commands[i].props, "command.description", bi->std_commands[i].description);
+
+ bi->std_commands[i].service = calloc(1, sizeof(struct commandService));
+ if (bi->std_commands[i].service != NULL) {
+ bi->std_commands[i].service->handle = context;
+ bi->std_commands[i].service->executeCommand = bi->std_commands[i].exec;
} else {
status = CELIX_ENOMEM;
break;
@@ -142,7 +112,7 @@ celix_status_t bundleActivator_create(bundle_context_pt context, void **userData
}
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
- celix_status_t status = CELIX_SUCCESS;
+ celix_status_t status;
bundle_instance_pt bi = (bundle_instance_pt) userData;
status = bundleContext_registerService(context, (char *) OSGI_SHELL_SERVICE_NAME, bi->shellService, NULL, &bi->registration);
@@ -169,8 +139,8 @@ celix_status_t bundleActivator_start(void * userData, bundle_context_pt context)
if (status == CELIX_SUCCESS) {
int i = 0;
- while (std_commands[i].exec != NULL) {
- status = bundleContext_registerService(context, (char *)OSGI_SHELL_COMMAND_SERVICE_NAME, std_commands[i].service, std_commands[i].props, &std_commands[i].reg);
+ while (bi->std_commands[i].exec != NULL) {
+ status = bundleContext_registerService(context, (char *)OSGI_SHELL_COMMAND_SERVICE_NAME, bi->std_commands[i].service, bi->std_commands[i].props, &bi->std_commands[i].reg);
if (status != CELIX_SUCCESS) {
break;
}
@@ -185,15 +155,15 @@ celix_status_t bundleActivator_start(void * userData, bundle_context_pt context)
}
celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
- celix_status_t status = CELIX_SUCCESS;
+ celix_status_t status;
bundle_instance_pt bi = (bundle_instance_pt) userData;
- //serviceRegistration_unregister(bi->registration);
int i = 0;
- while (std_commands[i].exec != NULL) {
- if (std_commands[i].reg!= NULL) {
- serviceRegistration_unregister(std_commands[i].reg);
- std_commands[i].reg = NULL;
+ while (bi->std_commands[i].exec != NULL) {
+ if (bi->std_commands[i].reg!= NULL) {
+ serviceRegistration_unregister(bi->std_commands[i].reg);
+ bi->std_commands[i].reg = NULL;
+ bi->std_commands[i].props = NULL;
}
i += 1;
}
@@ -207,19 +177,17 @@ celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt contex
bundle_instance_pt bi = (bundle_instance_pt) userData;
int i = 0;
- while (std_commands[i].exec != NULL) {
- if (std_commands[i].props != NULL) {
- properties_destroy(std_commands[i].props);
+ while (bi->std_commands[i].exec != NULL) {
+ if (bi->std_commands[i].props != NULL) {
+ properties_destroy(bi->std_commands[i].props);
}
- if (std_commands[i].service != NULL) {
- free(std_commands[i].service);
+ if (bi->std_commands[i].service != NULL) {
+ free(bi->std_commands[i].service);
}
i += 1;
}
- if (bi != NULL) {
free(bi);
- }
return CELIX_SUCCESS;
}
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/help_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/help_command.c b/shell/private/src/help_command.c
index b2ed30a..75be274 100644
--- a/shell/private/src/help_command.c
+++ b/shell/private/src/help_command.c
@@ -26,42 +26,23 @@
#include <stdlib.h>
#include <string.h>
-#include "command_impl.h"
#include "array_list.h"
#include "bundle_context.h"
-#include "bundle.h"
#include "shell.h"
-void helpCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-
-command_pt helpCommand_create(bundle_context_pt context) {
- command_pt command = (command_pt) malloc(sizeof(*command));
- command->bundleContext = context;
- command->name = "help";
- command->shortDescription = "display available command usage and description.";
- command->usage = "start [<command> ...]";
- command->executeCommand = helpCommand_execute;
- return command;
-}
-
-void helpCommand_destroy(command_pt command) {
- free(command);
-}
-
-
-void helpCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
+celix_status_t helpCommand_execute(void *handle, char * line, FILE *outStream, FILE *errStream) {
+ bundle_context_pt context = handle;
service_reference_pt shellService = NULL;
- bundleContext_getServiceReference(command->bundleContext, (char *) OSGI_SHELL_SERVICE_NAME, &shellService);
+ bundleContext_getServiceReference(context, (char *) OSGI_SHELL_SERVICE_NAME, &shellService);
if (shellService != NULL) {
shell_service_pt shell = NULL;
- bundleContext_getService(command->bundleContext, shellService, (void **) &shell);
+ bundleContext_getService(context, shellService, (void **) &shell);
if (shell != NULL) {
char delims[] = " ";
char * sub = NULL;
char *save_ptr = NULL;
- char outString[256];
sub = strtok_r(line, delims, &save_ptr);
sub = strtok_r(NULL, delims, &save_ptr);
@@ -71,10 +52,9 @@ void helpCommand_execute(command_pt command, char * line, void (*out)(char *), v
array_list_pt commands = shell->getCommands(shell->shell);
for (i = 0; i < arrayList_size(commands); i++) {
char *name = arrayList_get(commands, i);
- sprintf(outString, "%s\n", name);
- out(outString);
+ fprintf(outStream, "%s\n", name);
}
- out("\nUse 'help <command-name>' for more information.\n");
+ fprintf(outStream, "\nUse 'help <command-name>' for more information.\n");
} else {
bool found = false;
while (sub != NULL) {
@@ -87,15 +67,12 @@ void helpCommand_execute(command_pt command, char * line, void (*out)(char *), v
char *usage = shell->getCommandUsage(shell->shell, name);
if (found) {
- out("---\n");
+ fprintf(outStream, "---\n");
}
found = true;
- sprintf(outString, "Command : %s\n", name);
- out(outString);
- sprintf(outString, "Usage : %s\n", usage);
- out(outString);
- sprintf(outString, "Description : %s\n", desc);
- out(outString);
+ fprintf(outStream, "Command : %s\n", name);
+ fprintf(outStream, "Usage : %s\n", usage);
+ fprintf(outStream, "Description : %s\n", desc);
}
}
sub = strtok_r(NULL, delims, &save_ptr);
@@ -103,4 +80,5 @@ void helpCommand_execute(command_pt command, char * line, void (*out)(char *), v
}
}
}
+ return CELIX_SUCCESS;
}
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/inspect_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/inspect_command.c b/shell/private/src/inspect_command.c
index bac1ed9..68b7625 100644
--- a/shell/private/src/inspect_command.c
+++ b/shell/private/src/inspect_command.c
@@ -27,41 +27,23 @@
#include <stdlib.h>
#include <string.h>
-#include "command_impl.h"
#include "array_list.h"
#include "bundle_context.h"
-#include "bundle.h"
-#include "module.h"
-#include "constants.h"
-#include "service_registration.h"
-#include "service_reference.h"
-#include "hash_map.h"
+
+#include "std_commands.h"
#define SERVICE_TYPE "service"
#define CAPABILITY "capability"
#define REQUIREMENT "requirement"
-void inspectCommand_execute(command_pt command, char * commandline, void (*out)(char *), void (*err)(char *));
-celix_status_t inspectCommand_printExportedServices(command_pt command, array_list_pt ids, void (*out)(char *), void (*err)(char *));
-celix_status_t inspectCommand_printImportedServices(command_pt command, array_list_pt ids, void (*out)(char *), void (*err)(char *));
-
-command_pt inspectCommand_create(bundle_context_pt context) {
- command_pt command = (command_pt) malloc(sizeof(*command));
- command->bundleContext = context;
- command->name = "inspect";
- command->shortDescription = "inspect dependencies";
- command->usage = "inspect (service) (capability|requirement) [<id> ...]";
- command->executeCommand = inspectCommand_execute;
- return command;
-}
-
-void inspectCommand_destroy(command_pt command) {
- free(command);
-}
+celix_status_t inspectCommand_printExportedServices(bundle_context_pt context, array_list_pt ids, FILE *outStream, FILE *errStream);
+celix_status_t inspectCommand_printImportedServices(bundle_context_pt context, array_list_pt ids, FILE *outStream, FILE *errStream);
-void inspectCommand_execute(command_pt command, char * commandline, void (*out)(char *), void (*err)(char *)) {
+celix_status_t inspectCommand_execute(void *handle, char * commandline, FILE *outStream, FILE *errStream) {
celix_status_t status = CELIX_SUCCESS;
- char outString[256];
+
+ bundle_context_pt context = handle;
+
char *token;
strtok_r(commandline, " ", &token);
char *type = strtok_r(NULL, " ", &token);
@@ -79,43 +61,36 @@ void inspectCommand_execute(command_pt command, char * commandline, void (*out)(
if (strcmp(type, SERVICE_TYPE) == 0) {
if (strcmp(direction, CAPABILITY) == 0) {
- status = inspectCommand_printExportedServices(command, ids, out, err);
+ status = inspectCommand_printExportedServices(context, ids, outStream, errStream);
if (status != CELIX_SUCCESS) {
- out("INSPECT: Error\n");
+ fprintf(errStream, "INSPECT: Error\n");
}
} else if (strcmp(direction, REQUIREMENT) == 0) {
- status = inspectCommand_printImportedServices(command, ids, out, err);
+ status = inspectCommand_printImportedServices(context, ids, outStream, errStream);
if (status != CELIX_SUCCESS) {
- out("INSPECT: Error\n");
+ fprintf(errStream, "INSPECT: Error\n");
}
} else {
- out("INSPECT: Invalid argument\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(errStream, "INSPECT: Invalid argument\n");
}
} else {
- out("INSPECT: Invalid argument\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(errStream, "INSPECT: Invalid argument\n");
}
} else {
- out("INSPECT: Too few arguments\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(errStream, "INSPECT: Too few arguments\n");
}
} else {
- out("INSPECT: Too few arguments\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(errStream, "INSPECT: Too few arguments\n");
}
+ return status;
}
-celix_status_t inspectCommand_printExportedServices(command_pt command, array_list_pt ids, void (*out)(char *), void (*err)(char *)) {
+celix_status_t inspectCommand_printExportedServices(bundle_context_pt context, array_list_pt ids, FILE *outStream, FILE *errStream) {
celix_status_t status = CELIX_SUCCESS;
array_list_pt bundles = NULL;
if (arrayList_isEmpty(ids)) {
- status = bundleContext_getBundles(command->bundleContext, &bundles);
+ status = bundleContext_getBundles(context, &bundles);
} else {
unsigned int i;
@@ -124,13 +99,11 @@ celix_status_t inspectCommand_printExportedServices(command_pt command, array_li
char *idStr = (char *) arrayList_get(ids, i);
long id = atol(idStr);
bundle_pt b = NULL;
- celix_status_t st = bundleContext_getBundleById(command->bundleContext, id, &b);
+ celix_status_t st = bundleContext_getBundleById(context, id, &b);
if (st == CELIX_SUCCESS) {
arrayList_add(bundles, b);
} else {
- char line[256];
- sprintf(line, "INSPECT: Invalid bundle ID: %ld\n", id);
- out(line);
+ fprintf(outStream, "INSPECT: Invalid bundle ID: %ld\n", id);
}
}
}
@@ -141,26 +114,24 @@ celix_status_t inspectCommand_printExportedServices(command_pt command, array_li
bundle_pt bundle = (bundle_pt) arrayList_get(bundles, i);
if (i > 0) {
- out("\n");
+ fprintf(outStream, "\n");
}
if (bundle != NULL) {
array_list_pt refs = NULL;
if (bundle_getRegisteredServices(bundle, &refs) == CELIX_SUCCESS) {
- char line[256];
module_pt module = NULL;
char * name = NULL;
status = bundle_getCurrentModule(bundle, &module);
if (status == CELIX_SUCCESS) {
status = module_getSymbolicName(module, &name);
if (status == CELIX_SUCCESS) {
- sprintf(line, "%s provides services:\n", name);
- out(line);
- out("==============\n");
+ fprintf(outStream, "%s provides services:\n", name);
+ fprintf(outStream, "==============\n");
if (refs == NULL || arrayList_size(refs) == 0) {
- out("Nothing\n");
+ fprintf(outStream, "Nothing\n");
} else {
unsigned int j = 0;
for (j = 0; j < arrayList_size(refs); j++) {
@@ -169,19 +140,18 @@ celix_status_t inspectCommand_printExportedServices(command_pt command, array_li
char **keys;
serviceReference_getPropertyKeys(ref, &keys, &size);
- for (int i = 0; i < size; i++) {
- char *key = keys[i];
+ for (int k = 0; k < size; k++) {
+ char *key = keys[k];
char *value = NULL;
serviceReference_getProperty(ref, key, &value);
- sprintf(line, "%s = %s\n", key, value);
- out(line);
+ fprintf(outStream, "%s = %s\n", key, value);
}
// objectClass = properties_get(props, (char *) OSGI_FRAMEWORK_OBJECTCLASS);
// sprintf(line, "ObjectClass = %s\n", objectClass);
if ((j + 1) < arrayList_size(refs)) {
- out("----\n");
+ fprintf(outStream, "----\n");
}
}
}
@@ -196,12 +166,12 @@ celix_status_t inspectCommand_printExportedServices(command_pt command, array_li
return status;
}
-celix_status_t inspectCommand_printImportedServices(command_pt command, array_list_pt ids, void (*out)(char *), void (*err)(char *)) {
+celix_status_t inspectCommand_printImportedServices(bundle_context_pt context, array_list_pt ids, FILE *outStream, FILE *errStream) {
celix_status_t status = CELIX_SUCCESS;
array_list_pt bundles = NULL;
if (arrayList_isEmpty(ids)) {
- status = bundleContext_getBundles(command->bundleContext, &bundles);
+ status = bundleContext_getBundles(context, &bundles);
} else {
unsigned int i;
@@ -210,13 +180,11 @@ celix_status_t inspectCommand_printImportedServices(command_pt command, array_li
char *idStr = (char *) arrayList_get(ids, i);
long id = atol(idStr);
bundle_pt b = NULL;
- celix_status_t st = bundleContext_getBundleById(command->bundleContext, id, &b);
+ celix_status_t st = bundleContext_getBundleById(context, id, &b);
if (st == CELIX_SUCCESS) {
arrayList_add(bundles, b);
} else {
- char line[256];
- sprintf(line, "INSPECT: Invalid bundle ID: %ld\n", id);
- out(line);
+ fprintf(outStream, "INSPECT: Invalid bundle ID: %ld\n", id);
}
}
}
@@ -227,31 +195,28 @@ celix_status_t inspectCommand_printImportedServices(command_pt command, array_li
bundle_pt bundle = (bundle_pt) arrayList_get(bundles, i);
if (i > 0) {
- out("\n");
+ fprintf(outStream, "\n");
}
if (bundle != NULL) {
array_list_pt refs = NULL;
if (bundle_getServicesInUse(bundle, &refs) == CELIX_SUCCESS) {
- char line[256];
module_pt module = NULL;
char * name = NULL;
status = bundle_getCurrentModule(bundle, &module);
if (status == CELIX_SUCCESS) {
status = module_getSymbolicName(module, &name);
if (status == CELIX_SUCCESS) {
- sprintf(line, "%s requires services:\n", name);
- out(line);
- out("==============\n");
+ fprintf(outStream, "%s requires services:\n", name);
+ fprintf(outStream, "==============\n");
if (refs == NULL || arrayList_size(refs) == 0) {
- out("Nothing\n");
+ fprintf(outStream, "Nothing\n");
} else {
unsigned int j = 0;
for (j = 0; j < arrayList_size(refs); j++) {
service_reference_pt ref = (service_reference_pt) arrayList_get(refs, j);
- char line[256];
bundle_pt usedBundle = NULL;
module_pt usedModule = NULL;
char *usedSymbolicName = NULL;
@@ -262,26 +227,24 @@ celix_status_t inspectCommand_printImportedServices(command_pt command, array_li
bundle_getCurrentModule(usedBundle, &usedModule);
module_getSymbolicName(usedModule, &usedSymbolicName);
- sprintf(line, "%s [%ld]\n", usedSymbolicName, usedBundleId);
- out(line);
+ fprintf(outStream, "%s [%ld]\n", usedSymbolicName, usedBundleId);
unsigned int size = 0;
char **keys;
serviceReference_getPropertyKeys(ref, &keys, &size);
- for (int i = 0; i < size; i++) {
- char *key = keys[i];
+ for (int k = 0; k < size; k++) {
+ char *key = keys[k];
char *value = NULL;
serviceReference_getProperty(ref, key, &value);
- sprintf(line, "%s = %s\n", key, value);
- out(line);
+ fprintf(outStream, "%s = %s\n", key, value);
}
// objectClass = properties_get(props, (char *) OSGI_FRAMEWORK_OBJECTCLASS);
// sprintf(line, "ObjectClass = %s\n", objectClass);
if ((j + 1) < arrayList_size(refs)) {
- out("----\n");
+ fprintf(outStream, "----\n");
}
}
}
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/install_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/install_command.c b/shell/private/src/install_command.c
index 352cbfa..067ba2b 100644
--- a/shell/private/src/install_command.c
+++ b/shell/private/src/install_command.c
@@ -26,51 +26,28 @@
#include <stdlib.h>
#include <string.h>
-#include "command_impl.h"
#include "array_list.h"
#include "bundle_context.h"
-#include "bundle_archive.h"
-#include "module.h"
-#include "bundle.h"
-#include "install_command.h"
-void installCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-void installCommand_install(command_pt command, bundle_pt *bundle, char * location, void (*out)(char *), void (*err)(char *));
+void installCommand_execute(void *handle, char * line, FILE *outStream, FILE *errStream) {
+ bundle_context_pt context = handle;
-command_pt installCommand_create(bundle_context_pt context) {
- command_pt command = (command_pt) malloc(sizeof(*command));
- command->bundleContext = context;
- command->name = "install";
- command->shortDescription = "install bundle(s).";
- command->usage = "install <file> [<file> ...]";
- command->executeCommand = installCommand_execute;
- return command;
-}
-
-void installCommand_destroy(command_pt command) {
- free(command);
-}
-
-void installCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
char delims[] = " ";
char * sub = NULL;
char *save_ptr = NULL;
char info[256];
- char outString[256];
// ignore the command
sub = strtok_r(line, delims, &save_ptr);
sub = strtok_r(NULL, delims, &save_ptr);
if (sub == NULL) {
- err("Incorrect number of arguments.\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(errStream, "Incorrect number of arguments.\n");
} else {
info[0] = '\0';
while (sub != NULL) {
bundle_pt bundle = NULL;
- installCommand_install(command, &bundle, sub, out, err);
+ bundleContext_installBundle(context, sub, &bundle);
if (bundle != NULL) {
long id;
bundle_archive_pt archive = NULL;
@@ -87,17 +64,13 @@ void installCommand_execute(command_pt command, char * line, void (*out)(char *)
sub = strtok_r(NULL, delims, &save_ptr);
}
if (strchr(info, ',') != NULL) {
- out("Bundle IDs: ");
- out(info);
- out("\n");
+ fprintf(outStream, "Bundle IDs: ");
+ fprintf(outStream, "%s", info);
+ fprintf(outStream, "\n");
} else if (strlen(info) > 0) {
- out("Bundle ID: ");
- out(info);
- out("\n");
+ fprintf(outStream, "Bundle ID: ");
+ fprintf(outStream, "%s", info);
+ fprintf(outStream, "\n");
}
}
-}
-
-void installCommand_install(command_pt command, bundle_pt *bundle, char * location, void (*out)(char *), void (*err)(char *)) {
- bundleContext_installBundle(command->bundleContext, location, bundle);
-}
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/log_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/log_command.c b/shell/private/src/log_command.c
index 403ed65..95679ca 100644
--- a/shell/private/src/log_command.c
+++ b/shell/private/src/log_command.c
@@ -27,41 +27,23 @@
#include <stdlib.h>
#include <string.h>
-#include "command_impl.h"
-#include "log_command.h"
#include "bundle_context.h"
#include "log_reader_service.h"
-#include "log_entry.h"
#include "linked_list_iterator.h"
-void logCommand_execute(command_pt command, char *line, void (*out)(char *), void (*err)(char *));
-celix_status_t logCommand_levelAsString(command_pt command, log_level_t level, char **string);
+celix_status_t logCommand_levelAsString(bundle_context_pt context, log_level_t level, char **string);
-command_pt logCommand_create(bundle_context_pt context) {
- command_pt command = (command_pt) malloc(sizeof(*command));
- command->bundleContext = context;
- command->name = "log";
- command->shortDescription = "print log";
- command->usage = "log";
- command->executeCommand = logCommand_execute;
- return command;
-}
-
-void logCommand_destroy(command_pt command) {
- free(command);
-}
-
-void logCommand_execute(command_pt command, char *line, void (*out)(char *), void (*err)(char *)) {
+void logCommand_execute(bundle_context_pt context, char *line, FILE *outStream, FILE *errStream) {
service_reference_pt readerService = NULL;
- bundleContext_getServiceReference(command->bundleContext, (char *) OSGI_LOGSERVICE_READER_SERVICE_NAME, &readerService);
+ bundleContext_getServiceReference(context, (char *) OSGI_LOGSERVICE_READER_SERVICE_NAME, &readerService);
if (readerService != NULL) {
linked_list_pt list = NULL;
linked_list_iterator_pt iter = NULL;
log_reader_service_pt reader = NULL;
- bundleContext_getService(command->bundleContext, readerService, (void **) &reader);
+ bundleContext_getService(context, readerService, (void **) &reader);
reader->getLog(reader->reader, &list);
iter = linkedListIterator_create(list, 0);
while (linkedListIterator_hasNext(iter)) {
@@ -70,33 +52,26 @@ void logCommand_execute(command_pt command, char *line, void (*out)(char *), voi
char *level = NULL;
char errorString[256];
- size_t timeLength = strftime(time, 20, "%Y-%m-%d %H:%M:%S", localtime(&entry->time));
- logCommand_levelAsString(command, entry->level, &level);
+ logCommand_levelAsString(context, entry->level, &level);
if (entry->errorCode > 0) {
celix_strerror(entry->errorCode, errorString, 256);
- size_t length = timeLength + strlen(entry->bundleSymbolicName) + strlen(entry->message) + strlen(errorString) + 40;
- char line[length];
- snprintf(line, length, "%s - Bundle: %s - %s - %d %s\n", time, entry->bundleSymbolicName, entry->message, entry->errorCode, errorString);
- out(line);
+ fprintf(outStream, "%s - Bundle: %s - %s - %d %s\n", time, entry->bundleSymbolicName, entry->message, entry->errorCode, errorString);
} else {
- size_t length = timeLength + strlen(entry->bundleSymbolicName) + strlen(entry->message) + 20;
- char line[length];
- snprintf(line, length, "%s - Bundle: %s - %s\n", time, entry->bundleSymbolicName, entry->message);
- out(line);
+ fprintf(outStream, "%s - Bundle: %s - %s\n", time, entry->bundleSymbolicName, entry->message);
}
}
linkedListIterator_destroy(iter);
linkedList_destroy(list);
bool result = true;
- bundleContext_ungetService(command->bundleContext, readerService, &result);
- bundleContext_ungetServiceReference(command->bundleContext, readerService);
+ bundleContext_ungetService(context, readerService, &result);
+ bundleContext_ungetServiceReference(context, readerService);
} else {
- out("No log reader available\n");
+ fprintf(outStream, "No log reader available\n");
}
}
-celix_status_t logCommand_levelAsString(command_pt command, log_level_t level, char **string) {
+celix_status_t logCommand_levelAsString(bundle_context_pt context, log_level_t level, char **string) {
switch (level) {
case OSGI_LOGSERVICE_ERROR:
*string = "ERROR";
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/ps_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/ps_command.c b/shell/private/src/ps_command.c
index 7511391..ce84e99 100644
--- a/shell/private/src/ps_command.c
+++ b/shell/private/src/ps_command.c
@@ -28,9 +28,8 @@
#include "array_list.h"
#include "bundle_context.h"
-#include "bundle_archive.h"
-#include "module.h"
-#include "bundle.h"
+
+#include "std_commands.h"
static char * psCommand_stateString(bundle_state_e state);
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/shell.c
----------------------------------------------------------------------
diff --git a/shell/private/src/shell.c b/shell/private/src/shell.c
index 4b3e494..3500d89 100644
--- a/shell/private/src/shell.c
+++ b/shell/private/src/shell.c
@@ -54,6 +54,7 @@ celix_status_t shell_create(bundle_context_pt context, shell_service_pt* shellSe
lclService->getCommands = shell_getCommands;
lclService->getCommandDescription = shell_getCommandDescription;
lclService->getCommandUsage = shell_getCommandUsage;
+ lclService->getCommandReference = shell_getCommandReference;
lclService->executeCommand = shell_executeCommand;
*shellService = lclService;
@@ -125,21 +126,51 @@ array_list_pt shell_getCommands(shell_pt shell) {
}
char * shell_getCommandUsage(shell_pt shell, char * commandName) {
- //command_service_pt command = hashMap_get(shell->commandNameMap, commandName);
- // char *usage = NULL;
- // bundleContext_getProperty(shell->bundleContext, "command.usage", &usage);
- //return (command == NULL || usage == NULL) ? NULL : usage;
- return "TODO";
+ char *usage = NULL;
+ service_reference_pt reference = NULL;
+ shell_getCommandReference(shell, commandName, &reference);
+ if (reference) {
+ serviceReference_getProperty(reference, "command.usage", &usage);
+ }
+
+ return usage;
}
char * shell_getCommandDescription(shell_pt shell, char * commandName) {
- /*
- command_service_pt command = hashMap_get(shell->commandNameMap, commandName);
- char *desc = NULL;
- bundleContext_getProperty(shell->bundleContext, "command.description", &desc);
- return (command == NULL || desc == NULL) ? NULL : desc;
- */
- return "TODO";
+ char *description = NULL;
+ service_reference_pt reference = NULL;
+ shell_getCommandReference(shell, commandName, &reference);
+ if (reference) {
+ serviceReference_getProperty(reference, "command.description", &description);
+ }
+
+ return description;
+}
+
+celix_status_t shell_getCommandReference(shell_pt shell_ptr, char *command_name_str, service_reference_pt *command_reference_ptr) {
+ celix_status_t status = CELIX_SUCCESS;
+
+ if (!shell_ptr || !command_name_str || !command_reference_ptr) {
+ status = CELIX_ILLEGAL_ARGUMENT;
+ }
+
+ if (status == CELIX_SUCCESS) {
+ *command_reference_ptr = NULL;
+ hash_map_iterator_pt iter = hashMapIterator_create(shell_ptr->commandReferenceMap);
+ while (hashMapIterator_hasNext(iter)) {
+ hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);
+ service_reference_pt reference = hashMapEntry_getKey(entry);
+ char *name_str = NULL;
+ serviceReference_getProperty(reference, "command.name", &name_str);
+ if (strcmp(name_str, command_name_str) == 0) {
+ *command_reference_ptr = (service_reference_pt) hashMapEntry_getKey(entry);
+ break;
+ }
+ }
+ hashMapIterator_destroy(iter);
+ }
+
+ return status;
}
void shell_executeCommand(shell_pt shell, char * commandLine, void (*out)(char *), void (*error)(char *)) {
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/stop_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/stop_command.c b/shell/private/src/stop_command.c
index 2529d49..a790c74 100644
--- a/shell/private/src/stop_command.c
+++ b/shell/private/src/stop_command.c
@@ -26,41 +26,23 @@
#include <stdlib.h>
#include <string.h>
-#include "command_impl.h"
#include "array_list.h"
#include "bundle_context.h"
#include "bundle.h"
#include "utils.h"
-void stopCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-
-command_pt stopCommand_create(bundle_context_pt context) {
- command_pt command = (command_pt) malloc(sizeof(*command));
- command->bundleContext = context;
- command->name = "stop";
- command->shortDescription = "stop bundle(s).";
- command->usage = "start <id> [<id> ...]";
- command->executeCommand = stopCommand_execute;
- return command;
-}
-
-void stopCommand_destroy(command_pt command) {
- free(command);
-}
-
-void stopCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
+celix_status_t stopCommand_execute(void *handle, char *line, FILE *outStream, FILE *errStream) {
+ celix_status_t status = CELIX_SUCCESS;
+ bundle_context_pt context = handle;
char delims[] = " ";
char * sub = NULL;
char *save_ptr = NULL;
- char outString[256];
sub = strtok_r(line, delims, &save_ptr);
sub = strtok_r(NULL, delims, &save_ptr);
if (sub == NULL) {
- err("Incorrect number of arguments.\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(outStream, "Incorrect number of arguments.\n");
} else {
while (sub != NULL) {
bool numeric;
@@ -68,16 +50,18 @@ void stopCommand_execute(command_pt command, char * line, void (*out)(char *), v
if (numeric) {
long id = atol(sub);
bundle_pt bundle = NULL;
- bundleContext_getBundleById(command->bundleContext, id, &bundle);
+ bundleContext_getBundleById(context, id, &bundle);
if (bundle != NULL) {
bundle_stopWithOptions(bundle, 0);
} else {
- err("Bundle id is invalid.");
+ fprintf(outStream, "Bundle id is invalid.");
}
} else {
- err("Bundle id should be a number (bundle id).\n");
+ fprintf(outStream, "Bundle id should be a number (bundle id).\n");
}
sub = strtok_r(NULL, delims, &save_ptr);
}
}
+
+ return status;
}
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/uninstall_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/uninstall_command.c b/shell/private/src/uninstall_command.c
index da57319..973adf2 100644
--- a/shell/private/src/uninstall_command.c
+++ b/shell/private/src/uninstall_command.c
@@ -26,53 +26,32 @@
#include <stdlib.h>
#include <string.h>
-#include "command_impl.h"
#include "array_list.h"
#include "bundle_context.h"
-#include "bundle.h"
-#include "uninstall_command.h"
-void uninstallCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-
-command_pt uninstallCommand_create(bundle_context_pt context) {
- command_pt command = (command_pt) malloc(sizeof(*command));
- command->bundleContext = context;
- command->name = "uninstall";
- command->shortDescription = "uninstall bundle(s).";
- command->usage = "uninstall <id> [<id> ...]";
- command->executeCommand = uninstallCommand_execute;
- return command;
-}
-
-void uninstallCommand_destroy(command_pt command) {
- free(command);
-}
-
-
-void uninstallCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
+celix_status_t uninstallCommand_execute(void *handle, char * line, FILE *outStream, FILE *errStream) {
+ bundle_context_pt context = handle;
char delims[] = " ";
char * sub = NULL;
char *save_ptr = NULL;
- char outString[256];
sub = strtok_r(line, delims, &save_ptr);
sub = strtok_r(NULL, delims, &save_ptr);
if (sub == NULL) {
- err("Incorrect number of arguments.\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(errStream, "Incorrect number of arguments.\n");
} else {
while (sub != NULL) {
long id = atol(sub);
bundle_pt bundle = NULL;
- bundleContext_getBundleById(command->bundleContext, id, &bundle);
+ bundleContext_getBundleById(context, id, &bundle);
if (bundle != NULL) {
bundle_uninstall(bundle);
} else {
- err("Bundle id is invalid.");
+ fprintf(errStream, "Bundle id is invalid.");
}
sub = strtok_r(NULL, delims, &save_ptr);
}
}
+ return CELIX_SUCCESS;
}
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/private/src/update_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/update_command.c b/shell/private/src/update_command.c
index 9ddfba0..a0af0b0 100644
--- a/shell/private/src/update_command.c
+++ b/shell/private/src/update_command.c
@@ -27,50 +27,28 @@
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
-#include <curl/easy.h>
-#include "command_impl.h"
#include "array_list.h"
#include "bundle_context.h"
-#include "bundle.h"
-#include "update_command.h"
-void updateCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
-celix_status_t updateCommand_download(command_pt command, char * url, char **inputFile);
+celix_status_t updateCommand_download(bundle_context_pt context, char * url, char **inputFile);
size_t updateCommand_writeData(void *ptr, size_t size, size_t nmemb, FILE *stream);
-command_pt updateCommand_create(bundle_context_pt context) {
- command_pt command = (command_pt) malloc(sizeof(*command));
- command->bundleContext = context;
- command->name = "update";
- command->shortDescription = "update bundle.";
- command->usage = "update <id> [<URL>]";
- command->executeCommand = updateCommand_execute;
- return command;
-}
-
-void updateCommand_destroy(command_pt command) {
- free(command);
-}
-
-
-void updateCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
+void updateCommand_execute(void *handle, char * line, FILE *outStream, FILE *errStream) {
+ bundle_context_pt context = handle;
bundle_pt bundle = NULL;
char delims[] = " ";
char * sub = NULL;
char *save_ptr = NULL;
- char outString[256];
sub = strtok_r(line, delims, &save_ptr);
sub = strtok_r(NULL, delims, &save_ptr);
if (sub == NULL) {
- err("Incorrect number of arguments.\n");
- sprintf(outString, "%s\n", command->usage);
- out(outString);
+ fprintf(errStream, "Incorrect number of arguments.\n");
} else {
long id = atol(sub);
- bundleContext_getBundleById(command->bundleContext, id, &bundle);
+ bundleContext_getBundleById(context, id, &bundle);
if (bundle != NULL) {
char inputFile[256];
sub = strtok_r(NULL, delims, &save_ptr);
@@ -79,24 +57,22 @@ void updateCommand_execute(command_pt command, char * line, void (*out)(char *),
char *test = inputFile;
printf("URL: %s\n", sub);
- if (updateCommand_download(command, sub, &test) == CELIX_SUCCESS) {
+ if (updateCommand_download(context, sub, &test) == CELIX_SUCCESS) {
printf("Update bundle with stream\n");
bundle_update(bundle, inputFile);
} else {
- char error[256];
- sprintf(error, "Unable to download from %s\n", sub);
- err(error);
+ fprintf(errStream, "Unable to download from %s\n", sub);
}
} else {
bundle_update(bundle, NULL);
}
} else {
- err("Bundle id is invalid.\n");
+ fprintf(errStream, "Bundle id is invalid.\n");
}
}
}
-celix_status_t updateCommand_download(command_pt command, char * url, char **inputFile) {
+celix_status_t updateCommand_download(bundle_context_pt context, char * url, char **inputFile) {
CURL *curl = NULL;
CURLcode res = 0;
curl = curl_easy_init();
http://git-wip-us.apache.org/repos/asf/celix/blob/4d52743e/shell/public/include/shell.h
----------------------------------------------------------------------
diff --git a/shell/public/include/shell.h b/shell/public/include/shell.h
index 11a79b3..9fe60b4 100644
--- a/shell/public/include/shell.h
+++ b/shell/public/include/shell.h
@@ -39,6 +39,7 @@ struct shellService {
array_list_pt (*getCommands)(shell_pt shell);
char * (*getCommandUsage)(shell_pt shell, char * commandName);
char * (*getCommandDescription)(shell_pt shell, char * commandName);
+ celix_status_t (*getCommandReference)(shell_pt shell_ptr, char *command_name_str, service_reference_pt *command_reference_ptr);
void (*executeCommand)(shell_pt shell, char * commandLine, void (*out)(char *), void (*error)(char *));
};