Skip to content

Commit

Permalink
Merge pull request #77 from youngmonkeys/big-update
Browse files Browse the repository at this point in the history
big update
  • Loading branch information
tvd12 authored Feb 22, 2022
2 parents 766a592 + 1d98779 commit c1edabe
Show file tree
Hide file tree
Showing 25 changed files with 237 additions and 70 deletions.
2 changes: 1 addition & 1 deletion ezyfox-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-server</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<artifactId>ezyfox-server-boot</artifactId>
<version>1.2.2</version> <!-- ${project.version} -->
Expand Down
2 changes: 1 addition & 1 deletion ezyfox-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-server</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<artifactId>ezyfox-server-core</artifactId>
<version>1.2.2</version> <!-- ${project.version} -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tvd12.ezyfoxserver.context;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
Expand All @@ -21,7 +22,6 @@
import com.tvd12.ezyfoxserver.command.impl.EzyHandleExceptionImpl;
import com.tvd12.ezyfoxserver.controller.EzyEventController;
import com.tvd12.ezyfoxserver.event.EzyEvent;
import com.tvd12.ezyfoxserver.wrapper.EzyEventControllers;

@SuppressWarnings("rawtypes")
public abstract class EzyAbstractContext
Expand All @@ -47,10 +47,12 @@ protected void init0() {}

@SuppressWarnings("unchecked")
public void handleEvent(EzyConstant eventType, EzyEvent event) {
EzyEventControllers controllers = component.getEventControllers();
EzyEventController controller = controllers.getController(eventType);
if(controller != null)
List<EzyEventController> controllers = component
.getEventControllers()
.getControllers(eventType);
for (EzyEventController controller : controllers) {
controller.handle(this, event);
}
}

