-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Feature and Dynamic feature as a JDK services (#4833)
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
- Loading branch information
Showing
14 changed files
with
409 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...-client/src/test/java/org/glassfish/jersey/client/JaxRsFeatureRegistrationClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2021 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.client; | ||
|
||
import org.glassfish.jersey.CommonProperties; | ||
import org.junit.Test; | ||
|
||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.ClientRequestFilter; | ||
import javax.ws.rs.client.Invocation; | ||
import javax.ws.rs.core.Feature; | ||
import javax.ws.rs.core.FeatureContext; | ||
import javax.ws.rs.core.Response; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
public class JaxRsFeatureRegistrationClientTest { | ||
|
||
private static final String REGISTERED_FEATURE_RESPONSE = "RegisteredFeature"; | ||
|
||
private static final ClientRequestFilter component = | ||
requestContext -> requestContext | ||
.abortWith(Response.status(400).entity(REGISTERED_FEATURE_RESPONSE).build()); | ||
|
||
public static class FeatureClientImpl implements Feature { | ||
|
||
@Override | ||
public boolean configure(FeatureContext context) { | ||
if ("true".equals(context.getConfiguration().getProperty("runWithJaxRsClient"))) { | ||
context.register(component); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
@Test | ||
public void featureRegistrationTest() { | ||
final ClientConfig config = new ClientConfig().property("runWithJaxRsClient", "true"); | ||
final Client client = ClientBuilder.newClient(config); | ||
final Invocation.Builder request = client.target("").request(); | ||
|
||
assertEquals(REGISTERED_FEATURE_RESPONSE, request.get().readEntity(String.class)); | ||
|
||
client.close(); | ||
} | ||
|
||
@Test | ||
public void featureNotRegistrationTest() { | ||
final ClientConfig config = new ClientConfig() | ||
.property(CommonProperties.JAXRS_SERVICE_LOADING_ENABLE, false); | ||
final Client client = ClientBuilder.newClient(config); | ||
final Invocation.Builder request = client.target("").request(); | ||
|
||
assertFalse(client.getConfiguration().isRegistered(FeatureClientImpl.class)); | ||
|
||
client.close(); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
core-client/src/test/resources/META-INF/services/javax.ws.rs.core.Feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.glassfish.jersey.client.JaxRsFeatureRegistrationClientTest$FeatureClientImpl |
89 changes: 89 additions & 0 deletions
89
core-common/src/main/java/org/glassfish/jersey/AbstractFeatureConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2021 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; | ||
|
||
import org.glassfish.jersey.internal.AbstractServiceFinderConfigurator; | ||
import org.glassfish.jersey.internal.BootstrapBag; | ||
import org.glassfish.jersey.internal.ServiceFinder; | ||
import org.glassfish.jersey.internal.spi.AutoDiscoverable; | ||
import org.glassfish.jersey.internal.util.PropertiesHelper; | ||
|
||
import javax.ws.rs.RuntimeType; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public abstract class AbstractFeatureConfigurator<T> extends AbstractServiceFinderConfigurator<T> { | ||
/** | ||
* Create a new configurator. | ||
* | ||
* @param contract contract of the service providers bound by this binder. | ||
* @param runtimeType runtime (client or server) where the service finder binder is used. | ||
*/ | ||
protected AbstractFeatureConfigurator(Class contract, RuntimeType runtimeType) { | ||
super(contract, runtimeType); | ||
} | ||
|
||
/** | ||
* Specification specific implementation which allows find classes by specified classloader | ||
* | ||
* @param applicationProperties map of properties to check if search is allowed | ||
* @param loader specific classloader (must not be NULL) | ||
* @return list of found classes | ||
*/ | ||
protected List<Class<T>> loadImplementations(Map<String, Object> applicationProperties, ClassLoader loader) { | ||
if (PropertiesHelper.isMetaInfServicesEnabled(applicationProperties, getRuntimeType())) { | ||
return Stream.of(ServiceFinder.find(getContract(), loader, true).toClassArray()) | ||
.collect(Collectors.toList()); | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* Allows feature registration as part of autoDiscoverables list | ||
* | ||
* @param features list of features to be registered | ||
* @param bootstrapBag place where features are being registered | ||
*/ | ||
protected void registerFeatures(List<Class<T>> features, | ||
BootstrapBag bootstrapBag) { | ||
final List<AutoDiscoverable> autoDiscoverables = new ArrayList<>(); | ||
|
||
features.forEach(feature -> autoDiscoverables.add(registerClass(feature))); | ||
|
||
bootstrapBag.getAutoDiscoverables().addAll(autoDiscoverables); | ||
} | ||
|
||
/** | ||
* Register particular feature as an autoDiscoverable | ||
* | ||
* @param classToRegister class to be registered | ||
* @param <T> type of class which is being registered | ||
* @return initialized autoDiscoverable | ||
*/ | ||
private static <T> AutoDiscoverable registerClass(Class<T> classToRegister) { | ||
return context -> { | ||
if (!context.getConfiguration().isRegistered(classToRegister)) { | ||
context.register(classToRegister, AutoDiscoverable.DEFAULT_PRIORITY); | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
core-common/src/main/java/org/glassfish/jersey/internal/DynamicFeatureConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2021 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.internal; | ||
|
||
import org.glassfish.jersey.AbstractFeatureConfigurator; | ||
import org.glassfish.jersey.internal.inject.InjectionManager; | ||
import org.glassfish.jersey.internal.util.PropertiesHelper; | ||
|
||
import javax.ws.rs.RuntimeType; | ||
import javax.ws.rs.container.DynamicFeature; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Registers JAX-RS {@link DynamicFeature} which are listed as SPIs for registration. | ||
* Also checks if JAX-RS service loading is enabled by the jakarta.ws.rs.loadServices property. In order for | ||
* registration to proceed the property shall be true (or null). | ||
* | ||
* Configurator is used only at Server side. | ||
* | ||
* This configurator's instance shall be after {@link AutoDiscoverableConfigurator} | ||
* in the list of configurators due to same list of {@link org.glassfish.jersey.internal.spi.AutoDiscoverable} | ||
* used in the {@link BootstrapBag} to register discovered features. | ||
*/ | ||
public class DynamicFeatureConfigurator extends AbstractFeatureConfigurator<DynamicFeature> { | ||
|
||
/** | ||
* Create a new configurator. | ||
* | ||
* Must be used at server side only (takes no effect as a client). | ||
*/ | ||
public DynamicFeatureConfigurator() { | ||
super(DynamicFeature.class, RuntimeType.SERVER); | ||
} | ||
|
||
@Override | ||
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) { | ||
final Map<String, Object> properties = bootstrapBag.getConfiguration().getProperties(); | ||
if (PropertiesHelper.isJaxRsServiceLoadingEnabled(properties)) { | ||
final List<Class<DynamicFeature>> dynamicFeatures = loadImplementations(properties); | ||
dynamicFeatures.addAll(loadImplementations(properties, DynamicFeature.class.getClassLoader())); | ||
|
||
registerFeatures(dynamicFeatures, bootstrapBag); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
core-common/src/main/java/org/glassfish/jersey/internal/FeatureConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2021 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.internal; | ||
|
||
import org.glassfish.jersey.AbstractFeatureConfigurator; | ||
import org.glassfish.jersey.internal.inject.InjectionManager; | ||
import org.glassfish.jersey.internal.util.PropertiesHelper; | ||
|
||
import javax.ws.rs.RuntimeType; | ||
import javax.ws.rs.core.Feature; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Registers JAX-RS {@link Feature} which are listed as SPIs for registration. | ||
* Also checks if JAX-RS service loading is enabled by the jakarta.ws.rs.loadServices property. In order for | ||
* registration to proceed the property shall be true (or null). | ||
* | ||
* This configurator's instance shall be the last (or at least after {@link AutoDiscoverableConfigurator}) | ||
* in the list of configurators due to same list of {@link org.glassfish.jersey.internal.spi.AutoDiscoverable} | ||
* used in the {@link BootstrapBag} to register discovered features. | ||
*/ | ||
public class FeatureConfigurator extends AbstractFeatureConfigurator<Feature> { | ||
|
||
public FeatureConfigurator(RuntimeType runtimeType) { | ||
super(Feature.class, runtimeType); | ||
} | ||
|
||
@Override | ||
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) { | ||
final Map<String, Object> properties = bootstrapBag.getConfiguration().getProperties(); | ||
if (PropertiesHelper.isJaxRsServiceLoadingEnabled(properties)) { | ||
final List<Class<Feature>> features = loadImplementations(properties); | ||
features.addAll(loadImplementations(properties, Feature.class.getClassLoader())); | ||
|
||
registerFeatures(features, bootstrapBag); | ||
} | ||
} | ||
} |
Oops, something went wrong.