Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Avaje Inject Module #3416

Merged
merged 12 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions modules/jooby-avaje-inject/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>io.jooby</groupId>
<artifactId>modules</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>jooby-avaje-inject</artifactId>

<dependencies>
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby</artifactId>
<version>${jooby.version}</version>
</dependency>

<!-- Avaje Inject -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.avaje.inject;

import java.util.List;
import java.util.stream.Collectors;

import com.typesafe.config.Config;

import io.avaje.inject.BeanScope;
import io.avaje.inject.BeanScopeBuilder;
import io.jooby.Extension;
import io.jooby.Jooby;

/**
* Avaje Inject module: https://jooby.io/modules/avaje-inject.
*
* <p>Jooby integrates the {@link io.jooby.ServiceRegistry} into the Avaje DI framework.
*
* <p>Usage:
*
* <pre>{@code
* {
*
*
* install(AvajeInjectModule.of());
*
* }
*
* }</pre>
*
* Require calls are going to be resolved by Avaje inject now.
*
* @author josiah
* @since 3.0.0
*/
public class AvajeInjectModule implements Extension {

private final BeanScopeBuilder beanScope;

public static AvajeInjectModule of() {
return new AvajeInjectModule(BeanScope.builder());
}

public static AvajeInjectModule of(BeanScopeBuilder beanScope) {
return new AvajeInjectModule(beanScope);
}

AvajeInjectModule(BeanScopeBuilder beanScope) {
this.beanScope = beanScope;
}

@Override
public boolean lateinit() {
return true;
}

@Override
public void install(Jooby application) throws Exception {

application
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
.getServices()
.entrySet()
.forEach(
e -> {
final var key = e.getKey();
if (key.getName() == null) {
beanScope.provideDefault(key.getType(), e::getValue);
} else {
beanScope.bean(key.getName(), key.getType(), e.getValue());
}
});

final var environment = application.getEnvironment();
beanScope.profiles(environment.getActiveNames().toArray(String[]::new));

// configuration properties
final var config = environment.getConfig();
beanScope.propertyPlugin(new JoobyPropertyPlugin(config));
beanScope.bean(Config.class, config);

for (var entry : config.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue().unwrapped();

if (value instanceof List<?> values) {
value = values.stream().map(Object::toString).collect(Collectors.joining(","));
}
beanScope.bean(name, String.class, value.toString());
}

application.registry(new AvajeInjectRegistry(beanScope.build()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.jooby.avaje.inject;

import java.util.NoSuchElementException;

import io.avaje.inject.BeanScope;
import io.jooby.Registry;
import io.jooby.ServiceKey;
import io.jooby.exception.RegistryException;

class AvajeInjectRegistry implements Registry {

private final BeanScope beanScope;

public AvajeInjectRegistry(BeanScope beanScope) {
this.beanScope = beanScope;
}

@Override
public <T> T require(Class<T> type) throws RegistryException {
try {
return beanScope.get(type);
} catch (NoSuchElementException e) {
ServiceKey<T> key = ServiceKey.key(type);
throw new RegistryException("Provisioning of `" + key + "` resulted in exception", e);
}
}

@Override
public <T> T require(Class<T> type, String name) throws RegistryException {
try {
return beanScope.get(type, name);
} catch (NoSuchElementException e) {
ServiceKey<T> key = ServiceKey.key(type, name);
throw new RegistryException("Provisioning of `" + key + "` resulted in exception", e);
}
}

@Override
public <T> T require(ServiceKey<T> key) throws RegistryException {
return require(key.getType(), key.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.jooby.avaje.inject;

import java.util.Objects;
import java.util.Optional;

import com.typesafe.config.Config;

import io.avaje.inject.spi.PropertyRequiresPlugin;

public class JoobyPropertyPlugin implements PropertyRequiresPlugin {

private final Config config;

public JoobyPropertyPlugin(Config config) {
this.config = config;
}

@Override
public Optional<String> get(String property) {
return Optional.ofNullable(config.getString(property));
}

@Override
public boolean contains(String property) {
return config.hasPath(property);
}

@Override
public boolean equalTo(String property, String value) {
return config.hasPath(property) && Objects.equals(config.getString(property), value);
}
}
13 changes: 13 additions & 0 deletions modules/jooby-avaje-inject/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
/** Avaje Inject module. */
module io.jooby.avaje.inject {
exports io.jooby.avaje.inject;

requires transitive io.jooby;
requires transitive typesafe.config;
requires transitive io.avaje.inject;
}
6 changes: 6 additions & 0 deletions modules/jooby-avaje-jsonb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<artifactId>avaje-jsonb</artifactId>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>io.jooby</groupId>
Expand Down
5 changes: 5 additions & 0 deletions modules/jooby-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>jooby</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-avaje-inject</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-avaje-jsonb</artifactId>
Expand Down
1 change: 1 addition & 0 deletions modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<module>jooby-quartz</module>
<module>jooby-awssdk-v1</module>

<module>jooby-avaje-inject</module>
<module>jooby-guice</module>

<module>jooby-commons-email</module>
Expand Down
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<config.version>1.4.3</config.version>

<!-- dependency injection -->
<avaje-inject.version>9.12</avaje-inject.version>
<guice.version>7.0.0</guice.version>

<!-- logging -->
Expand Down Expand Up @@ -673,6 +674,19 @@
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- avaje-inject -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>${avaje-inject.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>${avaje-inject.version}</version>
</dependency>

<!-- avaje-json -->
<dependency>
Expand All @@ -681,6 +695,12 @@
<version>${avaje-jsonb.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<version>${avaje-jsonb.version}</version>
</dependency>

<!-- Freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
Expand Down
Loading