public void handleException(Thread thread, Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.tvd12.ezyfoxserver.wrapper;

import java.util.List;

import com.tvd12.ezyfox.constant.EzyConstant;
import com.tvd12.ezyfox.util.EzyDestroyable;
import com.tvd12.ezyfoxserver.controller.EzyEventController;

public interface EzyEventControllers extends EzyDestroyable {

@SuppressWarnings("rawtypes")
EzyEventController getController(EzyConstant eventType);
List<EzyEventController> getControllers(EzyConstant eventType);

@SuppressWarnings("rawtypes")
void addController(EzyConstant eventType, EzyEventController controller);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.tvd12.ezyfoxserver.wrapper.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -14,7 +17,7 @@
public class EzyEventControllersImpl implements EzyEventControllers {

@SuppressWarnings("rawtypes")
protected final Map<EzyConstant, EzyEventController> controllers
protected final Map<EzyConstant, List<EzyEventController>> controllers
= new ConcurrentHashMap<>();

@SuppressWarnings("rawtypes")
Expand All @@ -31,13 +34,19 @@ public static EzyEventControllers create(EzyEventControllersSetting setting) {
@SuppressWarnings("rawtypes")
@Override
public void addController(EzyConstant eventType, EzyEventController controller) {
controllers.put(eventType, controller);
controllers.compute(eventType, (k, v) -> {
List<EzyEventController> list = v != null
? new ArrayList<>(v)
: new ArrayList<>();
list.add(controller);
return list;
});
}

@SuppressWarnings("rawtypes")
@Override
public EzyEventController getController(EzyConstant eventType) {
return controllers.get(eventType);
public List<EzyEventController> getControllers(EzyConstant eventType) {
return controllers.getOrDefault(eventType, Collections.emptyList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.tvd12.ezyfoxserver.testing.wrapper;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import org.testng.annotations.Test;

import com.tvd12.ezyfox.util.EzyThreads;
import com.tvd12.ezyfoxserver.constant.EzyEventType;
import com.tvd12.ezyfoxserver.controller.EzyEventController;
import com.tvd12.ezyfoxserver.setting.EzySimpleEventControllerSetting;
import com.tvd12.ezyfoxserver.setting.EzySimpleEventControllersSetting;
import com.tvd12.ezyfoxserver.wrapper.EzyEventControllers;
import com.tvd12.ezyfoxserver.wrapper.impl.EzyEventControllersImpl;
import com.tvd12.test.assertion.Asserts;
import com.tvd12.test.base.BaseTest;
import static org.mockito.Mockito.*;

@SuppressWarnings("rawtypes")
public class EzyEventControllersImplTest extends BaseTest {
Expand All @@ -25,6 +34,52 @@ public void test() {
controllers.destroy();
}

@SuppressWarnings("unchecked")
@Test
public void multiThreadTest() {
EzyEventControllersImpl sut = new EzyEventControllersImpl();
ExecutorService executorService = Executors.newFixedThreadPool(12);
AtomicBoolean active = new AtomicBoolean(true);
executorService.execute(() -> {
while (active.get()) {
for (EzyEventType eventType : EzyEventType.values()) {
sut.addController(eventType, mock(EzyEventController.class));
}
EzyThreads.sleep(1);
}
});
executorService.execute(() -> {
while (active.get()) {
for (EzyEventType eventType : EzyEventType.values()) {
for(EzyEventController controller : sut.getControllers(eventType)) {
controller.handle(null, null);
}
}
EzyThreads.sleep(1);
}
});
EzyThreads.sleep(1000);
executorService.shutdown();
}

@Test
public void getListControllerTest() {
// given
EzyEventControllersImpl sut = new EzyEventControllersImpl();
EzyEventController c1 = mock(EzyEventController.class);
EzyEventController c2 = mock(EzyEventController.class);
sut.addController(EzyEventType.SERVER_INITIALIZING, c1);
sut.addController(EzyEventType.SERVER_INITIALIZING, c2);

// when
List<EzyEventController> controllers = sut.getControllers(EzyEventType.SERVER_INITIALIZING);

// then
Asserts.assertEquals(controllers, Arrays.asList(c1, c2), false);
Asserts.assertEmpty(sut.getControllers(EzyEventType.USER_ACCESS_APP));

}

public static class EventController1 implements EzyEventController {

@Override
Expand Down
2 changes: 1 addition & 1 deletion ezyfox-server-embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-server</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<artifactId>ezyfox-server-embedded</artifactId>
<version>1.2.2</version>
Expand Down
2 changes: 1 addition & 1 deletion ezyfox-server-nio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-server</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<artifactId>ezyfox-server-nio</artifactId>
<version>1.2.2</version> <!-- ${project.version} -->
Expand Down
2 changes: 1 addition & 1 deletion ezyfox-server-niorunner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-server</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<artifactId>ezyfox-server-niorunner</artifactId>
<version>1.2.2</version> <!-- ${project.version} -->
Expand Down
2 changes: 1 addition & 1 deletion ezyfox-server-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-server</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<artifactId>ezyfox-server-support</artifactId>
<version>1.2.2</version> <!-- ${project.version} -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@SuppressWarnings("rawtypes")
public class EzyRequestHandlersImplementer extends EzyLoggable {

private boolean allowOverrideCommand;
private EzyResponseFactory responseFactory;
private EzyFeatureCommandManager featureCommandManager;
private EzyRequestCommandManager requestCommandManager;
Expand All @@ -31,7 +32,7 @@ public Map<String, EzyUserRequestHandler> implement(Collection<Object> controlle
for(String command : map.keySet()) {
EzyUserRequestHandler handler = map.get(command);
EzyUserRequestHandler old = handlers.put(command, handler);
if(old != null) {
if(old != null && !allowOverrideCommand) {
throw new EzyDuplicateRequestHandlerException(command, old, handler);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public final class EzySupportConstants {

public static final String COMMANDS = "commands";
public static final String DEFAULT_PACKAGE_TO_SCAN = "com.tvd12.ezyfoxserver.support.boot";
public static final String PROPERTY_NAME_MODULE_ALLOW_OVERRIDE_URI = "module.allow_override_uri";

private EzySupportConstants() {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tvd12.ezyfoxserver.support.controller;

import static com.tvd12.ezyfox.io.EzyStrings.isNotBlank;
import static com.tvd12.ezyfoxserver.support.constant.EzySupportConstants.PROPERTY_NAME_MODULE_ALLOW_OVERRIDE_URI;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -237,6 +238,9 @@ private Map<String, EzyUserRequestHandler> implementClientRequestHandlers() {
implementer.setResponseFactory(responseFactory);
implementer.setFeatureCommandManager(featureCommandManager);
implementer.setRequestCommandManager(requestCommandManager);
implementer.setAllowOverrideCommand(
beanContext.getProperty(PROPERTY_NAME_MODULE_ALLOW_OVERRIDE_URI, boolean.class, false)
);
Map<String, EzyUserRequestHandler> implementedHandlers =
implementer.implement(singletonFactory.getSingletons(EzyRequestController.class));
for(String command : implementedHandlers.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.tvd12.ezyfoxserver.support.entry;

import static com.tvd12.ezyfox.core.util.EzyEventHandlerLists.sortEventHandlersByPriority;
import static com.tvd12.ezyfoxserver.support.constant.EzySupportConstants.COMMANDS;
import static com.tvd12.ezyfoxserver.support.constant.EzySupportConstants.DEFAULT_PACKAGE_TO_SCAN;

import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -11,10 +13,8 @@
import com.tvd12.ezyfox.bean.EzyBeanContext;
import com.tvd12.ezyfox.bean.EzyBeanContextBuilder;
import com.tvd12.ezyfox.binding.EzyBindingContext;
import com.tvd12.ezyfox.binding.EzyBindingContextBuilder;
import com.tvd12.ezyfox.binding.EzyMarshaller;
import com.tvd12.ezyfox.binding.EzyUnmarshaller;
import com.tvd12.ezyfox.binding.impl.EzySimpleBindingContext;
import com.tvd12.ezyfox.core.annotation.EzyEventHandler;
import com.tvd12.ezyfox.core.annotation.EzyExceptionHandler;
import com.tvd12.ezyfox.core.annotation.EzyRequestController;
Expand Down Expand Up @@ -59,6 +59,7 @@ protected void postConfig(EzyAppContext context, EzyBeanContext beanContext) {}
private void addEventControllers(EzyAppContext appContext, EzyBeanContext beanContext) {
EzySetup setup = appContext.get(EzySetup.class);
List<Object> eventControllers = beanContext.getSingletons(EzyEventHandler.class);
sortEventHandlersByPriority(eventControllers);
for (Object controller : eventControllers) {
Class<?> controllerType = controller.getClass();
EzyEventHandler annotation = controllerType.getAnnotation(EzyEventHandler.class);
Expand Down Expand Up @@ -107,33 +108,29 @@ protected EzyBeanContext createBeanContext(EzyAppContext context) {
beanContextBuilder.addPrototypeClasses(prototypeClasses);

Set<String> scanablePackages = internalGetScanableBeanPackages();
if(appSetting.getPackageName() != null)
if(appSetting.getPackageName() != null) {
scanablePackages.add(appSetting.getPackageName());
if(scanablePackages.size() > 0) {
EzyReflection reflection = new EzyReflectionProxy(scanablePackages);
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedExtendsClasses(
EzyEventHandler.class,
EzyAppEventController.class));
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedClasses(EzyRequestController.class));
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedClasses(EzyExceptionHandler.class));
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedClasses(EzyRequestInterceptor.class));
beanContextBuilder.scan(scanablePackages);
}
EzyReflection reflection = new EzyReflectionProxy(scanablePackages);
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedExtendsClasses(
EzyEventHandler.class,
EzyAppEventController.class));
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedClasses(EzyRequestController.class));
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedClasses(EzyExceptionHandler.class));
beanContextBuilder.addSingletonClasses(
(Set)reflection.getAnnotatedClasses(EzyRequestInterceptor.class));
beanContextBuilder.scan(scanablePackages);
setupBeanContext(context, beanContextBuilder);
return beanContextBuilder.build();
}

protected EzyBindingContext createBindingContext() {
EzyBindingContextBuilder builder = EzyBindingContext.builder();
Set<String> scanablePackages = internalGetScanableBindingPackages();
if(scanablePackages.size() > 0)
builder.scan(scanablePackages);
EzySimpleBindingContext answer = builder.build();
return answer;
return EzyBindingContext.builder()
.scan(internalGetScanableBindingPackages())
.build();
}

private EzyResponseFactory createAppResponseFactory(
Expand Down Expand Up @@ -165,13 +162,15 @@ protected String[] getScanableBindingPackages() {

private Set<String> internalGetScanableBeanPackages() {
Set<String> scanablePackages = new HashSet<String>();
scanablePackages.add(DEFAULT_PACKAGE_TO_SCAN);
scanablePackages.addAll(Arrays.asList(getScanablePackages()));
scanablePackages.addAll(Arrays.asList(getScanableBeanPackages()));
return scanablePackages;
}

private Set<String> internalGetScanableBindingPackages() {
Set<String> scanablePackages = new HashSet<String>();
scanablePackages.add(DEFAULT_PACKAGE_TO_SCAN);
scanablePackages.addAll(Arrays.asList(getScanablePackages()));
scanablePackages.addAll(Arrays.asList(getScanableBindingPackages()));
return scanablePackages;
Expand Down
Loading

0 comments on commit c1edabe

Please sign in to comment.