Skip to content

Commit 0731e94

Browse files
committed
Add a hook to add system properties to the Cli
1 parent 4d0a85f commit 0731e94

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.java.mcp.cli;
7+
8+
import java.util.Map;
9+
10+
/**
11+
* This class gives a hook to add system properties as the first step in main.
12+
* Implementations should ensure they don't trigger any classloading in the implementations to avoid race between adding a system property and using it.
13+
*/
14+
public interface CliSystemPropertiesInitializer {
15+
16+
Map<String, String> getSystemProperties();
17+
}

mcp/mcp-cli/src/main/java/software/amazon/smithy/java/mcp/cli/McpCli.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.List;
99
import java.util.ServiceLoader;
1010
import picocli.CommandLine;
11-
import picocli.CommandLine.Command;
1211
import software.amazon.smithy.java.mcp.cli.commands.Configure;
1312
import software.amazon.smithy.java.mcp.cli.commands.InstallBundle;
1413
import software.amazon.smithy.java.mcp.cli.commands.ListBundles;
@@ -22,14 +21,17 @@
2221
* It discovers and registers all available configuration commands and
2322
* sets up the command hierarchy.
2423
*/
25-
@Command(name = "mcp-registry", versionProvider = VersionProvider.class, mixinStandardHelpOptions = true,
26-
scope = CommandLine.ScopeType.INHERIT)
2724
public class McpCli {
2825

26+
private McpCli() {
27+
throw new UnsupportedOperationException();
28+
}
29+
2930
public static void main(String[] args) {
31+
addSystemProperties();
3032
var configureCommand = new CommandLine(new Configure());
3133
discoverConfigurationCommands().forEach(configureCommand::addSubcommand);
32-
var commandLine = new CommandLine(new McpCli())
34+
var commandLine = new CommandLine(new McpRegistry())
3335
.addSubcommand(new StartServer())
3436
.addSubcommand(new ListBundles())
3537
.addSubcommand(new InstallBundle())
@@ -38,6 +40,14 @@ public static void main(String[] args) {
3840
commandLine.execute(args);
3941
}
4042

43+
private static void addSystemProperties() {
44+
System.setProperty("jdk.httpclient.allowRestrictedHeaders", "host"); //This is required for JavaHttpClientTransport.
45+
var loader = ServiceLoader.load(CliSystemPropertiesInitializer.class);
46+
for (var initializer : loader) {
47+
initializer.getSystemProperties().forEach(System::setProperty);
48+
}
49+
}
50+
4151
/**
4252
* Discovers and loads all ConfigurationCommand implementations using the Java ServiceLoader.
4353
*
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.java.mcp.cli;
7+
8+
import picocli.CommandLine;
9+
10+
@CommandLine.Command(name = "mcp-registry", versionProvider = VersionProvider.class, mixinStandardHelpOptions = true,
11+
scope = CommandLine.ScopeType.INHERIT)
12+
class McpRegistry {}

0 commit comments

Comments
 (0)