Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent HK2 AbstractBinder from being configured twice. #4349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
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;
import java.util.stream.Collectors;

import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.Priorities;
Expand All @@ -42,11 +44,11 @@
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;
Expand All @@ -64,6 +66,7 @@
public class CommonConfig implements FeatureContext, ExtendedConfig {

private static final Logger LOGGER = Logger.getLogger(CommonConfig.class.getName());
private static final Function<Object, Binder> CAST_TO_BINDER = Binder.class::cast;

/**
* Configuration runtime type.
Expand Down Expand Up @@ -97,42 +100,6 @@ 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<BinderConfigurationFactory> BINDER_CONFIGURATION_FACTORIES;
static {
final ServiceFinder<BinderConfigurationFactory> factoriesFinder =
ServiceFinder.find(BinderConfigurationFactory.class);
final List<BinderConfigurationFactory> configurationFactories = new LinkedList<>();
configurationFactories.add(new JerseyBinderConfigurationFactory());
for (BinderConfigurationFactory configurationFactory : factoriesFinder) {
configurationFactories.add(configurationFactory);
}
BINDER_CONFIGURATION_FACTORIES = Collections.unmodifiableList(configurationFactories);
}

private final List<BinderConfigurationFactory.BinderConfiguration> 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.
*/
Expand Down Expand Up @@ -652,20 +619,40 @@ public void configureAutoDiscoverableProviders(final InjectionManager injectionM
*/
public void configureMetaProviders(InjectionManager injectionManager, ManagedObjectsFinalizer finalizer) {
// First, configure existing binders
BinderConfigurations binderConfigurations = new BinderConfigurations(componentBag);
binderConfigurations.configureBinders(injectionManager);
Set<Binder> configuredBinders = configureBinders(injectionManager, Collections.emptySet());

// Check whether meta providers have been initialized for a config this config has been loaded from.
if (!disableMetaProviderConfiguration) {
// Register external meta objects
configureExternalObjects(injectionManager);
// Next, configure all features
// Configure all features
configureFeatures(injectionManager, new HashSet<>(), resetRegistrations(), finalizer);
// Next, register external meta objects
configureExternalObjects(injectionManager);
// At last, configure any new binders added by features
binderConfigurations.configureBinders(injectionManager);
configureBinders(injectionManager, configuredBinders);
}
}

private Set<Binder> configureBinders(InjectionManager injectionManager, Set<Binder> configured) {
Set<Binder> allConfigured = Collections.newSetFromMap(new IdentityHashMap<>());
allConfigured.addAll(configured);

Collection<Binder> binders = getBinder(configured);
if (!binders.isEmpty()) {
injectionManager.register(CompositeBinder.wrap(binders));
allConfigured.addAll(binders);
}

return allConfigured;
}

private Collection<Binder> getBinder(Set<Binder> 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) {
componentBag.getInstances(model -> ComponentBag.EXTERNAL_ONLY.test(model, injectionManager))
.forEach(injectionManager::register);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 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
Expand Down Expand Up @@ -91,6 +91,8 @@ public void register(Binder binder) {
public void register(Object provider) throws IllegalArgumentException {
if (isRegistrable(provider.getClass())) {
providers.add((org.glassfish.hk2.utilities.Binder) provider);
} else if (Class.class.isInstance(provider) && isRegistrable((Class) provider)) {
providers.add((org.glassfish.hk2.utilities.Binder) createAndInitialize((Class) provider));
} else {
throw new IllegalArgumentException(LocalizationMessages.HK_2_PROVIDER_NOT_REGISTRABLE(provider.getClass()));
}
Expand Down
Loading