Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.cli.internal;

import java.lang.reflect.Modifier;
import org.kametic.specifications.AbstractSpecification;
import java.util.function.Predicate;
import org.seedstack.seed.cli.CommandLineHandler;
import org.seedstack.shed.reflect.ClassPredicates;

class CommandLineHandlerSpecification extends AbstractSpecification<Class<?>> {
class CommandLineHandlerSpecification implements Predicate<Class<?>> {
static CommandLineHandlerSpecification INSTANCE = new CommandLineHandlerSpecification();

private CommandLineHandlerSpecification() {
// no instantiation allowed
}

@Override
public boolean isSatisfiedBy(Class<?> candidate) {
public boolean test(Class<?> candidate) {
return ClassPredicates.classIsAssignableFrom(CommandLineHandler.class)
.and(ClassPredicates.classIsInterface().negate())
.and(ClassPredicates.classModifierIs(Modifier.ABSTRACT).negate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.cli.internal;

import io.nuun.kernel.api.plugin.InitState;
Expand Down Expand Up @@ -43,7 +44,7 @@ public void setup(SeedRuntime seedRuntime) {

@Override
public Collection<ClasspathScanRequest> classpathScanRequests() {
return classpathScanRequestBuilder().specification(CommandLineHandlerSpecification.INSTANCE).build();
return classpathScanRequestBuilder().predicate(CommandLineHandlerSpecification.INSTANCE).build();
}

@Override
Expand All @@ -54,7 +55,7 @@ public InitState initialize(InitContext initContext) {
return InitState.INITIALIZED;
}

Collection<Class<?>> cliHandlerCandidates = initContext.scannedTypesBySpecification()
Collection<Class<?>> cliHandlerCandidates = initContext.scannedTypesByPredicate()
.get(CommandLineHandlerSpecification.INSTANCE);
for (Class<?> candidate : cliHandlerCandidates) {
CliCommand cliCommand = candidate.getAnnotation(CliCommand.class);
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.nuun</groupId>
<groupId>io.nuun.kernel</groupId>
<artifactId>kernel-core</artifactId>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal;

import static org.seedstack.shed.misc.PriorityUtils.sortByPriority;
Expand All @@ -21,9 +22,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Provider;
import org.kametic.specifications.Specification;
import org.seedstack.seed.SeedInterceptor;
import org.seedstack.seed.core.internal.utils.SpecificationBuilder;
import org.seedstack.shed.misc.PriorityUtils;
import org.seedstack.shed.reflect.Classes;
import org.slf4j.Logger;
Expand All @@ -39,12 +38,6 @@ public class CorePlugin extends AbstractSeedPlugin {
static final String AUTODETECT_BINDINGS_KERNEL_PARAM = "seedstack.autodetectBindings";
static final String AUTODETECT_INTERCEPTORS_KERNEL_PARAM = "seedstack.autodetectInterceptors";
private static final String SEEDSTACK_PACKAGE = "org.seedstack";
private static final Specification<Class<?>> installSpecification = new SpecificationBuilder<>(
InstallResolver.INSTANCE).build();
private static final Specification<Class<?>> bindSpecification = new SpecificationBuilder<>(
BindResolver.INSTANCE).build();
private static final Specification<Class<?>> providerSpecification = new SpecificationBuilder<>(
ProvideResolver.INSTANCE).build();
private final Set<Class<? extends Module>> modules = new HashSet<>();
private final Set<Class<? extends Module>> overridingModules = new HashSet<>();
private final List<SeedInterceptor> methodInterceptors = new ArrayList<>();
Expand All @@ -64,9 +57,9 @@ public String pluginPackageRoot() {
@Override
public Collection<ClasspathScanRequest> classpathScanRequests() {
return classpathScanRequestBuilder()
.specification(installSpecification)
.specification(bindSpecification)
.specification(providerSpecification)
.predicate(InstallResolver.INSTANCE)
.predicate(BindResolver.INSTANCE)
.predicate(ProvideResolver.INSTANCE)
.subtypeOf(SeedInterceptor.class)
.build();
}
Expand All @@ -91,7 +84,7 @@ public InitState initialize(InitContext initContext) {

@SuppressWarnings("unchecked")
private void detectModules(InitContext initContext) {
initContext.scannedTypesBySpecification().get(installSpecification)
initContext.scannedTypesByPredicate().get(InstallResolver.INSTANCE)
.stream()
.filter(Module.class::isAssignableFrom)
.forEach(candidate -> InstallResolver.INSTANCE.apply(candidate).ifPresent(annotation -> {
Expand All @@ -107,7 +100,7 @@ private void detectModules(InitContext initContext) {

@SuppressWarnings("unchecked")
private void detectBindings(InitContext initContext) {
initContext.scannedTypesBySpecification().get(bindSpecification)
initContext.scannedTypesByPredicate().get(BindResolver.INSTANCE)
.forEach(candidate -> BindResolver.INSTANCE.apply(candidate).ifPresent(annotation -> {
if (annotation.override()) {
overridingBindings.add(new BindingDefinition<>(
Expand All @@ -127,7 +120,7 @@ private void detectBindings(InitContext initContext) {

@SuppressWarnings("unchecked")
private void detectProviders(InitContext initContext) {
initContext.scannedTypesBySpecification().get(providerSpecification)
initContext.scannedTypesByPredicate().get(ProvideResolver.INSTANCE)
.forEach(candidate -> ProvideResolver.INSTANCE.apply(candidate).ifPresent(annotation -> {
if (annotation.override()) {
overridingBindings.add(new ProviderDefinition<>((Class<Provider<Object>>) candidate));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © 2013-2020, The SeedStack authors <http://seedstack.org>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal.el;

import static org.seedstack.shed.reflect.ClassPredicates.classImplements;
import static org.seedstack.shed.reflect.ClassPredicates.classModifierIs;

import java.lang.reflect.Modifier;
import java.util.function.Predicate;
import org.seedstack.seed.el.spi.ELHandler;

class ELHandlerPredicate implements Predicate<Class<?>> {
static ELHandlerPredicate INSTANCE = new ELHandlerPredicate();

private ELHandlerPredicate() {
// no instantiation allowed
}

@Override
public boolean test(Class<?> candidate) {
return classImplements(ELHandler.class)
.and(classModifierIs(Modifier.ABSTRACT).negate())
.test(candidate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal.el;

import com.google.inject.Injector;
Expand All @@ -21,21 +22,14 @@
import org.seedstack.seed.el.spi.ELHandler;

class ELInterceptor implements MethodInterceptor {

private Class<? extends Annotation> annotationClass;

private ELBinder.ExecutionPolicy policy;

// Get a map of annotation handler
private final Class<? extends Annotation> annotationClass;
private final ELBinder.ExecutionPolicy policy;
@Inject
private Map<Class<? extends Annotation>, Class<ELHandler>> elMap;

private Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap;
@Inject
private ELService elService;

@Inject
private ELContextBuilder elContextBuilder;

@Inject
private Injector injector;

Expand All @@ -46,8 +40,8 @@ class ELInterceptor implements MethodInterceptor {

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<ELHandler> handlerClass = elMap.get(this.annotationClass);
ELHandler ELHandler = injector.getInstance(handlerClass);
Class<ELHandler<?>> handlerClass = elMap.get(this.annotationClass);
ELHandler<?> ELHandler = injector.getInstance(handlerClass);

// The policy defines if the EL is evaluated before the method, after or both.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal.el;

import com.google.common.collect.ImmutableMap;
Expand All @@ -19,9 +20,9 @@

class ELModule extends AbstractModule {
private final ExpressionFactory expressionFactory;
private final Map<Class<? extends Annotation>, Class<ELHandler>> elMap;
private final Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap;

ELModule(ExpressionFactory expressionFactory, Map<Class<? extends Annotation>, Class<ELHandler>> elMap) {
ELModule(ExpressionFactory expressionFactory, Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap) {
this.expressionFactory = expressionFactory;
this.elMap = elMap;
}
Expand All @@ -32,7 +33,7 @@ protected void configure() {
bind(ELService.class).to(ELServiceInternal.class);
bind(ELContextBuilder.class).to(ELContextBuilderImpl.class);

for (Class<ELHandler> elHandlerClass : elMap.values()) {
for (Class<ELHandler<?>> elHandlerClass : elMap.values()) {
bind(elHandlerClass);
}

Expand All @@ -41,6 +42,6 @@ protected void configure() {
}

private static class AnnotationHandlersTypeLiteral
extends TypeLiteral<Map<Class<? extends Annotation>, Class<ELHandler>>> {
extends TypeLiteral<Map<Class<? extends Annotation>, Class<ELHandler<?>>>> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal.el;

import io.nuun.kernel.api.plugin.InitState;
Expand All @@ -19,7 +20,6 @@
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import net.jodah.typetools.TypeResolver;
import org.kametic.specifications.Specification;
import org.seedstack.seed.SeedException;
import org.seedstack.seed.core.internal.AbstractSeedPlugin;
import org.seedstack.seed.el.spi.ELHandler;
Expand All @@ -32,7 +32,6 @@ public class ELPlugin extends AbstractSeedPlugin {
static final Class<? extends ELContext> JUEL_CONTEXT_CLASS;
private static final Object EXPRESSION_FACTORY;
private static final Logger LOGGER = LoggerFactory.getLogger(ELPlugin.class);
private final Specification<Class<?>> specificationELHandlers = classImplements(ELHandler.class);
private ELModule elModule;

static {
Expand Down Expand Up @@ -106,36 +105,31 @@ public String name() {

@Override
public Collection<ClasspathScanRequest> classpathScanRequests() {
return classpathScanRequestBuilder().specification(specificationELHandlers).build();
return classpathScanRequestBuilder().predicate(ELHandlerPredicate.INSTANCE).build();
}

@SuppressWarnings("unchecked")
@Override
public InitState initialize(InitContext initContext) {
if (isEnabled()) {
Map<Class<? extends Annotation>, Class<ELHandler>> elMap = new HashMap<>();

// Scan all the ExpressionLanguageHandler
Map<Specification, Collection<Class<?>>> scannedTypesBySpecification = initContext
.scannedTypesBySpecification();
Collection<Class<?>> elHandlerClasses = scannedTypesBySpecification.get(specificationELHandlers);
Map<Class<? extends Annotation>, Class<ELHandler<?>>> elMap = new HashMap<>();

// Look for their type parameters
for (Class<?> elHandlerClass : elHandlerClasses) {
// Scan all the ExpressionLanguageHandler and look for their type parameters
for (Class<?> elHandlerClass : initContext.scannedTypesByPredicate().get(ELHandlerPredicate.INSTANCE)) {
Class<Annotation> typeParameterClass = (Class<Annotation>) TypeResolver.resolveRawArguments(
ELHandler.class, (Class<ELHandler>) elHandlerClass)[0];
ELHandler.class, (Class<ELHandler<?>>) elHandlerClass)[0];
// transform this type parameters in a map of annotation, ExpressionHandler
if (elMap.get(typeParameterClass) != null) {
throw SeedException.createNew(ExpressionLanguageErrorCode.EL_ANNOTATION_IS_ALREADY_BIND)
.put("annotation", typeParameterClass.getSimpleName())
.put("handler", elHandlerClass);
}
elMap.put(typeParameterClass, (Class<ELHandler>) elHandlerClass);
elMap.put(typeParameterClass, (Class<ELHandler<?>>) elHandlerClass);
}

elModule = new ELModule((ExpressionFactory) EXPRESSION_FACTORY, elMap);
} else {
LOGGER.debug("Java EL is not present in the classpath, EL support disabled");
LOGGER.info("Java EL is not present in the classpath, EL support disabled");
}

return InitState.INITIALIZED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.seedstack.seed.core.internal.lifecycle;

import io.nuun.kernel.spi.KernelExtension;
Expand All @@ -26,22 +27,23 @@ public void starting(Collection<LifecycleManager> lifecycleManagers) {
// this phase is not supported by LifecycleManager
}

@Override
public void injected(Collection<LifecycleManager> lifecycleManagers) {
lifecycleManagers.forEach(LifecycleManager::starting);
}

@Override
public void started(Collection<LifecycleManager> lifecycleManagers) {
for (LifecycleManager lifecycleManager : lifecycleManagers) {
lifecycleManager.started();
}
lifecycleManagers.forEach(LifecycleManager::started);
}

@Override
public void stopping(Collection<LifecycleManager> lifecycleManagers) {
for (LifecycleManager lifecycleManager : lifecycleManagers) {
lifecycleManager.stopping();
}
lifecycleManagers.forEach(LifecycleManager::stopping);
}

@Override
public void stopped(Collection<LifecycleManager> lifecycleManagers) {
// this phase is not supported by LifecycleManager
lifecycleManagers.forEach(LifecycleManager::stopped);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
import java.lang.reflect.Method;

interface LifecycleManager {
void starting();

void started();

void stopping();

void stopped();

void registerPreDestroy(Object o, Method m);

void registerAutoCloseable(AutoCloseable autoCloseable);
Expand Down
Loading