Skip to content

Commit

Permalink
#1113 Drop explicitly scoped processors in favor of additional contex…
Browse files Browse the repository at this point in the history
…t passing
  • Loading branch information
GuusLieben committed Oct 14, 2024
1 parent 87d6429 commit f692eda
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

import org.checkerframework.checker.nullness.qual.NonNull;
import org.dockbox.hartshorn.inject.ContextKey;
import org.dockbox.hartshorn.inject.binding.DefaultBindingConfigurerContext;
import org.dockbox.hartshorn.inject.InjectionCapableApplication;
import org.dockbox.hartshorn.inject.binding.HierarchicalBinder;
import org.dockbox.hartshorn.inject.processing.ComponentProcessorRegistry;
import org.dockbox.hartshorn.inject.processing.CompositeHierarchicalBinderPostProcessor;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderPostProcessor;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderProcessorRegistry;
import org.dockbox.hartshorn.inject.processing.MultiMapComponentProcessorRegistry;
Expand Down Expand Up @@ -68,28 +68,31 @@ public class HierarchicalComponentProviderOrchestrator
private final transient ComponentRegistry registry;
private final transient ComponentPostConstructor postConstructor;

private final ComponentProcessorRegistry componentProcessorRegistry = new MultiMapComponentProcessorRegistry();
private final HierarchicalBinderProcessorRegistry binderProcessorRegistry = new MultiMapHierarchicalBinderProcessorRegistry();

private HierarchicalBinderAwareComponentProvider getOrCreateProvider(Scope scope) {
if (scope == null) {
scope = this.applicationScope;
}
synchronized (this.scopedProviders) {
return this.scopedProviders.computeIfAbsent(scope, this::createComponentProvider);
}
}
private final ComponentProcessorRegistry componentProcessorRegistry;
private final HierarchicalBinderProcessorRegistry binderProcessorRegistry;

protected HierarchicalComponentProviderOrchestrator(InjectionCapableApplication application, ComponentRegistry registry, ComponentPostConstructor postConstructor) {
this.registry = registry;
this.application = application;
this.postConstructor = postConstructor;

// Eagerly initialize the application provider
this.applicationScope = ScopeAdapter.of(this);
this.componentProcessorRegistry = new MultiMapComponentProcessorRegistry();
this.binderProcessorRegistry = new MultiMapHierarchicalBinderProcessorRegistry();

// Eagerly initialize the application provider
this.getOrCreateProvider(this.applicationScope);
}

private HierarchicalBinderAwareComponentProvider getOrCreateProvider(Scope scope) {
if (scope == null) {
scope = this.applicationScope;
}
synchronized (this.scopedProviders) {
return this.scopedProviders.computeIfAbsent(scope, this::createComponentProvider);
}
}

@NonNull
private HierarchicalBinderAwareComponentProvider createComponentProvider(Scope scope) {
HierarchicalBinderAwareComponentProvider provider = HierarchyAwareComponentProvider.create(
Expand All @@ -109,7 +112,8 @@ private HierarchicalBinderAwareComponentProvider createComponentProvider(Scope s
}
}

this.binderProcessorRegistry.process(this.application, provider.binder());
HierarchicalBinderPostProcessor binderPostProcessor = new CompositeHierarchicalBinderPostProcessor(this.binderProcessorRegistry()::processors);
binderPostProcessor.process(this.application, provider.scope(), provider.binder());
return provider;
}

Expand Down Expand Up @@ -200,8 +204,7 @@ public static ContextualInitializer<ComponentRegistry, ComponentProviderOrchestr

ComponentRegistry registry = context.input();
ComponentPostConstructor postConstructor = configurer.componentPostConstructor.initialize(SimpleSingleElementContext.create(application));
HierarchicalComponentProviderOrchestrator componentProvider = new HierarchicalComponentProviderOrchestrator(application, registry, postConstructor);
return componentProvider;
return new HierarchicalComponentProviderOrchestrator(application, registry, postConstructor);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.dockbox.hartshorn.inject.processing;

import java.util.function.Supplier;
import org.dockbox.hartshorn.inject.InjectionCapableApplication;
import org.dockbox.hartshorn.inject.binding.HierarchicalBinder;
import org.dockbox.hartshorn.inject.scope.Scope;
import org.dockbox.hartshorn.util.collections.MultiMap;

