From 1bf404c3cee3245f9f98bb46ba5a4e253f1e00a6 Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Tue, 11 Jun 2024 02:57:33 -0400 Subject: [PATCH] Rename Property Plugin from PropertyRequiresPlugin to ConfigPropertyPlugin (#572) * start * fix tests * deprecated * bump plugin * versions * format * Update ExternalProvider.java * format? * revert property plugin change * Revert "revert property plugin change" This reverts commit cf9f166ead09ce97896371f458d9880d42d915a6. * Rename PropertyPlugin * Update PropertyRequiresPlugin.java * Update ExternalProvider.java * update gradle plugin * revert config prop change * Update PropertyRequiresPlugin.java * Update DBeanScopeBuilder.java * move methods back how they were * Cleanup * fix conflict * fix conflicts * Update pom.xml * Update pom.xml * ConfigPropertyPlugin extends InjectExtension * Rename internal field names to propertyPlugin and fix javadoc --------- Co-authored-by: Rob Bygrave <130515035+rob-bygrave@users.noreply.github.com> Co-authored-by: Rob Bygrave --- .../example/myapp/ConfigPropertiesPlugin.java | 4 +- .../io/avaje/inject/BeanScopeBuilder.java | 20 +++++++--- .../io/avaje/inject/DBeanScopeBuilder.java | 34 +++++++++------- .../java/io/avaje/inject/DConfigProps.java | 6 +-- .../java/io/avaje/inject/DServiceLoader.java | 8 ++-- .../java/io/avaje/inject/DSystemProps.java | 4 +- .../main/java/io/avaje/inject/Profile.java | 4 +- .../io/avaje/inject/RequiresProperty.java | 4 +- .../java/io/avaje/inject/spi/Builder.java | 4 +- .../inject/spi/ConfigPropertyPlugin.java | 39 +++++++++++++++++++ .../java/io/avaje/inject/spi/DBuilder.java | 10 ++--- .../io/avaje/inject/spi/DBuilderExtn.java | 2 +- .../inject/spi/PropertyRequiresPlugin.java | 3 ++ 13 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 inject/src/main/java/io/avaje/inject/spi/ConfigPropertyPlugin.java diff --git a/blackbox-test-inject/src/main/java/org/example/myapp/ConfigPropertiesPlugin.java b/blackbox-test-inject/src/main/java/org/example/myapp/ConfigPropertiesPlugin.java index eea02006c..4200c5c7e 100644 --- a/blackbox-test-inject/src/main/java/org/example/myapp/ConfigPropertiesPlugin.java +++ b/blackbox-test-inject/src/main/java/org/example/myapp/ConfigPropertiesPlugin.java @@ -2,13 +2,13 @@ import io.avaje.config.Config; import io.avaje.inject.Component; -import io.avaje.inject.spi.PropertyRequiresPlugin; +import io.avaje.inject.spi.ConfigPropertyPlugin; import org.other.one.OtherComponent2; import java.util.Optional; @Component.Import(value = OtherComponent2.class) -public class ConfigPropertiesPlugin implements PropertyRequiresPlugin { +public class ConfigPropertiesPlugin implements ConfigPropertyPlugin { @Override public Optional get(String property) { diff --git a/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java b/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java index 132fcebd8..a438aabaf 100644 --- a/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java +++ b/inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java @@ -1,6 +1,7 @@ package io.avaje.inject; import io.avaje.inject.spi.AvajeModule; +import io.avaje.inject.spi.ConfigPropertyPlugin; import io.avaje.inject.spi.PropertyRequiresPlugin; import io.avaje.lang.NonNullApi; import io.avaje.lang.Nullable; @@ -80,17 +81,26 @@ public interface BeanScopeBuilder { BeanScopeBuilder modules(AvajeModule... modules); /** - * Set the PropertyPlugin used for this scope. This is serviceloaded automatically of not set + * Return the PropertyRequiresPlugin used for this scope. This is useful for plugins that want to + * use the scopes wiring properties. * - * @param propertyRequiresPlugin The plugin for conditions based on properties + * @deprecated use {@link #configPlugin()} instead */ - void propertyPlugin(PropertyRequiresPlugin propertyRequiresPlugin); + @Deprecated(forRemoval = true) + PropertyRequiresPlugin propertyPlugin(); + + /** + * Set the ConfigPropertyPlugin used for this scope. This is serviceloaded automatically of not set + * + * @param propertyPlugin The plugin for conditions based on properties + */ + void configPlugin(ConfigPropertyPlugin propertyPlugin); /** - * Return the PropertyPlugin used for this scope. This is useful for plugins that want to use + * Return the ConfigPropertyPlugin used for this scope. This is useful for plugins that want to use * the scopes wiring properties. */ - PropertyRequiresPlugin propertyPlugin(); + ConfigPropertyPlugin configPlugin(); /** * Supply a bean to the scope that will be used instead of any similar bean in the scope. diff --git a/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java b/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java index 2f943d3a3..58d95dad1 100644 --- a/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java +++ b/inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java @@ -37,7 +37,7 @@ final class DBeanScopeBuilder implements BeanScopeBuilder.ForTesting { private boolean parentOverride = true; private boolean shutdownHook; private ClassLoader classLoader; - private PropertyRequiresPlugin propertyRequiresPlugin; + private ConfigPropertyPlugin propertyPlugin; private Set profiles; /** Create a BeanScopeBuilder to ultimately load and return a new BeanScope. */ @@ -61,16 +61,24 @@ public BeanScopeBuilder modules(AvajeModule... modules) { } @Override - public void propertyPlugin(PropertyRequiresPlugin propertyRequiresPlugin) { - this.propertyRequiresPlugin = propertyRequiresPlugin; + public PropertyRequiresPlugin propertyPlugin() { + if (propertyPlugin == null) { + propertyPlugin = defaultPropertyPlugin(); + } + return propertyPlugin; } @Override - public PropertyRequiresPlugin propertyPlugin() { - if (propertyRequiresPlugin == null) { - propertyRequiresPlugin = defaultPropertyPlugin(); + public void configPlugin(ConfigPropertyPlugin propertyPlugin) { + this.propertyPlugin = propertyPlugin; + } + + @Override + public ConfigPropertyPlugin configPlugin() { + if (propertyPlugin == null) { + propertyPlugin = defaultPropertyPlugin(); } - return propertyRequiresPlugin; + return propertyPlugin; } @Override @@ -209,7 +217,7 @@ private void initClassLoader() { classLoader = Thread.currentThread().getContextClassLoader(); } } - private PropertyRequiresPlugin defaultPropertyPlugin() { + private ConfigPropertyPlugin defaultPropertyPlugin() { return detectAvajeConfig() ? new DConfigProps() : new DSystemProps(); } @@ -228,7 +236,7 @@ private boolean detectAvajeConfig() { private void initProfiles() { if (profiles == null) { profiles = - propertyRequiresPlugin + propertyPlugin .get("avaje.profiles") .map(p -> Set.of(p.split(","))) .orElse(emptySet()); @@ -242,8 +250,8 @@ public BeanScope build() { initClassLoader(); var serviceLoader = new DServiceLoader(classLoader); - if (propertyRequiresPlugin == null) { - propertyRequiresPlugin = serviceLoader.propertyPlugin().orElseGet(this::defaultPropertyPlugin); + if (propertyPlugin == null) { + propertyPlugin = serviceLoader.propertyPlugin().orElseGet(this::defaultPropertyPlugin); } serviceLoader.plugins().forEach(plugin -> plugin.apply(this)); @@ -265,11 +273,11 @@ public BeanScope build() { + " Refer to https://avaje.io/inject#gradle"); } - final var level = propertyRequiresPlugin.contains("printModules") ? INFO : DEBUG; + final var level = propertyPlugin.contains("printModules") ? INFO : DEBUG; initProfiles(); log.log(level, "building with avaje modules {0} profiles {1}", moduleNames, profiles); - final var builder = Builder.newBuilder(profiles, propertyRequiresPlugin, suppliedBeans, enrichBeans, parent, parentOverride); + final var builder = Builder.newBuilder(profiles, propertyPlugin, suppliedBeans, enrichBeans, parent, parentOverride); for (final var factory : factoryOrder.factories()) { builder.currentModule(factory.getClass()); factory.build(builder); diff --git a/inject/src/main/java/io/avaje/inject/DConfigProps.java b/inject/src/main/java/io/avaje/inject/DConfigProps.java index 51438e18d..d8d3f9c55 100644 --- a/inject/src/main/java/io/avaje/inject/DConfigProps.java +++ b/inject/src/main/java/io/avaje/inject/DConfigProps.java @@ -1,14 +1,14 @@ package io.avaje.inject; import io.avaje.config.Config; -import io.avaje.inject.spi.PropertyRequiresPlugin; +import io.avaje.inject.spi.ConfigPropertyPlugin; import java.util.Optional; /** - * Avaje-Config based implementation of PropertyRequiresPlugin. + * Avaje-Config based implementation of ConfigPropertyPlugin. */ -final class DConfigProps implements PropertyRequiresPlugin { +final class DConfigProps implements ConfigPropertyPlugin { @Override public Optional get(String property) { diff --git a/inject/src/main/java/io/avaje/inject/DServiceLoader.java b/inject/src/main/java/io/avaje/inject/DServiceLoader.java index 0176523c0..534ac5c6d 100644 --- a/inject/src/main/java/io/avaje/inject/DServiceLoader.java +++ b/inject/src/main/java/io/avaje/inject/DServiceLoader.java @@ -16,7 +16,7 @@ final class DServiceLoader { private final List plugins = new ArrayList<>(); private final List modules = new ArrayList<>(); private ModuleOrdering moduleOrdering; - private PropertyRequiresPlugin propertyPlugin; + private ConfigPropertyPlugin propertyPlugin; DServiceLoader(ClassLoader classLoader) { for (var spi : ServiceLoader.load(InjectExtension.class, classLoader)) { @@ -26,8 +26,8 @@ final class DServiceLoader { modules.add((AvajeModule) spi); } else if (spi instanceof ModuleOrdering) { moduleOrdering = (ModuleOrdering) spi; - } else if (spi instanceof PropertyRequiresPlugin) { - propertyPlugin = (PropertyRequiresPlugin) spi; + } else if (spi instanceof ConfigPropertyPlugin) { + propertyPlugin = (ConfigPropertyPlugin) spi; } } // older plugins and modules @@ -47,7 +47,7 @@ Optional moduleOrdering() { return Optional.ofNullable(moduleOrdering); } - Optional propertyPlugin() { + Optional propertyPlugin() { return Optional.ofNullable(propertyPlugin); } } diff --git a/inject/src/main/java/io/avaje/inject/DSystemProps.java b/inject/src/main/java/io/avaje/inject/DSystemProps.java index 70391f8ce..ed15ab2c7 100644 --- a/inject/src/main/java/io/avaje/inject/DSystemProps.java +++ b/inject/src/main/java/io/avaje/inject/DSystemProps.java @@ -2,7 +2,9 @@ import java.util.Optional; -final class DSystemProps implements io.avaje.inject.spi.PropertyRequiresPlugin { +import io.avaje.inject.spi.ConfigPropertyPlugin; + +final class DSystemProps implements ConfigPropertyPlugin { @Override public Optional get(String property) { diff --git a/inject/src/main/java/io/avaje/inject/Profile.java b/inject/src/main/java/io/avaje/inject/Profile.java index fa4518967..1cfd404d6 100644 --- a/inject/src/main/java/io/avaje/inject/Profile.java +++ b/inject/src/main/java/io/avaje/inject/Profile.java @@ -24,12 +24,12 @@ * } * *

In the sample above, the MyService bean will get wired only if avaje.profiles is - * set to "test" in the {@link io.avaje.inject.spi.PropertyRequiresPlugin}. + * set to "test" in the {@link io.avaje.inject.spi.ConfigPropertyPlugin}. * *

Avaje Config provides an implementation and if it is included in the classpath then Avaje * Config will be used to test the property conditions. * - *

If no PropertyRequiresPlugin is found then the default implementation is used which uses + *

If no ConfigPropertyPlugin is found then the default implementation is used which uses * {@link System#getProperty(String)} and {@link System#getenv(String)}. */ @Retention(RUNTIME) diff --git a/inject/src/main/java/io/avaje/inject/RequiresProperty.java b/inject/src/main/java/io/avaje/inject/RequiresProperty.java index f7bf7c508..ad0ae46b1 100644 --- a/inject/src/main/java/io/avaje/inject/RequiresProperty.java +++ b/inject/src/main/java/io/avaje/inject/RequiresProperty.java @@ -29,12 +29,12 @@ * In the sample above the MyService bean will get wired only if use.service * is set in Java system properties / Avaje Config. *

- * {@link io.avaje.inject.spi.PropertyRequiresPlugin} is used to test the property conditions and is loaded via {@link java.util.ServiceLoader}. + * {@link io.avaje.inject.spi.ConfigPropertyPlugin} is used to test the property conditions and is loaded via {@link java.util.ServiceLoader}. *

* Avaje Config provides an implementation and if it is included in the classpath then * Avaje Config will be used to test the property conditions. *

- * If no PropertyRequiresPlugin is found then the default implementation is used which uses + * If no ConfigPropertyPlugin is found then the default implementation is used which uses * {@link System#getProperty(String)} and {@link System#getenv(String)}. */ @Retention(RUNTIME) diff --git a/inject/src/main/java/io/avaje/inject/spi/Builder.java b/inject/src/main/java/io/avaje/inject/spi/Builder.java index 89b5dd3a2..8d9ca7b0a 100644 --- a/inject/src/main/java/io/avaje/inject/spi/Builder.java +++ b/inject/src/main/java/io/avaje/inject/spi/Builder.java @@ -25,7 +25,7 @@ public interface Builder { * @param parentOverride When false do not add beans that already exist on the parent */ @SuppressWarnings("rawtypes") - static Builder newBuilder(Set profiles, PropertyRequiresPlugin plugin, List suppliedBeans, List enrichBeans, BeanScope parent, boolean parentOverride) { + static Builder newBuilder(Set profiles, ConfigPropertyPlugin plugin, List suppliedBeans, List enrichBeans, BeanScope parent, boolean parentOverride) { if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) { // simple case, no mocks or spies return new DBuilder(profiles, plugin, parent, parentOverride); @@ -270,7 +270,7 @@ static Builder newBuilder(Set profiles, PropertyRequiresPlugin plugin, L /** * Return the plugin for required properties. */ - PropertyRequiresPlugin property(); + ConfigPropertyPlugin property(); /** * Build and return the bean scope. diff --git a/inject/src/main/java/io/avaje/inject/spi/ConfigPropertyPlugin.java b/inject/src/main/java/io/avaje/inject/spi/ConfigPropertyPlugin.java new file mode 100644 index 000000000..4d77aa86a --- /dev/null +++ b/inject/src/main/java/io/avaje/inject/spi/ConfigPropertyPlugin.java @@ -0,0 +1,39 @@ +package io.avaje.inject.spi; + +import java.util.Optional; + +import io.avaje.lang.NonNullApi; + +/** + * InjectPlugin interface which contains the application properties used for wiring. Used with + * {@link io.avaje.inject.RequiresProperty} and {@link io.avaje.inject.Profile}. + * + *

The plugin is loaded via ServiceLoader and defaults to an implementation that uses {@link + * System#getProperty(String)} and {@link System#getenv(String)}. + */ +@NonNullApi +public interface ConfigPropertyPlugin extends InjectExtension, PropertyRequiresPlugin { + + /** + * Return a configuration value that might not exist. + */ + Optional get(String property); + + /** + * Return true if the property is defined. + */ + boolean contains(String property); + + /** Return true if the property is not defined. */ + default boolean missing(String property) { + return !contains(property); + } + + /** Return true if the property is equal to the given value. */ + boolean equalTo(String property, String value); + + /** Return true if the property is not defined or not equal to the given value. */ + default boolean notEqualTo(String property, String value) { + return !equalTo(property, value); + } +} diff --git a/inject/src/main/java/io/avaje/inject/spi/DBuilder.java b/inject/src/main/java/io/avaje/inject/spi/DBuilder.java index e39300875..8355e4709 100644 --- a/inject/src/main/java/io/avaje/inject/spi/DBuilder.java +++ b/inject/src/main/java/io/avaje/inject/spi/DBuilder.java @@ -13,7 +13,7 @@ class DBuilder implements Builder { - private final PropertyRequiresPlugin propertyRequires; + private final ConfigPropertyPlugin propertyPlugin; private final Set profiles; /** List of Lifecycle methods. */ private final List postConstruct = new ArrayList<>(); @@ -36,8 +36,8 @@ class DBuilder implements Builder { private DBeanScopeProxy beanScopeProxy; - DBuilder(Set profiles, PropertyRequiresPlugin propertyRequires, BeanScope parent, boolean parentOverride) { - this.propertyRequires = propertyRequires; + DBuilder(Set profiles, ConfigPropertyPlugin propertyPlugin, BeanScope parent, boolean parentOverride) { + this.propertyPlugin = propertyPlugin; this.parent = parent; this.parentOverride = parentOverride; this.profiles = profiles; @@ -378,8 +378,8 @@ public boolean containsQualifier(String name) { } @Override - public PropertyRequiresPlugin property() { - return propertyRequires; + public ConfigPropertyPlugin property() { + return propertyPlugin; } private T getBean(Type type, String name) { diff --git a/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java b/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java index 01edd337d..6dd13585b 100644 --- a/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java +++ b/inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java @@ -20,7 +20,7 @@ final class DBuilderExtn extends DBuilder { private final boolean hasSuppliedBeans; @SuppressWarnings("rawtypes") - DBuilderExtn(Set profiles, PropertyRequiresPlugin plugin, BeanScope parent, boolean parentOverride, List suppliedBeans, List enrichBeans) { + DBuilderExtn(Set profiles, ConfigPropertyPlugin plugin, BeanScope parent, boolean parentOverride, List suppliedBeans, List enrichBeans) { super(profiles, plugin, parent, parentOverride); this.hasSuppliedBeans = (suppliedBeans != null && !suppliedBeans.isEmpty()); if (hasSuppliedBeans) { diff --git a/inject/src/main/java/io/avaje/inject/spi/PropertyRequiresPlugin.java b/inject/src/main/java/io/avaje/inject/spi/PropertyRequiresPlugin.java index 3789915d4..ed06cf238 100644 --- a/inject/src/main/java/io/avaje/inject/spi/PropertyRequiresPlugin.java +++ b/inject/src/main/java/io/avaje/inject/spi/PropertyRequiresPlugin.java @@ -10,8 +10,11 @@ * *

The plugin is loaded via ServiceLoader and defaults to an implementation that uses {@link * System#getProperty(String)} and {@link System#getenv(String)}. + * + * @deprecated use ConfigPropertyPlugin Instead */ @NonNullApi +@Deprecated(forRemoval = true) public interface PropertyRequiresPlugin extends InjectExtension { /**