From e12cd9bf4ff8d8d907e1366bfe85ff6aed7ad4c9 Mon Sep 17 00:00:00 2001 From: masesdevelopers <94312179+masesdevelopers@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:36:04 +0200 Subject: [PATCH 1/2] Update Launch method to convert input strings --- src/net/KNet/KNetCore.cs | 50 ++++++++++++++++++++++++++++++++++ src/net/KNetCLI/KNetCLICore.cs | 2 +- src/net/KNetConnect/Program.cs | 6 ++-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/net/KNet/KNetCore.cs b/src/net/KNet/KNetCore.cs index a8b5faee33..21a8d79911 100644 --- a/src/net/KNet/KNetCore.cs +++ b/src/net/KNet/KNetCore.cs @@ -21,6 +21,7 @@ using System.Collections.Generic; using System.IO; using MASES.JNet; +using MASES.JCOBridge.C2JBridge; namespace MASES.KNet { @@ -308,6 +309,55 @@ protected override IList PathToParse } } + /// + /// Launch the class with the arguments + /// + /// A type which is defined as Main-Class + /// The arguments of the main method + public static new void Launch(params string[] args) + where TClass : IJVMBridgeMain + { + Launch(typeof(TClass), args); + } + + /// + /// Launch the with the arguments + /// + /// The extending + /// The arguments of the main method + public static new void Launch(Type type, params string[] args) + { + if (type == null) throw new ArgumentNullException(nameof(type)); + + try + { + JNetCore.Launch(type, args); + } + catch (ArgumentException) + { + if (type.GetInterface(typeof(IJVMBridgeMain).Name) == null) throw; + var execType = type; + do + { + System.Reflection.MethodInfo method = execType.GetMethod("Main", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); + if (method != null) + { + Java.Lang.String[] strings = new Java.Lang.String[args.Length]; + for (int i = 0; i < args.Length; i++) + { + strings[i] = args[i]; + } + method.Invoke(null, new object[] { strings }); + return; + } + execType = execType.BaseType; + } + while (execType != null && execType != typeof(object)); + + } + throw new ArgumentException($"{type} does not define any IJVMBridgeMain type or interface", "type"); + } + #if DEBUG /// public override bool EnableDebug => true; diff --git a/src/net/KNetCLI/KNetCLICore.cs b/src/net/KNetCLI/KNetCLICore.cs index dd3371e0d3..ce887e741b 100644 --- a/src/net/KNetCLI/KNetCLICore.cs +++ b/src/net/KNetCLI/KNetCLICore.cs @@ -201,7 +201,7 @@ protected override string[] ProcessCommandLine() PrepareMainClassToRun(ClassToRun); - switch (ClassToRun.ToLowerInvariant()) + switch (ClassToRun?.ToLowerInvariant()) { case "verifiableconsumer": ApplicationHeapSize = "512M"; diff --git a/src/net/KNetConnect/Program.cs b/src/net/KNetConnect/Program.cs index 04c392483a..9bb6986372 100644 --- a/src/net/KNetConnect/Program.cs +++ b/src/net/KNetConnect/Program.cs @@ -79,7 +79,7 @@ static void ShowHelp(string errorString = null) var assembly = typeof(Program).Assembly; Console.WriteLine("KNetConnect - KNet Connect command line interface - Version " + assembly.GetName().Version.ToString()); - Console.WriteLine(assembly.GetName().Name + " -[d|s] connect-standalone.properties [-KafkaLocation kafkaFolder] "); + Console.WriteLine(assembly.GetName().Name + " -[d|s] [-k] connect-standalone.properties [-KafkaLocation kafkaFolder] "); Console.WriteLine(); if (!string.IsNullOrEmpty(errorString)) { @@ -88,6 +88,7 @@ static void ShowHelp(string errorString = null) Console.WriteLine("s: start Connect in standalone mode. "); Console.WriteLine("d: start Connect in distributed mode. "); + Console.WriteLine("k: start Connect in distributed/standalone mode using KNet version. "); Console.WriteLine("KafkaLocation: The folder where Kafka package is available. Default consider this application uses the package jars folder."); Console.WriteLine("ScalaVersion: the scala version to be used. The default version (2.13.6) is binded to the deafult Apache Kafka version available in the package."); Console.WriteLine("Log4JConfiguration: the log4j configuration file; the default uses the file within the package."); @@ -95,7 +96,8 @@ static void ShowHelp(string errorString = null) Console.WriteLine("ClassArguments: the arguments of the class. Depends on the ClassToRun value, to obtain them runs the application or look at Apache Kafka documentation."); Console.WriteLine(); Console.WriteLine("Examples:"); - Console.WriteLine(assembly.GetName().Name + " -ClassToRun ConsoleConsumer --bootstrap-server SERVER-ADDRESS:9093 --topic topic_name --from-beginning"); + Console.WriteLine(assembly.GetName().Name + " -s connect-standalone.properties specific-connector.properties"); + Console.WriteLine(assembly.GetName().Name + " -d connect-distributed.properties"); } } } \ No newline at end of file From 853489945f1256f4cb1e8946a05e6375e3d7ce2d Mon Sep 17 00:00:00 2001 From: masesdevelopers <94312179+masesdevelopers@users.noreply.github.com> Date: Wed, 12 Jun 2024 19:15:00 +0200 Subject: [PATCH 2/2] Update command execution --- src/net/KNet/KNetCore.cs | 27 ++++++++++++++----- .../KafkaClassToRunCmdletCommandBase.cs | 2 +- src/net/KNetPS/KNetPSHelper.cs | 5 ++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/net/KNet/KNetCore.cs b/src/net/KNet/KNetCore.cs index 21a8d79911..d23250f74c 100644 --- a/src/net/KNet/KNetCore.cs +++ b/src/net/KNet/KNetCore.cs @@ -22,6 +22,7 @@ using System.IO; using MASES.JNet; using MASES.JCOBridge.C2JBridge; +using System.Linq; namespace MASES.KNet { @@ -339,15 +340,27 @@ protected override IList PathToParse var execType = type; do { - System.Reflection.MethodInfo method = execType.GetMethod("Main", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); - if (method != null) + if (args.Length == 0) { - Java.Lang.String[] strings = new Java.Lang.String[args.Length]; - for (int i = 0; i < args.Length; i++) + System.Reflection.MethodInfo method = execType.GetMethods().FirstOrDefault(method => method.Name == "SExecute" & method.GetParameters().Length == 2 & method.IsGenericMethod == false); + if (method != null) { - strings[i] = args[i]; - } - method.Invoke(null, new object[] { strings }); + method.Invoke(null, new object[] { "main", new object[] { args } }); + return; + } + } + else + { + System.Reflection.MethodInfo method = execType.GetMethod("Main", new Type[] { typeof(Java.Lang.String[]) }); + if (method != null) + { + Java.Lang.String[] strings = new Java.Lang.String[args.Length]; + for (int i = 0; i < args.Length; i++) + { + strings[i] = args[i]; + } + method.Invoke(null, new object[] { strings }); + } return; } execType = execType.BaseType; diff --git a/src/net/KNetPS/Cmdlet/KafkaClassToRunCmdletCommandBase.cs b/src/net/KNetPS/Cmdlet/KafkaClassToRunCmdletCommandBase.cs index 2a14a62f88..d851177f95 100644 --- a/src/net/KNetPS/Cmdlet/KafkaClassToRunCmdletCommandBase.cs +++ b/src/net/KNetPS/Cmdlet/KafkaClassToRunCmdletCommandBase.cs @@ -50,7 +50,7 @@ protected override void OnAfterCreateGlobalInstance() try { - JNetPSHelper.Launch(KNetPSCore.MainClassToRun, arguments); + KNetPSHelper.Launch(KNetPSCore.MainClassToRun, arguments); } catch (TargetInvocationException tie) { diff --git a/src/net/KNetPS/KNetPSHelper.cs b/src/net/KNetPS/KNetPSHelper.cs index 6dbc3bc0ca..59821bcb8a 100644 --- a/src/net/KNetPS/KNetPSHelper.cs +++ b/src/net/KNetPS/KNetPSHelper.cs @@ -37,5 +37,10 @@ public static class KNetPSHelper where TClass : KNetCore public static void SetScalaVersion(string scalaVersion) { JNetPSHelper.Set(typeof(KNetCore<>), nameof(KNetPSCore.ApplicationScalaVersion), scalaVersion); } public static void SetDisableJMX(bool? disableJMX) { JNetPSHelper.Set(typeof(KNetCore<>), nameof(KNetPSCore.ApplicationDisableJMX), disableJMX); } + + public static void Launch(System.Type type, params string[] args) + { + typeof(TClass).RunStaticMethodOn(typeof(KNetCore), "Launch", type, args); + } } } \ No newline at end of file