Skip to content

Commit

Permalink
Prevent HK2 AbstractBinder from being configured twice.
Browse files Browse the repository at this point in the history
Revert the change that introduced BinderConfigurationFactory SPI.
Added possibility to register HK2 AbstractBinder class along with the instance option.

Signed-off-by: Jan Supol <[email protected]>
  • Loading branch information
jansupol committed Dec 17, 2019
1 parent 51abc78 commit 29913b9
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 363 deletions.

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

0 comments on commit 29913b9

Please sign in to comment.