Skip to content

Commit

Permalink
Rename Property Plugin from PropertyRequiresPlugin to ConfigPropertyP…
Browse files Browse the repository at this point in the history
…lugin (#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 cf9f166.

* 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 <[email protected]>
Co-authored-by: Rob Bygrave <[email protected]>
  • Loading branch information
3 people authored Jun 11, 2024
1 parent 47772bd commit 1bf404c
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> get(String property) {
Expand Down
20 changes: 15 additions & 5 deletions inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 21 additions & 13 deletions inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> profiles;

/** Create a BeanScopeBuilder to ultimately load and return a new BeanScope. */
Expand All @@ -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
Expand Down Expand Up @@ -209,7 +217,7 @@ private void initClassLoader() {
classLoader = Thread.currentThread().getContextClassLoader();
}
}
private PropertyRequiresPlugin defaultPropertyPlugin() {
private ConfigPropertyPlugin defaultPropertyPlugin() {
return detectAvajeConfig() ? new DConfigProps() : new DSystemProps();
}

Expand All @@ -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());
Expand All @@ -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));
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions inject/src/main/java/io/avaje/inject/DConfigProps.java
Original file line number Diff line number Diff line change
@@ -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<String> get(String property) {
Expand Down
8 changes: 4 additions & 4 deletions inject/src/main/java/io/avaje/inject/DServiceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class DServiceLoader {
private final List<InjectPlugin> plugins = new ArrayList<>();
private final List<AvajeModule> modules = new ArrayList<>();
private ModuleOrdering moduleOrdering;
private PropertyRequiresPlugin propertyPlugin;
private ConfigPropertyPlugin propertyPlugin;

DServiceLoader(ClassLoader classLoader) {
for (var spi : ServiceLoader.load(InjectExtension.class, classLoader)) {
Expand All @@ -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
Expand All @@ -47,7 +47,7 @@ Optional<ModuleOrdering> moduleOrdering() {
return Optional.ofNullable(moduleOrdering);
}

Optional<PropertyRequiresPlugin> propertyPlugin() {
Optional<ConfigPropertyPlugin> propertyPlugin() {
return Optional.ofNullable(propertyPlugin);
}
}
4 changes: 3 additions & 1 deletion inject/src/main/java/io/avaje/inject/DSystemProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> get(String property) {
Expand Down
4 changes: 2 additions & 2 deletions inject/src/main/java/io/avaje/inject/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
* }</pre>
*
* <p>In the sample above, the MyService bean will get wired only if <code>avaje.profiles</code> is
* set to "test" in the {@link io.avaje.inject.spi.PropertyRequiresPlugin}.
* set to "test" in the {@link io.avaje.inject.spi.ConfigPropertyPlugin}.
*
* <p>Avaje Config provides an implementation and if it is included in the classpath then Avaje
* Config will be used to test the property conditions.
*
* <p>If no PropertyRequiresPlugin is found then the default implementation is used which uses
* <p>If no ConfigPropertyPlugin is found then the default implementation is used which uses
* {@link System#getProperty(String)} and {@link System#getenv(String)}.
*/
@Retention(RUNTIME)
Expand Down
4 changes: 2 additions & 2 deletions inject/src/main/java/io/avaje/inject/RequiresProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
* In the sample above the MyService bean will get wired only if <code>use.service</code>
* is set in Java system properties / Avaje Config.
* <p>
* {@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}.
* <p>
* Avaje Config provides an implementation and if it is included in the classpath then
* Avaje Config will be used to test the property conditions.
* <p>
* 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)
Expand Down
4 changes: 2 additions & 2 deletions inject/src/main/java/io/avaje/inject/spi/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> profiles, PropertyRequiresPlugin plugin, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans, BeanScope parent, boolean parentOverride) {
static Builder newBuilder(Set<String> profiles, ConfigPropertyPlugin plugin, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans, BeanScope parent, boolean parentOverride) {
if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) {
// simple case, no mocks or spies
return new DBuilder(profiles, plugin, parent, parentOverride);
Expand Down Expand Up @@ -270,7 +270,7 @@ static Builder newBuilder(Set<String> profiles, PropertyRequiresPlugin plugin, L
/**
* Return the plugin for required properties.
*/
PropertyRequiresPlugin property();
ConfigPropertyPlugin property();

/**
* Build and return the bean scope.
Expand Down
39 changes: 39 additions & 0 deletions inject/src/main/java/io/avaje/inject/spi/ConfigPropertyPlugin.java
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>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<String> 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);
}
}
10 changes: 5 additions & 5 deletions inject/src/main/java/io/avaje/inject/spi/DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class DBuilder implements Builder {

private final PropertyRequiresPlugin propertyRequires;
private final ConfigPropertyPlugin propertyPlugin;
private final Set<String> profiles;
/** List of Lifecycle methods. */
private final List<Runnable> postConstruct = new ArrayList<>();
Expand All @@ -36,8 +36,8 @@ class DBuilder implements Builder {

private DBeanScopeProxy beanScopeProxy;

DBuilder(Set<String> profiles, PropertyRequiresPlugin propertyRequires, BeanScope parent, boolean parentOverride) {
this.propertyRequires = propertyRequires;
DBuilder(Set<String> profiles, ConfigPropertyPlugin propertyPlugin, BeanScope parent, boolean parentOverride) {
this.propertyPlugin = propertyPlugin;
this.parent = parent;
this.parentOverride = parentOverride;
this.profiles = profiles;
Expand Down Expand Up @@ -378,8 +378,8 @@ public boolean containsQualifier(String name) {
}

@Override
public PropertyRequiresPlugin property() {
return propertyRequires;
public ConfigPropertyPlugin property() {
return propertyPlugin;
}

private <T> T getBean(Type type, String name) {
Expand Down
2 changes: 1 addition & 1 deletion inject/src/main/java/io/avaje/inject/spi/DBuilderExtn.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class DBuilderExtn extends DBuilder {
private final boolean hasSuppliedBeans;

@SuppressWarnings("rawtypes")
DBuilderExtn(Set<String> profiles, PropertyRequiresPlugin plugin, BeanScope parent, boolean parentOverride, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
DBuilderExtn(Set<String> profiles, ConfigPropertyPlugin plugin, BeanScope parent, boolean parentOverride, List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) {
super(profiles, plugin, parent, parentOverride);
this.hasSuppliedBeans = (suppliedBeans != null && !suppliedBeans.isEmpty());
if (hasSuppliedBeans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
*
* <p>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 {

/**
Expand Down

0 comments on commit 1bf404c

Please sign in to comment.