Skip to content

Commit

Permalink
add EzyConfigurationAfterApplicationReady (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 authored Dec 29, 2024
1 parent ba4a309 commit d40e4ba
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>

<artifactId>ezyhttp-server-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>

<artifactId>ezyhttp-server-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Class> serviceClasses = getServiceClasses(reflection);
EzyPropertiesMap propertiesMap = getPropertiesMap(reflection);
EzyBeanContext beanContext = newBeanContextBuilder()
Expand All @@ -265,6 +262,7 @@ protected EzyBeanContext createBeanContext() {
.addSingletonClasses(bodyConverterClasses)
.addSingletonClasses(stringConverterClasses)
.addSingletonClasses(bootstrapClasses)
.addSingletonClasses(configurationAfterApplicationReadyClasses)
.propertiesMap(propertiesMap)
.addSingleton("systemObjectMapper", objectMapper)
.addSingleton("componentManager", componentManager)
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -42,6 +47,7 @@ public static EzyHttpApplication start(
return start(basePackage, classArray);
}

@SuppressWarnings("rawtypes")
public static EzyHttpApplication start(
String basePackage,
Class<?>... componentClasses
Expand All @@ -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;
}

Expand Down Expand Up @@ -90,4 +101,18 @@ protected static ApplicationContext createApplicationContext(
public void stop() {
applicationContext.destroy();
}

@SuppressWarnings({"rawtypes", "unchecked"})
private static void runConfigurationAfterApplicationObjects(
List configurationAfterApplicationObjects
) {
List<Object> objects = sortConfigurationAfterApplicationReadyObjects(
configurationAfterApplicationObjects
);
for (Object obj : objects) {
if (obj instanceof EzyBeanConfig) {
((EzyBeanConfig) obj).config();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Object> sortConfigurationAfterApplicationReadyObjects(
Collection<Object> objects
) {
List<Object> list = new ArrayList<>(objects);
list.sort(newConfigurationAfterApplicationReadyComparator());
return list;
}

private static Comparator<Object> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.tvd12.ezyhttp.server.core.test.config;

import com.tvd12.ezyhttp.server.core.annotation.EzyConfigurationAfterApplicationReady;

@EzyConfigurationAfterApplicationReady
public class ServerReadyConfig1 {}
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion ezyhttp-server-graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>
<artifactId>ezyhttp-server-graphql</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>

<artifactId>ezyhttp-server-jetty</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>
<artifactId>ezyhttp-server-management</artifactId>
<name>ezyhttp-server-management</name>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>
<artifactId>ezyhttp-server-thymeleaf</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</parent>

<artifactId>ezyhttp-server-tomcat</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.6</version>
</parent>
<artifactId>ezyhttp</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
<packaging>pom</packaging>

<name>ezyhttp</name>
Expand Down

0 comments on commit d40e4ba

Please sign in to comment.