Skip to content

Commit

Permalink
#1113 Only apply default bindings to global binder
Browse files Browse the repository at this point in the history
  • Loading branch information
GuusLieben committed Oct 25, 2024
1 parent 19c0d74 commit 66e05e9
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -39,7 +40,7 @@
import org.dockbox.hartshorn.inject.graph.strategy.MethodAwareBindingStrategyContext;
import org.dockbox.hartshorn.inject.graph.strategy.MethodInstanceBindingStrategy;
import org.dockbox.hartshorn.inject.graph.strategy.SimpleBindingStrategyRegistry;
import org.dockbox.hartshorn.inject.provider.ComponentProviderOrchestrator;
import org.dockbox.hartshorn.inject.provider.ComponentRegistryAwareComponentProvider;
import org.dockbox.hartshorn.util.ContextualInitializer;
import org.dockbox.hartshorn.util.Customizer;
import org.dockbox.hartshorn.util.LazyStreamableConfigurer;
Expand Down Expand Up @@ -128,14 +129,8 @@ public static ContextualInitializer<InjectionCapableApplication, DependencyResol
binder.bind(ConditionMatcher.class).singleton(conditionMatcher);
});

final ComponentRegistry componentRegistry;
if (application.environment() instanceof ManagedComponentEnvironment environment) {
componentRegistry = environment.componentRegistry();
}
else if (application.defaultProvider() instanceof ComponentProviderOrchestrator orchestrator) {
componentRegistry = orchestrator.componentRegistry();
}
else {
ComponentRegistry componentRegistry = configurer.registryLookup.apply(application);
if (componentRegistry == null) {
throw new ComponentConfigurationException("Could not resolve component registry from current application");
}
return new BindsMethodDependencyResolver(conditionMatcher, componentRegistry, registry);
Expand All @@ -155,6 +150,7 @@ public static class Configurer {
MethodInstanceBindingStrategy.create(Customizer.useDefaults())
);
private ContextualInitializer<InjectionCapableApplication, ConditionMatcher> conditionMatcher = context -> new ConditionMatcher(context.input());
private Function<InjectionCapableApplication, ComponentRegistry> registryLookup = new ComponentRegistryLookup();

public Configurer conditionMatcher(ConditionMatcher conditionMatcher) {
return this.conditionMatcher(ContextualInitializer.of(conditionMatcher));
Expand All @@ -169,5 +165,24 @@ public Configurer bindingStrategies(Customizer<StreamableConfigurer<InjectionCap
this.bindingStrategies.customizer(customizer);
return this;
}

public Configurer registryLookup(Function<InjectionCapableApplication, ComponentRegistry> registryLookup) {
this.registryLookup = registryLookup;
return this;
}

public static class ComponentRegistryLookup implements Function<InjectionCapableApplication, ComponentRegistry> {

@Override
public ComponentRegistry apply(InjectionCapableApplication application) {
if (application.environment() instanceof ManagedComponentEnvironment environment) {
return environment.componentRegistry();
} else if (application.defaultProvider() instanceof ComponentRegistryAwareComponentProvider orchestrator) {
return orchestrator.componentRegistry();
} else {
return null;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.dockbox.hartshorn.inject.ComponentKey;
import org.dockbox.hartshorn.inject.InjectionCapableApplication;
import org.dockbox.hartshorn.inject.binding.DefaultBindingConfigurerContext;
import org.dockbox.hartshorn.inject.component.ComponentContainer;
import org.dockbox.hartshorn.inject.populate.ComponentPopulator;
import org.dockbox.hartshorn.util.ContextualInitializer;
Expand Down Expand Up @@ -46,7 +47,11 @@ public static ContextualInitializer<InjectionCapableApplication, ComponentPostPr
return context -> {
Configurer configurer = new Configurer();
customizer.configure(configurer);
return new ContainerAwareComponentPopulatorPostProcessor(configurer.componentPopulator.initialize(context));
ComponentPopulator populator = configurer.componentPopulator.initialize(context);
DefaultBindingConfigurerContext.compose(context, binder -> {
binder.bind(ComponentPopulator.class).singleton(populator);
});
return new ContainerAwareComponentPopulatorPostProcessor(populator);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.dockbox.hartshorn.inject.collection.ComponentCollection;
import org.dockbox.hartshorn.inject.collection.ContainerAwareComponentCollection;
import org.dockbox.hartshorn.inject.component.ComponentContainer;
import org.dockbox.hartshorn.inject.provider.ComponentRegistryAwareComponentProvider;
import org.dockbox.hartshorn.inject.provider.ObjectContainer;
import org.dockbox.hartshorn.inject.provider.ComponentProviderOrchestrator;
import org.dockbox.hartshorn.proxy.ProxyFactory;
import org.dockbox.hartshorn.proxy.lookup.StateAwareProxyFactory;
import org.dockbox.hartshorn.util.ApplicationException;
Expand All @@ -45,13 +45,13 @@
*/
public class SimpleComponentProviderPostProcessor implements ComponentProviderPostProcessor {

private final ComponentProviderOrchestrator owner;
private final ComponentRegistryAwareComponentProvider owner;
private final ComponentPostProcessor processor;
private final InjectionCapableApplication application;
private final ComponentStoreCallback componentStoreCallback;

public SimpleComponentProviderPostProcessor(
ComponentProviderOrchestrator owner,
ComponentRegistryAwareComponentProvider owner,
ComponentPostProcessor processor,
InjectionCapableApplication application,
ComponentStoreCallback componentStoreCallback
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dockbox.hartshorn.inject.provider;

import org.dockbox.hartshorn.inject.component.ComponentRegistry;

public interface ComponentRegistryAwareComponentProvider extends ComponentProvider {

ComponentRegistry componentRegistry();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dockbox.hartshorn.inject.provider;

public interface ComponentRegistryAwareProviderOrchestrator extends ComponentRegistryAwareComponentProvider, ComponentProviderOrchestrator {
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.dockbox.hartshorn.inject.scope.Scope;
import org.dockbox.hartshorn.util.ContextualInitializer;
import org.dockbox.hartshorn.util.Customizer;
import org.dockbox.hartshorn.util.SimpleSingleElementContext;
import org.dockbox.hartshorn.util.collections.HashSetMultiMap;
import org.dockbox.hartshorn.util.collections.MultiMap;

Expand All @@ -60,7 +59,7 @@
*/
public class HierarchicalComponentProviderOrchestrator
extends DefaultFallbackCompatibleContext
implements HierarchicalComponentProvider, ComponentProviderOrchestrator, HierarchicalBinder {
implements HierarchicalComponentProvider, ComponentRegistryAwareProviderOrchestrator, HierarchicalBinder {

private final Map<Scope, HierarchicalBinderAwareComponentProvider> scopedProviders = Collections.synchronizedMap(new WeakHashMap<>());
private final Scope applicationScope;
Expand Down Expand Up @@ -207,7 +206,7 @@ public static ContextualInitializer<ComponentRegistry, ComponentProviderOrchestr
customizer.configure(configurer);

ComponentRegistry registry = context.input();
ComponentPostConstructor postConstructor = configurer.componentPostConstructor.initialize(SimpleSingleElementContext.create(application));
ComponentPostConstructor postConstructor = configurer.componentPostConstructor.initialize(context.transform(application));
return new HierarchicalComponentProviderOrchestrator(application, registry, postConstructor);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class HierarchyAwareComponentProvider extends StrategyChainComponentProvi
private final SingletonCache singletonCache;

public HierarchyAwareComponentProvider(
ComponentProviderOrchestrator orchestrator,
ComponentRegistryAwareProviderOrchestrator orchestrator,
ComponentPostConstructor postConstructor,
InjectionCapableApplication application,
SingletonCache singletonCache,
Expand Down Expand Up @@ -104,7 +104,7 @@ public HierarchyAwareComponentProvider(

protected static ComponentProviderPostProcessor createProviderPostProcessor(
SingletonCache singletonCache,
ComponentProviderOrchestrator orchestrator,
ComponentRegistryAwareProviderOrchestrator orchestrator,
InjectionCapableApplication application,
ComponentPostConstructor postConstructor
) {
Expand Down Expand Up @@ -169,7 +169,7 @@ public <T> BindingHierarchy<T> hierarchy(ComponentKey<T> key, boolean useGlobalI
}

public static HierarchyAwareComponentProvider create(
ComponentProviderOrchestrator orchestrator,
ComponentRegistryAwareProviderOrchestrator orchestrator,
ComponentPostConstructor postConstructor,
InjectionCapableApplication application,
SingletonCache singletonCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collection;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.dockbox.hartshorn.inject.binding.DefaultBindingConfigurerContext;
import org.dockbox.hartshorn.inject.provider.ComponentConstructorResolver;
import org.dockbox.hartshorn.inject.ComponentKey;
import org.dockbox.hartshorn.inject.InjectionCapableApplication;
Expand Down Expand Up @@ -113,7 +114,11 @@ public static ContextualInitializer<InjectionCapableApplication, ComponentPostPr
return context -> {
Configurer configurer = new Configurer();
customizer.configure(configurer);
return new ComponentPopulatorPostProcessor(configurer.componentPopulator.initialize(context));
ComponentPopulator populator = configurer.componentPopulator.initialize(context);
DefaultBindingConfigurerContext.compose(context, binder -> {
binder.bind(ComponentPopulator.class).singleton(populator);
});
return new ComponentPopulatorPostProcessor(populator);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.dockbox.hartshorn.inject.provider;

import org.dockbox.hartshorn.inject.component.ComponentRegistry;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderProcessorRegistry;
import org.dockbox.hartshorn.inject.scope.Scope;

Expand All @@ -29,8 +28,6 @@
*/
public interface ComponentProviderOrchestrator extends PostProcessingComponentProvider {

ComponentRegistry componentRegistry();

HierarchicalBinderProcessorRegistry binderProcessorRegistry();

HierarchicalComponentProvider applicationProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.dockbox.hartshorn.launchpad.activation.ActivatorHolder;
import org.dockbox.hartshorn.launchpad.activation.ContextActivatorHolder;
import org.dockbox.hartshorn.inject.binding.DefaultBindingConfigurer;
import org.dockbox.hartshorn.inject.binding.DefaultBindingConfigurerContext;
import org.dockbox.hartshorn.inject.ExceptionHandler;
import org.dockbox.hartshorn.launchpad.activation.ServiceActivatorContext;
import org.dockbox.hartshorn.inject.ApplicationPropertyHolder;
Expand Down Expand Up @@ -114,9 +113,6 @@ protected DelegatingApplicationContext(SingleElementContext<? extends Applicatio
this.componentProvider = configurer.componentProvider.initialize(applicationInitializerContext.transform(this.environment().componentRegistry()));

DefaultBindingConfigurer bindingConfigurer = configurer.defaultBindings.initialize(applicationInitializerContext);
for (DefaultBindingConfigurerContext configurerContext : initializerContext.contexts(DefaultBindingConfigurerContext.class)) {
bindingConfigurer = bindingConfigurer.compose(configurerContext.configurer());
}
this.componentProvider.binderProcessorRegistry().register(new BindingConfigurerBinderPostProcessor(bindingConfigurer));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dockbox.hartshorn.launchpad.configuration;

import org.dockbox.hartshorn.inject.InjectionCapableApplication;
import org.dockbox.hartshorn.inject.binding.HierarchicalBinder;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderPostProcessor;
import org.dockbox.hartshorn.inject.scope.Scope;

public abstract class AbstractScopeFilteredBinderPostProcessor implements HierarchicalBinderPostProcessor {

@Override
public void process(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder) {
if (this.supportsScope(scope)) {
this.processBinder(application, scope, binder);
}
}

protected abstract void processBinder(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder);

protected abstract boolean supportsScope(Scope scope);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dockbox.hartshorn.launchpad.configuration;

import org.dockbox.hartshorn.inject.InjectionCapableApplication;
import org.dockbox.hartshorn.inject.binding.HierarchicalBinder;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderPostProcessor;
import org.dockbox.hartshorn.inject.scope.Scope;
import org.dockbox.hartshorn.inject.scope.ScopeKey;

import java.util.Set;
import java.util.function.Predicate;

public class ScopeFilteredDelegateBinderPostProcessor extends AbstractScopeFilteredBinderPostProcessor {

private final HierarchicalBinderPostProcessor processor;
private final Predicate<ScopeKey> scopeFilter;

public ScopeFilteredDelegateBinderPostProcessor(HierarchicalBinderPostProcessor processor, Predicate<ScopeKey> scopeFilter) {
this.processor = processor;
this.scopeFilter = scopeFilter;
}

public static ScopeFilteredDelegateBinderPostProcessor create(HierarchicalBinderPostProcessor processor, Predicate<ScopeKey> scopeFilter) {
return new ScopeFilteredDelegateBinderPostProcessor(processor, scopeFilter);
}

public static ScopeFilteredDelegateBinderPostProcessor create(HierarchicalBinderPostProcessor processor, ScopeKey... permittedScopes) {
return new ScopeFilteredDelegateBinderPostProcessor(processor, Set.of(permittedScopes)::contains);
}

@Override
protected void processBinder(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder) {
this.processor.process(application, scope, binder);
}

@Override
protected boolean supportsScope(Scope scope) {
return scope != null && this.scopeFilter.test(scope.installableScopeType());
}

@Override
public int priority() {
return this.processor.priority();
}
}
Loading

0 comments on commit 66e05e9

Please sign in to comment.