ing
This commit is contained in:
parent
9a3853938b
commit
39fda40d69
|
@ -1,5 +1,10 @@
|
||||||
package com.loafle.overflow.core.interfaces;
|
package com.loafle.overflow.core.interfaces;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service
|
* Service
|
||||||
*/
|
*/
|
||||||
|
@ -12,11 +17,54 @@ public interface Service {
|
||||||
|
|
||||||
void destroyService();
|
void destroyService();
|
||||||
|
|
||||||
|
public static void execServices(Map<Class<?>, Object> services, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static enum ServiceMethodType {
|
public static enum ServiceMethodType {
|
||||||
INIT("initService"),
|
INIT("initService"), START("startService"), STOP("stopService"), DESTROY("destroyService");
|
||||||
START("startService"),
|
|
||||||
STOP("stopService"),
|
|
||||||
DESTROY("destroyService");
|
|
||||||
|
|
||||||
private String stringValue;
|
private String stringValue;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user