AtsJavaCodeGenerator.cs (316)
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 {{");
680WriteLine($" {memberName}(\"{member}\"){suffix}");
682WriteLine();
683WriteLine(" private final String value;");
684WriteLine();
685WriteLine($" {enumName}(String value) {{");
686WriteLine(" this.value = value;");
687WriteLine(" }");
688WriteLine();
689WriteLine(" public String getValue() { return value; }");
690WriteLine();
691WriteLine($" public static {enumName} fromValue(String value) {{");
692WriteLine($" for ({enumName} e : values()) {{");
693WriteLine(" if (e.value.equals(value)) return e;");
694WriteLine(" }");
695WriteLine(" throw new IllegalArgumentException(\"Unknown value: \" + value);");
696WriteLine(" }");
697WriteLine("}");
698WriteLine();
709WriteLine("// ============================================================================");
710WriteLine("// DTOs");
711WriteLine("// ============================================================================");
712WriteLine();
723WriteLine($"/** {dto.Name} DTO. */");
724WriteLine($"class {dtoName} implements JsonSerializable {{");
731WriteLine($" private {fieldType} {fieldName};");
733WriteLine();
741WriteLine($" public {fieldType} get{methodName}() {{ return {fieldName}; }}");
742WriteLine($" public void set{methodName}({fieldType} value) {{ this.{fieldName} = value; }}");
744WriteLine();
746WriteLine(" @SuppressWarnings(\"unchecked\")");
747WriteLine($" public static {dtoName} fromMap(Map<String, Object> map) {{");
748WriteLine($" var value = new {dtoName}();");
761WriteLine($" var {transportValueName} = map.get(\"{property.Name}\");");
762WriteLine($" value.set{methodName}({RenderJavaDtoPropertyTransportValueConversion(property.Type, transportValueName, property.IsOptional)});");
764WriteLine(" return value;");
765WriteLine(" }");
766WriteLine();
769WriteLine(" public Map<String, Object> toMap() {");
770WriteLine(" Map<String, Object> map = new HashMap<>();");
780WriteLine($" map.put(\"{property.Name}\", AspireClient.serializeValue({fieldName}));");
783WriteLine(" return map;");
784WriteLine(" }");
786WriteLine("}");
787WriteLine();
802WriteLine("// ============================================================================");
803WriteLine("// Exported Values");
804WriteLine("// ============================================================================");
805WriteLine();
809WriteLine($"final class {name} {{");
810WriteLine($" private {name}() {{ }}");
811WriteLine();
813WriteLine("}");
814WriteLine();
831WriteLine($"{indent}/** {valueInfo.Description} */");
836WriteLine($"{indent}public static final {javaType} {name} = {expression};");
840WriteLine($"{indent}public static final class {name} {{");
841WriteLine($"{indent} private {name}() {{ }}");
842WriteLine();
844WriteLine($"{indent}}}");
847WriteLine();
1038WriteLine("// ============================================================================");
1039WriteLine("// Options Types");
1040WriteLine("// ============================================================================");
1041WriteLine();
1045WriteLine($"/** Options for {className[..^"Options".Length]}. */");
1046WriteLine($"final class {className} {{");
1050WriteLine($" private {MapParameterToJava(parameter)} {parameterName};");
1052WriteLine();
1058WriteLine($" public {parameterType} {GetOptionGetterName(parameter)}() {{ return {parameterName}; }}");
1059WriteLine($" public {className} {parameterName}({parameterType} value) {{");
1060WriteLine($" this.{parameterName} = value;");
1061WriteLine(" return this;");
1062WriteLine(" }");
1063WriteLine();
1066WriteLine("}");
1067WriteLine();
1204WriteLine($" public {returnType} {methodName}({signature}) {{");
1207WriteLine($" return {methodName}({string.Join(", ", callArguments)});");
1211WriteLine($" {methodName}({string.Join(", ", callArguments)});");
1213WriteLine(" }");
1214WriteLine();
1240WriteLine("// ============================================================================");
1241WriteLine("// Handle Wrappers");
1242WriteLine("// ============================================================================");
1243WriteLine();
1247WriteLine($"/** Wrapper for {handleType.TypeId}. */");
1248WriteLine($"class {handleType.ClassName} extends {handleType.BaseClassName} {{");
1249WriteLine($" {handleType.ClassName}(Handle handle, AspireClient client) {{");
1250WriteLine(" super(handle, client);");
1251WriteLine(" }");
1252WriteLine();
1272WriteLine("}");
1273WriteLine();
1280WriteLine(" /** Gets the input with the specified name, or null if no input matches. */");
1281WriteLine(" public InteractionInput get(String name) {");
1282WriteLine(" for (var input : toArray()) {");
1283WriteLine(" if (input.getName() != null && input.getName().equalsIgnoreCase(name)) {");
1284WriteLine(" return input;");
1285WriteLine(" }");
1286WriteLine(" }");
1287WriteLine(" return null;");
1288WriteLine(" }");
1289WriteLine();
1291WriteLine(" /** Gets the input with the specified name, or throws if no input matches. */");
1292WriteLine(" public InteractionInput required(String name) {");
1293WriteLine(" var input = get(name);");
1294WriteLine(" if (input == null) {");
1295WriteLine(" throw new IllegalArgumentException(\"no input with name '\" + name + \"' was found\");");
1296WriteLine(" }");
1297WriteLine(" return input;");
1298WriteLine(" }");
1299WriteLine();
1301WriteLine(" /** Gets the value of the input with the specified name, or an empty string if no input matches or it has no value. */");
1302WriteLine(" public String value(String name) {");
1303WriteLine(" var input = get(name);");
1304WriteLine(" return input == null || input.getValue() == null ? \"\" : input.getValue();");
1305WriteLine(" }");
1306WriteLine();
1308WriteLine(" /** Gets the value of the input with the specified name, or throws if no input matches. */");
1309WriteLine(" public String requiredValue(String name) {");
1310WriteLine(" return required(name).getValue();");
1311WriteLine(" }");
1312WriteLine();
1321WriteLine(" /** Create a new distributed application builder. */");
1322WriteLine($" public static {builderClassName} CreateBuilder() throws Exception {{");
1323WriteLine(" return CreateBuilder((String[]) null);");
1324WriteLine(" }");
1325WriteLine();
1326WriteLine(" /** Create a new distributed application builder. */");
1327WriteLine($" public static {builderClassName} CreateBuilder(String[] args) throws Exception {{");
1328WriteLine(" CreateBuilderOptions options = new CreateBuilderOptions();");
1329WriteLine(" if (args != null) {");
1330WriteLine(" options.setArgs(args);");
1331WriteLine(" }");
1332WriteLine(" return CreateBuilder(options);");
1333WriteLine(" }");
1334WriteLine();
1335WriteLine(" /** Create a new distributed application builder. */");
1336WriteLine($" public static {builderClassName} CreateBuilder(CreateBuilderOptions options) throws Exception {{");
1337WriteLine(" return Aspire.createBuilder(options);");
1338WriteLine(" }");
1339WriteLine();
1426WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1433WriteLine($" return {methodName}({callArguments});");
1437WriteLine($" {methodName}({callArguments});");
1439WriteLine(" }");
1440WriteLine();
1489WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1492WriteLine($" return {methodName}({callArguments});");
1496WriteLine($" {methodName}({callArguments});");
1498WriteLine(" }");
1499WriteLine();
1534WriteLine($" public {returnInfo.ReturnType} {methodName}({parameterList}) {{");
1551WriteLine($" return {methodName}({string.Join(", ", callArguments)});");
1555WriteLine($" {methodName}({string.Join(", ", callArguments)});");
1557WriteLine(" }");
1558WriteLine();
1622WriteLine($" public {returnInfo.ReturnType} {methodName}({overloadParameters}) {{");
1629WriteLine($" return {methodName}({callArguments}, options);");
1633WriteLine($" {methodName}({callArguments}, options);");
1635WriteLine(" }");
1636WriteLine();
1647WriteLine($" 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)}"))}) {{");
1650WriteLine($" return {methodName}({callArguments});");
1654WriteLine($" {methodName}({callArguments});");
1656WriteLine(" }");
1657WriteLine();
1727WriteLine($" /** {capability.Description} */");
1730WriteLine($" public {returnInfo.ReturnType} {methodName}({publicParameterList}) {{");
1734WriteLine($" var {paramName} = optionsBag == null ? null : optionsBag.{GetOptionGetterName(parameter)}();");
1744WriteLine($" return {implementationMethodName}({string.Join(", ", implementationArguments)});");
1748WriteLine($" {implementationMethodName}({string.Join(", ", implementationArguments)});");
1750WriteLine(" }");
1751WriteLine();
1761WriteLine($" public {returnInfo.ReturnType} {methodName}({requiredParameterList}) {{");
1764WriteLine($" return {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});");
1768WriteLine($" {methodName}({AppendArgumentList(requiredParameters.Select(parameter => ToCamelCase(parameter.Name)), "null")});");
1770WriteLine(" }");
1771WriteLine();
1794WriteLine($" /** {capability.Description} */");
1798WriteLine($" {accessibility} {returnInfo.ReturnType} {methodName}({paramList}) {{");
1799WriteLine(" Map<String, Object> reqArgs = new HashMap<>();");
1800WriteLine($" reqArgs.put(\"{targetParamName}\", AspireClient.serializeValue(getHandle()));");
1808WriteLine($" if ({paramName}Id != null) {{");
1809WriteLine($" reqArgs.put(\"{parameter.Name}\", {paramName}Id);");
1810WriteLine(" }");
1816WriteLine($" if ({paramName} != null) {{");
1817WriteLine($" reqArgs.put(\"{parameter.Name}\", getClient().registerCancellation({paramName}));");
1818WriteLine(" }");
1824WriteLine($" if ({paramName} != null) {{");
1825WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));");
1826WriteLine(" }");
1830WriteLine($" reqArgs.put(\"{parameter.Name}\", AspireClient.serializeValue({paramName}));");
1836WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1837WriteLine(" return this;");
1841WriteLine($" var result = getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1842WriteLine($" return {RenderJavaTransportValueConversion(capability.ReturnType, "result", capability.ReturnType?.IsNullable == true)};");
1846WriteLine($" getClient().invokeCapability(\"{capability.CapabilityId}\", reqArgs);");
1849WriteLine(" }");
1850WriteLine();
1914WriteLine($" var {callbackName}Id = {callbackInitializer}getClient().registerCallback(args -> {{");
1916WriteLine(" });");
1930WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, i)};");
1938WriteLine($" return AspireClient.awaitValue({callbackInvocation});");
1942WriteLine($" {callbackInvocation};");
1943WriteLine(" return null;");
1970WriteLine($" map.put(\"{property.Name}\", {fieldName} == null ? null : (java.util.function.Function<Object, Object>) (transportArg -> {{");
1975WriteLine($" var {callbackParameterName} = {GetCallbackArgumentExpression(callbackParameter, "transportArg")};");
1982WriteLine($" return AspireClient.awaitValue({invocation});");
1986WriteLine($" {invocation};");
1987WriteLine(" return null;");
1989WriteLine(" }));");
2203WriteLine($" /** {capability.Description} */");
2207WriteLine($" private {fullType} {fieldName};");
2208WriteLine($" public {fullType} {methodName}() {{");
2209WriteLine($" if ({fieldName} == null) {{");
2210WriteLine($" {fieldName} = new {wrapperType}<>(getHandle(), getClient(), \"{capability.CapabilityId}\");");
2211WriteLine(" }");
2212WriteLine($" return {fieldName};");
2213WriteLine(" }");
2214WriteLine();
2221WriteLine("// ============================================================================");
2222WriteLine("// Handle wrapper registrations");
2223WriteLine("// ============================================================================");
2224WriteLine();
2225WriteLine("/** Static initializer to register handle wrappers. */");
2226WriteLine("class AspireRegistrations {");
2227WriteLine(" static {");
2231WriteLine($" AspireClient.registerHandleWrapper(\"{handleType.TypeId}\", (h, c) -> new {handleType.ClassName}(h, c));");
2243WriteLine($" AspireClient.registerHandleWrapper(\"{typeId}\", (h, c) -> new {wrapperType}<>(h, c));");
2246WriteLine(" }");
2247WriteLine();
2248WriteLine(" static void ensureRegistered() {");
2249WriteLine(" // Called to trigger static initializer");
2250WriteLine(" }");
2251WriteLine("}");
2252WriteLine();
2261WriteLine("// ============================================================================");
2262WriteLine("// Connection Helpers");
2263WriteLine("// ============================================================================");
2264WriteLine();
2265WriteLine("/** Main entry point for Aspire SDK. */");
2266WriteLine("public class Aspire {");
2267WriteLine(" /** Connect to the AppHost server. */");
2268WriteLine(" public static AspireClient connect() throws Exception {");
2269WriteLine(" BaseRegistrations.ensureRegistered();");
2270WriteLine(" AspireRegistrations.ensureRegistered();");
2271WriteLine(" String socketPath = System.getenv(\"REMOTE_APP_HOST_SOCKET_PATH\");");
2272WriteLine(" if (socketPath == null || socketPath.isEmpty()) {");
2273WriteLine(" throw new RuntimeException(\"REMOTE_APP_HOST_SOCKET_PATH environment variable not set. Run this application using `aspire run`.\");");
2274WriteLine(" }");
2275WriteLine(" AspireClient client = new AspireClient(socketPath);");
2276WriteLine(" client.connect();");
2277WriteLine(" String authToken = System.getenv(\"ASPIRE_REMOTE_APPHOST_TOKEN\");");
2278WriteLine(" if (authToken == null || authToken.isEmpty()) {");
2279WriteLine(" throw new RuntimeException(\"ASPIRE_REMOTE_APPHOST_TOKEN environment variable not set. Run this application using `aspire run`.\");");
2280WriteLine(" }");
2281WriteLine(" client.authenticate(authToken);");
2282WriteLine(" client.onDisconnect(() -> System.exit(1));");
2283WriteLine(" return client;");
2284WriteLine(" }");
2285WriteLine();
2286WriteLine($" /** Create a new distributed application builder. */");
2287WriteLine($" public static {builderClassName} createBuilder(CreateBuilderOptions options) throws Exception {{");
2288WriteLine(" AspireClient client = connect();");
2289WriteLine(" Map<String, Object> resolvedOptions = new HashMap<>();");
2290WriteLine(" if (options != null) {");
2291WriteLine(" resolvedOptions.putAll(options.toMap());");
2292WriteLine(" }");
2293WriteLine(" if (resolvedOptions.get(\"Args\") == null) {");
2302WriteLine(" String forwardedArgs = System.getenv(\"ASPIRE_APPHOST_ARGS\");");
2303WriteLine(" resolvedOptions.put(\"Args\", forwardedArgs == null || forwardedArgs.isEmpty()");
2304WriteLine(" ? new String[0]");
2305WriteLine(" : forwardedArgs.split(\"\\n\", -1));");
2306WriteLine(" }");
2309WriteLine(" if (resolvedOptions.get(\"ProjectDirectory\") == null) {");
2310WriteLine(" String projectDirectory = System.getenv(\"ASPIRE_PROJECT_DIRECTORY\");");
2311WriteLine(" if (projectDirectory == null || projectDirectory.isEmpty()) {");
2312WriteLine(" projectDirectory = System.getProperty(\"user.dir\");");
2313WriteLine(" }");
2314WriteLine(" resolvedOptions.put(\"ProjectDirectory\", projectDirectory);");
2315WriteLine(" }");
2316WriteLine(" if (resolvedOptions.get(\"AppHostFilePath\") == null) {");
2317WriteLine(" String appHostFilePath = System.getenv(\"ASPIRE_APPHOST_FILEPATH\");");
2318WriteLine(" if (appHostFilePath != null && !appHostFilePath.isEmpty()) {");
2319WriteLine(" resolvedOptions.put(\"AppHostFilePath\", appHostFilePath);");
2320WriteLine(" }");
2321WriteLine(" }");
2322WriteLine(" Map<String, Object> args = new HashMap<>();");
2323WriteLine(" args.put(\"argsOrOptions\", resolvedOptions);");
2324WriteLine($" return ({builderClassName}) client.invokeCapability(\"Aspire.Hosting/createBuilder\", args);");
2325WriteLine(" }");
2326WriteLine("}");
2327WriteLine();