From 3fd74560270855f52ace485f5ba693c12087e757 Mon Sep 17 00:00:00 2001 From: forge-bot Date: Sat, 18 Jul 2026 10:06:07 +0000 Subject: [PATCH] =?UTF-8?q?[framework]=20TxCore=20=E2=86=92=20common-frame?= =?UTF-8?q?work=20(TxContext=C2=B7MessageCodec)=20(ACM-FW-001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/TxServiceRegistry.java | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxServiceRegistry.java diff --git a/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxServiceRegistry.java b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxServiceRegistry.java new file mode 100644 index 0000000..07eda8d --- /dev/null +++ b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxServiceRegistry.java @@ -0,0 +1,192 @@ +package com.klaro.acquirecore.framework; + +import org.springframework.stereotype.Component; + +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; + +/** + * Service registry - equivalent to C tx_register/tx_lookup/tx_dispatch. + * Manages service handlers with priority-based dispatch. + */ +@Component +public class TxServiceRegistry { + + private final Map services = new ConcurrentHashMap<>(); + + /** + * Register a service handler - equivalent to C tx_register(). + */ + public TxServiceRegistry register(String name, T service, int priority) { + services.put(name, new ServiceEntry(name, service, priority)); + return this; + } + + /** + * Register a simple function handler. + */ + public TxServiceRegistry register(String name, Function handler, int priority) { + services.put(name, new ServiceEntry(name, new FunctionalTxService(name, handler), priority)); + return this; + } + + /** + * Lookup service by name - equivalent to C tx_lookup(). + */ + public TxService lookup(String name) { + ServiceEntry entry = services.get(name); + return entry != null ? entry.service() : null; + } + + /** + * Check if service exists. + */ + public boolean hasService(String name) { + return services.containsKey(name); + } + + /** + * Dispatch request to named service - equivalent to C tx_dispatch(). + */ + public TxResponse dispatch(String name, TxRequest request) { + ServiceEntry entry = services.get(name); + if (entry == null) { + return TxResponse.error(TxCore.TxResult.ERROR, "Service not found: " + name); + } + if (!entry.service().isEnabled()) { + return TxResponse.error(TxCore.TxResult.ERROR, "Service disabled: " + name); + } + try { + return entry.service().handle(request); + } catch (Exception e) { + return TxResponse.error(TxCore.TxResult.ERROR, e.getMessage()); + } + } + + /** + * Get all registered service names sorted by priority. + */ + public List getServiceNames() { + return services.values().stream() + .sorted(Comparator.comparingInt(ServiceEntry::priority)) + .map(ServiceEntry::name) + .toList(); + } + + public int getServiceCount() { + return services.size(); + } + + public TxServiceRegistry unregister(String name) { + services.remove(name); + return this; + } + + public void clear() { + services.clear(); + } + + private record ServiceEntry(String name, TxService service, int priority) {} + + // ===== TxService interface - equivalent to C tx_handler_fn ===== + + public interface TxService { + TxResponse handle(TxRequest request); + String getName(); + boolean isEnabled(); + void setEnabled(boolean enabled); + int getPriority(); + } + + // ===== AbstractTxService base class ===== + + public abstract static class AbstractTxService implements TxService { + private final String name; + private final int priority; + private volatile boolean enabled = true; + + protected AbstractTxService(String name, int priority) { + this.name = name; + this.priority = priority; + } + + protected AbstractTxService(String name) { + this(name, 0); + } + + @Override + public String getName() { + return name; + } + + @Override + public int getPriority() { + return priority; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @Override + public TxResponse handle(TxRequest request) { + if (!enabled) { + return TxResponse.error("Service is disabled: " + name); + } + try { + return doHandle(request); + } catch (Exception e) { + return TxResponse.error(e.getMessage()); + } + } + + protected abstract TxResponse doHandle(TxRequest request); + } + + // ===== Functional TxService wrapper ===== + + private static class FunctionalTxService implements TxService { + private final String name; + private final Function handler; + private volatile boolean enabled = true; + + FunctionalTxService(String name, Function handler) { + this.name = name; + this.handler = handler; + } + + @Override + public TxResponse handle(TxRequest request) { + return handler.apply(request); + } + + @Override + public String getName() { + return name; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @Override + public int getPriority() { + return 0; + } + } +}