|
| 1 | +/** |
| 2 | + * Copyright 2026 - 2026 the original author or authors. |
| 3 | + */ |
| 4 | +package io.modelcontextprotocol.json; |
| 5 | + |
| 6 | +import io.modelcontextprotocol.json.schema.JsonSchemaValidator; |
| 7 | +import io.modelcontextprotocol.json.schema.JsonSchemaValidatorSupplier; |
| 8 | +import io.modelcontextprotocol.util.McpServiceLoader; |
| 9 | + |
| 10 | +/** |
| 11 | + * This class is to be used to provide access to the default McpJsonMapper and to the |
| 12 | + * default JsonSchemaValidator instances via the static methods: getDefaultMcpJsonMapper |
| 13 | + * and getDefaultJsonSchemaValidator. |
| 14 | + * <p> |
| 15 | + * The initialization of (singleton) instances of this class is different in non-OSGi |
| 16 | + * environments and OSGi environments. Specifically, in non-OSGi environments The |
| 17 | + * McpJsonDefaults class will be loaded by whatever classloader is used to call one of the |
| 18 | + * existing static get methods for the first time. For servers, this will usually be in |
| 19 | + * response to the creation of the first McpServer instance. At that first time, the |
| 20 | + * mcpMapperServiceLoader and mcpValidatorServiceLoader will be null, and the |
| 21 | + * McpJsonDefaults constructor will be called, creating/initializing the |
| 22 | + * mcpMapperServiceLoader and the mcpValidatorServiceLoader...which will then be used to |
| 23 | + * call the ServiceLoader.load method. |
| 24 | + * <p> |
| 25 | + * In OSGi environments, upon bundle activation SCR will create a new (singleton) instance |
| 26 | + * of McpJsonDefaults (via the constructor), and then inject suppliers via the |
| 27 | + * setMcpJsonMapperSupplier and setJsonSchemaValidatorSupplier methods with the |
| 28 | + * SCR-discovered instances of those services. This does depend upon the jars/bundles |
| 29 | + * providing those suppliers to be started/activated. This SCR behavior is dictated by xml |
| 30 | + * files in OSGi-INF directory of mcp-core (this project/jar/bundle), and the jsonmapper |
| 31 | + * and jsonschemvalidator provider jars/bundles (e.g. mcp-json-jackson2, 3, or others). |
| 32 | + */ |
| 33 | +public class McpJsonDefaults { |
| 34 | + |
| 35 | + protected static McpServiceLoader<McpJsonMapperSupplier, McpJsonMapper> mcpMapperServiceLoader; |
| 36 | + |
| 37 | + protected static McpServiceLoader<JsonSchemaValidatorSupplier, JsonSchemaValidator> mcpValidatorServiceLoader; |
| 38 | + |
| 39 | + public McpJsonDefaults() { |
| 40 | + mcpMapperServiceLoader = new McpServiceLoader<McpJsonMapperSupplier, McpJsonMapper>( |
| 41 | + McpJsonMapperSupplier.class); |
| 42 | + mcpValidatorServiceLoader = new McpServiceLoader<JsonSchemaValidatorSupplier, JsonSchemaValidator>( |
| 43 | + JsonSchemaValidatorSupplier.class); |
| 44 | + } |
| 45 | + |
| 46 | + void setMcpJsonMapperSupplier(McpJsonMapperSupplier supplier) { |
| 47 | + mcpMapperServiceLoader.setSupplier(supplier); |
| 48 | + } |
| 49 | + |
| 50 | + void unsetMcpJsonMapperSupplier(McpJsonMapperSupplier supplier) { |
| 51 | + mcpMapperServiceLoader.unsetSupplier(supplier); |
| 52 | + } |
| 53 | + |
| 54 | + public synchronized static McpJsonMapper getDefaultMcpJsonMapper() { |
| 55 | + if (mcpMapperServiceLoader == null) { |
| 56 | + new McpJsonDefaults(); |
| 57 | + } |
| 58 | + return mcpMapperServiceLoader.getDefault(); |
| 59 | + } |
| 60 | + |
| 61 | + void setJsonSchemaValidatorSupplier(JsonSchemaValidatorSupplier supplier) { |
| 62 | + mcpValidatorServiceLoader.setSupplier(supplier); |
| 63 | + } |
| 64 | + |
| 65 | + void unsetJsonSchemaValidatorSupplier(JsonSchemaValidatorSupplier supplier) { |
| 66 | + mcpValidatorServiceLoader.unsetSupplier(supplier); |
| 67 | + } |
| 68 | + |
| 69 | + public synchronized static JsonSchemaValidator getDefaultJsonSchemaValidator() { |
| 70 | + if (mcpValidatorServiceLoader == null) { |
| 71 | + new McpJsonDefaults(); |
| 72 | + } |
| 73 | + return mcpValidatorServiceLoader.getDefault(); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments