Skip to content

Commit 9b05517

Browse files
committed
添加云方法和钩子两个注解
删除JBCloud中的JBCloudSetting,跟JBApp中重复了 JBObject添加了一些方法
1 parent 96ff5b6 commit 9b05517

5 files changed

Lines changed: 203 additions & 139 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,15 @@ public void setValue(String value) {
10041004
}
10051005
}
10061006

1007+
public enum HookSettingType {
1008+
BEFOREINSERT,
1009+
AFTERINSERT,
1010+
BEFOREUPDATE,
1011+
AFTERUPDATE,
1012+
BEFOREDELETE,
1013+
AFTERDELETE;
1014+
}
1015+
10071016
public enum JBAppConfigKey {
10081017
// 短信相关
10091018
SMS_TRY_LIMIT("baas.sms.tryLimit", "短信_重试次数", "5"),
Lines changed: 112 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.javabaas.javasdk;
22

3+
import com.javabaas.javasdk.annotation.JBCloudAnnotation;
4+
import com.javabaas.javasdk.annotation.JBHookAnnotation;
35
import com.javabaas.javasdk.callback.JBCloudCallback;
46
import com.javabaas.javasdk.callback.JBObjectCallback;
57

6-
import java.util.List;
7-
import java.util.Map;
8+
import java.lang.reflect.Field;
9+
import java.lang.reflect.Method;
10+
import java.util.*;
811

912
/**
1013
* 云方法相处理
@@ -13,14 +16,118 @@
1316

1417
public class JBCloud {
1518

19+
/**
20+
* 初始化云方法(注解方式) 同步
21+
*
22+
* @param callbackUrl 云方法地址
23+
* @return 初始化成功或失败
24+
* @throws JBException 异常信息
25+
*/
26+
public static boolean deploy(String callbackUrl) throws JBException {
27+
JBApp.CloudSetting cloudSetting = getCloudSetting(callbackUrl);
28+
deployToJavabaas(cloudSetting, true, new JBCloudCallback() {
29+
@Override
30+
public void done(boolean success, Map<String, Object> data, JBException e) {
31+
if (!success) {
32+
JBExceptionHolder.add(e);
33+
}
34+
}
35+
});
36+
if (JBExceptionHolder.exists()) {
37+
throw JBExceptionHolder.remove();
38+
}
39+
return true;
40+
}
41+
42+
/**
43+
* 初始化云方法(注解方式) 异步
44+
*
45+
* @param callbackUrl 云方法地址
46+
* @param callback 成功或失败回调
47+
*/
48+
public static void deployInBackground(String callbackUrl, JBCloudCallback callback) {
49+
JBApp.CloudSetting cloudSetting = getCloudSetting(callbackUrl);
50+
deployToJavabaas(cloudSetting, false, callback);
51+
}
52+
53+
private static JBApp.CloudSetting getCloudSetting(String remote) {
54+
JBApp.CloudSetting cloudSetting = new JBApp.CloudSetting();
55+
cloudSetting.setCustomerHost(remote);
56+
List<String> clouds = new ArrayList<>();
57+
Map<String, JBApp.HookSetting> hook = new HashMap<>();
58+
try {
59+
Field field = ClassLoader.class.getDeclaredField("classes");
60+
field.setAccessible(true);
61+
Vector<Class> vector = (Vector<Class>) field.get(ClassLoader.getSystemClassLoader());
62+
List<Class> classes = new ArrayList<>(vector);
63+
for (Class clazz : classes) {
64+
try {
65+
Method[] methods = clazz.getDeclaredMethods();
66+
for (Method method : methods) {
67+
if (method.getAnnotation(JBCloudAnnotation.class) != null) {
68+
JBCloudAnnotation annotation = method.getAnnotation(JBCloudAnnotation.class);
69+
clouds.add(annotation.value());
70+
}
71+
if (method.getAnnotation(JBHookAnnotation.class) != null) {
72+
JBHookAnnotation annotation = method.getAnnotation(JBHookAnnotation.class);
73+
JBApp.HookSetting hookSetting;
74+
if (hook.get(annotation.className()) != null) {
75+
hookSetting = hook.get(annotation.className());
76+
} else {
77+
hookSetting = new JBApp.HookSetting();
78+
}
79+
JBApp.HookSettingType[] types = annotation.hook();
80+
updateHookSetting(hookSetting, types);
81+
hook.put(annotation.className(), hookSetting);
82+
}
83+
}
84+
} catch (NoClassDefFoundError e) {
85+
}
86+
}
87+
cloudSetting.setCloudFunctions(clouds);
88+
cloudSetting.setHookSettings(hook);
89+
} catch (IllegalAccessException e) {
90+
} catch (NoSuchFieldException e) {
91+
}
92+
return cloudSetting;
93+
}
94+
95+
private static void updateHookSetting(JBApp.HookSetting hookSetting, JBApp.HookSettingType[] types) {
96+
if (hookSetting == null) {
97+
hookSetting = new JBApp.HookSetting();
98+
}
99+
for (JBApp.HookSettingType type : types) {
100+
switch (type) {
101+
case BEFOREINSERT:
102+
hookSetting.setBeforeInsert(true);
103+
break;
104+
case BEFOREUPDATE:
105+
hookSetting.setBeforeUpdate(true);
106+
break;
107+
case BEFOREDELETE:
108+
hookSetting.setBeforeDelete(true);
109+
break;
110+
case AFTERINSERT:
111+
hookSetting.setAfterInsert(true);
112+
break;
113+
case AFTERUPDATE:
114+
hookSetting.setAfterUpdate(true);
115+
break;
116+
case AFTERDELETE:
117+
hookSetting.setAfterDelete(true);
118+
break;
119+
}
120+
}
121+
}
122+
16123
/**
17124
* 初始化云方法 同步
18125
*
19126
* @param setting 云方法信息
20127
* @return 初始化成功或失败
21128
* @throws JBException 异常信息
22129
*/
23-
public static boolean deploy(JBCloudSetting setting) throws JBException {
130+
public static boolean deploy(JBApp.CloudSetting setting) throws JBException {
24131
deployToJavabaas(setting, true, new JBCloudCallback() {
25132
@Override
26133
public void done(boolean success, Map<String, Object> data, JBException e) {
@@ -41,11 +148,11 @@ public void done(boolean success, Map<String, Object> data, JBException e) {
41148
* @param setting 云方法信息
42149
* @param callback 成功或失败回调
43150
*/
44-
public static void deployInBackground(JBCloudSetting setting, JBCloudCallback callback) {
151+
public static void deployInBackground(JBApp.CloudSetting setting, JBCloudCallback callback) {
45152
deployToJavabaas(setting, false, callback);
46153
}
47154

48-
private static void deployToJavabaas(final JBCloudSetting setting, final boolean sync, final JBCloudCallback callback) {
155+
private static void deployToJavabaas(final JBApp.CloudSetting setting, final boolean sync, final JBCloudCallback callback) {
49156
String path = JBHttpClient.getCloudDeployPath();
50157
JBHttpClient.INSTANCE().sendRequest(path, JBHttpMethod.POST, null, setting, true, new JBObjectCallback() {
51158
@Override
@@ -171,131 +278,4 @@ public void onFailure(JBException error) {
171278
});
172279
}
173280

174-
public static class JBCloudSetting {
175-
private String customerHost;
176-
private List<String> cloudFunctions;
177-
private Map<String, HookSetting> hookSettings;
178-
179-
public String getCustomerHost() {
180-
return customerHost;
181-
}
182-
183-
public void setCustomerHost(String customerHost) {
184-
this.customerHost = customerHost;
185-
}
186-
187-
public List<String> getCloudFunctions() {
188-
return cloudFunctions;
189-
}
190-
191-
public void setCloudFunctions(List<String> cloudFunctions) {
192-
this.cloudFunctions = cloudFunctions;
193-
}
194-
195-
public Map<String, HookSetting> getHookSettings() {
196-
return hookSettings;
197-
}
198-
199-
public void setHookSettings(Map<String, HookSetting> hookSettings) {
200-
this.hookSettings = hookSettings;
201-
}
202-
203-
public HookSetting getHookSetting(String name) {
204-
return hookSettings == null ? null : hookSettings.get(name);
205-
}
206-
207-
public boolean hasFunction(String name) {
208-
if (cloudFunctions == null) {
209-
return false;
210-
} else {
211-
for (String function : cloudFunctions) {
212-
if (function.equals(name)) {
213-
return true;
214-
}
215-
}
216-
return false;
217-
}
218-
}
219-
}
220-
221-
public static class HookSetting {
222-
223-
private boolean beforeInsert;
224-
private boolean afterInsert;
225-
private boolean beforeUpdate;
226-
private boolean afterUpdate;
227-
private boolean beforeDelete;
228-
private boolean afterDelete;
229-
230-
public HookSetting() {
231-
}
232-
233-
public HookSetting(boolean enable) {
234-
this.beforeInsert = enable;
235-
this.afterInsert = enable;
236-
this.beforeUpdate = enable;
237-
this.afterUpdate = enable;
238-
this.beforeDelete = enable;
239-
this.afterDelete = enable;
240-
}
241-
242-
public HookSetting(boolean beforeInsert, boolean afterInsert, boolean beforeUpdate, boolean afterUpdate, boolean beforeDelete,
243-
boolean afterDelete) {
244-
this.beforeInsert = beforeInsert;
245-
this.afterInsert = afterInsert;
246-
this.beforeUpdate = beforeUpdate;
247-
this.afterUpdate = afterUpdate;
248-
this.beforeDelete = beforeDelete;
249-
this.afterDelete = afterDelete;
250-
}
251-
252-
public boolean isBeforeInsert() {
253-
return beforeInsert;
254-
}
255-
256-
public void setBeforeInsert(boolean beforeInsert) {
257-
this.beforeInsert = beforeInsert;
258-
}
259-
260-
public boolean isAfterInsert() {
261-
return afterInsert;
262-
}
263-
264-
public void setAfterInsert(boolean afterInsert) {
265-
this.afterInsert = afterInsert;
266-
}
267-
268-
public boolean isBeforeUpdate() {
269-
return beforeUpdate;
270-
}
271-
272-
public void setBeforeUpdate(boolean beforeUpdate) {
273-
this.beforeUpdate = beforeUpdate;
274-
}
275-
276-
public boolean isAfterUpdate() {
277-
return afterUpdate;
278-
}
279-
280-
public void setAfterUpdate(boolean afterUpdate) {
281-
this.afterUpdate = afterUpdate;
282-
}
283-
284-
public boolean isBeforeDelete() {
285-
return beforeDelete;
286-
}
287-
288-
public void setBeforeDelete(boolean beforeDelete) {
289-
this.beforeDelete = beforeDelete;
290-
}
291-
292-
public boolean isAfterDelete() {
293-
return afterDelete;
294-
}
295-
296-
public void setAfterDelete(boolean afterDelete) {
297-
this.afterDelete = afterDelete;
298-
}
299-
}
300-
301281
}

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,10 @@ public void setObjectId(String objectId) {
5353
this.objectId = objectId;
5454
}
5555

56-
public String getUpdatedAt() {
57-
return updatedAt;
58-
}
59-
6056
public void setUpdatedAt(String updatedAt) {
6157
this.updatedAt = updatedAt;
6258
}
6359

64-
public String getCreatedAt() {
65-
return createdAt;
66-
}
6760

6861
public void setCreatedAt(String createdAt) {
6962
this.createdAt = createdAt;
@@ -201,10 +194,62 @@ private boolean checkKey(String key) {
201194
return true;
202195
}
203196

197+
public String getString(String key) {
198+
Object object = get(key);
199+
if (object instanceof String) {
200+
return (String) object;
201+
} else {
202+
return null;
203+
}
204+
}
205+
206+
public int getInt(String key) {
207+
Number v = (Number) get(key);
208+
if (v != null)
209+
return v.intValue();
210+
return 0;
211+
}
212+
213+
public long getLong(String key) {
214+
Number number = (Number) get(key);
215+
if (number != null)
216+
return number.longValue();
217+
return 0L;
218+
}
219+
220+
public double getDouble(String key) {
221+
Number number = (Number) get(key);
222+
if (number != null)
223+
return number.doubleValue();
224+
return 0;
225+
}
226+
227+
public boolean getBoolean(String key) {
228+
Boolean b = (Boolean) get(key);
229+
return b == null ? false : b;
230+
}
231+
204232
public Date getDate(String key) {
205233
return JBUtils.dateFromString((String) get(key));
206234
}
207235

236+
public Date getCreatedAt() {
237+
return JBUtils.dateFromString(createdAt);
238+
}
239+
240+
public Date getUpdatedAt() {
241+
return JBUtils.dateFromString(updatedAt);
242+
}
243+
244+
public List getList(String key) {
245+
return (List) get(key);
246+
}
247+
248+
public <V> Map<String, V> getMap(String key) {
249+
return (Map<String, V>) this.get(key);
250+
}
251+
252+
208253
/**
209254
* 原子操作 删除删除字段值
210255
*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.javabaas.javasdk.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* Created by zangyilin on 2017/11/29.
7+
*/
8+
@Target(ElementType.METHOD)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Documented
11+
public @interface JBCloudAnnotation {
12+
String value();
13+
}

0 commit comments

Comments
 (0)