This is an automated email from the ASF dual-hosted git repository.

pengzheng pushed a commit to branch feature/refactor_bundle_cache
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/feature/refactor_bundle_cache 
by this push:
     new b44c58b3 Extract common Bundle Command Execution.
b44c58b3 is described below

commit b44c58b35a242228bd5ecd360df83cfe5f2d5a9e
Author: PengZheng <[email protected]>
AuthorDate: Tue Feb 21 20:29:52 2023 +0800

    Extract common Bundle Command Execution.
---
 bundles/shell/shell/CMakeLists.txt              |  2 +-
 bundles/shell/shell/gtest/src/ShellTestSuite.cc |  3 ++
 bundles/shell/shell/src/bundle_command.c        | 57 +++++++++++++++++++++++++
 bundles/shell/shell/src/bundle_command.h        | 39 +++++++++++++++++
 bundles/shell/shell/src/start_command.c         | 38 +----------------
 bundles/shell/shell/src/stop_command.c          | 38 +----------------
 bundles/shell/shell/src/uninstall_command.c     | 40 +----------------
 bundles/shell/shell/src/update_command.c        | 44 +++----------------
 8 files changed, 111 insertions(+), 150 deletions(-)

diff --git a/bundles/shell/shell/CMakeLists.txt 
b/bundles/shell/shell/CMakeLists.txt
index 15731a84..a07c76b6 100644
--- a/bundles/shell/shell/CMakeLists.txt
+++ b/bundles/shell/shell/CMakeLists.txt
@@ -46,7 +46,7 @@ if (SHELL)
                        src/query_command.c
                        src/quit_command.c
                        src/std_commands.c
-       )
+                       src/bundle_command.c)
        target_include_directories(shell_commands PRIVATE src)
        target_link_libraries(shell_commands PRIVATE Celix::shell_api 
Celix::log_helper)
        celix_deprecated_utils_headers(shell_commands)