public class CompositeHierarchicalBinderPostProcessor implements HierarchicalBinderPostProcessor {

private final Supplier<MultiMap<Integer, HierarchicalBinderPostProcessor>> postProcessors;

public CompositeHierarchicalBinderPostProcessor(Supplier<MultiMap<Integer, HierarchicalBinderPostProcessor>> postProcessors) {
this.postProcessors = postProcessors;
}

@Override
public void process(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder) {
MultiMap<Integer, HierarchicalBinderPostProcessor> processors = this.postProcessors.get();
for (Integer priority : processors.keySet()) {
for(HierarchicalBinderPostProcessor processor : processors.get(priority)) {
processor.process(application, scope, binder);
}
}
}

@Override
public int priority() {
return ProcessingPriority.NORMAL_PRECEDENCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

/**
* A post processor for hierarchical binders. This can be used to add additional functionality to a binder, or to modify
Expand All @@ -17,7 +18,7 @@
*/
public interface HierarchicalBinderPostProcessor {

void process(InjectionCapableApplication application, HierarchicalBinder binder);
void process(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder);

int priority();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,18 @@

package org.dockbox.hartshorn.inject.processing;

import org.dockbox.hartshorn.inject.scope.ScopeKey;
import org.dockbox.hartshorn.util.collections.MultiMap;
import org.dockbox.hartshorn.util.option.Option;

public interface HierarchicalBinderProcessorRegistry extends HierarchicalBinderPostProcessor{
public interface HierarchicalBinderProcessorRegistry {

void register(HierarchicalBinderPostProcessor processor);

void register(ScopeKey scope, HierarchicalBinderPostProcessor processor);

void unregister(HierarchicalBinderPostProcessor processor);

void unregister(ScopeKey scope, HierarchicalBinderPostProcessor processor);

boolean isRegistered(Class<? extends HierarchicalBinderPostProcessor> componentProcessor);

boolean isRegistered(ScopeKey scope, Class<? extends HierarchicalBinderPostProcessor> componentProcessor);

<T extends HierarchicalBinderPostProcessor> Option<T> lookup(Class<T> componentProcessor);

<T extends HierarchicalBinderPostProcessor> Option<T> lookup(ScopeKey scope, Class<T> componentProcessor);

MultiMap<Integer, HierarchicalBinderPostProcessor> processors();

MultiMap<Integer, HierarchicalBinderPostProcessor> processors(ScopeKey scope);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.dockbox.hartshorn.inject.processing;

import java.util.Map;

import org.dockbox.hartshorn.inject.InjectionCapableApplication;
import org.dockbox.hartshorn.inject.binding.HierarchicalBinder;
import org.dockbox.hartshorn.inject.scope.ScopeKey;
import org.dockbox.hartshorn.util.collections.ConcurrentSetTreeMultiMap;
import org.dockbox.hartshorn.util.collections.MultiMap;
import org.dockbox.hartshorn.util.option.Option;
Expand All @@ -18,33 +13,17 @@ public void register(HierarchicalBinderPostProcessor processor) {
this.globalProcessors.put(processor.priority(), processor);
}

@Override
public void register(ScopeKey scope, HierarchicalBinderPostProcessor processor) {
// TODO: #1113 Implement scoped registration
}

@Override
public void unregister(HierarchicalBinderPostProcessor processor) {
this.globalProcessors.remove(processor.priority(), processor);
}

@Override
public void unregister(ScopeKey scope, HierarchicalBinderPostProcessor processor) {
// TODO: #1113 Implement scoped registration
}

@Override
public boolean isRegistered(Class<? extends HierarchicalBinderPostProcessor> componentProcessor) {
return this.globalProcessors.allValues().stream()
.anyMatch(processor -> processor.getClass().equals(componentProcessor));
}

@Override
public boolean isRegistered(ScopeKey scope, Class<? extends HierarchicalBinderPostProcessor> componentProcessor) {
// TODO: #1113 Implement scoped registration
return false;
}

@Override
public <T extends HierarchicalBinderPostProcessor> Option<T> lookup(Class<T> componentProcessor) {
return Option.of(this.globalProcessors.allValues().stream()
Expand All @@ -53,35 +32,8 @@ public <T extends HierarchicalBinderPostProcessor> Option<T> lookup(Class<T> com
.findFirst());
}

@Override
public <T extends HierarchicalBinderPostProcessor> Option<T> lookup(ScopeKey scope, Class<T> componentProcessor) {
// TODO: #1113 Implement scoped registration
return null;
}

@Override
public MultiMap<Integer, HierarchicalBinderPostProcessor> processors() {
return this.globalProcessors;
}

@Override
public MultiMap<Integer, HierarchicalBinderPostProcessor> processors(ScopeKey scope) {
// TODO: #1113 Implement scoped registration
return null;
}

@Override
public void process(InjectionCapableApplication application, HierarchicalBinder binder) {
MultiMap<Integer, HierarchicalBinderPostProcessor> processors = this.processors();
for (Integer priority : processors.keySet()) {
for(HierarchicalBinderPostProcessor processor : processors.get(priority)) {
processor.process(application, binder);
}
}
}

@Override
public int priority() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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 class SampleBinderPostProcessor implements HierarchicalBinderPostProcessor {

public static final String HELLO_WORLD = "Hello, World!";

@Override
public void process(InjectionCapableApplication application, HierarchicalBinder binder) {
public void process(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder) {
binder.bind(String.class).singleton(HELLO_WORLD);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import org.dockbox.hartshorn.inject.binding.HierarchicalBinder;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderPostProcessor;
import org.dockbox.hartshorn.inject.processing.ProcessingPriority;
import org.dockbox.hartshorn.inject.scope.Scope;

public record BindingConfigurerBinderPostProcessor(
DefaultBindingConfigurer configurer
) implements HierarchicalBinderPostProcessor {

@Override
public void process(InjectionCapableApplication application, HierarchicalBinder binder) {
public void process(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder) {
this.configurer.configure(binder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class DefaultConfigurationBinderPostProcessor implements HierarchicalBinderPostProcessor {

@Override
public void process(InjectionCapableApplication application, HierarchicalBinder binder) {
public void process(InjectionCapableApplication application, Scope scope, HierarchicalBinder binder) {
// Application environment
binder.bind(InjectorEnvironment.class).singleton(application.environment());
if (application.environment() instanceof ApplicationEnvironment applicationEnvironment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.dockbox.hartshorn.inject.processing.ComponentProcessorRegistry;
import org.dockbox.hartshorn.inject.processing.CompositeHierarchicalBinderPostProcessor;
import org.dockbox.hartshorn.inject.processing.ContainerAwareComponentPopulatorPostProcessor;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderPostProcessor;
import org.dockbox.hartshorn.inject.processing.HierarchicalBinderProcessorRegistry;
import org.dockbox.hartshorn.inject.provider.ComponentProviderOrchestrator;
import org.dockbox.hartshorn.inject.provider.PostProcessingComponentProvider;
import org.dockbox.hartshorn.inject.scope.Scope;
import org.dockbox.hartshorn.launchpad.ApplicationContext;
import org.dockbox.hartshorn.launchpad.Hartshorn;
import org.dockbox.hartshorn.launchpad.ProcessableApplicationContext;
Expand Down Expand Up @@ -159,7 +161,9 @@ private void registerComponentProcessors(ApplicationContext applicationContext,
this.componentProcessorRegistrar.registerBinderProcessors(registry, applicationContext.environment().introspector(), activators);
// Global binder is already initialized (albeit unused until this point), so need to ensure that it is processed
// TODO #1113: Inspect if we can move this to the initialization of the global binder
registry.process(applicationContext, applicationContext.defaultBinder());
Scope applicationScope = applicationContext.scope();
HierarchicalBinderPostProcessor processor = new CompositeHierarchicalBinderPostProcessor(registry::processors);
processor.process(applicationContext, applicationScope, applicationContext.defaultBinder());
}
else {
this.buildContext.logger().warn("Default component provider is not orchestrating binders, binder processors will not be registered");
Expand Down

0 comments on commit f692eda

Please sign in to comment.