Skip to content

Commit

Permalink
runtime-core: Update with changes to Injector
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Feb 3, 2017
1 parent e932be5 commit bc30386
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
Expand Up @@ -46,7 +46,9 @@
* @author Emil Forslund * @author Emil Forslund
* @since 3.0.0 * @since 3.0.0
*/ */
public interface ApplicationBuilder<APP extends Speedment, BUILDER extends ApplicationBuilder<APP, BUILDER>> { public interface ApplicationBuilder<
APP extends Speedment,
BUILDER extends ApplicationBuilder<APP, BUILDER>> {


/** /**
* Configures a parameter for the identified dbms. The consumer will then be * Configures a parameter for the identified dbms. The consumer will then be
Expand Down Expand Up @@ -564,11 +566,17 @@ default <I extends HasDbmsName> BUILDER withConnectionUrl(I id, String connectio
* {@link Inject} will be dependency injected. Methods annotated with * {@link Inject} will be dependency injected. Methods annotated with
* {@link ExecuteBefore} will also be executed as part of the application * {@link ExecuteBefore} will also be executed as part of the application
* configuration phase. * configuration phase.
*
* @deprecated The parameter {@code key} is not forwarded to the dependency
* injection framework, so this method has exactly the same
* behaviour as {@link #withComponent(java.lang.Class)}, so it
* is redundant.
* *
* @param key the key to store it under * @param key the key to store it under
* @param componentClass the implementation class * @param componentClass the implementation class
* @return this instance * @return this instance
*/ */
@Deprecated
BUILDER withComponent(String key, Class<?> componentClass); BUILDER withComponent(String key, Class<?> componentClass);


/** /**
Expand Down
Expand Up @@ -18,6 +18,7 @@


import com.speedment.common.injector.InjectBundle; import com.speedment.common.injector.InjectBundle;
import com.speedment.common.injector.Injector; import com.speedment.common.injector.Injector;
import com.speedment.common.injector.InjectorBuilder;
import com.speedment.common.injector.exception.CyclicReferenceException; import com.speedment.common.injector.exception.CyclicReferenceException;
import com.speedment.common.injector.internal.InjectorImpl; import com.speedment.common.injector.internal.InjectorImpl;
import static com.speedment.common.invariant.NullUtil.requireNonNulls; import static com.speedment.common.invariant.NullUtil.requireNonNulls;
Expand Down Expand Up @@ -49,15 +50,12 @@
import com.speedment.runtime.core.exception.SpeedmentException; import com.speedment.runtime.core.exception.SpeedmentException;
import com.speedment.runtime.core.manager.Manager; import com.speedment.runtime.core.manager.Manager;
import com.speedment.runtime.core.util.DatabaseUtil; import com.speedment.runtime.core.util.DatabaseUtil;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.util.Optional; import java.util.Optional;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;


Expand All @@ -73,13 +71,16 @@
* @since 2.0.0 * @since 2.0.0
*/ */
public abstract class AbstractApplicationBuilder< public abstract class AbstractApplicationBuilder<
APP extends Speedment, BUILDER extends AbstractApplicationBuilder<APP, BUILDER>> implements ApplicationBuilder<APP, BUILDER> { APP extends Speedment,
BUILDER extends AbstractApplicationBuilder<APP, BUILDER>
> implements ApplicationBuilder<APP, BUILDER> {


private final static Logger LOGGER = LoggerManager.getLogger(LogType.APPLICATION_BUILDER.getLoggerName()); private final static Logger LOGGER = LoggerManager.getLogger(
LogType.APPLICATION_BUILDER.getLoggerName());


private final List<Tuple3<Class<? extends Document>, String, BiConsumer<Injector, ? extends Document>>> withsNamed; private final List<Tuple3<Class<? extends Document>, String, BiConsumer<Injector, ? extends Document>>> withsNamed;
private final List<Tuple2<Class<? extends Document>, BiConsumer<Injector, ? extends Document>>> withsAll; private final List<Tuple2<Class<? extends Document>, BiConsumer<Injector, ? extends Document>>> withsAll;
private final Injector.Builder injector; private final InjectorBuilder injectorBuilder;


private boolean skipCheckDatabaseConnectivity; private boolean skipCheckDatabaseConnectivity;
private boolean skipValidateRuntimeConfig; private boolean skipValidateRuntimeConfig;
Expand All @@ -90,9 +91,9 @@ protected AbstractApplicationBuilder(
Class<? extends ApplicationMetadata> metadataClass) { Class<? extends ApplicationMetadata> metadataClass) {


this(Injector.builder() this(Injector.builder()
.putInBundle(RuntimeBundle.class) .withBundle(RuntimeBundle.class)
.put(applicationImplClass) .withComponent(applicationImplClass)
.put(metadataClass) .withComponent(metadataClass)
); );
} }


Expand All @@ -102,16 +103,16 @@ protected AbstractApplicationBuilder(
Class<? extends ApplicationMetadata> metadataClass) { Class<? extends ApplicationMetadata> metadataClass) {


this(Injector.builder(classLoader) this(Injector.builder(classLoader)
.putInBundle(RuntimeBundle.class) .withBundle(RuntimeBundle.class)
.put(applicationImplClass) .withComponent(applicationImplClass)
.put(metadataClass) .withComponent(metadataClass)
); );
} }


protected AbstractApplicationBuilder(Injector.Builder injector) { protected AbstractApplicationBuilder(InjectorBuilder injectorBuilder) {
this.injector = requireNonNull(injector); this.injectorBuilder = requireNonNull(injectorBuilder);
this.withsNamed = new ArrayList<>(); this.withsNamed = new ArrayList<>();
this.withsAll = new ArrayList<>(); this.withsAll = new ArrayList<>();
this.skipCheckDatabaseConnectivity = false; this.skipCheckDatabaseConnectivity = false;
this.skipValidateRuntimeConfig = false; this.skipValidateRuntimeConfig = false;
} }
Expand All @@ -133,7 +134,7 @@ public <C extends Document & HasEnabled> BUILDER with(Class<C> type, BiConsumer<
@Override @Override
public BUILDER withParam(String key, String value) { public BUILDER withParam(String key, String value) {
requireNonNulls(key, value); requireNonNulls(key, value);
injector.putParam(key, value); injectorBuilder.withParam(key, value);
return self(); return self();
} }


Expand Down Expand Up @@ -237,7 +238,7 @@ public BUILDER withConnectionUrl(String dbmsName, String connectionUrl) {
@Override @Override
public <M extends Manager<?>> BUILDER withManager(Class<M> managerImplType) { public <M extends Manager<?>> BUILDER withManager(Class<M> managerImplType) {
requireNonNull(managerImplType); requireNonNull(managerImplType);
withInjectable(injector, managerImplType, M::getEntityClass); withInjectable(injectorBuilder, managerImplType);
return self(); return self();
} }


Expand All @@ -262,21 +263,22 @@ public BUILDER withSkipLogoPrintout() {
@Override @Override
public BUILDER withBundle(Class<? extends InjectBundle> bundleClass) { public BUILDER withBundle(Class<? extends InjectBundle> bundleClass) {
requireNonNull(bundleClass); requireNonNull(bundleClass);
injector.putInBundle(bundleClass); injectorBuilder.withBundle(bundleClass);
return self(); return self();
} }


@Override @Override
public BUILDER withComponent(Class<?> injectableClass) { public BUILDER withComponent(Class<?> injectableClass) {
requireNonNull(injectableClass); requireNonNull(injectableClass);
injector.put(injectableClass); injectorBuilder.withComponent(injectableClass);
return self(); return self();
} }


@Override @Override
@Deprecated
public BUILDER withComponent(String key, Class<?> injectableClass) { public BUILDER withComponent(String key, Class<?> injectableClass) {
requireNonNulls(key, injectableClass); requireNonNulls(key, injectableClass);
injector.put(key, injectableClass); injectorBuilder.withComponent(injectableClass);
return self(); return self();
} }


Expand All @@ -294,7 +296,7 @@ public final APP build() {
final Injector inj; final Injector inj;


try { try {
inj = injector.build(); inj = injectorBuilder.build();
} catch (final InstantiationException | CyclicReferenceException ex) { } catch (final InstantiationException | CyclicReferenceException ex) {
throw new SpeedmentException("Error in dependency injection.", ex); throw new SpeedmentException("Error in dependency injection.", ex);
} }
Expand Down Expand Up @@ -536,26 +538,10 @@ Optional<Boolean> isVersionOk(String versionString) {
} }


private static <T> void withInjectable( private static <T> void withInjectable(
Injector.Builder injector, InjectorBuilder injector,
Class<T> injectableImplType, Class<T> injectableImplType) {
Function<T, Class<?>> keyExtractor) {


requireNonNull(injectableImplType); requireNonNull(injectableImplType);

injector.withComponent(injectableImplType);
final T injectable;
try {
final Constructor<T> constructor = injectableImplType.getDeclaredConstructor();
constructor.setAccessible(true);
injectable = constructor.newInstance();
} catch (final InstantiationException
| IllegalAccessException
| NoSuchMethodException
| InvocationTargetException ex) {

throw new SpeedmentException(ex);
}

final Class<?> key = keyExtractor.apply(injectable);
injector.put(key.getName(), injectableImplType);
} }
} }
Expand Up @@ -17,12 +17,14 @@
package com.speedment.runtime.core.internal; package com.speedment.runtime.core.internal;


import com.speedment.common.injector.Injector; import com.speedment.common.injector.Injector;
import com.speedment.common.injector.InjectorBuilder;
import com.speedment.runtime.core.ApplicationMetadata; import com.speedment.runtime.core.ApplicationMetadata;
import com.speedment.runtime.core.Speedment; import com.speedment.runtime.core.Speedment;


/** /**
* *
* @author Emil Forslund * @author Emil Forslund
* @since 3.0.0
*/ */
public final class DefaultApplicationBuilder extends public final class DefaultApplicationBuilder extends
AbstractApplicationBuilder<Speedment, DefaultApplicationBuilder> { AbstractApplicationBuilder<Speedment, DefaultApplicationBuilder> {
Expand All @@ -40,8 +42,8 @@ public DefaultApplicationBuilder(
super(SpeedmentImpl.class, metadataClass); super(SpeedmentImpl.class, metadataClass);
} }


public DefaultApplicationBuilder(Injector.Builder injector) { public DefaultApplicationBuilder(InjectorBuilder injectorBuilder) {
super(injector); super(injectorBuilder);
} }


@Override @Override
Expand All @@ -50,7 +52,5 @@ protected Speedment build(Injector injector) {
} }


@Override @Override
protected void printWelcomeMessage(Injector injector) { protected void printWelcomeMessage(Injector injector) {}
}

} }

0 comments on commit bc30386

Please sign in to comment.