Skip to content

Commit

Permalink
Adapt to reworked configuration sources (6.3 change)
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Sep 25, 2024
1 parent b917f57 commit 84b2ad8
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 131 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
The AppBuilder module is a builder for Magnolia apps in a java comparable to Blossom DialogBuilder.

## Requirements
* Java 11
* Magnolia >= 6.0
* Java 17
* Magnolia >= 6.3

## Setup

Expand Down Expand Up @@ -43,19 +43,19 @@ import org.apache.commons.lang3.reflect.TypeLiteral;
import org.reflections.Reflections;

import com.google.inject.multibindings.Multibinder;
import com.namics.oss.magnolia.appbuilder.AppRegistrar;
import com.namics.oss.magnolia.appbuilder.ChooserDialogRegistrar;
import com.namics.oss.magnolia.appbuilder.annotations.AppFactories;
import com.namics.oss.magnolia.appbuilder.annotations.AppFactory;
import com.namics.oss.magnolia.appbuilder.annotations.ChooserDialogFactories;
import com.namics.oss.magnolia.appbuilder.annotations.ChooserDialogFactory;

public class GuiceComponentConfigurer extends AbstractGuiceComponentConfigurer {
@Override
protected void configure() {
// Here we use Reflections, but you can also use ClassPathScanningCandidateComponentProvider or bind each factory manually
final Multibinder<Class<?>> appFactoryMultibinder = Multibinder.newSetBinder(binder(), new TypeLiteral<>() {}, AppRegistrar.AppFactories.class);
final Multibinder<Class<?>> appFactoryMultibinder = Multibinder.newSetBinder(binder(), new TypeLiteral<>() {}, AppFactories.class);
new Reflections(getClass()).getTypesAnnotatedWith(AppFactory.class).forEach(clazz -> appFactoryMultibinder.addBinding().toInstance(clazz));

final Multibinder<Class<?>> chooserDialogFactoryMultibinder = Multibinder.newSetBinder(binder(), new TypeLiteral<>() {}, ChooserDialogRegistrar.ChooserDialogFactories.class);
final Multibinder<Class<?>> chooserDialogFactoryMultibinder = Multibinder.newSetBinder(binder(), new TypeLiteral<>() {}, ChooserDialogFactories.class);
new Reflections(getClass()).getTypesAnnotatedWith(ChooserDialogFactory.class).forEach(clazz -> chooserDialogFactoryMultibinder.addBinding().toInstance(clazz));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import info.magnolia.module.ModuleLifecycle;
import info.magnolia.module.ModuleLifecycleContext;
import info.magnolia.ui.api.app.registry.AppDescriptorRegistry;
import info.magnolia.ui.dialog.DialogDefinitionRegistry;

import java.lang.invoke.MethodHandles;

Expand All @@ -12,23 +14,31 @@

public class AppBuilderModule implements ModuleLifecycle {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final AppRegistrar appRegistrar;
private final ChooserDialogRegistrar chooserDialogRegistrar;
private final DialogDefinitionRegistry dialogDefinitionRegistry;
private final ChooserDialogConfigurationSource chooserDialogRegistrar;
private final AppDescriptorRegistry appDescriptorRegistry;
private final AppConfigurationSource appConfigurationSource;

@Inject
public AppBuilderModule(
final AppRegistrar appRegistrar,
final ChooserDialogRegistrar chooserDialogRegistrar
final AppDescriptorRegistry appDescriptorRegistry,
final AppConfigurationSource appConfigurationSource,
final DialogDefinitionRegistry dialogDefinitionRegistry,
final ChooserDialogConfigurationSource chooserDialogRegistrar
) {
this.appRegistrar = appRegistrar;
this.appDescriptorRegistry = appDescriptorRegistry;
this.appConfigurationSource = appConfigurationSource;
this.dialogDefinitionRegistry = dialogDefinitionRegistry;
this.chooserDialogRegistrar = chooserDialogRegistrar;
}

@Override
public void start(ModuleLifecycleContext moduleLifecycleContext) {
LOG.debug("Starting AppBuilder Module");
appRegistrar.register();
chooserDialogRegistrar.register();
appDescriptorRegistry.bindTo(appConfigurationSource);
dialogDefinitionRegistry.bindTo(chooserDialogRegistrar);
appConfigurationSource.start();
chooserDialogRegistrar.start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.namics.oss.magnolia.appbuilder;

import static com.google.common.base.CaseFormat.*;

import info.magnolia.config.registry.DefinitionMetadata;
import info.magnolia.config.registry.DefinitionProvider;
import info.magnolia.config.registry.DefinitionType;
import info.magnolia.config.registry.RegistryMap;
import info.magnolia.config.source.ConfigurationSource;
import info.magnolia.config.source.ConfigurationSourceType;
import info.magnolia.config.source.ConfigurationSourceTypes;
import info.magnolia.objectfactory.Components;
import info.magnolia.ui.api.app.AppDescriptor;
import info.magnolia.ui.api.app.registry.DefinitionTypes;

import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.namics.oss.magnolia.appbuilder.annotations.AppFactories;

public class AppConfigurationSource implements ConfigurationSource<AppDescriptor> {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final RegistryMap<AppDescriptor> registryMap = new RegistryMap<>();
private final Set<Class<?>> appFactories;
private final LegacyAppDescriptorProvider.ColumnDefinitionFilter legacyAppColumnDefinitionFilter;

@Inject
public AppConfigurationSource(
@AppFactories final Set<Class<?>> appFactories,
final LegacyAppDescriptorProvider.ColumnDefinitionFilter legacyAppColumnDefinitionFilter
) {
this.appFactories = appFactories;
this.legacyAppColumnDefinitionFilter = legacyAppColumnDefinitionFilter;
}

@Override
public ConfigurationSourceType type() {
return ConfigurationSourceTypes.code;
}

@Override
public DefinitionType getDefinitionType() {
return DefinitionTypes.APP;
}

@Override
public void start() {
LOG.info("Setting up {} for {} definitions", getClass().getSimpleName(), LOWER_CAMEL.to(LOWER_HYPHEN, getDefinitionType().getName()));
registryMap.removeAndPutAll(
registryMap.keySet(),
appFactories.stream()
.flatMap(this::definitionProviders)
.collect(Collectors.toSet())
);
}

private Stream<DefinitionProvider<AppDescriptor>> definitionProviders(final Class<?> factoryClass) {
LOG.info("Registered app '{}' from {}", factoryClass.getSimpleName(), factoryClass.getName());
final Object factory = Components.newInstance(factoryClass);
return Stream.concat(
Stream.of(new AppDescriptorProvider(factory)),
Stream.of(new LegacyAppDescriptorProvider(factory, legacyAppColumnDefinitionFilter)).filter(LegacyAppDescriptorProvider::shouldRegister)
);
}

@Override
public DefinitionProvider<AppDescriptor> getProvider(final DefinitionMetadata id) {
return registryMap.get(id);
}

@Override
public DefinitionProvider<AppDescriptor> getProvider(final String id) {
return registryMap.getByStringKey(id);
}

@Override
public Collection<DefinitionMetadata> getAllMetadata() {
return registryMap.keySet();
}

@Override
public Collection<DefinitionProvider<AppDescriptor>> getAllProviders() {
return registryMap.values();
}
}
62 changes: 0 additions & 62 deletions src/main/java/com/namics/oss/magnolia/appbuilder/AppRegistrar.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.namics.oss.magnolia.appbuilder;

import static com.google.common.base.CaseFormat.*;

import info.magnolia.config.registry.DefinitionMetadata;
import info.magnolia.config.registry.DefinitionProvider;
import info.magnolia.config.registry.DefinitionType;
import info.magnolia.config.registry.RegistryMap;
import info.magnolia.config.source.ConfigurationSource;
import info.magnolia.config.source.ConfigurationSourceType;
import info.magnolia.config.source.ConfigurationSourceTypes;
import info.magnolia.objectfactory.Components;
import info.magnolia.ui.dialog.DefinitionTypes;
import info.magnolia.ui.dialog.DialogDefinition;

import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.namics.oss.magnolia.appbuilder.annotations.ChooserDialogFactories;

public class ChooserDialogConfigurationSource implements ConfigurationSource<DialogDefinition> {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final RegistryMap<DialogDefinition> registryMap = new RegistryMap<>();
private final Set<Class<?>> chooserDialogFactories;

@Inject
public ChooserDialogConfigurationSource(@ChooserDialogFactories final Set<Class<?>> chooserDialogFactories) {
this.chooserDialogFactories = chooserDialogFactories;
}

@Override
public ConfigurationSourceType type() {
return ConfigurationSourceTypes.code;
}

@Override
public DefinitionType getDefinitionType() {
return DefinitionTypes.DIALOG;
}

@Override
public void start() {
LOG.info("Setting up {} for {} definitions", getClass().getSimpleName(), LOWER_CAMEL.to(LOWER_HYPHEN, getDefinitionType().getName()));
registryMap.removeAndPutAll(
registryMap.keySet(),
chooserDialogFactories.stream()
.flatMap(this::definitionProviders)
.collect(Collectors.toSet())
);
}

private Stream<DefinitionProvider<DialogDefinition>> definitionProviders(final Class<?> factoryClass) {
LOG.info("Registered dialog '{}' from {}", factoryClass.getSimpleName(), factoryClass.getName());
final Object factory = Components.newInstance(factoryClass);
return Stream.of(new ChooserDialogDefinitionProvider<>(factory));
}

@Override
public DefinitionProvider<DialogDefinition> getProvider(final DefinitionMetadata id) {
return registryMap.get(id);
}

@Override
public DefinitionProvider<DialogDefinition> getProvider(final String id) {
return registryMap.getByStringKey(id);
}

@Override
public Collection<DefinitionMetadata> getAllMetadata() {
return registryMap.keySet();
}

@Override
public Collection<DefinitionProvider<DialogDefinition>> getAllProviders() {
return registryMap.values();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.namics.oss.magnolia.appbuilder.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.google.inject.BindingAnnotation;

@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface AppFactories {}
Loading

0 comments on commit 84b2ad8

Please sign in to comment.