diff --git a/bundles/shell/shell/gtest/src/ShellTestSuite.cc 
b/bundles/shell/shell/gtest/src/ShellTestSuite.cc
index cb29036c..36d1f9c2 100644
--- a/bundles/shell/shell/gtest/src/ShellTestSuite.cc
+++ b/bundles/shell/shell/gtest/src/ShellTestSuite.cc
@@ -99,10 +99,13 @@ TEST_F(ShellTestSuite, testAllCommandsAreCallable) {
     callCommand(ctx, "query", true);
     callCommand(ctx, "q -v", true);
     callCommand(ctx, "stop not-a-number", false);
+    callCommand(ctx, "stop", false); // incorrect number of arguments
     callCommand(ctx, "start not-a-number", false);
     callCommand(ctx, "start", false); // incorrect number of arguments
     callCommand(ctx, "uninstall not-a-number", false);
+    callCommand(ctx, "uninstall", false); // incorrect number of arguments
     callCommand(ctx, "update not-a-number", false);
+    callCommand(ctx, "update", false); // incorrect number of arguments
     callCommand(ctx, "stop 15", false); //non existing bundle id
     callCommand(ctx, "start 15", false); //non existing bundle id
     callCommand(ctx, "uninstall 15", false); //non existing bundle id
diff --git a/bundles/shell/shell/src/bundle_command.c 
b/bundles/shell/shell/src/bundle_command.c
new file mode 100644
index 00000000..cbf9dd17
--- /dev/null
+++ b/bundles/shell/shell/src/bundle_command.c
@@ -0,0 +1,57 @@
+/*
+ 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.
+ */
+
+#include "bundle_command.h"
+#include "celix_bundle_context.h"
+#include "celix_convert_utils.h"
+#include "celix_utils.h"
+#include <stdlib.h>
+#include <string.h>
+
+bool bundleCommand_execute(void *handle, const char *constCommandLine, FILE 
*outStream, FILE *errStream, bundle_control_fpt ctrl) {
+    celix_bundle_context_t *ctx = handle;
+
+    char* sub = NULL;
+    char* savePtr = NULL;
+    char* command = celix_utils_strdup(constCommandLine);
+    strtok_r(command, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr); //ignore 
command name
+    sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr);
+
+    bool succeeded = false;
+    if (sub == NULL) {
+        fprintf(errStream, "Incorrect number of arguments.\n");
+    }
+    for (; sub != NULL; sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, 
&savePtr)) {
+        bool converted;
+        long bndId = celix_utils_convertStringToLong(sub, 0, &converted);
+        if (!converted) {
+            fprintf(errStream, "Cannot convert '%s' to long (bundle id).\n", 
sub);
+            continue;
+        }
+        if (!celix_bundleContext_isBundleInstalled(ctx, bndId)) {
+            fprintf(errStream, "No bundle with id %li.\n", bndId);
+            continue;
+        }
+        celix_framework_t* fw = celix_bundleContext_getFramework(ctx);
+        ctrl(fw, bndId);
+        succeeded = true;
+    }
+    free(command);
+    return succeeded;
+}
diff --git a/bundles/shell/shell/src/bundle_command.h 
b/bundles/shell/shell/src/bundle_command.h
new file mode 100644
index 00000000..b48d0b40
--- /dev/null
+++ b/bundles/shell/shell/src/bundle_command.h
@@ -0,0 +1,39 @@
+/*
+ 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.
+ */
+
+#ifndef CELIX_BUNDLE_COMMAND_H
+#define CELIX_BUNDLE_COMMAND_H
+
+#include "celix_framework.h"
+#include "std_commands.h"
+#include <stdbool.h>
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void (*bundle_control_fpt)(celix_framework_t *fw, long bndId);
+
+bool bundleCommand_execute(void *handle, const char *constCommandLine, FILE 
*outStream, FILE *errStream, bundle_control_fpt ctrl);
+
+#ifdef __cplusplus
+}
+#endif
+#endif //CELIX_BUNDLE_COMMAND_H
diff --git a/bundles/shell/shell/src/start_command.c 
b/bundles/shell/shell/src/start_command.c
index 3e08d111..2318d296 100644
--- a/bundles/shell/shell/src/start_command.c
+++ b/bundles/shell/shell/src/start_command.c
@@ -17,42 +17,8 @@
  * under the License.
  */
 
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "celix_api.h"
-#include "std_commands.h"
-#include "celix_convert_utils.h"
+#include "bundle_command.h"
 
 bool startCommand_execute(void *handle, const char *constCommandLine, FILE 
*outStream, FILE *errStream) {
-    celix_bundle_context_t *ctx = handle;
-
-    char* sub = NULL;
-    char* savePtr = NULL;
-    char* command = celix_utils_strdup(constCommandLine);
-    strtok_r(command, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr); //ignore 
command name
-    sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr);
-
-    bool startSucceeded = false;
-    if (sub == NULL) {
-        fprintf(outStream, "Incorrect number of arguments.\n");
-    }
-    for (; sub != NULL; sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, 
&savePtr)) {
-        bool converted;
-        long bndId = celix_utils_convertStringToLong(sub, 0, &converted);
-        if (!converted) {
-            fprintf(errStream, "Cannot convert '%s' to long (bundle id).\n", 
sub);
-            continue;
-        }
-        if (!celix_bundleContext_isBundleInstalled(ctx, bndId)) {
-            fprintf(outStream, "No bundle with id %li.\n", bndId);
-            continue;
-        }
-        celix_framework_t* fw = celix_bundleContext_getFramework(ctx);
-        celix_framework_startBundleAsync(fw, bndId);
-        startSucceeded = true;
-    }
-    free(command);
-    return startSucceeded;
+    return bundleCommand_execute(handle, constCommandLine, outStream, 
errStream, celix_framework_startBundleAsync);
 }
diff --git a/bundles/shell/shell/src/stop_command.c 
b/bundles/shell/shell/src/stop_command.c
index 9f578174..1f17e3a0 100644
--- a/bundles/shell/shell/src/stop_command.c
+++ b/bundles/shell/shell/src/stop_command.c
@@ -17,42 +17,8 @@
  * under the License.
  */
 
-#include <stdlib.h>
-#include <string.h>
-
-#include "celix_api.h"
-#include "std_commands.h"
-#include "celix_convert_utils.h"
+#include "bundle_command.h"
 
 bool stopCommand_execute(void *handle, const char *constCommandLine, FILE 
*outStream, FILE *errStream) {
-    celix_bundle_context_t *ctx = handle;
-
-    char* sub = NULL;
-    char* savePtr = NULL;
-    char* command = celix_utils_strdup(constCommandLine);
-    strtok_r(command, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr); //ignore 
command name
-    sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr);
-
-    bool stoppedCalledAndSucceeded = false;
-    if (sub == NULL) {
-        fprintf(outStream, "Incorrect number of arguments.\n");
-    } else {
-        while (sub != NULL) {
-            bool converted;
-            long bndId = celix_utils_convertStringToLong(sub, 0, &converted);
-            bool exists = celix_bundleContext_isBundleInstalled(ctx, bndId);
-            if (!converted) {
-                fprintf(errStream, "Cannot convert '%s' to long (bundle 
id).\n", sub);
-            } else if (!exists) {
-                fprintf(outStream, "No bundle with id %li.\n", bndId);
-            } else {
-                celix_framework_t* fw = celix_bundleContext_getFramework(ctx);
-                celix_framework_stopBundleAsync(fw, bndId);
-                stoppedCalledAndSucceeded = true;
-            }
-            sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr);
-        }
-    }
-    free(command);
-    return stoppedCalledAndSucceeded;
+    return bundleCommand_execute(handle, constCommandLine, outStream, 
errStream, celix_framework_stopBundleAsync);
 }
