Switch default restart to Sonic MF Management API via Domain Manager.
Use ConnectionUrl and SMC credentials with IAgentProxy.restart; keep WinRM only as optional fallback. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import com.sonicsw.mf.jmx.client.JMSConnectorAddress;
|
||||
import com.sonicsw.mf.jmx.client.JMSConnectorClient;
|
||||
import com.sonicsw.mf.mgmtapi.runtime.IAgentProxy;
|
||||
import com.sonicsw.mf.mgmtapi.runtime.MFProxyFactory;
|
||||
|
||||
/**
|
||||
* Sonic MF Management Runtime API helper (Aurea CX Messenger / Progress Sonic).
|
||||
*
|
||||
* Documented path (Management Application API):
|
||||
* Hashtable env with ConnectionURLs / DefaultUser / DefaultPassword
|
||||
* -> JMSConnectorAddress / JMSConnectorClient.connect
|
||||
* -> MFProxyFactory.createAgentProxy(connector, ObjectName)
|
||||
* -> IAgentProxy.restart() (same lifecycle action as SMC Restart)
|
||||
*
|
||||
* ObjectName pattern: {domain}.{container}:ID=AGENT
|
||||
*
|
||||
* Usage:
|
||||
* java -cp "...libs...;." SonicMfContainerTool ping|list|restart
|
||||
* --domain proalpha-test
|
||||
* --url tcp://host:13070
|
||||
* --user Administrator
|
||||
* --password *** (or env ESB_SONIC_PASSWORD)
|
||||
* [--container DE-Test] (required for restart)
|
||||
* [--timeout 120]
|
||||
*/
|
||||
public final class SonicMfContainerTool
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
Args a = Args.parse(args);
|
||||
if (a.command == null)
|
||||
{
|
||||
fail("Usage: SonicMfContainerTool ping|list|restart --domain D --url U --user U [--password P] [--container C] [--timeout SEC]");
|
||||
return;
|
||||
}
|
||||
|
||||
if (isBlank(a.domain) || isBlank(a.url) || isBlank(a.user))
|
||||
{
|
||||
fail("domain, url und user sind Pflicht.");
|
||||
return;
|
||||
}
|
||||
|
||||
if ("restart".equals(a.command) && isBlank(a.container))
|
||||
{
|
||||
fail("restart erfordert --container.");
|
||||
return;
|
||||
}
|
||||
|
||||
long timeoutMs = Math.max(5_000L, a.timeoutSec * 1000L);
|
||||
JMSConnectorClient connector = connect(a.url, a.user, a.password, timeoutMs);
|
||||
try
|
||||
{
|
||||
if ("ping".equals(a.command))
|
||||
{
|
||||
ping(connector, a.domain);
|
||||
}
|
||||
else if ("list".equals(a.command))
|
||||
{
|
||||
list(connector, a.domain);
|
||||
}
|
||||
else if ("restart".equals(a.command))
|
||||
{
|
||||
restart(connector, a.domain, a.container);
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("Unbekannter Befehl: " + a.command);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { connector.disconnect(); } catch (Exception ignore) { /* ignore */ }
|
||||
}
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
fail(t.getClass().getSimpleName() + ": " + t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static JMSConnectorClient connect(String url, String user, String password, long timeoutMs)
|
||||
throws Exception
|
||||
{
|
||||
Hashtable env = new Hashtable();
|
||||
env.put("ConnectionURLs", url);
|
||||
env.put("DefaultUser", user);
|
||||
env.put("DefaultPassword", password == null ? "" : password);
|
||||
|
||||
JMSConnectorAddress address = new JMSConnectorAddress(env);
|
||||
JMSConnectorClient connector = new JMSConnectorClient();
|
||||
connector.connect(address, timeoutMs);
|
||||
System.out.println("INFO:Connected url=" + url + " user=" + user);
|
||||
return connector;
|
||||
}
|
||||
|
||||
private static void ping(JMSConnectorClient connector, String domain) throws Exception
|
||||
{
|
||||
Set names = connector.queryNames(new ObjectName("*:ID=AGENT"), null);
|
||||
int matched = 0;
|
||||
if (names != null)
|
||||
{
|
||||
for (Iterator it = names.iterator(); it.hasNext(); )
|
||||
{
|
||||
ObjectName on = (ObjectName) it.next();
|
||||
if (belongsToDomain(on, domain))
|
||||
{
|
||||
matched++;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("OK:MfApiPing domain=" + domain + " agents=" + matched);
|
||||
}
|
||||
|
||||
private static void list(JMSConnectorClient connector, String domain) throws Exception
|
||||
{
|
||||
Set names = connector.queryNames(new ObjectName("*:ID=AGENT"), null);
|
||||
int count = 0;
|
||||
if (names != null)
|
||||
{
|
||||
for (Iterator it = names.iterator(); it.hasNext(); )
|
||||
{
|
||||
ObjectName on = (ObjectName) it.next();
|
||||
String container = extractContainer(on, domain);
|
||||
if (container != null)
|
||||
{
|
||||
System.out.println(container);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count == 0)
|
||||
{
|
||||
System.out.println("WARN:KeineAgentContainer Domain=" + domain);
|
||||
}
|
||||
System.out.println("OK:List count=" + count);
|
||||
}
|
||||
|
||||
private static void restart(JMSConnectorClient connector, String domain, String container) throws Exception
|
||||
{
|
||||
String shortName = shortContainer(container);
|
||||
ObjectName on = resolveAgentObjectName(connector, domain, shortName);
|
||||
System.out.println("INFO:ObjectName=" + on);
|
||||
|
||||
IAgentProxy agent = MFProxyFactory.createAgentProxy(connector, on);
|
||||
String stateBefore = safeState(agent);
|
||||
System.out.println("INFO:StateBefore=" + stateBefore);
|
||||
|
||||
agent.restart();
|
||||
System.out.println("OK:RestartInvoked container=" + shortName + " domain=" + domain
|
||||
+ " stateBefore=" + stateBefore);
|
||||
}
|
||||
|
||||
private static ObjectName resolveAgentObjectName(JMSConnectorClient connector, String domain, String container)
|
||||
throws Exception
|
||||
{
|
||||
ObjectName preferred = new ObjectName(domain + "." + container + ":ID=AGENT");
|
||||
Set exact = connector.queryNames(preferred, null);
|
||||
if (exact != null && !exact.isEmpty())
|
||||
{
|
||||
return (ObjectName) exact.iterator().next();
|
||||
}
|
||||
|
||||
Set names = connector.queryNames(new ObjectName("*:ID=AGENT"), null);
|
||||
if (names != null)
|
||||
{
|
||||
for (Iterator it = names.iterator(); it.hasNext(); )
|
||||
{
|
||||
ObjectName on = (ObjectName) it.next();
|
||||
String c = extractContainer(on, domain);
|
||||
if (c != null && c.equalsIgnoreCase(container))
|
||||
{
|
||||
return on;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: documented canonical name even if not yet in query result
|
||||
return preferred;
|
||||
}
|
||||
|
||||
private static boolean belongsToDomain(ObjectName on, String domain)
|
||||
{
|
||||
return extractContainer(on, domain) != null;
|
||||
}
|
||||
|
||||
private static String extractContainer(ObjectName on, String domain)
|
||||
{
|
||||
if (on == null || isBlank(domain))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String jmxDomain = on.getDomain();
|
||||
if (jmxDomain == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String prefix = domain + ".";
|
||||
if (jmxDomain.regionMatches(true, 0, prefix, 0, prefix.length()))
|
||||
{
|
||||
return jmxDomain.substring(prefix.length());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String shortContainer(String container)
|
||||
{
|
||||
if (container == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
int dot = container.indexOf('.');
|
||||
if (dot >= 0 && dot < container.length() - 1)
|
||||
{
|
||||
return container.substring(dot + 1);
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
private static String safeState(IAgentProxy agent)
|
||||
{
|
||||
try
|
||||
{
|
||||
String s = agent.getStateString();
|
||||
return s == null ? "?" : s;
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
private static void fail(String message)
|
||||
{
|
||||
System.err.println("ERROR:" + message);
|
||||
System.exit(2);
|
||||
}
|
||||
|
||||
private static boolean isBlank(String s)
|
||||
{
|
||||
return s == null || s.trim().length() == 0;
|
||||
}
|
||||
|
||||
private static final class Args
|
||||
{
|
||||
String command;
|
||||
String domain;
|
||||
String url;
|
||||
String user;
|
||||
String password;
|
||||
String container;
|
||||
int timeoutSec = 120;
|
||||
|
||||
static Args parse(String[] args)
|
||||
{
|
||||
Args a = new Args();
|
||||
if (args == null || args.length == 0)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
a.command = args[0].toLowerCase();
|
||||
for (int i = 1; i < args.length; i++)
|
||||
{
|
||||
String key = args[i];
|
||||
if (!key.startsWith("--") || i + 1 >= args.length)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
String val = args[++i];
|
||||
if ("--domain".equals(key)) a.domain = val;
|
||||
else if ("--url".equals(key)) a.url = val;
|
||||
else if ("--user".equals(key)) a.user = val;
|
||||
else if ("--password".equals(key)) a.password = val;
|
||||
else if ("--container".equals(key)) a.container = val;
|
||||
else if ("--timeout".equals(key))
|
||||
{
|
||||
try { a.timeoutSec = Integer.parseInt(val); } catch (Exception ignore) { /* keep default */ }
|
||||
}
|
||||
}
|
||||
|
||||
if (isBlank(a.password))
|
||||
{
|
||||
String env = System.getenv("ESB_SONIC_PASSWORD");
|
||||
if (!isBlank(env))
|
||||
{
|
||||
a.password = env;
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user