AtsJavaCodeGenerator.cs (317)
625WriteLine("// Aspire.java - Capability-based Aspire SDK");
626WriteLine("// GENERATED CODE - DO NOT EDIT");
627WriteLine();
628WriteLine("package aspire;");
629WriteLine();
634WriteLine("import java.util.ArrayList;");
635WriteLine("import java.util.HashMap;");
636WriteLine("import java.util.LinkedHashMap;");
637WriteLine("import java.util.List;");
638WriteLine("import java.util.Map;");
639WriteLine("import java.util.Set;");
640WriteLine("import java.util.UUID;");
641WriteLine("import java.util.function.BiFunction;");
642WriteLine("import java.util.function.Consumer;");
643WriteLine("import java.util.function.Function;");
644WriteLine();
659WriteLine("// ============================================================================");
660WriteLine("// Enums");
661WriteLine("// ============================================================================");
662WriteLine();
672WriteLine($"/** {enumType.Name} enum. */");
673WriteLine($"enum {enumName} implements WireValueEnum {{");
695WriteLine($" {memberName}(\"{member}\"){suffix}");
697WriteLine();
698WriteLine(" private final String value;");
699WriteLine();
700WriteLine($" {enumName}(String value) {{");
701WriteLine(" this.value = value;");
702WriteLine(" }");
703WriteLine();
704WriteLine(" public String getValue() { return value; }");
705WriteLine();
706WriteLine($" public static {enumName} fromValue(String value) {{");
707WriteLine($" for ({enumName} e : values()) {{");
708WriteLine(" if (e.value.equals(value)) return e;");
709WriteLine(" }");
710WriteLine(" throw new IllegalArgumentException(\"Unknown value: \" + value);");
711WriteLine(" }");
712WriteLine("}");
713WriteLine();
724WriteLine("// ============================================================================");
725WriteLine("// DTOs");
726WriteLine("// ============================================================================");
727WriteLine();
738WriteLine($"/** {dto.Name} DTO. */");
739WriteLine($"class {dtoName} implements JsonSerializable {{");
746WriteLine($" private {fieldType} {fieldName};");
748WriteLine();
756WriteLine($" public {fieldType} get{methodName}() {{ return {fieldName}; }}");
757WriteLine($" public void set{methodName}({fieldType} value) {{ this.{fieldName} = value; }}");
759WriteLine();
761WriteLine(" @SuppressWarnings(\"unchecked\")");
762WriteLine($" public static {dtoName} fromMap(Map<String, Object> map) {{");
763WriteLine($" var value = new {dtoName}();");
776WriteLine($" var {transportValueName} = map.get(\"{property.Name}\");");
777WriteLine($" value.set{methodName}({RenderJavaDtoPropertyTransportValueConversion(property.Type, transportValueName, property.IsOptional)});");
779WriteLine(" return value;");
780WriteLine(" }");
781WriteLine();
784WriteLine(" public Map<String, Object> toMap() {");
785WriteLine(" Map<String, Object> map = new HashMap<>();");
795WriteLine($" map.put(\"{property.Name}\", AspireClient.serializeValue({fieldName}));");
798WriteLine(" return map;");
799WriteLine(" }");
801WriteLine("}");
802WriteLine();
817WriteLine("// ============================================================================");
818WriteLine("// Exported Values");
819WriteLine("// ============================================================================");
820WriteLine();
824WriteLine($"final class {name} {{");
825WriteLine($" private {name}() {{ }}");
826WriteLine();
828WriteLine("}");
829WriteLine();
846WriteLine($"{indent}/** {valueInfo.Description} */");
851WriteLine($"{indent}public static final {javaType} {name} = {expression};");
855WriteLine($"{indent}public static final class {name} {{");
856WriteLine($"{indent} private {name}() {{ }}");
857WriteLine();
859WriteLine($"{indent}}}");
862WriteLine();
1053WriteLine("// ============================================================================");
1054WriteLine("// Options Types");
1055WriteLine("// ============================================================================");
1056WriteLine();
1060WriteLine($"/** Options for {className[..^"Options".Length]}. */");
1061WriteLine($"final class {className} {{");
1065WriteLine($" private {MapParameterToJava(parameter)} {parameterName};");
1067WriteLine();
1073WriteLine($" public {parameterType} {GetOptionGetterName(parameter)}() {{ return {parameterName}; }}");
1074WriteLine($" public {className} {parameterName}({parameterType} value) {{");
1075WriteLine($" this.{parameterName} = value;");
1076WriteLine(" return this;");
1077WriteLine(" }");
1078WriteLine();
1081WriteLine("}");
1082WriteLine();
1223WriteLine($" public {returnType} {methodName}({signature}) {{");
1226WriteLine($" return {methodName}({string.Join(", ", callArguments)});");
1230WriteLine($" {methodName}({string.Join(", ", callArguments)});");
1232WriteLine(" }");
1233WriteLine();
1259WriteLine("// ============================================================================");
1260WriteLine("// Handle Wrappers");
1261WriteLine("// ============================================================================");
1262WriteLine();
1266WriteLine($"/** Wrapper for {handleType.TypeId}. */");
1267WriteLine($"class {handleType.ClassName} extends {handleType.BaseClassName} {{");
1268WriteLine($" {handleType.ClassName}(Handle handle, AspireClient client) {{");
1269WriteLine(" super(handle, client);");
1270WriteLine(" }");
1271WriteLine();
1291WriteLine("}");
1292WriteLine();
1299WriteLine(" /** Gets the input with the specified name, or null if no input matches. */");
1300WriteLine(" public InteractionInput get(String name) {");
1301WriteLine(" for (var input : toArray()) {");
1302WriteLine(" if (input.getName() != null && input.getName().equalsIgnoreCase(name)) {");
1303WriteLine(" return input;");
1304WriteLine(" }");
1305WriteLine(" }");
1306WriteLine(" return null;");
1307WriteLine(" }");
1308WriteLine();
1310WriteLine(" /** Gets the input with the specified name, or throws if no input matches. */");
1311WriteLine(" public InteractionInput required(String name) {");
1312WriteLine(" var input = get(name);");
1313WriteLine(" if (input == null) {");
1314WriteLine(" throw new IllegalArgumentException(\"no input with name '\" + name + \"' was found\");");
1315WriteLine(" }");
1316WriteLine(" return input;");
1317WriteLine(" }");
1318WriteLine();
1320WriteLine(" /** Gets the value of the input with the specified name, or an empty string if no input matches or it has no value. */");
1321WriteLine(" public String value(String name) {");
1322WriteLine(" var input = get(name);");
1323WriteLine(" return input == null || input.getValue() == null ? \"\" : input.getValue();");
1324WriteLine(" }");
1325WriteLine();
1327WriteLine(" /** Gets the value of the input with the specified name, or throws if no input matches. */");
1328WriteLine(" public String requiredValue(String name) {");
1329WriteLine(" return required(name).getValue();");
1330WriteLine(" }");
1331WriteLine();
1340WriteLine(" /** Create a new distributed application builder. */");
1341WriteLine($" public static {builderClassName} CreateBuilder() throws Exception {{");
1342WriteLine(" return CreateBuilder((String[]) null);");
1343WriteLine(" }");
1344WriteLine();
1345WriteLine(" /** Create a new distributed application builder. */");
1346WriteLine($" public static {builderClassName} CreateBuilder(String[] args) throws Exception {{");
1347WriteLine(" CreateBuilderOptions options = new CreateBuilderOptions();");
1348WriteLine(" if (args != null) {");
1349WriteLine(" options.setArgs(args);");
1350WriteLine(" }");
1351WriteLine(" return CreateBuilder(options);");
1352WriteLine(" }");
1353WriteLine();
1354WriteLine(" /** Create a new distributed application builder. */");
1355WriteLine($" public static {builderClassName} CreateBuilder(CreateBuilderOptions options) throws Exception {{");
1356WriteLine(" return Aspire.createBuilder(options);");
1357WriteLine(" }");
1358WriteLine();
1445WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1452WriteLine($" return {methodName}({callArguments});");
1456WriteLine($" {methodName}({callArguments});");
1458WriteLine(" }");
1459WriteLine();
1508WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1511WriteLine($" return {methodName}({callArguments});");
1515WriteLine($" {methodName}({callArguments});");
1517WriteLine(" }");
1518WriteLine();
1553WriteLine($" public {returnInfo.ReturnType} {methodName}({parameterList}) {{");
1570WriteLine($" return {methodName}({string.Join(", ", callArguments)});");
1574WriteLine($" {methodName}({string.Join(", ", callArguments)});");
1576WriteLine(" }");
1577WriteLine();
1641WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1648WriteLine($" return {methodName}({callArguments}, options);");
1652WriteLine($" {methodName}({callArguments}, options);");
1654WriteLine(" }");
1655WriteLine();
1666WriteLine($" public {returnInfo.ReturnType} {methodName}({string.Join(", ", requiredParameters.Select(parameter => ReferenceEquals(parameter, unionParameter) ? $"{MapInputTypeToJava(unionType, unionParameter.IsOptional || unionParameter.IsNullable)} {ToCamelCase(parameter.Name)}" : $"{MapParameterToJava(parameter)} {ToCamelCase(parameter.Name)}"))}) {{");
1669WriteLine($" return {methodName}({callArguments});");
1673WriteLine($" {methodName}({callArguments});");
1675WriteLine(" }");
1676WriteLine();
1746WriteLine($" /** {capability.Description} */");
1749WriteLine($" public {returnInfo.ReturnType} {methodName}({publicParameterList}) {{");
1753WriteLine($" var {paramName} = optionsBag == null ? null : optionsBag.{GetOptionGetterName(parameter)}();");
1763WriteLine($" return {implementationMethodName}({string.Join(", ", implementationArguments)});");
1767WriteLine($" {implementationMethodName}({string.Join(", ", implementationArguments)});");
1769WriteLine(" }");
1770WriteLine();
1780WriteLine($" public {returnInfo.ReturnType} {methodName}({requiredParameterList}) {{");
1783WriteLine($" return {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});");
1787WriteLine($" {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});");
1789WriteLine(" }");
1790WriteLine();
1813WriteLine($" /** {capability.Description} */");
1817WriteLine($" {accessibility} {returnInfo.ReturnType} {methodName}({paramList}) {{");
1818WriteLine(" Map<String, Object> reqArgs = new HashMap<>();");
1819WriteLine($" reqArgs.put(\"{targetParamName}\", AspireClient.serializeValue(getHandle()));");
1827WriteLine($" if ({paramName}Id != null) {{");
1828WriteLine($" reqArgs.put(\"{parameter.Name}\", {paramName}Id);");
1829WriteLine(" }");
1835WriteLine($" if ({paramName} != null) {{");
1836WriteLine($" reqArgs.put(\"{parameter.Name}\", {paramName});");
1837WriteLine(" }");
1843WriteLine($" if ({paramName} != null) {{");
1844WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));");
1845WriteLine(" }");
1849WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));");
1855WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1856WriteLine(" return this;");
1860WriteLine($" var result = getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1861WriteLine($" return {RenderJavaTransportValueConversion(capability.ReturnType, "result", capability.ReturnType?.IsNullable == true)};");
1865WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1868WriteLine(" }");
1869WriteLine();
1936WriteLine($" var {callbackName}Id = {callbackInitializer}getClient().registerCallback(args -> {{");
1938WriteLine(" });");
1952WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, i)};");
1960WriteLine($" return AspireClient.awaitValue({callbackInvocation});");
1964WriteLine($" {callbackInvocation};");
1971WriteLine($"{indent}var __aspireCallbackArguments = new HashMap<String, Object>();");
1974WriteLine($"{indent}__aspireCallbackArguments.put(\"p{i}\", {arguments[i]});");
1976WriteLine($"{indent}return __aspireCallbackArguments;");
2003WriteLine($" map.put(\"{property.Name}\", {fieldName} == null ? null : (java.util.function.Function<Object, Object>) (transportArg -> {{");
2007WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, "transportArg")};");
2014WriteLine($" return AspireClient.awaitValue({invocation});");
2018WriteLine($" {invocation};");
2021WriteLine(" }));");
2284WriteLine($" /** {capability.Description} */");
2288WriteLine($" private {fullType} {fieldName};");
2289WriteLine($" public {fullType} {methodName}() {{");
2290WriteLine($" if ({fieldName} == null) {{");
2291WriteLine($" {fieldName} = new {wrapperType}<>(getHandle(), getClient(), \"{capability.CapabilityId}\");");
2292WriteLine(" }");
2293WriteLine($" return {fieldName};");
2294WriteLine(" }");
2295WriteLine();
2302WriteLine("// ============================================================================");
2303WriteLine("// Handle wrapper registrations");
2304WriteLine("// ============================================================================");
2305WriteLine();
2306WriteLine("/** Static initializer to register handle wrappers. */");
2307WriteLine("class AspireRegistrations {");
2308WriteLine(" static {");
2312WriteLine($" AspireClient.registerHandleWrapper(\"{handleType.TypeId}\", (h, c) -> new {handleType.ClassName}(h, c));");
2324WriteLine($" AspireClient.registerHandleWrapper(\"{typeId}\", (h, c) -> new {wrapperType}<>(h, c));");
2327WriteLine(" }");
2328WriteLine();
2329WriteLine(" static void ensureRegistered() {");
2330WriteLine(" // Called to trigger static initializer");
2331WriteLine(" }");
2332WriteLine("}");
2333WriteLine();
2342WriteLine("// ============================================================================");
2343WriteLine("// Connection Helpers");
2344WriteLine("// ============================================================================");
2345WriteLine();
2346WriteLine("/** Main entry point for Aspire SDK. */");
2347WriteLine("public class Aspire {");
2348WriteLine(" /** Connect to the AppHost server. */");
2349WriteLine(" public static AspireClient connect() throws Exception {");
2350WriteLine(" BaseRegistrations.ensureRegistered();");
2351WriteLine(" AspireRegistrations.ensureRegistered();");
2352WriteLine(" String socketPath = System.getenv(\"REMOTE_APP_HOST_SOCKET_PATH\");");
2353WriteLine(" if (socketPath == null || socketPath.isEmpty()) {");
2354WriteLine(" throw new RuntimeException(\"REMOTE_APP_HOST_SOCKET_PATH environment variable not set. Run this application using `aspire run`.\");");
2355WriteLine(" }");
2356WriteLine(" AspireClient client = new AspireClient(socketPath);");
2357WriteLine(" client.connect();");
2358WriteLine(" String authToken = System.getenv(\"ASPIRE_REMOTE_APPHOST_TOKEN\");");
2359WriteLine(" if (authToken == null || authToken.isEmpty()) {");
2360WriteLine(" throw new RuntimeException(\"ASPIRE_REMOTE_APPHOST_TOKEN environment variable not set. Run this application using `aspire run`.\");");
2361WriteLine(" }");
2362WriteLine(" client.authenticate(authToken);");
2363WriteLine(" client.onDisconnect(() -> System.exit(1));");
2364WriteLine(" return client;");
2365WriteLine(" }");
2366WriteLine();
2367WriteLine($" /** Create a new distributed application builder. */");
2368WriteLine($" public static {builderClassName} createBuilder(CreateBuilderOptions options) throws Exception {{");
2369WriteLine(" AspireClient client = connect();");
2370WriteLine(" Map<String, Object> resolvedOptions = new HashMap<>();");
2371WriteLine(" if (options != null) {");
2372WriteLine(" resolvedOptions.putAll(options.toMap());");
2373WriteLine(" }");
2374WriteLine(" if (resolvedOptions.get(\"Args\") == null) {");
2383WriteLine(" String forwardedArgs = System.getenv(\"ASPIRE_APPHOST_ARGS\");");
2384WriteLine(" resolvedOptions.put(\"Args\", forwardedArgs == null || forwardedArgs.isEmpty()");
2385WriteLine(" ? new String[0]");
2386WriteLine(" : forwardedArgs.split(\"\\n\", -1));");
2387WriteLine(" }");
2390WriteLine(" if (resolvedOptions.get(\"ProjectDirectory\") == null) {");
2391WriteLine(" String projectDirectory = System.getenv(\"ASPIRE_PROJECT_DIRECTORY\");");
2392WriteLine(" if (projectDirectory == null || projectDirectory.isEmpty()) {");
2393WriteLine(" projectDirectory = System.getProperty(\"user.dir\");");
2394WriteLine(" }");
2395WriteLine(" resolvedOptions.put(\"ProjectDirectory\", projectDirectory);");
2396WriteLine(" }");
2397WriteLine(" if (resolvedOptions.get(\"AppHostFilePath\") == null) {");
2398WriteLine(" String appHostFilePath = System.getenv(\"ASPIRE_APPHOST_FILEPATH\");");
2399WriteLine(" if (appHostFilePath != null && !appHostFilePath.isEmpty()) {");
2400WriteLine(" resolvedOptions.put(\"AppHostFilePath\", appHostFilePath);");
2401WriteLine(" }");
2402WriteLine(" }");
2403WriteLine(" Map<String, Object> args = new HashMap<>();");
2404WriteLine(" args.put(\"argsOrOptions\", resolvedOptions);");
2405WriteLine($" return ({builderClassName}) client.invokeCapability(\"Aspire.Hosting/createBuilder\", args);");
2406WriteLine(" }");
2407WriteLine("}");
2408WriteLine();