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

shuber pushed a commit to branch unomi-3-dev
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/unomi-3-dev by this push:
     new 21c8deff3 UNOMI-879 Refactor tenant management in Karaf shell commands
21c8deff3 is described below

commit 21c8deff3be69b7cbea8f520fe591624a2a72cd8
Author: Serge Huber <[email protected]>
AuthorDate: Thu Jan 15 18:36:00 2026 +0100

    UNOMI-879 Refactor tenant management in Karaf shell commands
    
    - Updated `TenantEndpoint` to return a `Response` object with appropriate 
status codes for tenant retrieval.
    - Introduced `TenantContextHelper` to manage tenant context initialization 
in shell sessions.
    - Modified `CacheCommands`, `ListCommandSupport`, 
`TenantGetCurrentCommand`, and `TenantSetCurrentCommand` to utilize the new 
`TenantContextHelper` for session-based tenant management.
    - Enhanced commands to ensure proper execution context is set based on the 
current tenant ID stored in the session.
---
 .../apache/unomi/rest/tenants/TenantEndpoint.java  |  9 ++-
 .../unomi/shell/dev/commands/CacheCommands.java    |  8 +++
 .../shell/dev/commands/ListCommandSupport.java     | 11 ++++
 .../shell/dev/commands/TenantContextHelper.java    | 74 ++++++++++++++++++++++
 .../commands/tenants/TenantGetCurrentCommand.java  | 12 ++--
 .../commands/tenants/TenantSetCurrentCommand.java  | 10 ++-
 6 files changed, 113 insertions(+), 11 deletions(-)

