bridge extension and plugin map
This commit is contained in:
parent
15b5bebcf0
commit
920aab1035
@ -1,9 +1,12 @@
|
||||
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.jscore.JSDecoder;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description: Doric
|
||||
@ -11,8 +14,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
* @CreateDate: 2019-07-18
|
||||
*/
|
||||
public class DoricContext {
|
||||
private static AtomicInteger sCounter = new AtomicInteger();
|
||||
private final String mContextId;
|
||||
private final Map<String, DoricNativePlugin> mPluginMap = new HashMap<>();
|
||||
|
||||
DoricContext(String contextId) {
|
||||
this.mContextId = contextId;
|
||||
@ -33,5 +36,15 @@ public class DoricContext {
|
||||
|
||||
public void teardown() {
|
||||
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();
|
||||
switch (type) {
|
||||
case "w":
|
||||
DoricLog.w(message, "_js");
|
||||
DoricLog.w("_js", message);
|
||||
break;
|
||||
case "e":
|
||||
DoricLog.e(message, "_js");
|
||||
DoricLog.e("_js", message);
|
||||
break;
|
||||
default:
|
||||
DoricLog.d(message, "_js");
|
||||
DoricLog.d("_js", message);
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -79,7 +79,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
|
||||
String name = args[0].string();
|
||||
String content = Doric.getJSBundle(name);
|
||||
if (TextUtils.isEmpty(content)) {
|
||||
DoricLog.e("error");
|
||||
DoricLog.e("require js bundle:%s is empty", name);
|
||||
return new JavaValue(false);
|
||||
}
|
||||
mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name);
|
||||
|
@ -1,24 +1,59 @@
|
||||
package com.github.pengfeizhou.doric.extension;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.github.pengfeizhou.doric.DoricContext;
|
||||
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.JavaValue;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description: Doric
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2019-07-18
|
||||
*/
|
||||
public class DoricBridgeExtension {
|
||||
public DoricBridgeExtension() {
|
||||
private Map<String, DoricPluginInfo> pluginInfoMap = new HashMap<>();
|
||||
|
||||
public DoricBridgeExtension() {
|
||||
registerExtension(ModalPlugin.class);
|
||||
}
|
||||
|
||||
public JavaValue callNative(String contextId, String module, String method, String callbackId, JSDecoder jsDecoder) {
|
||||
DoricContext doricContext = DoricDriver.getInstance().getContext(contextId);
|
||||
public void registerExtension(Class<? extends DoricNativePlugin> pluginClass) {
|
||||
DoricPluginInfo doricPluginInfo = new DoricPluginInfo(pluginClass);
|
||||
if (!TextUtils.isEmpty(doricPluginInfo.getName())) {
|
||||
pluginInfoMap.put(doricPluginInfo.getName(), doricPluginInfo);
|
||||
}
|
||||
}
|
||||
|
||||
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 null;
|
||||
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;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.pengfeizhou.doric.Doric;
|
||||
@ -12,27 +13,38 @@ import com.github.pengfeizhou.doric.Doric;
|
||||
public class DoricLog {
|
||||
private static String TAG = Doric.class.getSimpleName();
|
||||
|
||||
public static void d(String message, String... suffix) {
|
||||
StringBuilder stringBuilder = new StringBuilder(TAG);
|
||||
for (String s : suffix) {
|
||||
stringBuilder.append(s);
|
||||
}
|
||||
Log.d(stringBuilder.toString(), message);
|
||||
|
||||
public static void d(String message, Object... args) {
|
||||
Log.d(suffixTag(null), format(message, args));
|
||||
}
|
||||
|
||||
public static void w(String message, String... suffix) {
|
||||
StringBuilder stringBuilder = new StringBuilder(TAG);
|
||||
for (String s : suffix) {
|
||||
stringBuilder.append(s);
|
||||
}
|
||||
Log.w(stringBuilder.toString(), message);
|
||||
public static void w(String message, Object... args) {
|
||||
Log.w(suffixTag(null), format(message, args));
|
||||
|
||||
}
|
||||
|
||||
public static void e(String message, String... suffix) {
|
||||
StringBuilder stringBuilder = new StringBuilder(TAG);
|
||||
for (String s : suffix) {
|
||||
stringBuilder.append(s);
|
||||
public static void e(String message, Object... args) {
|
||||
Log.e(suffixTag(null), format(message, args));
|
||||
}
|
||||
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