11package com .javabaas .javasdk ;
22
3+ import com .javabaas .javasdk .annotation .JBCloudAnnotation ;
4+ import com .javabaas .javasdk .annotation .JBHookAnnotation ;
35import com .javabaas .javasdk .callback .JBCloudCallback ;
46import 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 * 云方法相处理
1316
1417public 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}
0 commit comments