AtsJavaCodeGenerator.cs (317)
626WriteLine("// Aspire.java - Capability-based Aspire SDK");
627WriteLine("// GENERATED CODE - DO NOT EDIT");
628WriteLine();
629WriteLine("package aspire;");
630WriteLine();
635WriteLine("import java.util.ArrayList;");
636WriteLine("import java.util.HashMap;");
637WriteLine("import java.util.LinkedHashMap;");
638WriteLine("import java.util.List;");
639WriteLine("import java.util.Map;");
640WriteLine("import java.util.Set;");
641WriteLine("import java.util.UUID;");
642WriteLine("import java.util.function.BiFunction;");
643WriteLine("import java.util.function.Consumer;");
644WriteLine("import java.util.function.Function;");
645WriteLine();
660WriteLine("// ============================================================================");
661WriteLine("// Enums");
662WriteLine("// ============================================================================");
663WriteLine();
673WriteLine($"/** {enumType.Name} enum. */");
674WriteLine($"enum {enumName} implements WireValueEnum {{");
696WriteLine($" {memberName}(\"{member}\"){suffix}");
698WriteLine();
699WriteLine(" private final String value;");
700WriteLine();
701WriteLine($" {enumName}(String value) {{");
702WriteLine(" this.value = value;");
703WriteLine(" }");
704WriteLine();
705WriteLine(" public String getValue() { return value; }");
706WriteLine();
707WriteLine($" public static {enumName} fromValue(String value) {{");
708WriteLine($" for ({enumName} e : values()) {{");
709WriteLine(" if (e.value.equals(value)) return e;");
710WriteLine(" }");
711WriteLine(" throw new IllegalArgumentException(\"Unknown value: \" + value);");
712WriteLine(" }");
713WriteLine("}");
714WriteLine();
725WriteLine("// ============================================================================");
726WriteLine("// DTOs");
727WriteLine("// ============================================================================");
728WriteLine();
739WriteLine($"/** {dto.Name} DTO. */");
740WriteLine($"class {dtoName} implements JsonSerializable {{");
747WriteLine($" private {fieldType} {fieldName};");
749WriteLine();
757WriteLine($" public {fieldType} get{methodName}() {{ return {fieldName}; }}");
758WriteLine($" public void set{methodName}({fieldType} value) {{ this.{fieldName} = value; }}");
760WriteLine();
762WriteLine(" @SuppressWarnings(\"unchecked\")");
763WriteLine($" public static {dtoName} fromMap(Map<String, Object> map) {{");
764WriteLine($" var value = new {dtoName}();");
777WriteLine($" var {transportValueName} = map.get(\"{JsonNamingPolicy.CamelCase.ConvertName(property.Name)}\");");
778WriteLine($" value.set{methodName}({RenderJavaDtoPropertyTransportValueConversion(property.Type, transportValueName, property.IsOptional)});");
780WriteLine(" return value;");
781WriteLine(" }");
782WriteLine();
785WriteLine(" public Map<String, Object> toMap() {");
786WriteLine(" Map<String, Object> map = new HashMap<>();");
796WriteLine($" map.put(\"{JsonNamingPolicy.CamelCase.ConvertName(property.Name)}\", AspireClient.serializeValue({fieldName}));");
799WriteLine(" return map;");
800WriteLine(" }");
802WriteLine("}");
803WriteLine();
818WriteLine("// ============================================================================");
819WriteLine("// Exported Values");
820WriteLine("// ============================================================================");
821WriteLine();
825WriteLine($"final class {name} {{");
826WriteLine($" private {name}() {{ }}");
827WriteLine();
829WriteLine("}");
830WriteLine();
847WriteLine($"{indent}/** {valueInfo.Description} */");
852WriteLine($"{indent}public static final {javaType} {name} = {expression};");
856WriteLine($"{indent}public static final class {name} {{");
857WriteLine($"{indent} private {name}() {{ }}");
858WriteLine();
860WriteLine($"{indent}}}");
863WriteLine();
1054WriteLine("// ============================================================================");
1055WriteLine("// Options Types");
1056WriteLine("// ============================================================================");
1057WriteLine();
1061WriteLine($"/** Options for {className[..^"Options".Length]}. */");
1062WriteLine($"final class {className} {{");
1066WriteLine($" private {MapParameterToJava(parameter)} {parameterName};");
1068WriteLine();
1074WriteLine($" public {parameterType} {GetOptionGetterName(parameter)}() {{ return {parameterName}; }}");
1075WriteLine($" public {className} {parameterName}({parameterType} value) {{");
1076WriteLine($" this.{parameterName} = value;");
1077WriteLine(" return this;");
1078WriteLine(" }");
1079WriteLine();
1082WriteLine("}");
1083WriteLine();
1224WriteLine($" public {returnType} {methodName}({signature}) {{");
1227WriteLine($" return {methodName}({string.Join(", ", callArguments)});");
1231WriteLine($" {methodName}({string.Join(", ", callArguments)});");
1233WriteLine(" }");
1234WriteLine();
1260WriteLine("// ============================================================================");
1261WriteLine("// Handle Wrappers");
1262WriteLine("// ============================================================================");
1263WriteLine();
1267WriteLine($"/** Wrapper for {handleType.TypeId}. */");
1268WriteLine($"class {handleType.ClassName} extends {handleType.BaseClassName} {{");
1269WriteLine($" {handleType.ClassName}(Handle handle, AspireClient client) {{");
1270WriteLine(" super(handle, client);");
1271WriteLine(" }");
1272WriteLine();
1292WriteLine("}");
1293WriteLine();
1300WriteLine(" /** Gets the input with the specified name, or null if no input matches. */");
1301WriteLine(" public InteractionInput get(String name) {");
1302WriteLine(" for (var input : toArray()) {");
1303WriteLine(" if (input.getName() != null && input.getName().equalsIgnoreCase(name)) {");
1304WriteLine(" return input;");
1305WriteLine(" }");
1306WriteLine(" }");
1307WriteLine(" return null;");
1308WriteLine(" }");
1309WriteLine();
1311WriteLine(" /** Gets the input with the specified name, or throws if no input matches. */");
1312WriteLine(" public InteractionInput required(String name) {");
1313WriteLine(" var input = get(name);");
1314WriteLine(" if (input == null) {");
1315WriteLine(" throw new IllegalArgumentException(\"no input with name '\" + name + \"' was found\");");
1316WriteLine(" }");
1317WriteLine(" return input;");
1318WriteLine(" }");
1319WriteLine();
1321WriteLine(" /** Gets the value of the input with the specified name, or an empty string if no input matches or it has no value. */");
1322WriteLine(" public String value(String name) {");
1323WriteLine(" var input = get(name);");
1324WriteLine(" return input == null || input.getValue() == null ? \"\" : input.getValue();");
1325WriteLine(" }");
1326WriteLine();
1328WriteLine(" /** Gets the value of the input with the specified name, or throws if no input matches. */");
1329WriteLine(" public String requiredValue(String name) {");
1330WriteLine(" return required(name).getValue();");
1331WriteLine(" }");
1332WriteLine();
1341WriteLine(" /** Create a new distributed application builder. */");
1342WriteLine($" public static {builderClassName} CreateBuilder() throws Exception {{");
1343WriteLine(" return CreateBuilder((String[]) null);");
1344WriteLine(" }");
1345WriteLine();
1346WriteLine(" /** Create a new distributed application builder. */");
1347WriteLine($" public static {builderClassName} CreateBuilder(String[] args) throws Exception {{");
1348WriteLine(" CreateBuilderOptions options = new CreateBuilderOptions();");
1349WriteLine(" if (args != null) {");
1350WriteLine(" options.setArgs(args);");
1351WriteLine(" }");
1352WriteLine(" return CreateBuilder(options);");
1353WriteLine(" }");
1354WriteLine();
1355WriteLine(" /** Create a new distributed application builder. */");
1356WriteLine($" public static {builderClassName} CreateBuilder(CreateBuilderOptions options) throws Exception {{");
1357WriteLine(" return Aspire.createBuilder(options);");
1358WriteLine(" }");
1359WriteLine();
1446WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1453WriteLine($" return {methodName}({callArguments});");
1457WriteLine($" {methodName}({callArguments});");
1459WriteLine(" }");
1460WriteLine();
1509WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1512WriteLine($" return {methodName}({callArguments});");
1516WriteLine($" {methodName}({callArguments});");
1518WriteLine(" }");
1519WriteLine();
1554WriteLine($" public {returnInfo.ReturnType} {methodName}({parameterList}) {{");
1571WriteLine($" return {methodName}({string.Join(", ", callArguments)});");
1575WriteLine($" {methodName}({string.Join(", ", callArguments)});");
1577WriteLine(" }");
1578WriteLine();
1642WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1649WriteLine($" return {methodName}({callArguments}, options);");
1653WriteLine($" {methodName}({callArguments}, options);");
1655WriteLine(" }");
1656WriteLine();
1667WriteLine($" 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)}"))}) {{");
1670WriteLine($" return {methodName}({callArguments});");
1674WriteLine($" {methodName}({callArguments});");
1676WriteLine(" }");
1677WriteLine();
1747WriteLine($" /** {capability.Description} */");
1750WriteLine($" public {returnInfo.ReturnType} {methodName}({publicParameterList}) {{");
1754WriteLine($" var {paramName} = optionsBag == null ? null : optionsBag.{GetOptionGetterName(parameter)}();");
1764WriteLine($" return {implementationMethodName}({string.Join(", ", implementationArguments)});");
1768WriteLine($" {implementationMethodName}({string.Join(", ", implementationArguments)});");
1770WriteLine(" }");
1771WriteLine();
1781WriteLine($" public {returnInfo.ReturnType} {methodName}({requiredParameterList}) {{");
1784WriteLine($" return {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});");
1788WriteLine($" {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});");
1790WriteLine(" }");
1791WriteLine();
1814WriteLine($" /** {capability.Description} */");
1818WriteLine($" {accessibility} {returnInfo.ReturnType} {methodName}({paramList}) {{");
1819WriteLine(" Map<String, Object> reqArgs = new HashMap<>();");
1820WriteLine($" reqArgs.put(\"{targetParamName}\", AspireClient.serializeValue(getHandle()));");
1828WriteLine($" if ({paramName}Id != null) {{");
1829WriteLine($" reqArgs.put(\"{parameter.Name}\", {paramName}Id);");
1830WriteLine(" }");
1836WriteLine($" if ({paramName} != null) {{");
1837WriteLine($" reqArgs.put(\"{parameter.Name}\", {paramName});");
1838WriteLine(" }");
1844WriteLine($" if ({paramName} != null) {{");
1845WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));");
1846WriteLine(" }");
1850WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));");
1856WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1857WriteLine(" return this;");
1861WriteLine($" var result = getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1862WriteLine($" return {RenderJavaTransportValueConversion(capability.ReturnType, "result", capability.ReturnType?.IsNullable == true)};");
1866WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1869WriteLine(" }");
1870WriteLine();
1937WriteLine($" var {callbackName}Id = {callbackInitializer}getClient().registerCallback(args -> {{");
1939WriteLine(" });");
1953WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, i)};");
1961WriteLine($" return AspireClient.awaitValue({callbackInvocation});");
1965WriteLine($" {callbackInvocation};");
1972WriteLine($"{indent}var __aspireCallbackArguments = new HashMap<String, Object>();");
1975WriteLine($"{indent}__aspireCallbackArguments.put(\"p{i}\", {arguments[i]});");
1977WriteLine($"{indent}return __aspireCallbackArguments;");
2004WriteLine($" map.put(\"{JsonNamingPolicy.CamelCase.ConvertName(property.Name)}\", {fieldName} == null ? null : (java.util.function.Function<Object, Object>) (transportArg -> {{");
2008WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, "transportArg")};");
2015WriteLine($" return AspireClient.awaitValue({invocation});");
2019WriteLine($" {invocation};");
2022WriteLine(" }));");
2285WriteLine($" /** {capability.Description} */");
2289WriteLine($" private {fullType} {fieldName};");
2290WriteLine($" public {fullType} {methodName}() {{");
2291WriteLine($" if ({fieldName} == null) {{");
2292WriteLine($" {fieldName} = new {wrapperType}<>(getHandle(), getClient(), \"{capability.CapabilityId}\");");
2293WriteLine(" }");
2294WriteLine($" return {fieldName};");
2295WriteLine(" }");
2296WriteLine();
2303WriteLine("// ============================================================================");
2304WriteLine("// Handle wrapper registrations");
2305WriteLine("// ============================================================================");
2306WriteLine();
2307WriteLine("/** Static initializer to register handle wrappers. */");
2308WriteLine("class AspireRegistrations {");
2309WriteLine(" static {");
2313WriteLine($" AspireClient.registerHandleWrapper(\"{handleType.TypeId}\", (h, c) -> new {handleType.ClassName}(h, c));");
2325WriteLine($" AspireClient.registerHandleWrapper(\"{typeId}\", (h, c) -> new {wrapperType}<>(h, c));");
2328WriteLine(" }");
2329WriteLine();
2330WriteLine(" static void ensureRegistered() {");
2331WriteLine(" // Called to trigger static initializer");
2332WriteLine(" }");
2333WriteLine("}");
2334WriteLine();
2343WriteLine("// ============================================================================");
2344WriteLine("// Connection Helpers");
2345WriteLine("// ============================================================================");
2346WriteLine();
2347WriteLine("/** Main entry point for Aspire SDK. */");
2348WriteLine("public class Aspire {");
2349WriteLine(" /** Connect to the AppHost server. */");
2350WriteLine(" public static AspireClient connect() throws Exception {");
2351WriteLine(" BaseRegistrations.ensureRegistered();");
2352WriteLine(" AspireRegistrations.ensureRegistered();");
2353WriteLine(" String socketPath = System.getenv(\"REMOTE_APP_HOST_SOCKET_PATH\");");
2354WriteLine(" if (socketPath == null || socketPath.isEmpty()) {");
2355WriteLine(" throw new RuntimeException(\"REMOTE_APP_HOST_SOCKET_PATH environment variable not set. Run this application using `aspire run`.\");");
2356WriteLine(" }");
2357WriteLine(" AspireClient client = new AspireClient(socketPath);");
2358WriteLine(" client.connect();");
2359WriteLine(" String authToken = System.getenv(\"ASPIRE_REMOTE_APPHOST_TOKEN\");");
2360WriteLine(" if (authToken == null || authToken.isEmpty()) {");
2361WriteLine(" throw new RuntimeException(\"ASPIRE_REMOTE_APPHOST_TOKEN environment variable not set. Run this application using `aspire run`.\");");
2362WriteLine(" }");
2363WriteLine(" client.authenticate(authToken);");
2364WriteLine(" client.onDisconnect(() -> System.exit(1));");
2365WriteLine(" return client;");
2366WriteLine(" }");
2367WriteLine();
2368WriteLine($" /** Create a new distributed application builder. */");
2369WriteLine($" public static {builderClassName} createBuilder(CreateBuilderOptions options) throws Exception {{");
2370WriteLine(" AspireClient client = connect();");
2371WriteLine(" Map<String, Object> resolvedOptions = new HashMap<>();");
2372WriteLine(" if (options != null) {");
2373WriteLine(" resolvedOptions.putAll(options.toMap());");
2374WriteLine(" }");
2375WriteLine(" if (resolvedOptions.get(\"args\") == null) {");
2384WriteLine(" String forwardedArgs = System.getenv(\"ASPIRE_APPHOST_ARGS\");");
2385WriteLine(" resolvedOptions.put(\"args\", forwardedArgs == null || forwardedArgs.isEmpty()");
2386WriteLine(" ? new String[0]");
2387WriteLine(" : forwardedArgs.split(\"\\n\", -1));");
2388WriteLine(" }");
2391WriteLine(" if (resolvedOptions.get(\"projectDirectory\") == null) {");
2392WriteLine(" String projectDirectory = System.getenv(\"ASPIRE_PROJECT_DIRECTORY\");");
2393WriteLine(" if (projectDirectory == null || projectDirectory.isEmpty()) {");
2394WriteLine(" projectDirectory = System.getProperty(\"user.dir\");");
2395WriteLine(" }");
2396WriteLine(" resolvedOptions.put(\"projectDirectory\", projectDirectory);");
2397WriteLine(" }");
2398WriteLine(" if (resolvedOptions.get(\"appHostFilePath\") == null) {");
2399WriteLine(" String appHostFilePath = System.getenv(\"ASPIRE_APPHOST_FILEPATH\");");
2400WriteLine(" if (appHostFilePath != null && !appHostFilePath.isEmpty()) {");
2401WriteLine(" resolvedOptions.put(\"appHostFilePath\", appHostFilePath);");
2402WriteLine(" }");
2403WriteLine(" }");
2404WriteLine(" Map<String, Object> args = new HashMap<>();");
2405WriteLine(" args.put(\"argsOrOptions\", resolvedOptions);");
2406WriteLine($" return ({builderClassName}) client.invokeCapability(\"Aspire.Hosting/createBuilder\", args);");
2407WriteLine(" }");
2408WriteLine("}");
2409WriteLine();