diff --git a/core-common/src/main/java/org/glassfish/jersey/innate/inject/package-info.java b/core-common/src/main/java/org/glassfish/jersey/innate/inject/package-info.java new file mode 100644 index 0000000000..b276f3c87d --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/innate/inject/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +/** + * Jersey innate injection related packages. The innate packages will not be opened by JPMS outside of Jersey. + * Not for public use. + */ +package org.glassfish.jersey.innate.inject; diff --git a/core-common/src/main/java/org/glassfish/jersey/innate/inject/spi/ExternalRegistrables.java b/core-common/src/main/java/org/glassfish/jersey/innate/inject/spi/ExternalRegistrables.java new file mode 100644 index 0000000000..0f0c4179e0 --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/innate/inject/spi/ExternalRegistrables.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.innate.inject.spi; + +import javax.ws.rs.RuntimeType; +import java.util.List; + +/** + * Provide a list of classes or interfaces the InjectionManager can support. + */ +public interface ExternalRegistrables { + + /** + * Contract - RuntimeType pair. For a contract applicable on both client and server, use {@code null} as RuntimeType. + */ + public static final class ClassRuntimeTypePair { + private final Class contract; + private final RuntimeType runtimeType; + + public ClassRuntimeTypePair(Class contract, RuntimeType runtimeType) { + this.contract = contract; + this.runtimeType = runtimeType; + } + + public Class getContract() { + return contract; + } + + public RuntimeType getRuntimeType() { + return runtimeType; + } + } + + /** + * List of contracts that can be registered into Jersey to be passed by the external injection framework. + * @return list of contracts allowed to be registered in Jersey. + */ + List registrableContracts(); +} diff --git a/core-common/src/main/java/org/glassfish/jersey/innate/inject/spi/package-info.java b/core-common/src/main/java/org/glassfish/jersey/innate/inject/spi/package-info.java new file mode 100644 index 0000000000..5ad36ddc40 --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/innate/inject/spi/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +/** + * Jersey innate injection related packages. The innate packages will not be opened by JPMS outside of Jersey. + * Not for public use. + */ +package org.glassfish.jersey.innate.inject.spi; diff --git a/core-common/src/main/java/org/glassfish/jersey/innate/package-info.java b/core-common/src/main/java/org/glassfish/jersey/innate/package-info.java new file mode 100644 index 0000000000..a1835bb74a --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/innate/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +/** + * Jersey innate packages. The innate packages will not be opened by JPMS outside of Jersey. + * Not for public use. + */ +package org.glassfish.jersey.innate; diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java b/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java index 2e909a8cb4..72e2f4b935 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -43,7 +43,9 @@ import javax.annotation.Priority; import org.glassfish.jersey.JerseyPriorities; +import org.glassfish.jersey.innate.inject.spi.ExternalRegistrables; import org.glassfish.jersey.internal.LocalizationMessages; +import org.glassfish.jersey.internal.ServiceFinder; import org.glassfish.jersey.model.ContractProvider; import org.glassfish.jersey.model.internal.RankedComparator; import org.glassfish.jersey.model.internal.RankedProvider; @@ -103,6 +105,14 @@ private static Map, ProviderRuntime> getExternalProviderInterfaces() { interfaces.putAll(JAX_RS_PROVIDER_INTERFACE_WHITELIST); interfaces.put(javax.ws.rs.core.Feature.class, ProviderRuntime.BOTH); interfaces.put(Binder.class, ProviderRuntime.BOTH); + + try { + ServiceFinder registerables = ServiceFinder.find(ExternalRegistrables.class, true); + registerables.forEach(regs -> regs.registrableContracts() + .forEach(pair -> interfaces.put(pair.getContract(), ProviderRuntime.fromRuntimeType(pair.getRuntimeType())))); + } catch (Throwable t) { + LOGGER.warning(LocalizationMessages.ERROR_EXTERNAL_REGISTERABLES_IGNORED(t.getMessage())); + } return interfaces; } @@ -119,6 +129,10 @@ private ProviderRuntime(final RuntimeType runtime) { public RuntimeType getRuntime() { return runtime; } + + private static ProviderRuntime fromRuntimeType(RuntimeType type) { + return type == null ? BOTH : (type == RuntimeType.SERVER ? SERVER : CLIENT); + } } /** diff --git a/core-common/src/main/resources/org/glassfish/jersey/internal/localization.properties b/core-common/src/main/resources/org/glassfish/jersey/internal/localization.properties index 285b9de36e..e546e16639 100644 --- a/core-common/src/main/resources/org/glassfish/jersey/internal/localization.properties +++ b/core-common/src/main/resources/org/glassfish/jersey/internal/localization.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved. # Copyright (c) 2018 Payara Foundation and/or its affiliates. # # This program and the accompanying materials are made available under the @@ -39,6 +39,7 @@ error.entity.stream.closed=Entity input stream has already been closed. error.entity.provider.basictypes.character.morechars=A single character expected in the entity input stream. error.entity.provider.basictypes.constructor=Error converting entity to {0} type by single String constructor. error.entity.provider.basictypes.unkwnown=Unsupported basic type {0}. +error.external.registerables.ignored=Error reading external registrable contracts: {0}. error.finding.exception.mapper.type=Could not find exception type for given ExceptionMapper class: {0}. error.interceptor.reader.proceed=Last reader interceptor in the chain called the method proceed. error.interceptor.writer.proceed=Last writer interceptor in the chain called the method proceed. diff --git a/docs/src/main/docbook/custom-di.xml b/docs/src/main/docbook/custom-di.xml index 53a5a645f5..e1a66b5875 100644 --- a/docs/src/main/docbook/custom-di.xml +++ b/docs/src/main/docbook/custom-di.xml @@ -1,7 +1,7 @@