[framework] TxCore → common-framework (TxContext·MessageCodec) (ACM-FW-001)
This commit is contained in:
parent
d15ff5673c
commit
3fd7456027
1 changed files with 192 additions and 0 deletions
|
|
@ -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<String, ServiceEntry> services = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a service handler - equivalent to C tx_register().
|
||||||
|
*/
|
||||||
|
public <T extends TxService> 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<TxRequest, TxResponse> 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<String> 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<TxRequest, TxResponse> handler;
|
||||||
|
private volatile boolean enabled = true;
|
||||||
|
|
||||||
|
FunctionalTxService(String name, Function<TxRequest, TxResponse> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in a new issue