This commit is contained in:
crusader 2018-04-25 23:17:04 +09:00
parent 67fc64a274
commit 88cd40bc05

View File

@ -1,9 +1,6 @@
package com.loafle.overflow.container.general.server;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.loafle.overflow.container.general.service.Service;
@ -14,6 +11,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import static com.loafle.overflow.core.interfaces.Service.execServices;
import static com.loafle.overflow.core.interfaces.Service.ServiceMethodType;
@Component
public class GeneralContainerServer extends ContainerServer {
@Autowired
@ -34,78 +34,31 @@ public class GeneralContainerServer extends ContainerServer {
}
this.services = new HashMap<>();
services.forEach((name, bean)->{
services.forEach((name, bean) -> {
this.services.put(bean.getClass(), bean);
});
this.execServices(this.services, com.loafle.overflow.core.interfaces.Service.ServiceMethodType.INIT, Service.OrderedServices, false);
execServices(this.services, ServiceMethodType.INIT, Service.OrderedServices, false);
}
@Override
protected void onStart() throws Exception {
super.onStart();
this.execServices(this.services, com.loafle.overflow.core.interfaces.Service.ServiceMethodType.START, Service.OrderedServices, false);
execServices(this.services, ServiceMethodType.START, Service.OrderedServices, false);
}
@Override
protected void onStop() throws Exception {
this.execServices(this.services, com.loafle.overflow.core.interfaces.Service.ServiceMethodType.STOP, Service.OrderedServices, true);
execServices(this.services, ServiceMethodType.STOP, Service.OrderedServices, true);
super.onStop();
}
@Override
protected void destroy() throws Exception {
this.execServices(this.services, com.loafle.overflow.core.interfaces.Service.ServiceMethodType.DESTROY, Service.OrderedServices, true);
execServices(this.services, ServiceMethodType.DESTROY, Service.OrderedServices, true);
super.destroy();
}
private void execServices(Map<Class<?>, Object> services, com.loafle.overflow.core.interfaces.Service.ServiceMethodType methodType, Class<?>[] orderedTypes, boolean reverse) throws Exception {
if (null == services || 0 == services.size()) {
return;
}
if (null == orderedTypes || 0 == orderedTypes.length) {
return;
}
List<Object> orderedServices = new ArrayList<>(orderedTypes.length);
if (reverse) {
for (int i = orderedTypes.length - 1; i >= 0; i--) {
Class<?> clazz = orderedTypes[i];
Object service = services.get(clazz);
if (null == service) {
continue;
}
orderedServices.add(service);
}
} else {
for (int i = 0; i < orderedTypes.length; i++) {
Class<?> clazz = orderedTypes[i];
Object service = services.get(clazz);
if (null == service) {
continue;
}
orderedServices.add(service);
}
}
if (0 == orderedServices.size()) {
return;
}
Class<?>[] parameterTypes = new Class<?>[]{};
for (int i = 0; i < orderedServices.size(); i++) {
Object service = orderedServices.get(i);
Class<?> clazz = service.getClass();
Method method = clazz.getMethod(methodType.toString(), parameterTypes);
if (null == method) {
continue;
}
method.invoke(service);
}
}
}