From 8fd9025c7e27028761f629aa8a22569cd847fed0 Mon Sep 17 00:00:00 2001 From: Jan Supol Date: Thu, 2 May 2019 18:07:41 +0200 Subject: [PATCH 1/2] Allow for registering HK2 AbstractBinder to retain backwards compatibility with 2.25. Signed-off-by: Jan Supol --- .../spi/BinderConfigurationFactory.java | 64 +++++++++ .../jersey/inject/spi/package-info.java | 20 +++ .../JerseyBinderConfigurationFactory.java | 78 +++++++++++ .../jersey/model/internal/CommonConfig.java | 67 ++++++---- .../hk2/HK2BinderConfigurationFactory.java | 121 ++++++++++++++++++ ...rsey.inject.spi.BinderConfigurationFactory | 1 + .../HK2BinderConfigurationFactoryTest.java | 33 +++++ tests/e2e-inject/hk2/pom.xml | 24 ++++ .../hk2/AbstractBinderTestResource.java | 30 +++++ .../tests/e2e/inject/hk2/Injectable.java | 20 +++ .../tests/e2e/inject/hk2/InjectableImpl.java | 24 ++++ .../e2e/inject/hk2/InjectableTestFilter.java | 34 +++++ .../e2e/inject/hk2/HK2AbstractBinderTest.java | 63 +++++++++ .../inject/hk2/JerseyAbstractBinderTest.java | 61 +++++++++ tests/e2e-inject/pom.xml | 1 + 15 files changed, 615 insertions(+), 26 deletions(-) create mode 100644 core-common/src/main/java/org/glassfish/jersey/inject/spi/BinderConfigurationFactory.java create mode 100644 core-common/src/main/java/org/glassfish/jersey/inject/spi/package-info.java create mode 100644 core-common/src/main/java/org/glassfish/jersey/internal/inject/JerseyBinderConfigurationFactory.java create mode 100644 inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactory.java create mode 100644 inject/hk2/src/main/resources/META-INF/services/org.glassfish.jersey.inject.spi.BinderConfigurationFactory create mode 100644 inject/hk2/src/test/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactoryTest.java create mode 100644 tests/e2e-inject/hk2/pom.xml create mode 100644 tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/AbstractBinderTestResource.java create mode 100644 tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/Injectable.java create mode 100644 tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableImpl.java create mode 100644 tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableTestFilter.java create mode 100644 tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java create mode 100644 tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/JerseyAbstractBinderTest.java diff --git a/core-common/src/main/java/org/glassfish/jersey/inject/spi/BinderConfigurationFactory.java b/core-common/src/main/java/org/glassfish/jersey/inject/spi/BinderConfigurationFactory.java new file mode 100644 index 0000000000..c42c2b457d --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/inject/spi/BinderConfigurationFactory.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019 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.inject.spi; + +import org.glassfish.jersey.internal.inject.InjectionManager; +import org.glassfish.jersey.model.ContractProvider; + +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * Factory class creating instances of {@link BinderConfiguration} classes used to configure Binders registered + * to a configuration. While it is created for a backward compatibility with HK2 {@code AbstractBinder} to be + * able to be registered to a configuration (while HK2 injection module is on the classpath), a factory + * implementing this interface is used for registering Jersey {@code AbstractBinder}, too. + * + * @since 2.29 + */ +public interface BinderConfigurationFactory { + + /** + * Creates a {@link BinderConfiguration} instance that has a reference to {@code getInstances} function returning all + * registered instances filtered by provided {@link Predicate} that can be further traversed and configured. + * + * @param getInstances a function returning filtered instances registered to a configuration. The + * {@link Predicate} is used to filter out all the instances not to be further processed by the + * {@link BinderConfiguration}. + * @return {@link BinderConfiguration} instance used to register/configure the provided filtered instances. + */ + BinderConfiguration createBinderConfiguration(Function, Set> getInstances); + + /** + * This configuration object configure all the instances provided by the {@code getInstances} function passed from the + * factory {@link BinderConfigurationFactory#createBinderConfiguration(Function)} method. + *

+ * The implementation possibly can hold a list of already configured {@code Binders} so that consecutive calls do + * not register the already registered {@code Binders} again. + */ + interface BinderConfiguration { + /** + * The provided {@code getInstances} function is used to get registered (filtered) instances in a + * {@link javax.ws.rs.core.Configuration} + * + * @param injectionManager {@link InjectionManager} to be used to configure the {@code Binder} + * @return {@code true} if a {@code Binder} has been configured. + */ + boolean configureBinders(InjectionManager injectionManager); + } +} diff --git a/core-common/src/main/java/org/glassfish/jersey/inject/spi/package-info.java b/core-common/src/main/java/org/glassfish/jersey/inject/spi/package-info.java new file mode 100644 index 0000000000..11ec894e29 --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/inject/spi/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2019 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 + */ + +/** + * Common Jersey core injection SPI classes. + */ +package org.glassfish.jersey.inject.spi; diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/inject/JerseyBinderConfigurationFactory.java b/core-common/src/main/java/org/glassfish/jersey/internal/inject/JerseyBinderConfigurationFactory.java new file mode 100644 index 0000000000..78a3901cb4 --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/internal/inject/JerseyBinderConfigurationFactory.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2019 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.inject; + +import org.glassfish.jersey.inject.spi.BinderConfigurationFactory; +import org.glassfish.jersey.model.ContractProvider; +import org.glassfish.jersey.model.internal.ComponentBag; + +import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * An implementation of {@link BinderConfigurationFactory} used to configure {@link Binder} instances. + */ +public class JerseyBinderConfigurationFactory implements BinderConfigurationFactory { + + @Override + public BinderConfiguration createBinderConfiguration(Function, Set> getInstances) { + return new JerseyBinderConfiguration(getInstances); + } + + private static class JerseyBinderConfiguration implements BinderConfiguration { + private Set configuredBinders = Collections.emptySet(); + private final Function, Set> getInstances; + private static final Function CAST_TO_BINDER = Binder.class::cast; + + private JerseyBinderConfiguration(Function, Set> getInstances) { + this.getInstances = getInstances; + } + + @Override + public boolean configureBinders(InjectionManager injectionManager) { + configuredBinders = configureBinders(injectionManager, configuredBinders); + return !configuredBinders.isEmpty(); + } + + private Set configureBinders(InjectionManager injectionManager, Set configured) { + Set allConfigured = Collections.newSetFromMap(new IdentityHashMap<>()); + allConfigured.addAll(configured); + + Collection binders = getBinder(configured); + if (!binders.isEmpty()) { + injectionManager.register(CompositeBinder.wrap(binders)); + allConfigured.addAll(binders); + } + + return allConfigured; + } + + private Collection getBinder(Set configured) { + return getInstances + .apply(ComponentBag.BINDERS_ONLY) + .stream() + .map(CAST_TO_BINDER) + .filter(binder -> !configured.contains(binder)) + .collect(Collectors.toList()); + } + } +} diff --git a/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java b/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java index a084891a05..742567a458 100644 --- a/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java +++ b/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java @@ -32,7 +32,6 @@ import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; import javax.ws.rs.ConstrainedTo; import javax.ws.rs.Priorities; @@ -44,11 +43,12 @@ import javax.annotation.Priority; import org.glassfish.jersey.ExtendedConfig; +import org.glassfish.jersey.inject.spi.BinderConfigurationFactory; import org.glassfish.jersey.internal.LocalizationMessages; import org.glassfish.jersey.internal.ServiceFinder; import org.glassfish.jersey.internal.inject.Binder; -import org.glassfish.jersey.internal.inject.CompositeBinder; import org.glassfish.jersey.internal.inject.InjectionManager; +import org.glassfish.jersey.internal.inject.JerseyBinderConfigurationFactory; import org.glassfish.jersey.internal.inject.ProviderBinder; import org.glassfish.jersey.internal.spi.AutoDiscoverable; import org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable; @@ -66,7 +66,6 @@ public class CommonConfig implements FeatureContext, ExtendedConfig { private static final Logger LOGGER = Logger.getLogger(CommonConfig.class.getName()); - private static final Function CAST_TO_BINDER = Binder.class::cast; /** * Configuration runtime type. @@ -100,6 +99,42 @@ public class CommonConfig implements FeatureContext, ExtendedConfig { */ private boolean disableMetaProviderConfiguration; + /** + * A utility class that binds binders on all {@link BinderConfigurationFactory.BinderConfiguration BinderConfiguration} + * created upon creation of this BinderConfigurations from all {@link BinderConfigurationFactory BinderConfigurationFactories} + */ + private static final class BinderConfigurations { + private static final List BINDER_CONFIGURATION_FACTORIES; + static { + final ServiceFinder factoriesFinder = + ServiceFinder.find(BinderConfigurationFactory.class); + final List configurationFactories = new LinkedList<>(); + configurationFactories.add(new JerseyBinderConfigurationFactory()); + for (BinderConfigurationFactory configurationFactory : factoriesFinder) { + configurationFactories.add(configurationFactory); + } + BINDER_CONFIGURATION_FACTORIES = Collections.unmodifiableList(configurationFactories); + } + + private final List binderConfigurations; + + private BinderConfigurations(ComponentBag componentBag) { + binderConfigurations = new LinkedList<>(); + for (BinderConfigurationFactory factory : BINDER_CONFIGURATION_FACTORIES) { + BinderConfigurationFactory.BinderConfiguration configuration = + factory.createBinderConfiguration(componentBag::getInstances); + binderConfigurations.add(configuration); + } + } + + private void configureBinders(InjectionManager injectionManager) { + for (BinderConfigurationFactory.BinderConfiguration configuration : binderConfigurations) { + configuration.configureBinders(injectionManager); + } + } + + } + /** * A single feature registration record. */ @@ -619,7 +654,8 @@ public void configureAutoDiscoverableProviders(final InjectionManager injectionM */ public void configureMetaProviders(InjectionManager injectionManager, ManagedObjectsFinalizer finalizer) { // First, configure existing binders - Set configuredBinders = configureBinders(injectionManager, Collections.emptySet()); + BinderConfigurations binderConfigurations = new BinderConfigurations(componentBag); + binderConfigurations.configureBinders(injectionManager); // Check whether meta providers have been initialized for a config this config has been loaded from. if (!disableMetaProviderConfiguration) { @@ -628,29 +664,8 @@ public void configureMetaProviders(InjectionManager injectionManager, ManagedObj // Next, configure all features configureFeatures(injectionManager, new HashSet<>(), resetRegistrations(), finalizer); // At last, configure any new binders added by features - configureBinders(injectionManager, configuredBinders); - } - } - - private Set configureBinders(InjectionManager injectionManager, Set configured) { - Set allConfigured = Collections.newSetFromMap(new IdentityHashMap<>()); - allConfigured.addAll(configured); - - Collection binders = getBinder(configured); - if (!binders.isEmpty()) { - injectionManager.register(CompositeBinder.wrap(binders)); - allConfigured.addAll(binders); + binderConfigurations.configureBinders(injectionManager); } - - return allConfigured; - } - - private Collection getBinder(Set configured) { - return componentBag.getInstances(ComponentBag.BINDERS_ONLY) - .stream() - .map(CAST_TO_BINDER) - .filter(binder -> !configured.contains(binder)) - .collect(Collectors.toList()); } private void configureExternalObjects(InjectionManager injectionManager) { diff --git a/inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactory.java b/inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactory.java new file mode 100644 index 0000000000..d0b377d67b --- /dev/null +++ b/inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactory.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2019 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.inject.hk2; + +import org.glassfish.hk2.api.DynamicConfiguration; +import org.glassfish.hk2.api.DynamicConfigurationService; +import org.glassfish.hk2.api.ServiceLocator; +import org.glassfish.hk2.utilities.Binder; +import org.glassfish.jersey.inject.spi.BinderConfigurationFactory; +import org.glassfish.jersey.internal.inject.InjectionManager; +import org.glassfish.jersey.model.ContractProvider; + +import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * Implementation of {@link BinderConfigurationFactory} that binds HK2 {@code AbstractBinder} as well as other + * HK2 {@code Binder} implementation + */ +public class HK2BinderConfigurationFactory implements BinderConfigurationFactory { + @Override + public BinderConfiguration createBinderConfiguration(Function, Set> getInstances) { + return new HK2BinderConfiguration(getInstances); + } + + private static class HK2BinderConfiguration implements BinderConfigurationFactory.BinderConfiguration { + private final Function, Set> getInstances; + + /** + * A filtering strategy that includes only models that contain HK2 Binder provider contract. + *

+ * This filter predicate returns {@code true} for all {@link org.glassfish.jersey.model.ContractProvider contract provider models} + * that represent a provider registered to provide HK2 {@link org.glassfish.hk2.utilities.Binder} contract. + *

+ */ + private static final Predicate BINDERS_ONLY = new Predicate() { + @Override + public boolean test(ContractProvider model) { + return Binder.class.isAssignableFrom(model.getImplementationClass()); + } + }; + + private static final Function CAST_TO_BINDER = new Function() { + @Override + public Binder apply(final Object input) { + return Binder.class.cast(input); + } + }; + + + private Set configuredBinders = Collections.emptySet(); + + public HK2BinderConfiguration(Function, Set> getInstances) { + this.getInstances = getInstances; + } + + @Override + public boolean configureBinders(InjectionManager injectionManager) { + final ServiceLocator serviceLocator = getServiceLocator(injectionManager); + if (serviceLocator != null) { + configuredBinders = configureBinders(serviceLocator, configuredBinders); + return !configuredBinders.isEmpty(); + } + return false; + } + + private Set configureBinders(final ServiceLocator locator, final Set configured) { + final Set allConfigured = Collections.newSetFromMap(new IdentityHashMap<>()); + allConfigured.addAll(configured); + + final Collection binders = getBinders(configured); + if (!binders.isEmpty()) { + final DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); + final DynamicConfiguration dc = dcs.createDynamicConfiguration(); + + for (final Binder binder : binders) { + binder.bind(dc); + allConfigured.add(binder); + } + dc.commit(); + } + + return allConfigured; + } + + private Collection getBinders(final Set configured) { + return getInstances + .apply(BINDERS_ONLY) + .stream() + .map(CAST_TO_BINDER) + .filter(binder -> !configured.contains(binder)) + .collect(Collectors.toList()); + } + + private static ServiceLocator getServiceLocator(InjectionManager injectionManager) { + if (AbstractHk2InjectionManager.class.isInstance(injectionManager)) { + return ((AbstractHk2InjectionManager) injectionManager).getServiceLocator(); + } + return null; + } + } +} diff --git a/inject/hk2/src/main/resources/META-INF/services/org.glassfish.jersey.inject.spi.BinderConfigurationFactory b/inject/hk2/src/main/resources/META-INF/services/org.glassfish.jersey.inject.spi.BinderConfigurationFactory new file mode 100644 index 0000000000..30e9c77749 --- /dev/null +++ b/inject/hk2/src/main/resources/META-INF/services/org.glassfish.jersey.inject.spi.BinderConfigurationFactory @@ -0,0 +1 @@ +org.glassfish.jersey.inject.hk2.HK2BinderConfigurationFactory \ No newline at end of file diff --git a/inject/hk2/src/test/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactoryTest.java b/inject/hk2/src/test/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactoryTest.java new file mode 100644 index 0000000000..add954dd92 --- /dev/null +++ b/inject/hk2/src/test/java/org/glassfish/jersey/inject/hk2/HK2BinderConfigurationFactoryTest.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 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.inject.hk2; + +import org.glassfish.jersey.inject.spi.BinderConfigurationFactory; +import org.glassfish.jersey.internal.ServiceFinder; +import org.junit.Test; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +public class HK2BinderConfigurationFactoryTest { + @Test + public void testServiceFound() { + ServiceFinder factoryFinder = ServiceFinder.find(BinderConfigurationFactory.class); + assertTrue(factoryFinder.iterator().hasNext()); + assertSame(factoryFinder.iterator().next().getClass(), HK2BinderConfigurationFactory.class); + } +} diff --git a/tests/e2e-inject/hk2/pom.xml b/tests/e2e-inject/hk2/pom.xml new file mode 100644 index 0000000000..e15ade0635 --- /dev/null +++ b/tests/e2e-inject/hk2/pom.xml @@ -0,0 +1,24 @@ + + + + e2e-inject + org.glassfish.jersey.tests + 2.29-SNAPSHOT + + 4.0.0 + + e2e-inject-hk2 + + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-bundle + pom + test + + + + + \ No newline at end of file diff --git a/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/AbstractBinderTestResource.java b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/AbstractBinderTestResource.java new file mode 100644 index 0000000000..0ba4223c8d --- /dev/null +++ b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/AbstractBinderTestResource.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 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.tests.e2e.inject.hk2; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; + +@Path("/") +public class AbstractBinderTestResource { + @GET + public String resource(@Context HttpHeaders headers) { + return headers.getRequestHeader(InjectableTestFilter.class.getName()).get(0); + } +} diff --git a/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/Injectable.java b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/Injectable.java new file mode 100644 index 0000000000..f0d222ec88 --- /dev/null +++ b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/Injectable.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2019 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.tests.e2e.inject.hk2; + +public interface Injectable { +} diff --git a/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableImpl.java b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableImpl.java new file mode 100644 index 0000000000..030a50a3f3 --- /dev/null +++ b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableImpl.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 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.tests.e2e.inject.hk2; + +public class InjectableImpl implements Injectable { + @Override + public String toString() { + return getClass().getName(); + } +} diff --git a/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableTestFilter.java b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableTestFilter.java new file mode 100644 index 0000000000..28fb2c6aa5 --- /dev/null +++ b/tests/e2e-inject/hk2/src/main/java/org/glassfish/jersey/tests/e2e/inject/hk2/InjectableTestFilter.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 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.tests.e2e.inject.hk2; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.core.Context; +import java.io.IOException; + +public class InjectableTestFilter implements ContainerRequestFilter { + @Context + public Injectable injectable; + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + requestContext.getHeaders().add( + InjectableTestFilter.class.getName(), injectable == null ? "NULL" : injectable.toString() + ); + } +} \ No newline at end of file diff --git a/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java b/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java new file mode 100644 index 0000000000..f9e42436f2 --- /dev/null +++ b/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2019 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.tests.e2e.inject.hk2; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Test; + +import javax.inject.Singleton; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Feature; +import javax.ws.rs.core.FeatureContext; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Test for older hk2 binders to be used + */ +public class HK2AbstractBinderTest extends JerseyTest { + + public static class InjectableHK2Binder extends org.glassfish.hk2.utilities.binding.AbstractBinder { + @Override + protected void configure() { + bindAsContract(InjectableImpl.class).to(Injectable.class).in(Singleton.class); + } + } + + public static class HK2BindingFeature implements Feature { + @Override + public boolean configure(FeatureContext context) { + context.register(new InjectableHK2Binder()); + return true; + } + } + + @Override + protected Application configure() { + return new ResourceConfig(HK2BindingFeature.class, AbstractBinderTestResource.class, InjectableTestFilter.class); + } + + @Test + public void testInjectableInjection() { + String response = target().request().get(String.class); + System.out.println(response); + assertThat(response, is(InjectableImpl.class.getName())); + } + +} diff --git a/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/JerseyAbstractBinderTest.java b/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/JerseyAbstractBinderTest.java new file mode 100644 index 0000000000..36a969e771 --- /dev/null +++ b/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/JerseyAbstractBinderTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2019 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.tests.e2e.inject.hk2; + +import org.glassfish.jersey.internal.inject.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Test; + +import javax.inject.Singleton; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Feature; +import javax.ws.rs.core.FeatureContext; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Comparison test using new Jersey AbstractBinder for functionality that is to be working with HK2 AbstractBinder + */ +public class JerseyAbstractBinderTest extends JerseyTest { + public static class InjectableBinder extends AbstractBinder { + @Override + protected void configure() { + bindAsContract(InjectableImpl.class).to(Injectable.class).in(Singleton.class); + } + } + + public static class BindingFeature implements Feature { + @Override + public boolean configure(FeatureContext context) { + context.register(new InjectableBinder()); + return true; + } + } + + @Override + protected Application configure() { + return new ResourceConfig(BindingFeature.class, AbstractBinderTestResource.class, InjectableTestFilter.class); + } + + @Test + public void testInjectableInjection() { + String response = target().request().get(String.class); + assertThat(response, is(InjectableImpl.class.getName())); + } +} diff --git a/tests/e2e-inject/pom.xml b/tests/e2e-inject/pom.xml index 62f875e08e..3676cd7844 100644 --- a/tests/e2e-inject/pom.xml +++ b/tests/e2e-inject/pom.xml @@ -34,5 +34,6 @@ cdi2-se + hk2 From bec3656596279875a7895fa0164ba2b4e6a143f3 Mon Sep 17 00:00:00 2001 From: Jan Supol Date: Thu, 2 May 2019 20:48:28 +0200 Subject: [PATCH 2/2] Clean up Signed-off-by: Jan Supol --- .../java/org/glassfish/jersey/model/internal/CommonConfig.java | 2 -- .../jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java | 1 - 2 files changed, 3 deletions(-) diff --git a/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java b/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java index 742567a458..02483d9aee 100644 --- a/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java +++ b/core-common/src/main/java/org/glassfish/jersey/model/internal/CommonConfig.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; -import java.util.function.Function; import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.Logger; @@ -46,7 +45,6 @@ import org.glassfish.jersey.inject.spi.BinderConfigurationFactory; import org.glassfish.jersey.internal.LocalizationMessages; import org.glassfish.jersey.internal.ServiceFinder; -import org.glassfish.jersey.internal.inject.Binder; import org.glassfish.jersey.internal.inject.InjectionManager; import org.glassfish.jersey.internal.inject.JerseyBinderConfigurationFactory; import org.glassfish.jersey.internal.inject.ProviderBinder; diff --git a/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java b/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java index f9e42436f2..7f67df6e0e 100644 --- a/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java +++ b/tests/e2e-inject/hk2/src/test/java/org/glassfish/jersey/tests/e2e/inject/hk2/HK2AbstractBinderTest.java @@ -56,7 +56,6 @@ protected Application configure() { @Test public void testInjectableInjection() { String response = target().request().get(String.class); - System.out.println(response); assertThat(response, is(InjectableImpl.class.getName())); }