Add Sonic container scan with manual multi-select after system setup.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-29 09:03:01 +02:00
co-authored by Cursor
parent 09b31a4ac9
commit f7e9bd43e2
7 changed files with 1160 additions and 406 deletions
@@ -1,68 +1,72 @@
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.management.ObjectName;
/**
* Sonic-MfApi-Hilfsprogramm.
*
* Unterstützte Befehle:
*
* validate
* Prüft Anmeldung und lesenden Zugriff auf den Container.
* Es wird kein Neustart ausgeführt.
*
* restart
* Startet den angegebenen Container neu.
*
* Kompilierung mit Java 8:
* Befehle:
* validate - Anmeldung + lesender Container-Zugriff
* restart - Container neu starten (stop/restart)
* list - Container der Domain auflisten
*
* javac -source 8 -target 8 SonicMfContainerTool.java
*/
public final class SonicMfConta*nerTool
public final class SonicMfContainerTool
{
public static final *tring TOOL_VERSION =
"2026*07-27-validate-and-restart";
public static final String TOOL_VERSION =
"2026-07-29-validate-restart-list";
*ublic static void main(String[] ar*s)
public static void main(String[] args)
{
info("ToolVersion*" + TOOL_VERSION);
info("ToolVersion=" + TOOL_VERSION);
try
* {
Args parsedArgu*ents = Args.parse(args);
{
Args parsedArguments = Args.parse(args);
* if (!"validate".equals(parsedAr*uments.command)
&&*!"restart".equals(parsedArguments.*ommand))
if (!"validate".equals(parsedArguments.command)
&& !"restart".equals(parsedArguments.command)
&& !"list".equals(parsedArguments.command))
{
* fail(
"Usa*e: SonicMfContainerTool "
* + "<validate|restart> "* + "--domain D *
+ "--url U "
* + "--user U "
* + "--container C*"
+ "[--timeout SEC]");
return;
* }
if (isBl*nk(parsedArguments.domain)
* || isBlank(parsedArguments*url)
|| isBlank(pa*sedArguments.user)
*|| isBlank(parsedArguments.contain*r))
{
*ail(
"domain, *rl, user und container sind Pflich*.");
return;
* }
if (isBlank(*arsedArguments.password))
* {
fail(
* "Das Sonic-Kennwort wurde nicht über "
+ "ESB_SONIC_PASSWORD bereitgestellt.");
"Usage: SonicMfContainerTool "
+ "<validate|restart|list> "
+ "--domain D "
+ "--url U "
+ "--user U "
+ "[--container C] "
+ "[--timeout SEC]");
return;
}
if (isBlank(parsedArguments.domain)
|| isBlank(parsedArguments.url)
|| isBlank(parsedArguments.user))
{
fail("domain, url und user sind Pflicht.");
return;
}
boolean needsContainer =
"validate".equals(parsedArguments.command)
|| "restart".equals(parsedArguments.command);
if (needsContainer
&& isBlank(parsedArguments.container))
{
fail("container ist Pflicht fuer validate/restart.");
return;
}
if (isBlank(parsedArguments.password))
{
fail(
"Das Sonic-Kennwort wurde nicht ueber "
+ "ESB_SONIC_PASSWORD bereitgestellt.");
return;
}
@@ -78,7 +82,13 @@ public final class SonicMfConta*nerTool
try
{
if ("validate".equals(
if ("list".equals(parsedArguments.command))
{
listContainers(
connector,
parsedArguments.domain);
}
else if ("validate".equals(
parsedArguments.command))
{
validateConnection(
@@ -102,7 +112,6 @@ public final class SonicMfConta*nerTool
catch (Throwable throwable)
{
Throwable root = rootOf(throwable);
String message = root.getMessage();
if (message == null)
@@ -117,6 +126,115 @@ public final class SonicMfConta*nerTool
}
}
private static void listContainers(
Object connector,
String domain)
throws Exception
{
Method queryNamesMethod =
findMethod(
connector.getClass(),
"queryNames",
new Class[]
{
ObjectName.class,
javax.management.QueryExp.class
});
if (queryNamesMethod == null)
{
throw new IllegalStateException(
"queryNames(ObjectName, QueryExp) wurde nicht gefunden.");
}
ObjectName pattern =
new ObjectName("*:ID=AGENT");
Object rawResult = queryNamesMethod.invoke(
connector,
new Object[]
{
pattern,
null
});
if (!(rawResult instanceof Set))
{
throw new IllegalStateException(
"queryNames lieferte kein Set.");
}
Set names = (Set) rawResult;
String domainPrefix = domain + ".";
int count = 0;
System.out.println("OK:ContainerListBegin");
Iterator iterator = names.iterator();
while (iterator.hasNext())
{
Object entry = iterator.next();
if (!(entry instanceof ObjectName))
{
continue;
}
ObjectName objectName = (ObjectName) entry;
String canonical = objectName.getCanonicalName();
if (canonical == null)
{
continue;
}
// Erwartet: domain.containerName:ID=AGENT
if (!canonical.regionMatches(
true,
0,
domainPrefix,
0,
domainPrefix.length()))
{
continue;
}
int colon = canonical.indexOf(':');
if (colon <= domainPrefix.length())
{
continue;
}
String containerName =
canonical.substring(
domainPrefix.length(),
colon);
if (isBlank(containerName))
{
continue;
}
System.out.println(
"CONTAINER="
+ containerName.trim());
count++;
}
System.out.println(
"OK:ContainerListEnd count="
+ count
+ " domain="
+ domain
+ " tool="
+ TOOL_VERSION);
System.out.flush();
}
private static Object connect(
String url,
String user,
@@ -126,17 +244,9 @@ public final class SonicMfConta*nerTool
{
Hashtable environment = new Hashtable();
environment.put(
"ConnectionURLs",
url);
environment.put(
"DefaultUser",
user);
environment.put(
"DefaultPassword",
password);
environment.put("ConnectionURLs", url);
environment.put("DefaultUser", user);
environment.put("DefaultPassword", password);
Class addressClass = Class.forName(
"com.sonicsw.mf.jmx.client.JMSConnectorAddress");
@@ -160,18 +270,14 @@ public final class SonicMfConta*nerTool
.getConstructor(new Class[] {})
.newInstance(new Object[] {});
trySetTimeout(
connector,
timeoutMilliseconds);
trySetTimeout(connector, timeoutMilliseconds);
Method connectMethod =
findConnect(clientClass);
Method connectMethod = findConnect(clientClass);
if (connectMethod == null)
{
throw new IllegalStateException(
"JMSConnectorClient.connect(...) "
+ "wurde nicht gefunden.");
"JMSConnectorClient.connect(...) wurde nicht gefunden.");
}
Class[] parameterTypes =
@@ -202,33 +308,18 @@ public final class SonicMfConta*nerTool
return connector;
}
/**
* Führt ausschließlich eine lesende Prüfung aus.
*
* Die erfolgreiche Verbindung bestätigt bereits die Anmeldung.
* Zusätzlich wird versucht, die MBean-Information des Containers
* zu lesen. Es wird keine stop-, start- oder restart-Operation
* aufgerufen.
*/
private static void validateConnection(
Object connector,
String domain,
String container)
throws Exception
{
String shortName =
shortContainer(container);
String shortName = shortContainer(container);
ObjectName objectName =
new ObjectName(
domain
+ "."
+ shortName
+ ":ID=AGENT");
ObjectName objectName = new ObjectName(
domain + "." + shortName + ":ID=AGENT");
info(
"ValidatingObjectName="
+ objectName);
info("ValidatingObjectName=" + objectName);
Method getMBeanInfoMethod =
findMethod(
@@ -242,9 +333,7 @@ public final class SonicMfConta*nerTool
if (getMBeanInfoMethod == null)
{
throw new IllegalStateException(
"Die lesende MfApi-Methode "
+ "getMBeanInfo(ObjectName) "
+ "wurde nicht gefunden.");
"getMBeanInfo(ObjectName) wurde nicht gefunden.");
}
Object result = getMBeanInfoMethod.invoke(
@@ -278,61 +367,34 @@ public final class SonicMfConta*nerTool
String container)
throws Exception
{
String shortName =
shortContainer(container);
String shortName = shortContainer(container);
ObjectName objectName =
new ObjectName(
domain
+ "."
+ shortName
+ ":ID=AGENT");
ObjectName objectName = new ObjectName(
domain + "." + shortName + ":ID=AGENT");
info(
"ObjectName="
+ objectName);
info("ObjectName=" + objectName);
StringBuffer attempts =
new StringBuffer();
String[] operations =
new String[]
{
"stop",
"restart"
};
for (int index = 0;
index < operations.length;
index++)
StringBuffer attempts = new StringBuffer();
String[] operations = new String[]
{
String operation =
operations[index];
"stop",
"restart"
};
for (int index = 0; index < operations.length; index++)
{
String operation = operations[index];
try
{
info(
"Trying MBean.invoke("
+ operation
+ ")");
invokeNoArgs(
connector,
objectName,
operation);
okRestart(
"MBean." + operation,
shortName,
domain);
info("Trying MBean.invoke(" + operation + ")");
invokeNoArgs(connector, objectName, operation);
okRestart("MBean." + operation, shortName, domain);
return;
}
catch (Throwable throwable)
{
Throwable root =
rootOf(throwable);
Throwable root = rootOf(throwable);
String line =
"MBean."
+ operation
@@ -342,30 +404,12 @@ public final class SonicMfConta*nerTool
+ root.getMessage();
warn(line);
attempts
.append(line)
.append('\n');
if ("stop".equals(operation)
&& looksLikeStopSideEffect(root))
{
okRestart(
"MBean.stop(side-effect)",
shortName,
domain);
return;
}
attempts.append(line).append('\n');
}
}
throw new IllegalStateException(
"Neustart fehlgeschlagen. "
+ "ToolVersion="
+ TOOL_VERSION
+ "\nAttempts:\n"
+ attempts.toString());
"Neustart fehlgeschlagen:\n" + attempts.toString());
}
private static void invokeNoArgs(
@@ -375,7 +419,8 @@ public final class SonicMfConta*nerTool
throws Exception
{
Method invokeMethod =
connector.getClass().getMethod(
findMethod(
connector.getClass(),
"invoke",
new Class[]
{
@@ -385,86 +430,34 @@ public final class SonicMfConta*nerTool
String[].class
});
if (invokeMethod == null)
{
throw new IllegalStateException(
"invoke(...) wurde nicht gefunden.");
}
invokeMethod.invoke(
connector,
new Object[]
{
objectName,
operation,
new Object[0],
new String[0]
null,
null
});
}
private static void disconnect(
Object connector)
{
if (connector == null)
{
return;
}
try
{
connector
.getClass()
.getMethod("disconnect")
.invoke(connector);
}
catch (Exception ignored)
{
// Die eigentliche Prüfung oder der Neustart
// darf durch einen Disconnect-Fehler nicht
// überschrieben werden.
}
}
private static Method findMethod(
Class targetClass,
String methodName,
Class[] parameterTypes)
{
try
{
return targetClass.getMethod(
methodName,
parameterTypes);
}
catch (Exception ignored)
{
return null;
}
}
private static boolean looksLikeStopSideEffect(
Throwable root)
{
if (root == null
|| root.getMessage() == null)
{
return false;
}
String message =
root.getMessage().toLowerCase();
return message.indexOf("disconnect") >= 0
|| message.indexOf("closed") >= 0
|| message.indexOf("not connected") >= 0
|| message.indexOf("connection lost") >= 0;
}
private static void okRestart(
String method,
String shortName,
String path,
String container,
String domain)
{
System.out.println(
"OK:RestartInvoked"
+ " method="
+ method
+ " path="
+ path
+ " container="
+ shortName
+ container
+ " domain="
+ domain
+ " tool="
@@ -473,43 +466,24 @@ public final class SonicMfConta*nerTool
System.out.flush();
}
private static Method findConnect(
Class clientClass)
private static void disconnect(Object connector)
{
Method[] methods =
clientClass.getMethods();
Method bestMethod = null;
for (int index = 0;
index < methods.length;
index++)
try
{
Method method =
methods[index];
findMethod(
connector.getClass(),
"disconnect",
new Class[] {});
if (!"connect".equals(
method.getName()))
if (method != null)
{
continue;
}
Class[] parameterTypes =
method.getParameterTypes();
if (parameterTypes.length == 1
|| parameterTypes.length == 2)
{
bestMethod = method;
if (parameterTypes.length == 2)
{
return method;
}
method.invoke(connector, new Object[] {});
}
}
return bestMethod;
catch (Throwable ignored)
{
}
}
private static void trySetTimeout(
@@ -518,238 +492,197 @@ public final class SonicMfConta*nerTool
{
try
{
connector
.getClass()
.getMethod(
Method method =
findMethod(
connector.getClass(),
"setTimeout",
new Class[]
{
Long.TYPE
})
.invoke(
});
if (method != null)
{
method.invoke(
connector,
new Object[]
{
Long.valueOf(
timeoutMilliseconds)
Long.valueOf(timeoutMilliseconds)
});
}
}
catch (Exception ignored)
catch (Throwable ignored)
{
try
}
}
private static Method findConnect(Class clientClass)
{
Method[] methods = clientClass.getMethods();
for (int index = 0; index < methods.length; index++)
{
Method method = methods[index];
if (!"connect".equals(method.getName()))
{
connector
.getClass()
.getMethod(
"setRequestTimeout",
new Class[]
{
Long.TYPE
})
.invoke(
connector,
new Object[]
{
Long.valueOf(
timeoutMilliseconds)
});
continue;
}
catch (Exception ignoredSecond)
Class[] types = method.getParameterTypes();
if (types.length == 1 || types.length == 2)
{
// Optional, nicht jede Sonic-Version
// besitzt diese Methode.
return method;
}
}
return null;
}
private static Method findMethod(
Class type,
String name,
Class[] parameterTypes)
{
try
{
return type.getMethod(name, parameterTypes);
}
catch (NoSuchMethodException exception)
{
return null;
}
}
private static Object boxTimeout(
Class type,
Class parameterType,
long timeoutMilliseconds)
{
if (type == Integer.TYPE
|| type == Integer.class)
if (parameterType == Integer.TYPE
|| parameterType == Integer.class)
{
return Integer.valueOf(
(int) Math.min(
Integer.MAX_VALUE,
timeoutMilliseconds));
long seconds =
Math.max(1L, timeoutMilliseconds / 1000L);
if (seconds > Integer.MAX_VALUE)
{
seconds = Integer.MAX_VALUE;
}
return Integer.valueOf((int) seconds);
}
return Long.valueOf(
timeoutMilliseconds);
return Long.valueOf(timeoutMilliseconds);
}
private static String shortContainer(
String container)
private static String shortContainer(String container)
{
String value =
container.trim();
String value = container.trim();
int colon = value.indexOf(':');
int dotIndex =
value.lastIndexOf('.');
if (colon > 0)
{
value = value.substring(0, colon);
}
return dotIndex >= 0
? value.substring(dotIndex + 1)
: value;
int dot = value.lastIndexOf('.');
if (dot >= 0 && dot < value.length() - 1)
{
value = value.substring(dot + 1);
}
return value;
}
private static Throwable rootOf(
Throwable throwable)
private static Throwable rootOf(Throwable throwable)
{
Throwable current =
throwable;
Throwable current = throwable;
while (current.getCause() != null
&& current.getCause() != current)
{
current =
current.getCause();
current = current.getCause();
}
return current;
}
private static boolean isBlank(
String value)
private static boolean isBlank(String value)
{
return value == null
|| value.trim().length() == 0;
return value == null || value.trim().length() == 0;
}
private static void info(
String message)
private static void info(String message)
{
System.out.println(
"INFO:" + message);
System.out.flush();
System.err.println("INFO: " + message);
}
private static void warn(
String message)
private static void warn(String message)
{
System.out.println(
"WARN:" + message);
System.out.flush();
System.err.println("WARN: " + message);
}
private static void fail(
String message)
private static void fail(String message)
{
System.err.println(
"ERROR:" + message);
System.err.flush();
System.err.println("ERROR: " + message);
System.exit(1);
}
private static final class Args
{
String command;
String domain;
String url;
String user;
String password;
String container;
private String command;
private String domain;
private String url;
private String user;
private String password;
private String container;
private int timeoutSec = 60;
int timeoutSec = 120;
static Args parse(
String[] arguments)
private static Args parse(String[] args)
{
Args parsedArguments =
new Args();
Args result = new Args();
if (arguments == null
|| arguments.length == 0)
if (args == null || args.length == 0)
{
return parsedArguments;
fail("Kein Befehl angegeben.");
}
parsedArguments.command =
arguments[0];
result.command = args[0];
result.password =
System.getenv("ESB_SONIC_PASSWORD");
for (int index = 1;
index < arguments.length;
index++)
for (int index = 1; index < args.length; index++)
{
String key =
arguments[index];
String token = args[index];
String value =
index + 1 < arguments.length
? arguments[index + 1]
: null;
if ("--domain".equals(key)
&& value != null)
if ("--domain".equals(token) && index + 1 < args.length)
{
parsedArguments.domain = value;
index++;
result.domain = args[++index];
}
else if ("--url".equals(key)
&& value != null)
else if ("--url".equals(token) && index + 1 < args.length)
{
parsedArguments.url = value;
index++;
result.url = args[++index];
}
else if ("--user".equals(key)
&& value != null)
else if ("--user".equals(token) && index + 1 < args.length)
{
parsedArguments.user = value;
index++;
result.user = args[++index];
}
else if ("--password".equals(key)
&& value != null)
else if ("--container".equals(token)
&& index + 1 < args.length)
{
parsedArguments.password = value;
index++;
result.container = args[++index];
}
else if ("--container".equals(key)
&& value != null)
else if ("--timeout".equals(token)
&& index + 1 < args.length)
{
parsedArguments.container = value;
index++;
}
else if ("--timeout".equals(key)
&& value != null)
{
try
{
parsedArguments.timeoutSec =
Integer.parseInt(value);
}
catch (Exception ignored)
{
// Standardwert beibehalten.
}
index++;
result.timeoutSec =
Integer.parseInt(args[++index]);
}
}
if (isBlank(
parsedArguments.password))
{
String environmentPassword =
System.getenv(
"ESB_SONIC_PASSWORD");
if (!isBlank(
environmentPassword))
{
parsedArguments.password =
environmentPassword;
}
}
return parsedArguments;
return result;
}
}
private SonicMfContainerTool()
{
}
}
}