Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Launch method to convert input strings #497

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/net/KNet/KNetCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using System.Collections.Generic;
using System.IO;
using MASES.JNet;
using MASES.JCOBridge.C2JBridge;
using System.Linq;

namespace MASES.KNet
{
Expand Down Expand Up @@ -308,6 +310,67 @@ protected override IList<string> PathToParse
}
}

/// <summary>
/// Launch the <typeparamref name="TClass"/> class with the <paramref name="args"/> arguments
/// </summary>
/// <typeparam name="TClass">A type which is defined as Main-Class</typeparam>
/// <param name="args">The arguments of the main method</param>
public static new void Launch<TClass>(params string[] args)
where TClass : IJVMBridgeMain
{
Launch(typeof(TClass), args);
}

/// <summary>
/// Launch the <paramref name="type"/> with the <paramref name="args"/> arguments
/// </summary>
/// <param name="type">The <see cref="Type"/> extending <see cref="IJVMBridgeMain"/></param>
/// <param name="args">The arguments of the main method</param>
public static new void Launch(Type type, params string[] args)
masesdevelopers marked this conversation as resolved.
Show resolved Hide resolved
{
if (type == null) throw new ArgumentNullException(nameof(type));

try
{
JNetCore<T>.Launch(type, args);
}
catch (ArgumentException)
{
if (type.GetInterface(typeof(IJVMBridgeMain).Name) == null) throw;
var execType = type;
do
{
if (args.Length == 0)
{
System.Reflection.MethodInfo method = execType.GetMethods().FirstOrDefault(method => method.Name == "SExecute" & method.GetParameters().Length == 2 & method.IsGenericMethod == false);
if (method != null)
{
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;
}
while (execType != null && execType != typeof(object));

}
throw new ArgumentException($"{type} does not define any IJVMBridgeMain type or interface", "type");
}

#if DEBUG
/// <inheritdoc cref="JNetCoreBase{T}.EnableDebug"/>
public override bool EnableDebug => true;
Expand Down
2 changes: 1 addition & 1 deletion src/net/KNetCLI/KNetCLICore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected override string[] ProcessCommandLine()

PrepareMainClassToRun(ClassToRun);

switch (ClassToRun.ToLowerInvariant())
switch (ClassToRun?.ToLowerInvariant())
{
case "verifiableconsumer":
ApplicationHeapSize = "512M";
Expand Down
6 changes: 4 additions & 2 deletions src/net/KNetConnect/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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] <JCOBridgeArguments> <ClassArguments>");
Console.WriteLine(assembly.GetName().Name + " -[d|s] [-k] connect-standalone.properties [-KafkaLocation kafkaFolder] <JCOBridgeArguments> <ClassArguments>");
Console.WriteLine();
if (!string.IsNullOrEmpty(errorString))
{
Expand All @@ -88,14 +88,16 @@ 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.");
Console.WriteLine("JCOBridgeArguments: the arguments of JCOBridge (see online at https://www.jcobridge.com/net-examples/command-line-options/ for the possible values). ");
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");
}
}
}
2 changes: 1 addition & 1 deletion src/net/KNetPS/Cmdlet/KafkaClassToRunCmdletCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void OnAfterCreateGlobalInstance()

try
{
JNetPSHelper<KNetPSCore>.Launch(KNetPSCore.MainClassToRun, arguments);
KNetPSHelper<KNetPSCore>.Launch(KNetPSCore.MainClassToRun, arguments);
}
catch (TargetInvocationException tie)
{
Expand Down
5 changes: 5 additions & 0 deletions src/net/KNetPS/KNetPSHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ public static class KNetPSHelper<TClass> where TClass : KNetCore<TClass>
public static void SetScalaVersion(string scalaVersion) { JNetPSHelper<TClass>.Set(typeof(KNetCore<>), nameof(KNetPSCore.ApplicationScalaVersion), scalaVersion); }

public static void SetDisableJMX(bool? disableJMX) { JNetPSHelper<TClass>.Set(typeof(KNetCore<>), nameof(KNetPSCore.ApplicationDisableJMX), disableJMX); }

public static void Launch(System.Type type, params string[] args)
{
typeof(TClass).RunStaticMethodOn(typeof(KNetCore<TClass>), "Launch", type, args);
}
}
}