Skip to content

Commit 3099e9e

Browse files
committed
调整云代码及钩子调用模式 全面使用注解进行调用
1 parent 4136f69 commit 3099e9e

24 files changed

Lines changed: 1073 additions & 368 deletions
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package com.javabaas.javasdk;
2+
3+
import com.javabaas.javasdk.annotation.HookEvent;
4+
import com.javabaas.javasdk.callback.JBStatusCallback;
5+
import com.javabaas.javasdk.cloud.*;
6+
7+
import java.util.Date;
8+
import java.util.HashMap;
9+
import java.util.LinkedHashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* Created by Codi on 2018/7/16.
14+
*/
15+
public class JB {
16+
private static final JB INSTANCE = new JB();
17+
private JBConfig config;
18+
19+
private final ScannerEngine engine = new ScannerEngine();
20+
private HashMap<String, CloudListener> cloudListeners = new LinkedHashMap<>();
21+
private HashMap<String, HookListener> hookListeners = new LinkedHashMap<>();
22+
23+
private JB() {
24+
config = new JBConfig();
25+
}
26+
27+
public HashMap<String, CloudListener> getCloudListeners() {
28+
return cloudListeners;
29+
}
30+
31+
public HashMap<String, HookListener> getHookListeners() {
32+
return hookListeners;
33+
}
34+
35+
/**
36+
* 初始化普通权限<br/>
37+
* 该方法默认设置plat为"cloud",建议服务端初始化时使用该方法
38+
*
39+
* @param remote 服务地址,例如"http://127.0.0.1:8080/api"
40+
* @param appId 应用id
41+
* @param key 应用普通权限key
42+
*/
43+
public static void init(String remote, String appId, String key) {
44+
initConfig(remote, appId, key, null, null, "cloud");
45+
updateAdjustTime();
46+
}
47+
48+
/**
49+
* 初始化普通权限
50+
*
51+
* @param remote 服务地址,例如"http://127.0.0.1:8080/api"
52+
* @param appId 应用id
53+
* @param key 应用普通权限key
54+
* @param plat 平台:"ios","android","js","cloud"等
55+
*/
56+
public static void init(String remote, String appId, String key, String plat) {
57+
initConfig(remote, appId, key, null, null, plat);
58+
updateAdjustTime();
59+
}
60+
61+
/**
62+
* 初始化管理员权限
63+
*
64+
* @param remote 服务地址,例如"http://127.0.0.1:8080/api"
65+
* @param adminKey 在服务端配置文件中配置的"baas.auth.key"的key值, 例如"JavaBaas"
66+
*/
67+
public static void initAdmin(String remote, String adminKey) {
68+
initConfig(remote, null, null, null, adminKey, "cloud");
69+
}
70+
71+
/**
72+
* 初始化超级权限
73+
*
74+
* @param remote 服务地址,例如"http://127.0.0.1:8080/api"
75+
* @param appId 应用id
76+
* @param masterKey 应用的masterKey
77+
*/
78+
public static void initMaster(String remote, String appId, String masterKey) {
79+
initConfig(remote, appId, null, masterKey, null, "cloud");
80+
}
81+
82+
/**
83+
* 设置请求超时时间,建议不要随便用
84+
*
85+
* @param connectTimeout 链接超时 单位秒 默认10秒
86+
* @param writeTimeout 读超时 单位秒 默认10秒
87+
* @param readTimeout 写超时 单位秒 默认10秒
88+
*/
89+
public static void initHttpTimeout(long connectTimeout, long writeTimeout, long readTimeout) {
90+
connectTimeout = connectTimeout <= 0 ? 10 : connectTimeout;
91+
writeTimeout = writeTimeout <= 0 ? 10 : writeTimeout;
92+
readTimeout = readTimeout <= 0 ? 10 : readTimeout;
93+
JBHttpClient.setTimeout(connectTimeout, writeTimeout, readTimeout);
94+
}
95+
96+
/**
97+
* 切换应用
98+
*
99+
* @param app 应用信息
100+
*/
101+
public static void useApp(JBApp app) {
102+
if (app == null) {
103+
removeAppConfig();
104+
} else {
105+
initConfig(null, app.getId(), app.getKey(), app.getMasterKey(), null, "cloud");
106+
updateAdjustTime();
107+
}
108+
}
109+
110+
public static JB getInstance() {
111+
return INSTANCE;
112+
}
113+
114+
public JBConfig getConfig() {
115+
return config;
116+
}
117+
118+
public void addListeners(Object listeners, Class<?> listenersClass) {
119+
engine.scan(INSTANCE, listeners, listenersClass);
120+
}
121+
122+
public void addCloudListener(String name, CloudListener listener) {
123+
cloudListeners.put(name, listener);
124+
}
125+
126+
public void addHookListener(String name, HookEvent event, HookListener listener) {
127+
hookListeners.put(HookSetting.hookName(name, event), listener);
128+
}
129+
130+
/**
131+
* JavaBaas云代码请求入口
132+
*
133+
* @param requestType 请求类型
134+
* @param body 请求主体
135+
*/
136+
public static JBResponse onRequest(String requestType, String body) {
137+
try {
138+
//整理请求体
139+
if (requestType.equals(JBRequest.REQUEST_CLOUD)) {
140+
//云方法
141+
CloudRequest cloudRequest = JBUtils.readValue(body, CloudRequest.class);
142+
return onCloudRequest(cloudRequest);
143+
} else if (requestType.equals(JBRequest.REQUEST_HOOK)) {
144+
//钩子
145+
HookRequest hookRequest = JBUtils.readValue(body, HookRequest.class);
146+
return onHookRequest(hookRequest);
147+
} //请求类型不匹配
148+
} catch (JBException ignored) {
149+
}
150+
return null;
151+
}
152+
153+
private static CloudResponse onCloudRequest(CloudRequest cloudRequest) {
154+
CloudListener listener = INSTANCE.cloudListeners.get(cloudRequest.getName());
155+
return listener.onCloud(cloudRequest);
156+
}
157+
158+
private static HookResponse onHookRequest(HookRequest hookRequest) {
159+
HookListener listener = INSTANCE.hookListeners.get(HookSetting.hookName(hookRequest.getName(), hookRequest.getEvent()));
160+
return listener.onHook(hookRequest);
161+
}
162+
163+
164+
private static void initConfig(String remote, String appId, String key, String masterKey, String adminKey, String plat) {
165+
if (!JBUtils.isEmpty(remote)) {
166+
INSTANCE.config.remote = remote.endsWith("/") ? remote : remote + "/";
167+
}
168+
if (!JBUtils.isEmpty(appId)) {
169+
INSTANCE.config.appId = appId;
170+
}
171+
if (!JBUtils.isEmpty(key)) {
172+
INSTANCE.config.key = key;
173+
}
174+
if (!JBUtils.isEmpty(masterKey)) {
175+
INSTANCE.config.masterKey = masterKey;
176+
}
177+
if (!JBUtils.isEmpty(adminKey)) {
178+
INSTANCE.config.adminKey = adminKey;
179+
}
180+
if (!JBUtils.isEmpty(plat)) {
181+
INSTANCE.config.plat = plat;
182+
}
183+
INSTANCE.config.finishInit = true;
184+
}
185+
186+
private static void removeAppConfig() {
187+
INSTANCE.config.masterKey = null;
188+
INSTANCE.config.key = null;
189+
INSTANCE.config.appId = null;
190+
}
191+
192+
private static void updateAdjustTime() {
193+
final long timestamp = new Date().getTime();
194+
JBStatus.getStatusInBackground(new JBStatusCallback() {
195+
@Override
196+
public void done(boolean success, Map<String, Object> status, JBException e) {
197+
if (success && status.get("time") != null) {
198+
long serverTime = (long) status.get("time");
199+
if (serverTime > 0) {
200+
INSTANCE.config.adjustTime = timestamp - serverTime;
201+
}
202+
}
203+
}
204+
});
205+
}
206+
207+
208+
}

