From 39fda40d69a8063ead09cfcdd37c7c10a4c8f1f7 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 25 Apr 2018 23:13:40 +0900 Subject: [PATCH] ing --- .../overflow/core/interfaces/Service.java | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/loafle/overflow/core/interfaces/Service.java b/src/main/java/com/loafle/overflow/core/interfaces/Service.java index eed8d5e..056464e 100644 --- a/src/main/java/com/loafle/overflow/core/interfaces/Service.java +++ b/src/main/java/com/loafle/overflow/core/interfaces/Service.java @@ -1,5 +1,10 @@ package com.loafle.overflow.core.interfaces; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + /** * Service */ @@ -12,11 +17,54 @@ public interface Service { void destroyService(); + public static void execServices(Map, 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 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 { - INIT("initService"), - START("startService"), - STOP("stopService"), - DESTROY("destroyService"); + INIT("initService"), START("startService"), STOP("stopService"), DESTROY("destroyService"); private String stringValue;