diff --git a/bundles/shell/shell/src/uninstall_command.c 
b/bundles/shell/shell/src/uninstall_command.c
index 8824a054..a33dcf24 100644
--- a/bundles/shell/shell/src/uninstall_command.c
+++ b/bundles/shell/shell/src/uninstall_command.c
@@ -17,44 +17,8 @@
  * under the License.
  */
 
-#include <stdlib.h>
-#include <string.h>
-
-#include "celix_api.h"
-#include "std_commands.h"
-#include "celix_convert_utils.h"
-
+#include "bundle_command.h"
 
 bool uninstallCommand_execute(void *handle, const char* constCommandLine, FILE 
*outStream, FILE *errStream) {
-       celix_bundle_context_t *ctx = handle;
-
-    char* sub = NULL;
-    char* savePtr = NULL;
-    char* command = celix_utils_strdup(constCommandLine);
-    strtok_r(command, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr); //ignore 
command name
-    sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr);
-
-       bool uninstallSucceeded = false;
-
-       if (sub == NULL) {
-               fprintf(errStream, "Incorrect number of arguments.\n");
-       } else {
-               while (sub != NULL) {
-            bool converted;
-            long bndId = celix_utils_convertStringToLong(sub, 0, &converted);
-            bool exists = celix_bundleContext_isBundleInstalled(ctx, bndId);
-            if (!converted) {
-                fprintf(errStream, "Cannot convert '%s' to long (bundle 
id).\n", sub);
-            } else if (!exists) {
-                fprintf(outStream, "No bundle with id %li.\n", bndId);
-            } else {
-                celix_framework_t* fw = celix_bundleContext_getFramework(ctx);
-                celix_framework_uninstallBundleAsync(fw, bndId);
-                uninstallSucceeded = true;
-            }
-                       sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, 
&savePtr);
-               }
-       }
-       free(command);
-       return uninstallSucceeded;
+    return bundleCommand_execute(handle, constCommandLine, outStream, 
errStream, celix_framework_uninstallBundleAsync);
 }
\ No newline at end of file
diff --git a/bundles/shell/shell/src/update_command.c 
b/bundles/shell/shell/src/update_command.c
index 4b4f47f4..8a7217b9 100644
--- a/bundles/shell/shell/src/update_command.c
+++ b/bundles/shell/shell/src/update_command.c
@@ -18,47 +18,13 @@
  */
 
 
-#include <stdlib.h>
-#include <string.h>
+#include "bundle_command.h"
 
-#include "std_commands.h"
-#include "celix_utils.h"
-#include "celix_array_list.h"
-#include "celix_bundle_context.h"
-#include "celix_framework.h"
-#include "celix_convert_utils.h"
+static void updateBundleAsync(celix_framework_t *fw, long bndId) {
+    celix_framework_updateBundleAsync(fw, bndId, NULL);
+}
 
 bool updateCommand_execute(void *handle, const char *constCommandLine, FILE 
*outStream, FILE *errStream) {
-    celix_bundle_context_t *ctx = handle;
-
-    char* sub = NULL;
-    char* savePtr = NULL;
-    char* command = celix_utils_strdup(constCommandLine);
-    strtok_r(command, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr); //ignore 
command name
-    sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr);
-
-    bool updateSucceeded = false;
-    if (sub == NULL) {
-        fprintf(errStream, "Incorrect number of arguments.\n");
-    } else {
-        while (sub != NULL) {
-            bool converted;
-            long bndId = celix_utils_convertStringToLong(sub, 0, &converted);
-            bool exists = celix_bundleContext_isBundleInstalled(ctx, bndId);
-            if (!converted) {
-                fprintf(errStream, "Cannot convert '%s' to long (bundle 
id).\n", sub);
-            } else if (!exists) {
-                fprintf(outStream, "No bundle with id %li.\n", bndId);
-            } else {
-                fprintf(errStream, "Update bundle is not yet fully supported. 
Use at your own risk.\n");
-                celix_framework_t* fw = celix_bundleContext_getFramework(ctx);
-                celix_framework_updateBundleAsync(fw, bndId, NULL);
-                updateSucceeded = true;
-            }
-            sub = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &savePtr);
-        }
-    }
-    free(command);
-    return updateSucceeded;
+    return bundleCommand_execute(handle, constCommandLine, outStream, 
errStream, updateBundleAsync);
 }
 

Reply via email to