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; package com.loafle.overflow.container.general.server;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.loafle.overflow.container.general.service.Service; 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.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import static com.loafle.overflow.core.interfaces.Service.execServices;
import static com.loafle.overflow.core.interfaces.Service.ServiceMethodType;
@Component @Component
public class GeneralContainerServer extends ContainerServer { public class GeneralContainerServer extends ContainerServer {
@Autowired @Autowired
@ -38,74 +38,27 @@ public class GeneralContainerServer extends ContainerServer {
this.services.put(bean.getClass(), 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 @Override
protected void onStart() throws Exception { protected void onStart() throws Exception {
super.onStart(); 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 @Override
protected void onStop() throws Exception { 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(); super.onStop();
} }
@Override @Override
protected void destroy() throws Exception { 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(); 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);
}
}
} }