bridge extension and plugin map
This commit is contained in:
		| @@ -1,9 +1,12 @@ | |||||||
| package com.github.pengfeizhou.doric; | package com.github.pengfeizhou.doric; | ||||||
|  |  | ||||||
|  | import com.github.pengfeizhou.doric.bridge.DoricNativePlugin; | ||||||
|  | import com.github.pengfeizhou.doric.extension.DoricPluginInfo; | ||||||
| import com.github.pengfeizhou.doric.utils.DoricSettableFuture; | import com.github.pengfeizhou.doric.utils.DoricSettableFuture; | ||||||
| import com.github.pengfeizhou.jscore.JSDecoder; | import com.github.pengfeizhou.jscore.JSDecoder; | ||||||
|  |  | ||||||
| import java.util.concurrent.atomic.AtomicInteger; | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @Description: Doric |  * @Description: Doric | ||||||
| @@ -11,8 +14,8 @@ import java.util.concurrent.atomic.AtomicInteger; | |||||||
|  * @CreateDate: 2019-07-18 |  * @CreateDate: 2019-07-18 | ||||||
|  */ |  */ | ||||||
| public class DoricContext { | public class DoricContext { | ||||||
|     private static AtomicInteger sCounter = new AtomicInteger(); |  | ||||||
|     private final String mContextId; |     private final String mContextId; | ||||||
|  |     private final Map<String, DoricNativePlugin> mPluginMap = new HashMap<>(); | ||||||
|  |  | ||||||
|     DoricContext(String contextId) { |     DoricContext(String contextId) { | ||||||
|         this.mContextId = contextId; |         this.mContextId = contextId; | ||||||
| @@ -33,5 +36,15 @@ public class DoricContext { | |||||||
|  |  | ||||||
|     public void teardown() { |     public void teardown() { | ||||||
|         DoricDriver.getInstance().destroyContext(mContextId); |         DoricDriver.getInstance().destroyContext(mContextId); | ||||||
|  |         mPluginMap.clear(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public DoricNativePlugin obtainPlugin(DoricPluginInfo doricPluginInfo) { | ||||||
|  |         DoricNativePlugin plugin = mPluginMap.get(doricPluginInfo.getName()); | ||||||
|  |         if (plugin == null) { | ||||||
|  |             plugin = doricPluginInfo.createPlugin(this); | ||||||
|  |             mPluginMap.put(doricPluginInfo.getName(), plugin); | ||||||
|  |         } | ||||||
|  |         return plugin; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -57,13 +57,13 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time | |||||||
|                     String message = args[1].string(); |                     String message = args[1].string(); | ||||||
|                     switch (type) { |                     switch (type) { | ||||||
|                         case "w": |                         case "w": | ||||||
|                             DoricLog.w(message, "_js"); |                             DoricLog.w("_js", message); | ||||||
|                             break; |                             break; | ||||||
|                         case "e": |                         case "e": | ||||||
|                             DoricLog.e(message, "_js"); |                             DoricLog.e("_js", message); | ||||||
|                             break; |                             break; | ||||||
|                         default: |                         default: | ||||||
|                             DoricLog.d(message, "_js"); |                             DoricLog.d("_js", message); | ||||||
|                             break; |                             break; | ||||||
|                     } |                     } | ||||||
|                 } catch (Exception e) { |                 } catch (Exception e) { | ||||||
| @@ -79,7 +79,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time | |||||||
|                     String name = args[0].string(); |                     String name = args[0].string(); | ||||||
|                     String content = Doric.getJSBundle(name); |                     String content = Doric.getJSBundle(name); | ||||||
|                     if (TextUtils.isEmpty(content)) { |                     if (TextUtils.isEmpty(content)) { | ||||||
|                         DoricLog.e("error"); |                         DoricLog.e("require js bundle:%s is empty", name); | ||||||
|                         return new JavaValue(false); |                         return new JavaValue(false); | ||||||
|                     } |                     } | ||||||
|                     mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name); |                     mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name); | ||||||
|   | |||||||
| @@ -1,24 +1,59 @@ | |||||||
| package com.github.pengfeizhou.doric.extension; | package com.github.pengfeizhou.doric.extension; | ||||||
|  |  | ||||||
|  | import android.text.TextUtils; | ||||||
|  |  | ||||||
| import com.github.pengfeizhou.doric.DoricContext; | import com.github.pengfeizhou.doric.DoricContext; | ||||||
| import com.github.pengfeizhou.doric.DoricDriver; | import com.github.pengfeizhou.doric.DoricDriver; | ||||||
|  | import com.github.pengfeizhou.doric.bridge.DoricNativePlugin; | ||||||
|  | import com.github.pengfeizhou.doric.bridge.ModalPlugin; | ||||||
|  | import com.github.pengfeizhou.doric.utils.DoricLog; | ||||||
| import com.github.pengfeizhou.jscore.JSDecoder; | import com.github.pengfeizhou.jscore.JSDecoder; | ||||||
| import com.github.pengfeizhou.jscore.JavaValue; | import com.github.pengfeizhou.jscore.JavaValue; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @Description: Doric |  * @Description: Doric | ||||||
|  * @Author: pengfei.zhou |  * @Author: pengfei.zhou | ||||||
|  * @CreateDate: 2019-07-18 |  * @CreateDate: 2019-07-18 | ||||||
|  */ |  */ | ||||||
| public class DoricBridgeExtension { | public class DoricBridgeExtension { | ||||||
|  |     private Map<String, DoricPluginInfo> pluginInfoMap = new HashMap<>(); | ||||||
|  |  | ||||||
|     public DoricBridgeExtension() { |     public DoricBridgeExtension() { | ||||||
|  |         registerExtension(ModalPlugin.class); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public JavaValue callNative(String contextId, String module, String method, String callbackId, JSDecoder jsDecoder) { |     public void registerExtension(Class<? extends DoricNativePlugin> pluginClass) { | ||||||
|         DoricContext doricContext = DoricDriver.getInstance().getContext(contextId); |         DoricPluginInfo doricPluginInfo = new DoricPluginInfo(pluginClass); | ||||||
|  |         if (!TextUtils.isEmpty(doricPluginInfo.getName())) { | ||||||
|  |             pluginInfoMap.put(doricPluginInfo.getName(), doricPluginInfo); | ||||||
|         return null; |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public JavaValue callNative(String contextId, String module, String methodName, String callbackId, JSDecoder jsDecoder) { | ||||||
|  |         DoricContext context = DoricDriver.getInstance().getContext(contextId); | ||||||
|  |         DoricPluginInfo pluginInfo = pluginInfoMap.get(module); | ||||||
|  |         if (pluginInfo == null) { | ||||||
|  |             DoricLog.e("Cannot find plugin class:%s", module); | ||||||
|  |             return new JavaValue(false); | ||||||
|  |         } | ||||||
|  |         DoricNativePlugin doricNativePlugin = context.obtainPlugin(pluginInfo); | ||||||
|  |         if (doricNativePlugin == null) { | ||||||
|  |             DoricLog.e("Cannot obtain plugin instance:%s,method:%", module); | ||||||
|  |             return new JavaValue(false); | ||||||
|  |         } | ||||||
|  |         Method method = pluginInfo.getMethod(methodName); | ||||||
|  |         if (method == null) { | ||||||
|  |             DoricLog.e("Cannot find plugin method in class:%s,method:%s", module, methodName); | ||||||
|  |             return new JavaValue(false); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         Class[] classes = method.getParameterTypes(); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         return new JavaValue(true); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -0,0 +1,64 @@ | |||||||
|  | package com.github.pengfeizhou.doric.extension; | ||||||
|  |  | ||||||
|  | import android.text.TextUtils; | ||||||
|  |  | ||||||
|  | import com.github.pengfeizhou.doric.DoricContext; | ||||||
|  | import com.github.pengfeizhou.doric.bridge.DoricNativePlugin; | ||||||
|  | import com.github.pengfeizhou.doric.utils.DoricLog; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Constructor; | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.Map; | ||||||
|  | import java.util.concurrent.ConcurrentHashMap; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @Description: Doric | ||||||
|  |  * @Author: pengfei.zhou | ||||||
|  |  * @CreateDate: 2019-07-18 | ||||||
|  |  */ | ||||||
|  | public class DoricPluginInfo { | ||||||
|  |  | ||||||
|  |     private Constructor<? extends DoricNativePlugin> pluginConstructor; | ||||||
|  |  | ||||||
|  |     private Map<String, Method> methodMap = new ConcurrentHashMap<>(); | ||||||
|  |     public boolean stringify = false; | ||||||
|  |     private String name; | ||||||
|  |  | ||||||
|  |     public DoricPluginInfo(Class<? extends DoricNativePlugin> pluginClass) { | ||||||
|  |         try { | ||||||
|  |             this.pluginConstructor = pluginClass.getConstructor(DoricContext.class); | ||||||
|  |             DoricComponent doricComponent = pluginClass.getAnnotation(DoricComponent.class); | ||||||
|  |             this.name = doricComponent.name(); | ||||||
|  |             Method[] methods = pluginClass.getMethods(); | ||||||
|  |             for (Method method : methods) { | ||||||
|  |                 DoricMethod doricMethod = method.getAnnotation(DoricMethod.class); | ||||||
|  |                 if (doricMethod != null) { | ||||||
|  |                     if (TextUtils.isEmpty(doricMethod.name())) { | ||||||
|  |                         methodMap.put(method.getName(), method); | ||||||
|  |                     } else { | ||||||
|  |                         methodMap.put(doricMethod.name(), method); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             DoricLog.e("Error to create doric for " + e.getLocalizedMessage()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public String getName() { | ||||||
|  |         return name; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public DoricNativePlugin createPlugin(DoricContext doricContext) { | ||||||
|  |         try { | ||||||
|  |             return pluginConstructor.newInstance(doricContext); | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             DoricLog.e("Error to create doric plugin for " + e.getLocalizedMessage()); | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Method getMethod(String name) { | ||||||
|  |         return methodMap.get(name); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,44 +0,0 @@ | |||||||
| package com.github.pengfeizhou.doric.extension; |  | ||||||
|  |  | ||||||
| import android.text.TextUtils; |  | ||||||
|  |  | ||||||
| import java.lang.reflect.Method; |  | ||||||
| import java.util.HashMap; |  | ||||||
| import java.util.Map; |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * @Description: Doric |  | ||||||
|  * @Author: pengfei.zhou |  | ||||||
|  * @CreateDate: 2019-07-18 |  | ||||||
|  */ |  | ||||||
| public class ModuleClassInfo { |  | ||||||
|  |  | ||||||
|     private Class clz; |  | ||||||
|  |  | ||||||
|     private Object instance; |  | ||||||
|  |  | ||||||
|     private Map<String, Method> methodMap = new HashMap<>(); |  | ||||||
|     public boolean stringify = false; |  | ||||||
|  |  | ||||||
|     public ModuleClassInfo(Class clz) { |  | ||||||
|         this.clz = clz; |  | ||||||
|         try { |  | ||||||
|             this.instance = clz.newInstance(); |  | ||||||
|         } catch (Exception e) { |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |  | ||||||
|         Method[] methods = clz.getMethods(); |  | ||||||
|         if (methods != null) { |  | ||||||
|             for (Method method : methods) { |  | ||||||
|                 DoricMethod doricMethod = method.getAnnotation(DoricMethod.class); |  | ||||||
|                 if (doricMethod != null) { |  | ||||||
|                     if (TextUtils.isEmpty(doricMethod.name())) { |  | ||||||
|                         methodMap.put(method.getName(), method); |  | ||||||
|                     } else { |  | ||||||
|                         methodMap.put(doricMethod.name(), method); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,5 +1,6 @@ | |||||||
| package com.github.pengfeizhou.doric.utils; | package com.github.pengfeizhou.doric.utils; | ||||||
|  |  | ||||||
|  | import android.text.TextUtils; | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
|  |  | ||||||
| import com.github.pengfeizhou.doric.Doric; | import com.github.pengfeizhou.doric.Doric; | ||||||
| @@ -12,27 +13,38 @@ import com.github.pengfeizhou.doric.Doric; | |||||||
| public class DoricLog { | public class DoricLog { | ||||||
|     private static String TAG = Doric.class.getSimpleName(); |     private static String TAG = Doric.class.getSimpleName(); | ||||||
|  |  | ||||||
|     public static void d(String message, String... suffix) { |  | ||||||
|         StringBuilder stringBuilder = new StringBuilder(TAG); |     public static void d(String message, Object... args) { | ||||||
|         for (String s : suffix) { |         Log.d(suffixTag(null), format(message, args)); | ||||||
|             stringBuilder.append(s); |  | ||||||
|         } |  | ||||||
|         Log.d(stringBuilder.toString(), message); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void w(String message, String... suffix) { |     public static void w(String message, Object... args) { | ||||||
|         StringBuilder stringBuilder = new StringBuilder(TAG); |         Log.w(suffixTag(null), format(message, args)); | ||||||
|         for (String s : suffix) { |  | ||||||
|             stringBuilder.append(s); |  | ||||||
|         } |  | ||||||
|         Log.w(stringBuilder.toString(), message); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void e(String message, String... suffix) { |     public static void e(String message, Object... args) { | ||||||
|         StringBuilder stringBuilder = new StringBuilder(TAG); |         Log.e(suffixTag(null), format(message, args)); | ||||||
|         for (String s : suffix) { |  | ||||||
|             stringBuilder.append(s); |  | ||||||
|     } |     } | ||||||
|         Log.e(stringBuilder.toString(), message); |  | ||||||
|  |     public static void d(String suffix, String message, Object... args) { | ||||||
|  |         Log.d(suffixTag(suffix), format(message, args)); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static void w(String suffix, String message, Object... args) { | ||||||
|  |         Log.w(suffixTag(suffix), format(message, args)); | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static void e(String suffix, String message, Object... args) { | ||||||
|  |         Log.e(suffixTag(suffix), format(message, args)); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private static String suffixTag(String suffix) { | ||||||
|  |         return TextUtils.isEmpty(suffix) ? TAG : suffix + TAG; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private static String format(String message, Object... args) { | ||||||
|  |         return String.format(message, args); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user