feat(core): support configurable port, clear collision error handling and discovery proposal
This commit is contained in:
+35
-4
@@ -22,12 +22,17 @@ public class CyberCoreSignerApplication {
|
||||
private static boolean developmentMode = false;
|
||||
private static boolean headlessMode = false;
|
||||
private static boolean noWaitMode = false;
|
||||
private static int configuredPort = CcspServer.DEFAULT_PORT;
|
||||
private static volatile CcspServer runningServer = null;
|
||||
|
||||
public static CcspServer getRunningServer() {
|
||||
return runningServer;
|
||||
}
|
||||
|
||||
public static int getConfiguredPort() {
|
||||
return configuredPort;
|
||||
}
|
||||
|
||||
public static void stopServer() {
|
||||
if (runningServer != null) {
|
||||
runningServer.stop();
|
||||
@@ -98,7 +103,7 @@ public class CyberCoreSignerApplication {
|
||||
logger.info("Modo de desenvolvimento ativo (com certificados reais do sistema).");
|
||||
}
|
||||
SigningService signingService = new SigningService(certManager);
|
||||
runningServer = new CcspServer(certManager, signingService, null);
|
||||
runningServer = new CcspServer(configuredPort, certManager, signingService, br.com.cybercore.signer.core.security.ApplicationRegistry.getInstance(), null);
|
||||
runningServer.start();
|
||||
logger.info("Servidor local CCSP/1 pronto em http://127.0.0.1:{}", runningServer.getPort());
|
||||
|
||||
@@ -113,14 +118,34 @@ public class CyberCoreSignerApplication {
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (java.net.BindException e) {
|
||||
logger.error("ALERTA DE COLISÃO DE PORTA: A porta {} já está em uso por outra instância do CyberCore Signer ou outro serviço. Para usar uma porta alternativa, utilize o parâmetro --port <porta> ou defina a variável de ambiente CYBERCORE_PORT.", configuredPort);
|
||||
} catch (Exception e) {
|
||||
logger.error("Falha ao inicializar servidor local CCSP/1 no modo console: {}", e.getMessage(), e);
|
||||
if (e.getCause() instanceof java.net.BindException || (e.getMessage() != null && e.getMessage().contains("Address already in use"))) {
|
||||
logger.error("ALERTA DE COLISÃO DE PORTA: A porta {} já está em uso por outra instância do CyberCore Signer ou outro serviço. Para usar uma porta alternativa, utilize o parâmetro --port <porta> ou defina a variável de ambiente CYBERCORE_PORT.", configuredPort);
|
||||
} else {
|
||||
logger.error("Falha ao inicializar servidor local CCSP/1 no modo console: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseArguments(String[] args) {
|
||||
public static void parseArguments(String[] args) {
|
||||
String envPort = System.getenv("CYBERCORE_PORT");
|
||||
if (envPort != null && !envPort.isBlank()) {
|
||||
try {
|
||||
configuredPort = Integer.parseInt(envPort.trim());
|
||||
} catch (NumberFormatException ignored) {}
|
||||
}
|
||||
String propPort = System.getProperty("cybercore.port");
|
||||
if (propPort != null && !propPort.isBlank()) {
|
||||
try {
|
||||
configuredPort = Integer.parseInt(propPort.trim());
|
||||
} catch (NumberFormatException ignored) {}
|
||||
}
|
||||
|
||||
if (args != null) {
|
||||
for (String arg : args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String arg = args[i];
|
||||
if ("--dev".equalsIgnoreCase(arg) || "-dev".equalsIgnoreCase(arg)) {
|
||||
developmentMode = true;
|
||||
} else if ("--headless".equalsIgnoreCase(arg) || "-headless".equalsIgnoreCase(arg)
|
||||
@@ -129,6 +154,12 @@ public class CyberCoreSignerApplication {
|
||||
} else if ("--no-wait".equalsIgnoreCase(arg) || "-no-wait".equalsIgnoreCase(arg)
|
||||
|| "--test".equalsIgnoreCase(arg)) {
|
||||
noWaitMode = true;
|
||||
} else if (("--port".equalsIgnoreCase(arg) || "-p".equalsIgnoreCase(arg)) && i + 1 < args.length) {
|
||||
try {
|
||||
configuredPort = Integer.parseInt(args[++i].trim());
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warn("Porta inválida especificada em --port: '{}'. Usando porta padrão {}.", args[i], CcspServer.DEFAULT_PORT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-1
@@ -11,10 +11,20 @@ class CyberCoreSignerApplicationTest {
|
||||
@DisplayName("Deve inicializar a aplicação com argumentos sem erros")
|
||||
void shouldBootstrapWithoutErrors() {
|
||||
try {
|
||||
assertThatCode(() -> CyberCoreSignerApplication.main(new String[]{"--dev", "--headless", "--no-wait"}))
|
||||
assertThatCode(() -> CyberCoreSignerApplication.main(new String[]{"--dev", "--headless", "--no-wait", "--port", "29999"}))
|
||||
.doesNotThrowAnyException();
|
||||
} finally {
|
||||
CyberCoreSignerApplication.stopServer();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Deve realizar o parse correto do argumento --port")
|
||||
void shouldParsePortArgument() {
|
||||
CyberCoreSignerApplication.parseArguments(new String[]{"--port", "25555"});
|
||||
org.assertj.core.api.Assertions.assertThat(CyberCoreSignerApplication.getConfiguredPort()).isEqualTo(25555);
|
||||
|
||||
CyberCoreSignerApplication.parseArguments(new String[]{"-p", "26666"});
|
||||
org.assertj.core.api.Assertions.assertThat(CyberCoreSignerApplication.getConfiguredPort()).isEqualTo(26666);
|
||||
}
|
||||
}
|
||||
|
||||
+16
-3
@@ -510,21 +510,34 @@ public class CyberCoreDesktopApplication extends Application {
|
||||
}
|
||||
};
|
||||
|
||||
int targetPort = resolveConfiguredPort();
|
||||
try {
|
||||
this.ccspServer = new CcspServer(this.certificateManager, this.signingService, promptListener);
|
||||
this.ccspServer = new CcspServer(targetPort, this.certificateManager, this.signingService, ApplicationRegistry.getInstance(), promptListener);
|
||||
this.ccspServer.start();
|
||||
logger.info("Servidor local CCSP/1 inicializado na porta {}", this.ccspServer.getPort());
|
||||
} catch (java.net.BindException be) {
|
||||
logger.warn("Porta 21325 já está ocupada por outra instância do CyberCore Signer em execução. O agente continuará ativo na interface gráfica.");
|
||||
logger.warn("ALERTA DE COLISÃO DE PORTA: A porta {} já está ocupada por outra instância do CyberCore Signer ou outro serviço. Para usar outra porta, configure CYBERCORE_PORT ou use --port <porta>. O agente continuará ativo na interface gráfica.", targetPort);
|
||||
} catch (Exception e) {
|
||||
if (e.getCause() instanceof java.net.BindException || (e.getMessage() != null && e.getMessage().contains("Address already in use"))) {
|
||||
logger.warn("Porta 21325 já está ocupada por outra instância do CyberCore Signer em execução. O agente continuará ativo na interface gráfica.");
|
||||
logger.warn("ALERTA DE COLISÃO DE PORTA: A porta {} já está ocupada por outra instância do CyberCore Signer ou outro serviço. Para usar outra porta, configure CYBERCORE_PORT ou use --port <porta>. O agente continuará ativo na interface gráfica.", targetPort);
|
||||
} else {
|
||||
logger.error("Falha ao inicializar servidor local CCSP/1: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int resolveConfiguredPort() {
|
||||
String env = System.getenv("CYBERCORE_PORT");
|
||||
if (env != null && !env.isBlank()) {
|
||||
try { return Integer.parseInt(env.trim()); } catch (Exception ignored) {}
|
||||
}
|
||||
String prop = System.getProperty("cybercore.port");
|
||||
if (prop != null && !prop.isBlank()) {
|
||||
try { return Integer.parseInt(prop.trim()); } catch (Exception ignored) {}
|
||||
}
|
||||
return CcspServer.DEFAULT_PORT;
|
||||
}
|
||||
|
||||
private void initSmartCardWatcher() {
|
||||
if (Boolean.getBoolean("cybercore.diag.no-smartcard-watcher")) {
|
||||
logger.warn("Diagnóstico: Monitor dinâmico de smart cards DESATIVADO via -Dcybercore.diag.no-smartcard-watcher=true");
|
||||
|
||||
Reference in New Issue
Block a user