diff --git 
a/rest/src/main/java/org/apache/unomi/rest/tenants/TenantEndpoint.java 
b/rest/src/main/java/org/apache/unomi/rest/tenants/TenantEndpoint.java
index 93b313f04..0372ed6b1 100644
--- a/rest/src/main/java/org/apache/unomi/rest/tenants/TenantEndpoint.java
+++ b/rest/src/main/java/org/apache/unomi/rest/tenants/TenantEndpoint.java
@@ -63,18 +63,17 @@ public class TenantEndpoint {
      * Retrieves a specific tenant by ID.
      *
      * @param tenantId the ID of the tenant to retrieve
-     * @return the requested tenant
-     * @throws WebApplicationException with 404 status if tenant is not found
+     * @return the requested tenant with 200 status, or 404 if tenant is not 
found
      */
     @GET
     @Path("/{tenantId}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Tenant getTenant(@PathParam("tenantId") String tenantId) {
+    public Response getTenant(@PathParam("tenantId") String tenantId) {
         Tenant tenant = tenantService.getTenant(tenantId);
         if (tenant == null) {
-            throw new WebApplicationException("Tenant not found", 
Response.Status.NOT_FOUND);
+            return Response.status(Response.Status.NOT_FOUND).build();
         }
-        return tenant;
+        return Response.ok(tenant).build();
     }
 
     /**
diff --git 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/CacheCommands.java
 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/CacheCommands.java
index 5d4de4d6e..7b1e98da6 100644
--- 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/CacheCommands.java
+++ 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/CacheCommands.java
@@ -21,11 +21,13 @@ import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.Option;
 import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.apache.unomi.api.services.ExecutionContextManager;
 import org.apache.unomi.api.services.cache.MultiTypeCacheService;
 import 
org.apache.unomi.api.services.cache.MultiTypeCacheService.CacheStatistics;
 import 
org.apache.unomi.api.services.cache.MultiTypeCacheService.CacheStatistics.TypeStatistics;
 import org.apache.unomi.api.tenants.TenantService;
+import org.apache.unomi.shell.dev.commands.TenantContextHelper;
 
 import java.io.Serializable;
 import java.text.SimpleDateFormat;
@@ -48,6 +50,9 @@ public class CacheCommands implements Action {
     @Reference
     private ExecutionContextManager executionContextManager;
 
+    @Reference
+    private Session session;
+
     @Option(name = "--stats", description = "Display cache statistics", 
required = false)
     private boolean showStats = false;
 
@@ -88,6 +93,9 @@ public class CacheCommands implements Action {
             return null;
         }
 
+        // Initialize execution context from session
+        TenantContextHelper.initializeExecutionContext(session, 
executionContextManager);
+
         // Set default tenant if not specified
         if (tenantId == null) {
             tenantId = 
executionContextManager.getCurrentContext().getTenantId();
diff --git 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/ListCommandSupport.java
 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/ListCommandSupport.java
index 0568e5084..832ea8b54 100644
--- 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/ListCommandSupport.java
+++ 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/ListCommandSupport.java
@@ -18,8 +18,11 @@ package org.apache.unomi.shell.dev.commands;
 
 import org.apache.karaf.shell.api.action.Action;
 import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.console.Session;
 import org.apache.karaf.shell.support.table.Row;
 import org.apache.karaf.shell.support.table.ShellTable;
+import org.apache.unomi.api.services.ExecutionContextManager;
 import org.apache.unomi.common.DataTable;
 
 import java.util.ArrayList;
@@ -29,6 +32,12 @@ import java.util.ArrayList;
  */
 public abstract class ListCommandSupport implements Action {
 
+    @Reference
+    protected Session session;
+
+    @Reference
+    protected ExecutionContextManager executionContextManager;
+
     @Option(name = "--csv", description = "Output table in CSV format", 
required = false, multiValued = false)
     boolean csv;
 
@@ -47,6 +56,8 @@ public abstract class ListCommandSupport implements Action {
     protected abstract DataTable buildDataTable();
 
     public Object execute() throws Exception {
+        // Initialize execution context from session before executing command
+        TenantContextHelper.initializeExecutionContext(session, 
executionContextManager);
 
         DataTable dataTable = buildDataTable();
 
diff --git 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/TenantContextHelper.java
 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/TenantContextHelper.java
new file mode 100644
index 000000000..b81b8751d
--- /dev/null
+++ 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/TenantContextHelper.java
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+package org.apache.unomi.shell.dev.commands;
+
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.unomi.api.services.ExecutionContextManager;
+
+/**
+ * Utility class for managing tenant context in Karaf shell sessions.
+ * Provides centralized access to session-based tenant storage and execution 
context initialization.
+ */
+public final class TenantContextHelper {
+
+    /**
+     * Session key for storing the current tenant ID in the Karaf shell 
session.
+     */
+    public static final String SESSION_TENANT_ID_KEY = "unomi.tenantId";
+
+    private TenantContextHelper() {
+        // Utility class - prevent instantiation
+    }
+
+    /**
+     * Initialize the execution context from the Karaf shell session.
+     * Retrieves the tenant ID from the session and sets it in the execution 
context.
+     * If no tenant is set in the session, defaults to "system" context.
+     *
+     * @param session the Karaf shell session
+     * @param executionContextManager the execution context manager
+     */
+    public static void initializeExecutionContext(Session session, 
ExecutionContextManager executionContextManager) {
+        String tenantId = getTenantId(session);
+        if (tenantId != null) {
+            
executionContextManager.setCurrentContext(executionContextManager.createContext(tenantId));
+        } else {
+            // Default to system context if no tenant is set
+            
executionContextManager.setCurrentContext(executionContextManager.createContext("system"));
+        }
+    }
+
+    /**
+     * Get the tenant ID from the Karaf shell session.
+     *
+     * @param session the Karaf shell session
+     * @return the tenant ID stored in the session, or null if not set
+     */
+    public static String getTenantId(Session session) {
+        return (String) session.get(SESSION_TENANT_ID_KEY);
+    }
+
+    /**
+     * Set the tenant ID in the Karaf shell session.
+     *
+     * @param session the Karaf shell session
+     * @param tenantId the tenant ID to store
+     */
+    public static void setTenantId(Session session, String tenantId) {
+        session.put(SESSION_TENANT_ID_KEY, tenantId);
+    }
+}
diff --git 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantGetCurrentCommand.java
 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantGetCurrentCommand.java
index c94eb664a..1b4f4de97 100644
--- 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantGetCurrentCommand.java
+++ 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantGetCurrentCommand.java
@@ -20,20 +20,22 @@ import org.apache.karaf.shell.api.action.Action;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
-import org.apache.unomi.api.services.ExecutionContextManager;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.unomi.shell.dev.commands.TenantContextHelper;
 
 @Command(scope = "unomi", name = "tenant-get", description = "Get the current 
tenant ID for this shell session")
 @Service
 public class TenantGetCurrentCommand implements Action {
 
     @Reference
-    private ExecutionContextManager executionContextManager;
+    private Session session;
 
     @Override
     public Object execute() throws Exception {
-        String currentTenantId = 
executionContextManager.getCurrentContext().getTenantId();
-        if (currentTenantId != null) {
-            System.out.println("Current tenant ID: " + currentTenantId);
+        // Retrieve tenant ID from the Karaf shell session
+        String tenantId = TenantContextHelper.getTenantId(session);
+        if (tenantId != null) {
+            System.out.println("Current tenant ID: " + tenantId);
         } else {
             System.out.println("No current tenant set");
         }
diff --git 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantSetCurrentCommand.java
 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantSetCurrentCommand.java
index b649bedae..db5d57ce6 100644
--- 
a/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantSetCurrentCommand.java
+++ 
b/tools/shell-dev-commands/src/main/java/org/apache/unomi/shell/dev/commands/tenants/TenantSetCurrentCommand.java
@@ -22,9 +22,11 @@ import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.Completion;
 import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.Session;
 import org.apache.unomi.api.services.ExecutionContextManager;
 import org.apache.unomi.api.tenants.Tenant;
 import org.apache.unomi.api.tenants.TenantService;
+import org.apache.unomi.shell.dev.commands.TenantContextHelper;
 import org.apache.unomi.shell.dev.completers.TenantCompleter;
 
 @Command(scope = "unomi", name = "tenant-set", description = "Set the current 
tenant ID for this shell session")
@@ -37,6 +39,9 @@ public class TenantSetCurrentCommand implements Action {
     @Reference
     private ExecutionContextManager executionContextManager;
 
+    @Reference
+    private Session session;
+
     @Argument(index = 0, name = "tenantId", description = "Tenant ID to set as 
current", required = true)
     @Completion(TenantCompleter.class)
     String tenantId;
@@ -50,7 +55,10 @@ public class TenantSetCurrentCommand implements Action {
             return null;
         }
 
-        // Set the current tenant
+        // Store tenant ID in the Karaf shell session
+        TenantContextHelper.setTenantId(session, tenantId);
+
+        // Set the current tenant in execution context
         
executionContextManager.setCurrentContext(executionContextManager.createContext(tenantId));
         System.out.println("Current tenant set to: " + tenantId);
 

Reply via email to