src/main/java/com/javabaas/javasdk/JBApp.java

Lines changed: 1 addition & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.javabaas.javasdk;
22

33
import com.javabaas.javasdk.callback.*;
4+
import com.javabaas.javasdk.cloud.CloudSetting;
45

56
import java.util.*;
67

@@ -745,131 +746,6 @@ public String toString() {
745746
}
746747
}
747748

748-
public static class CloudSetting {
749-
private String customerHost;
750-
private List<String> cloudFunctions;
751-
private Map<String, HookSetting> hookSettings;
752-
753-
public String getCustomerHost() {
754-
return customerHost;
755-
}
756-
757-
public void setCustomerHost(String customerHost) {
758-
this.customerHost = customerHost;
759-
}
760-
761-
public List<String> getCloudFunctions() {
762-
return cloudFunctions;
763-
}
764-
765-
public void setCloudFunctions(List<String> cloudFunctions) {
766-
this.cloudFunctions = cloudFunctions;
767-
}
768-
769-
public Map<String, HookSetting> getHookSettings() {
770-
return hookSettings;
771-
}
772-
773-
public void setHookSettings(Map<String, HookSetting> hookSettings) {
774-
this.hookSettings = hookSettings;
775-
}
776-
777-
public HookSetting getHookSetting(String name) {
778-
return hookSettings == null ? null : hookSettings.get(name);
779-
}
780-
781-
public boolean hasFunction(String name) {
782-
if (cloudFunctions == null) {
783-
return false;
784-
} else {
785-
for (String function : cloudFunctions) {
786-
if (function.equals(name)) {
787-
return true;
788-
}
789-
}
790-
return false;
791-
}
792-
}
793-
}
794-
public static class HookSetting {
795-
private boolean beforeInsert;
796-
private boolean afterInsert;
797-
private boolean beforeUpdate;
798-
private boolean afterUpdate;
799-
private boolean beforeDelete;
800-
private boolean afterDelete;
801-
802-
public HookSetting() {
803-
}
804-
805-
public HookSetting(boolean enable) {
806-
this.beforeInsert = enable;
807-
this.afterInsert = enable;
808-
this.beforeUpdate = enable;
809-
this.afterUpdate = enable;
810-
this.beforeDelete = enable;
811-
this.afterDelete = enable;
812-
}
813-
814-
public HookSetting(boolean beforeInsert, boolean afterInsert, boolean beforeUpdate, boolean afterUpdate, boolean beforeDelete,
815-
boolean afterDelete) {
816-
this.beforeInsert = beforeInsert;
817-
this.afterInsert = afterInsert;
818-
this.beforeUpdate = beforeUpdate;
819-
this.afterUpdate = afterUpdate;
820-
this.beforeDelete = beforeDelete;
821-
this.afterDelete = afterDelete;
822-
}
823-
824-
public boolean isBeforeInsert() {
825-
return beforeInsert;
826-
}
827-
828-
public void setBeforeInsert(boolean beforeInsert) {
829-
this.beforeInsert = beforeInsert;
830-
}
831-
832-
public boolean isAfterInsert() {
833-
return afterInsert;
834-
}
835-
836-
public void setAfterInsert(boolean afterInsert) {
837-
this.afterInsert = afterInsert;
838-
}
839-
840-
public boolean isBeforeUpdate() {
841-
return beforeUpdate;
842-
}
843-
844-
public void setBeforeUpdate(boolean beforeUpdate) {
845-
this.beforeUpdate = beforeUpdate;
846-
}
847-
848-
public boolean isAfterUpdate() {
849-
return afterUpdate;
850-
}
851-
852-
public void setAfterUpdate(boolean afterUpdate) {
853-
this.afterUpdate = afterUpdate;
854-
}
855-
856-
public boolean isBeforeDelete() {
857-
return beforeDelete;
858-
}
859-
860-
public void setBeforeDelete(boolean beforeDelete) {
861-
this.beforeDelete = beforeDelete;
862-
}
863-
864-
public boolean isAfterDelete() {
865-
return afterDelete;
866-
}
867-
868-
public void setAfterDelete(boolean afterDelete) {
869-
this.afterDelete = afterDelete;
870-
}
871-
}
872-
873749
public static class JBAppExport {
874750
private String id;
875751
private String name;

0 commit comments

Comments
 (0)