diff --git a/ezyhttp-client/pom.xml b/ezyhttp-client/pom.xml index 8ec01c50..e3f8d844 100644 --- a/ezyhttp-client/pom.xml +++ b/ezyhttp-client/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-client diff --git a/ezyhttp-core/pom.xml b/ezyhttp-core/pom.xml index 8ea47ddc..73ba9488 100644 --- a/ezyhttp-core/pom.xml +++ b/ezyhttp-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-core diff --git a/ezyhttp-server-boot/pom.xml b/ezyhttp-server-boot/pom.xml index d6c6631d..0a7bb88f 100644 --- a/ezyhttp-server-boot/pom.xml +++ b/ezyhttp-server-boot/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-server-boot diff --git a/ezyhttp-server-core/pom.xml b/ezyhttp-server-core/pom.xml index 44b20fd5..91695dd9 100644 --- a/ezyhttp-server-core/pom.xml +++ b/ezyhttp-server-core/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-server-core diff --git a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/ApplicationContextBuilder.java b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/ApplicationContextBuilder.java index 598a6624..1777d926 100644 --- a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/ApplicationContextBuilder.java +++ b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/ApplicationContextBuilder.java @@ -31,13 +31,7 @@ import com.tvd12.ezyhttp.core.codec.DataConverters; import com.tvd12.ezyhttp.core.constant.HttpMethod; import com.tvd12.ezyhttp.core.resources.ResourceDownloadManager; -import com.tvd12.ezyhttp.server.core.annotation.ApplicationBootstrap; -import com.tvd12.ezyhttp.server.core.annotation.ComponentClasses; -import com.tvd12.ezyhttp.server.core.annotation.ComponentsScan; -import com.tvd12.ezyhttp.server.core.annotation.Controller; -import com.tvd12.ezyhttp.server.core.annotation.ExceptionHandler; -import com.tvd12.ezyhttp.server.core.annotation.PropertiesSources; -import com.tvd12.ezyhttp.server.core.annotation.Service; +import com.tvd12.ezyhttp.server.core.annotation.*; import com.tvd12.ezyhttp.server.core.asm.ExceptionHandlersImplementer; import com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer; import com.tvd12.ezyhttp.server.core.constant.PropertyNames; @@ -253,6 +247,9 @@ protected EzyBeanContext createBeanContext() { Set bodyConverterClasses = reflection.getAnnotatedClasses(BodyConvert.class); Set stringConverterClasses = reflection.getAnnotatedClasses(StringConvert.class); Set bootstrapClasses = reflection.getAnnotatedClasses(ApplicationBootstrap.class); + Set configurationAfterApplicationReadyClasses = reflection.getAnnotatedClasses( + EzyConfigurationAfterApplicationReady.class + ); Map serviceClasses = getServiceClasses(reflection); EzyPropertiesMap propertiesMap = getPropertiesMap(reflection); EzyBeanContext beanContext = newBeanContextBuilder() @@ -265,6 +262,7 @@ protected EzyBeanContext createBeanContext() { .addSingletonClasses(bodyConverterClasses) .addSingletonClasses(stringConverterClasses) .addSingletonClasses(bootstrapClasses) + .addSingletonClasses(configurationAfterApplicationReadyClasses) .propertiesMap(propertiesMap) .addSingleton("systemObjectMapper", objectMapper) .addSingleton("componentManager", componentManager) diff --git a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/EzyHttpApplication.java b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/EzyHttpApplication.java index 8ede4ab4..7b9c14f4 100644 --- a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/EzyHttpApplication.java +++ b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/EzyHttpApplication.java @@ -1,22 +1,27 @@ package com.tvd12.ezyhttp.server.core; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - +import com.tvd12.ezyfox.bean.EzyBeanConfig; +import com.tvd12.ezyfox.bean.EzyBeanContext; import com.tvd12.ezyfox.util.EzyLoggable; import com.tvd12.ezyfox.util.EzyStartable; import com.tvd12.ezyfox.util.EzyStoppable; import com.tvd12.ezyhttp.server.core.annotation.ApplicationBootstrap; +import com.tvd12.ezyhttp.server.core.annotation.EzyConfigurationAfterApplicationReady; import com.tvd12.ezyhttp.server.core.util.BannerPrinter; - import lombok.Getter; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static com.tvd12.ezyhttp.server.core.util.EzyConfigurationAfterApplicationReadyAnnotations.sortConfigurationAfterApplicationReadyObjects; + +@Getter public class EzyHttpApplication extends EzyLoggable implements EzyStartable, EzyStoppable { - @Getter protected final ApplicationContext applicationContext; public EzyHttpApplication(ApplicationContext applicationContext) { @@ -42,6 +47,7 @@ public static EzyHttpApplication start( return start(basePackage, classArray); } + @SuppressWarnings("rawtypes") public static EzyHttpApplication start( String basePackage, Class... componentClasses @@ -50,6 +56,11 @@ public static EzyHttpApplication start( = createApplicationContext(basePackage, componentClasses); EzyHttpApplication application = new EzyHttpApplication(applicationContext); application.start(); + EzyBeanContext beanContext = applicationContext.getBeanContext(); + List configurationAfterApplicationObjects = beanContext.getSingletons( + EzyConfigurationAfterApplicationReady.class + ); + runConfigurationAfterApplicationObjects(configurationAfterApplicationObjects); return application; } @@ -90,4 +101,18 @@ protected static ApplicationContext createApplicationContext( public void stop() { applicationContext.destroy(); } + + @SuppressWarnings({"rawtypes", "unchecked"}) + private static void runConfigurationAfterApplicationObjects( + List configurationAfterApplicationObjects + ) { + List objects = sortConfigurationAfterApplicationReadyObjects( + configurationAfterApplicationObjects + ); + for (Object obj : objects) { + if (obj instanceof EzyBeanConfig) { + ((EzyBeanConfig) obj).config(); + } + } + } } diff --git a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/annotation/EzyConfigurationAfterApplicationReady.java b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/annotation/EzyConfigurationAfterApplicationReady.java new file mode 100644 index 00000000..17374045 --- /dev/null +++ b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/annotation/EzyConfigurationAfterApplicationReady.java @@ -0,0 +1,13 @@ +package com.tvd12.ezyhttp.server.core.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface EzyConfigurationAfterApplicationReady { + + int priority() default 0; +} diff --git a/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/util/EzyConfigurationAfterApplicationReadyAnnotations.java b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/util/EzyConfigurationAfterApplicationReadyAnnotations.java new file mode 100644 index 00000000..c7d753d0 --- /dev/null +++ b/ezyhttp-server-core/src/main/java/com/tvd12/ezyhttp/server/core/util/EzyConfigurationAfterApplicationReadyAnnotations.java @@ -0,0 +1,45 @@ +package com.tvd12.ezyhttp.server.core.util; + +import com.tvd12.ezyhttp.server.core.annotation.EzyConfigurationAfterApplicationReady; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; + +public class EzyConfigurationAfterApplicationReadyAnnotations { + + private EzyConfigurationAfterApplicationReadyAnnotations() {} + + public static List sortConfigurationAfterApplicationReadyObjects( + Collection objects + ) { + List list = new ArrayList<>(objects); + list.sort(newConfigurationAfterApplicationReadyComparator()); + return list; + } + + private static Comparator newConfigurationAfterApplicationReadyComparator() { + return Comparator.comparingInt( + EzyConfigurationAfterApplicationReadyAnnotations::getPriority + ); + } + + private static int getPriority(Object object) { + return getPriority( + object + .getClass() + .getAnnotation(EzyConfigurationAfterApplicationReady.class) + ); + } + + private static int getPriority( + EzyConfigurationAfterApplicationReady annotation + ) { + int priority = 0; + if (annotation != null) { + priority = annotation.priority(); + } + return priority; + } +} diff --git a/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig1.java b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig1.java new file mode 100644 index 00000000..f7535568 --- /dev/null +++ b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig1.java @@ -0,0 +1,6 @@ +package com.tvd12.ezyhttp.server.core.test.config; + +import com.tvd12.ezyhttp.server.core.annotation.EzyConfigurationAfterApplicationReady; + +@EzyConfigurationAfterApplicationReady +public class ServerReadyConfig1 {} diff --git a/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig2.java b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig2.java new file mode 100644 index 00000000..fed4e7ee --- /dev/null +++ b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig2.java @@ -0,0 +1,11 @@ +package com.tvd12.ezyhttp.server.core.test.config; + +import com.tvd12.ezyfox.bean.EzyBeanConfig; +import com.tvd12.ezyhttp.server.core.annotation.EzyConfigurationAfterApplicationReady; + +@EzyConfigurationAfterApplicationReady(priority = Integer.MAX_VALUE) +public class ServerReadyConfig2 implements EzyBeanConfig { + + @Override + public void config() {} +} diff --git a/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig3.java b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig3.java new file mode 100644 index 00000000..293d88d1 --- /dev/null +++ b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/config/ServerReadyConfig3.java @@ -0,0 +1,11 @@ +package com.tvd12.ezyhttp.server.core.test.config; + +import com.tvd12.ezyfox.bean.EzyBeanConfig; +import com.tvd12.ezyhttp.server.core.annotation.EzyConfigurationAfterApplicationReady; + +@EzyConfigurationAfterApplicationReady(priority = Integer.MIN_VALUE) +public class ServerReadyConfig3 implements EzyBeanConfig { + + @Override + public void config() {} +} diff --git a/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/util/EzyConfigurationAfterApplicationReadyAnnotationsTest.java b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/util/EzyConfigurationAfterApplicationReadyAnnotationsTest.java new file mode 100644 index 00000000..0ede7b48 --- /dev/null +++ b/ezyhttp-server-core/src/test/java/com/tvd12/ezyhttp/server/core/test/util/EzyConfigurationAfterApplicationReadyAnnotationsTest.java @@ -0,0 +1,24 @@ +package com.tvd12.ezyhttp.server.core.test.util; + +import com.tvd12.ezyhttp.server.core.util.EzyConfigurationAfterApplicationReadyAnnotations; +import com.tvd12.test.assertion.Asserts; +import com.tvd12.test.reflect.MethodInvoker; +import org.testng.annotations.Test; + +public class EzyConfigurationAfterApplicationReadyAnnotationsTest { + + @Test + public void getPriorityTest() { + // given + // when + int priority = (int) MethodInvoker + .create() + .staticClass(EzyConfigurationAfterApplicationReadyAnnotations.class) + .method("getPriority") + .param(Object.class, this) + .invoke(); + + // then + Asserts.assertZero(priority); + } +} diff --git a/ezyhttp-server-graphql/pom.xml b/ezyhttp-server-graphql/pom.xml index 945daf02..8188ca28 100644 --- a/ezyhttp-server-graphql/pom.xml +++ b/ezyhttp-server-graphql/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-server-graphql diff --git a/ezyhttp-server-jetty/pom.xml b/ezyhttp-server-jetty/pom.xml index 74cb235f..393a47d5 100644 --- a/ezyhttp-server-jetty/pom.xml +++ b/ezyhttp-server-jetty/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-server-jetty diff --git a/ezyhttp-server-management/pom.xml b/ezyhttp-server-management/pom.xml index 178cc69c..07daa4e7 100644 --- a/ezyhttp-server-management/pom.xml +++ b/ezyhttp-server-management/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-server-management ezyhttp-server-management diff --git a/ezyhttp-server-thymeleaf/pom.xml b/ezyhttp-server-thymeleaf/pom.xml index 92ac55d1..9d954b9d 100644 --- a/ezyhttp-server-thymeleaf/pom.xml +++ b/ezyhttp-server-thymeleaf/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-server-thymeleaf diff --git a/ezyhttp-server-tomcat/pom.xml b/ezyhttp-server-tomcat/pom.xml index 74dfcae6..ef803408 100644 --- a/ezyhttp-server-tomcat/pom.xml +++ b/ezyhttp-server-tomcat/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezyhttp - 1.3.3 + 1.3.4 ezyhttp-server-tomcat diff --git a/pom.xml b/pom.xml index d2fe865c..7b8dd5e3 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 1.0.6 ezyhttp - 1.3.3 + 1.3.4 pom ezyhttp