• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos;
17 
18 import com.alibaba.fastjson.JSON;
19 import com.alibaba.fastjson.JSONObject;
20 import com.alibaba.fastjson.JSONArray;
21 import com.alibaba.fastjson.JSONException;
22 
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.HashMap;
27 
28 class ModuleJsonUtil {
29     private static final String APP = "app";
30     private static final String BUNDLE_TYPE = "bundleType";
31     private static final String ABILITIES = "abilities";
32     private static final String VERSIONCODE = "versionCode";
33     private static final String VERSIONNAME = "versionName";
34     private static final String MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode";
35     private static final String API_VERSION = "apiVersion";
36     private static final String MIN_API_VERSION = "minAPIVersion";
37     private static final String TARGET_API_VERSION = "targetAPIVersion";
38     private static final String API_RELEASE_TYPE = "apiReleaseType";
39     private static final String DEBUG = "debug";
40     private static final String COMPATIBLE = "compatible";
41     private static final String RELEASE_TYPE = "releaseType";
42     private static final String TARGET = "target";
43     private static final String VERSION = "version";
44     private static final String CODE = "code";
45     private static final String NAME = "name";
46     private static final String MODULE = "module";
47     private static final String MODULES = "modules";
48     private static final String MODULE_NAME = "moduleName";
49     private static final String MODULE_TYPE = "moduleType";
50     private static final String DISTRO = "distro";
51     private static final String PACKAGE = "package";
52     private static final String PACKAGES = "packages";
53     private static final String SUMMARY = "summary";
54     private static final String BUNDLE_NAME = "bundleName";
55     private static final String ENTRY = "entry";
56     private static final char DOT = '.';
57     private static final String CONFIG_JSON = "config.json";
58     private static final String MODULE_JSON = "module.json";
59     private static final String DEVICE_TYPE = "deviceType";
60     private static final String DEVICE_TYPES = "deviceTypes";
61     private static final String TYPE= "type";
62     private static final String VENDOR = "vendor";
63     private static final String METADATA = "metadata";
64     private static final String RESOURCE = "resource";
65     private static final String PROFILE = "$profile:";
66     private static final String VALUE = "value";
67     private static final String JSON_PERFIX = ".json";
68     private static final String DISTRO_FILTER = "distroFilter";
69     private static final String DISTRIBUTION_FILTER = "distributionFilter";
70     private static final String DEPENDENCIES = "dependencies";
71     private static final String EXTENSION_ABILITIES = "extensionAbilities";
72     private static final String INSTALLATION_FREE = "installationFree";
73     private static final String PATCH_JSON = "patch.json";
74     private static final String PATCH_VERSION_CODE = "patchVersionCode";
75     private static final String PATCH_VERSION_NAME = "patchVersionName";
76     private static final String ORIGINAL_MODULE_HASH = "originalModuleHash";
77     private static final String EMPTY_STRING = "";
78     private static final String COMPRESS_NATIVE_LIBS = "compressNativeLibs";
79     private static final String ASAN_ENABLED = "asanEnabled";
80     private static final String ATOMIC_SERVICE = "atomicService";
81     private static final String SPLIT = "split";
82     private static final String MAIN = "main";
83     private static final String PRELOADS = "preloads";
84     private static final String SHARED = "shared";
85     private static final String REQUEST_PERMISSIONS = "requestPermissions";
86     private static final String TARGET_MODULE_NAME = "targetModuleName";
87     private static final String TARGET_PRIORITY = "targetPriority";
88     private static final String TARGET_BUNDLE_NAME = "targetBundleName";
89     private static final String DEVICE_CONFIG = "deviceConfig";
90     private static final String DEFAULT = "default";
91     private static final String COMPILE_SDK_VERSION = "compileSdkVersion";
92     private static final String COMPILE_SDK_TYPE = "compileSdkType";
93     private static final String PROXY_DATAS = "proxyDatas";
94     private static final String PROXY_DATA = "proxyData";
95     private static final String PROXY_URI = "uri";
96 
97     private static final Log LOG = new Log(ModuleJsonUtil.class.toString());
98 
99     /**
100      * get the version from json file for stage module.
101      *
102      * @param jsonString uncompress json object
103      * @return the result
104      * @throws BundleException Throws this exception if the json is not standard.
105      */
parseStageVersion(String jsonString)106     public static Version parseStageVersion(String jsonString) throws BundleException {
107         Version version = new Version();
108         JSONObject jsonObject;
109         try {
110             jsonObject = JSON.parseObject(jsonString);
111         } catch (JSONException exception) {
112             String errMsg = "parse JSONobject failed.";
113             LOG.error(errMsg);
114             throw new BundleException(errMsg);
115         }
116         if (jsonObject.containsKey(APP)) {
117             JSONObject appObj = jsonObject.getJSONObject(APP);
118             if (appObj.containsKey(VERSIONCODE) && appObj.containsKey(VERSIONNAME)) {
119                 version.versionCode = appObj.getIntValue(VERSIONCODE);
120                 version.versionName = appObj.getString(VERSIONNAME);
121             } else {
122                 String errMsg = "ModuleJsonUtil:parseStageVersion json file do not contain version.";
123                 LOG.error(errMsg);
124                 throw new BundleException(errMsg);
125             }
126             if (appObj.containsKey(MIN_COMPATIBLE_VERSION_CODE)) {
127                 version.minCompatibleVersionCode = appObj.getIntValue(MIN_COMPATIBLE_VERSION_CODE);
128             } else {
129                 version.minCompatibleVersionCode = version.versionCode;
130             }
131         } else {
132             String errMsg = "ModuleJsonUtil:parseStageVersion json file do not contain app.";
133             LOG.error(errMsg);
134             throw new BundleException(errMsg);
135         }
136 
137         return version;
138     }
139 
140     /**
141      * get the version from json file for fa module.
142      *
143      * @param jsonString uncompress json object
144      * @return the result
145      * @throws BundleException Throws this exception if the json is not standard.
146      */
parseFaVersion(String jsonString)147     public static Version parseFaVersion(String jsonString) throws BundleException {
148         Version version = new Version();
149         JSONObject jsonObject;
150         try {
151             jsonObject = JSON.parseObject(jsonString);
152         } catch (JSONException exception) {
153             String errMsg = "parse JSONobject failed.";
154             LOG.error(errMsg);
155             throw new BundleException(errMsg);
156         }
157         JSONObject appObj = jsonObject.getJSONObject(APP);
158         if (appObj == null) {
159             LOG.error("ModuleJsonUtil:parseFaVersion failed : json file do not contain app.");
160             throw new BundleException("ModuleJsonUtil:parseFaVersion failed : json file do not contain app.");
161         }
162         JSONObject versionObj = appObj.getJSONObject(VERSION);
163         if (versionObj == null) {
164             LOG.error("ModuleJsonUtil:parseFaVersion failed : json file do not version.");
165             throw new BundleException("ModuleJsonUtil:parseFaVersion failed : json file do not version.");
166         }
167         if (versionObj.containsKey(CODE) && versionObj.containsKey(NAME)) {
168             version.versionName = versionObj.getString(NAME);
169             version.versionCode = versionObj.getIntValue(CODE);
170         } else {
171             LOG.error("ModuleJsonUtil:parseFaVersion failed : json file do not version name or version code.");
172             throw new BundleException(
173                 "ModuleJsonUtil:parseFaVersion failed : json file do not version name or version code.");
174         }
175         if (versionObj.containsKey(MIN_COMPATIBLE_VERSION_CODE)) {
176             version.minCompatibleVersionCode = versionObj.getIntValue(MIN_COMPATIBLE_VERSION_CODE);
177         } else {
178             version.minCompatibleVersionCode = version.versionCode;
179         }
180         return version;
181     }
182 
183     /**
184      * get the bundleType fa module.
185      *
186      * @param jsonString uncompress json object
187      * @return the result
188      * @throws BundleException Throws this exception if the json is not standard.
189      */
parseFaBundleType(String jsonString)190     public static String parseFaBundleType(String jsonString) throws BundleException {
191         boolean installationFree = parseFAInstallationFree(jsonString);
192         if (installationFree) {
193             return ATOMIC_SERVICE;
194         }
195         return APP;
196     }
197 
198     /**
199      * get the apiVersion from json file for stage module.
200      *
201      * @param jsonString uncompress json object
202      * @return the result
203      * @throws BundleException Throws this exception if the json is not standard.
204      */
parseStageModuleApiVersion(String jsonString)205     public static ModuleApiVersion parseStageModuleApiVersion(String jsonString) throws BundleException {
206         JSONObject jsonObject;
207         try {
208             jsonObject = JSON.parseObject(jsonString);
209         } catch (JSONException exception) {
210             String errMsg = "parse JSONobject failed.";
211             LOG.error(errMsg);
212             throw new BundleException(errMsg);
213         }
214         JSONObject appObj = jsonObject.getJSONObject(APP);
215         if (appObj == null) {
216             LOG.error("ModuleJsonUtil:parseFaVersion failed : json file do not contain app.");
217             throw new BundleException("ModuleJsonUtil:parseFaVersion failed : json file do not contain app.");
218         }
219         ModuleApiVersion moduleApiVersion = new ModuleApiVersion();
220         if (appObj.containsKey(MIN_API_VERSION)) {
221             moduleApiVersion.setCompatibleApiVersion(appObj.getIntValue(MIN_API_VERSION));
222         }
223         if (appObj.containsKey(TARGET_API_VERSION)) {
224             moduleApiVersion.setTargetApiVersion(appObj.getIntValue(TARGET_API_VERSION));
225         }
226         if (appObj.containsKey(API_RELEASE_TYPE)) {
227             moduleApiVersion.setReleaseType(appObj.getString(API_RELEASE_TYPE));
228         }
229         return moduleApiVersion;
230     }
231 
232     /**
233      * get the apiVersion from json file for fa module.
234      *
235      * @param jsonString uncompress json object
236      * @return the result
237      * @throws BundleException Throws this exception if the json is not standard.
238      */
parseFAModuleApiVersion(String jsonString)239     public static ModuleApiVersion parseFAModuleApiVersion(String jsonString) throws BundleException {
240         JSONObject jsonObject;
241         try {
242             jsonObject = JSON.parseObject(jsonString);
243         } catch (JSONException exception) {
244             String errMsg = "parse JSONobject failed";
245             LOG.error(errMsg);
246             throw new BundleException(errMsg);
247         }
248         JSONObject appObj = jsonObject.getJSONObject(APP);
249         if (appObj == null) {
250             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain app.");
251         }
252         if (!appObj.containsKey(API_VERSION)) {
253             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain apiVersion.");
254         }
255         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
256         ModuleApiVersion moduleApiVersion = new ModuleApiVersion();
257         if (apiVersionObj.containsKey(COMPATIBLE)) {
258             moduleApiVersion.setCompatibleApiVersion(apiVersionObj.getIntValue(COMPATIBLE));
259         }
260         if (apiVersionObj.containsKey(RELEASE_TYPE)) {
261             moduleApiVersion.setReleaseType(apiVersionObj.getString(RELEASE_TYPE));
262         }
263         if (apiVersionObj.containsKey(TARGET)) {
264             moduleApiVersion.setTargetApiVersion(apiVersionObj.getIntValue(TARGET));
265         }
266         return moduleApiVersion;
267     }
268 
269     /**
270      * get the module name from json file for stage module.
271      *
272      * @param jsonString uncompress json object
273      * @return the result
274      * @throws BundleException Throws this exception if the json is not standard.
275      */
parseStageModuleName(String jsonString)276     public static String parseStageModuleName(String jsonString) throws BundleException {
277         String moduleName = "";
278         JSONObject jsonObject;
279         try {
280             jsonObject = JSON.parseObject(jsonString);
281             JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
282             if (moduleObj == null) {
283                 LOG.error("ModuleJsonUtil:parseStageModuleName failed: json file do not contain module.");
284                 throw new BundleException("ModuleJsonUtil:parseStageModuleName failed: json file do not contain module.");
285             }
286             if (moduleObj.containsKey(NAME)) {
287                 moduleName = moduleObj.getString(NAME);
288             } else {
289                 LOG.error("ModuleJsonUtil:parseStageModuleName failed: json file do not contain module name.");
290                 throw new BundleException("ModuleJsonUtil:parseStageModuleName failed: json file do not contain module name.");
291             }
292         } catch (BundleException e) {
293             LOG.error("ModuleJsonUtil:parseStageModuleName failed.");
294             throw new BundleException("ModuleJsonUtil:parseStageModuleName failed.");
295         }
296         return moduleName;
297     }
298 
299     /**
300      * get the moduleName from json file for stage module.
301      *
302      * @param jsonString uncompress json object
303      * @return the result
304      * @throws BundleException Throws this exception if the json is not standard.
305      */
parseFaModuleName(String jsonString)306     public static String parseFaModuleName(String jsonString) throws BundleException {
307         String moduleName = "";
308         JSONObject jsonObject;
309         try {
310             jsonObject = JSON.parseObject(jsonString);
311             JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
312             if (moduleObj == null) {
313                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain module.");
314                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain module.");
315             }
316             JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
317             if (distroObj == null) {
318                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
319                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
320             }
321             if (!distroObj.containsKey(MODULE_NAME)) {
322                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain moduleName.");
323                 throw new BundleException(
324                     "ModuleJsonUtil:parseFaModuleName failed: json file do not contain moduleName.");
325             }
326             moduleName = distroObj.getString(MODULE_NAME);
327         } catch (BundleException | JSONException e) {
328             LOG.error("ModuleJsonUtil:parseFaModuleName failed");
329             throw new BundleException("ModuleJsonUtil:parseFaModuleName failed");
330         }
331         return moduleName;
332     }
333 
334     /**
335      * get the moduleName from json file for hqf.
336      *
337      * @param jsonString uncompress json object
338      * @return the result
339      * @throws BundleException Throws this exception if the json is not standard.
340      */
parsePatchModuleName(String jsonString)341     public static String parsePatchModuleName(String jsonString) throws BundleException {
342         String moduleName = "";
343         JSONObject jsonObject;
344         try {
345             jsonObject = JSON.parseObject(jsonString);
346             JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
347             if (moduleObj == null) {
348                 LOG.error("ModuleJsonUtil:parsePatchModuleName failed: json file do not contain module.");
349                 throw new BundleException(
350                     "ModuleJsonUtil:parsePatchModuleName failed: json file do not contain module.");
351             }
352             if (!moduleObj.containsKey(NAME)) {
353                 LOG.error("ModuleJsonUtil:parsePatchModuleName failed: json file do not contain moduleName.");
354                 throw new BundleException(
355                         "ModuleJsonUtil:parsePatchModuleName failed: json file do not contain moduleName.");
356             }
357             moduleName = moduleObj.getString(NAME);
358         } catch (BundleException | JSONException e) {
359             LOG.error("ModuleJsonUtil:parseFaModuleName failed");
360             throw new BundleException("ModuleJsonUtil:parseFaModuleName failed");
361         }
362         return moduleName;
363     }
364 
365     /**
366      * get the package from json file for stage module.
367      *
368      * @param jsonString uncompress json object
369      * @return the result
370      * @throws BundleException Throws this exception if the json is not standard.
371      */
parseFaPackageStr(String jsonString)372     public static String parseFaPackageStr(String jsonString) throws BundleException {
373         String packageStr = "";
374         JSONObject jsonObject;
375         try {
376             jsonObject = JSON.parseObject(jsonString);
377             JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
378             if (moduleObj == null) {
379                 LOG.error("ModuleJsonUtil:parseFaPackageStr failed: json file do not contain module.");
380                 throw new BundleException("ModuleJsonUtil:parseFaPackageStr failed: json file do not contain module.");
381             }
382             if (moduleObj.containsKey(PACKAGE)) {
383                 packageStr = moduleObj.getString(PACKAGE);
384             } else {
385                 LOG.error("ModuleJsonUtil:parseFaPackageStr failed: json file do not contain package.");
386                 throw new BundleException("ModuleJsonUtil:parseFaPackageStr failed: json file do not contain package.");
387             }
388         } catch (BundleException | JSONException e) {
389             LOG.error("ModuleJsonUtil:parseFaPackageStr failed.");
390             throw new BundleException("ModuleJsonUtil:parseFaPackageStr failed.");
391         }
392         return packageStr;
393     }
394 
395     /**
396      * get the bundleName from json file.
397      *
398      * @param jsonString uncompress json object
399      * @return the result
400      * @throws BundleException Throws this exception if the json is not standard.
401      */
parseBundleName(String jsonString)402     public static String parseBundleName(String jsonString) throws BundleException {
403         JSONObject jsonObject;
404         try {
405             jsonObject = JSON.parseObject(jsonString);
406         } catch (JSONException exception) {
407             String errMsg = "parse JSONobject failed.";
408             LOG.error(errMsg);
409             throw new BundleException(errMsg);
410         }
411         JSONObject appObject = jsonObject.getJSONObject(APP);
412         if (appObject == null) {
413             LOG.error("ModuleJsonUtil::parseStageBundleName json object do not contain app.");
414             throw new BundleException("ModuleJsonUtil::parseStageBundleName json object do not contain app.");
415         }
416         String bundleName = "";
417         if (appObject.containsKey(BUNDLE_NAME)) {
418             bundleName = appObject.getString(BUNDLE_NAME);
419         } else {
420             LOG.error("ModuleJsonUtil::parseStageBundleName json object do not contain bundleNames.");
421             throw new BundleException("ModuleJsonUtil::parseStageBundleName json object do not contain bundleNames.");
422         }
423         return bundleName;
424     }
425 
426     /**
427      * get the vendor from json file.
428      *
429      * @param jsonString uncompress json object
430      * @return the result
431      * @throws BundleException Throws this exception if the json is not standard.
432      */
parseVendor(String jsonString)433     public static String parseVendor(String jsonString) throws BundleException {
434         JSONObject jsonObject;
435         try {
436             jsonObject = JSON.parseObject(jsonString);
437         } catch (JSONException exception) {
438             String errMsg = "parse JSONobject failed.";
439             LOG.error(errMsg);
440             throw new BundleException(errMsg);
441         }
442         JSONObject appObject = jsonObject.getJSONObject(APP);
443         if (appObject == null) {
444             LOG.error("ModuleJsonUtil::parseStageBundleName json object do not contain app.");
445             throw new BundleException("ModuleJsonUtil::parseStageBundleName json object do not contain app.");
446         }
447         String vendor = "";
448         if (appObject.containsKey(VENDOR)) {
449             vendor = appObject.getString(VENDOR);
450         } else {
451             return vendor;
452         }
453         return vendor;
454     }
455 
456     /**
457      * merge two pack.info file into one pack.info file.
458      *
459      * @param finalPackInfo is the final packInfo
460      * @param srcPackInfo is the packInfo to be merged
461      * @return the result
462      * @throws BundleException Throws this exception if the json is not standard.
463      */
mergeTwoPackInfo(String finalPackInfo, String srcPackInfo)464     public static String mergeTwoPackInfo(String finalPackInfo, String srcPackInfo) throws BundleException {
465         String desPackInfo = "";
466         JSONObject finalPackObj;
467         try {
468             finalPackObj = JSON.parseObject(finalPackInfo);
469             JSONObject srcPackObj = JSON.parseObject(srcPackInfo);
470             if (!verifyPackInfo(finalPackObj, srcPackObj)) {
471                 LOG.error("ModuleJsonUtil:mergeTwoPackInfo verify pack.info failed.");
472                 throw new BundleException("ModuleJsonUtil:mergeTwoPackInfo verify pack.info failed.");
473             }
474             desPackInfo = mergePackInfoObj(finalPackObj, srcPackObj);
475         } catch (BundleException | JSONException e) {
476             LOG.error("ModuleJsonUtil:mergeTwoPackInfo merge pack.info failed.");
477             throw new BundleException("ModuleJsonUtil:mergeTwoPackInfo merge pack.info failed.");
478         }
479         return desPackInfo;
480     }
481 
482     /**
483      * verify pack.info file.
484      *
485      * @param finalPackObj is the final pack.info object
486      * @param srcPackObj is the src pack.info object
487      * @return the result
488      */
verifyPackInfo(JSONObject finalPackObj, JSONObject srcPackObj)489     public static boolean verifyPackInfo(JSONObject finalPackObj, JSONObject srcPackObj) throws BundleException {
490         if (finalPackObj == null || srcPackObj == null) {
491             LOG.error("ModuleJsonUtil:verifyPackInfo fail to read pack.info.");
492             return false;
493         }
494         JSONObject finalSummaryObj = finalPackObj.getJSONObject(SUMMARY);
495         JSONObject srcSummaryObj = srcPackObj.getJSONObject(SUMMARY);
496         if (finalSummaryObj == null || srcSummaryObj == null) {
497             LOG.error("ModuleJsonUtil:verifyPackInfo pack.info do not contain summary.");
498             return false;
499         }
500         // check app info
501         JSONObject finalAppObj = finalSummaryObj.getJSONObject(APP);
502         JSONObject srcAppObj = srcSummaryObj.getJSONObject(APP);
503         if (finalAppObj == null || srcAppObj == null) {
504             LOG.error("ModuleJsonUtil:verifyPackInfo pack.info do not contain app.");
505             return false;
506         }
507         if (!verifyAppInPackInfo(finalAppObj, srcAppObj)) {
508             LOG.error("ModuleJsonUtil:verifyPackInfo verify app failed.");
509             return false;
510         }
511 
512         return true;
513     }
514 
515     /**
516      * verify app in pack.info file.
517      *
518      * @param finalAppObj is the final pack.info app object
519      * @param srcAppObj is the src pack.info app object
520      * @return the result
521      */
verifyAppInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj)522     public static boolean verifyAppInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj) {
523         if (finalAppObj == null || srcAppObj == null) {
524             LOG.error("ModuleJsonUtil:verifyAppInPackInfo input null json object.");
525             return false;
526         }
527         // check bundleName
528         String finalBundleName = finalAppObj.getString(BUNDLE_NAME);
529         String srcBundleName = srcAppObj.getString(BUNDLE_NAME);
530         if (!finalBundleName.equals(srcBundleName)) {
531             LOG.error("ModuleJsonUtil:verifyAppInPackInfo bundleName is different.");
532             return false;
533         }
534         // check bundleType
535         if (!checkBundleTypeInPackInfo(finalAppObj, srcAppObj)) {
536             LOG.error("ModuleJsonUtil:verifyAppInPackInfo bundleType is different.");
537             return false;
538         }
539         // check version
540         JSONObject finalVersionObj = finalAppObj.getJSONObject(VERSION);
541         JSONObject srcVersionObj = srcAppObj.getJSONObject(VERSION);
542         if (finalVersionObj == null || srcVersionObj == null) {
543             LOG.error("ModuleJsonUtil:verifyAppInPackInfo version object is empty.");
544             return false;
545         }
546         String finalVersionName = finalVersionObj.getString(NAME);
547         String srcVersionName = srcVersionObj.getString(NAME);
548         if (!finalVersionName.equals(srcVersionName)) {
549             LOG.error("ModuleJsonUtil:verifyAppInPackInfo versionName is different.");
550             return false;
551         }
552         int finalVersionCode = finalVersionObj.getIntValue(CODE);
553         int srcVersionCode = srcVersionObj.getIntValue(CODE);
554         if (finalVersionCode != srcVersionCode) {
555             LOG.error("ModuleJsonUtil:verifyAppInPackInfo versionCode is different.");
556             return false;
557         }
558         return true;
559     }
560 
561     /**
562      * verify bundleType in pack.info file.
563      *
564      * @param finalAppObj is the final pack.info app objects
565      * @param srcAppObj is the src pack.info app objects
566      * @return the result
567      */
checkBundleTypeInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj)568     public static boolean checkBundleTypeInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj) {
569         if (finalAppObj.isEmpty() || srcAppObj.isEmpty()) {
570             LOG.error("ModuleJsonUtil:checkBundleTypeInPackInfo pack.info has empty module.");
571             return false;
572         }
573         String finalBundleType = "app";
574         String srcBundleType = "app";
575         if (finalAppObj.containsKey(BUNDLE_TYPE)) {
576             finalBundleType = getJsonString(finalAppObj, BUNDLE_TYPE);
577         }
578         if (srcAppObj.containsKey(BUNDLE_TYPE)) {
579             srcBundleType = getJsonString(srcAppObj, BUNDLE_TYPE);
580         }
581         if (!finalBundleType.equals(srcBundleType)) {
582             LOG.error("bundleType in pack.info is not same.");
583             return false;
584         }
585         return true;
586     }
587 
588     /**
589      * verify module in pack.info file.
590      *
591      * @param finalModuleObs is the final pack.info module objects
592      * @param srcModuleObs is the src pack.info module objects
593      * @return the result
594      */
verifyModuleInPackInfo(JSONArray finalModuleObs, JSONArray srcModuleObs)595     public static boolean verifyModuleInPackInfo(JSONArray finalModuleObs, JSONArray srcModuleObs)
596             throws BundleException {
597         if (finalModuleObs.isEmpty() || srcModuleObs.isEmpty()) {
598             LOG.error("ModuleJsonUtil:verifyModuleInPackInfo pack.info has empty module.");
599             throw new BundleException("ModuleJsonUtil:verifyModuleInPackInfo pack.info has empty module.");
600         }
601         List<String> moduleNames = new ArrayList<>();
602         for (int i = 0; i < finalModuleObs.size(); ++i) {
603             JSONObject finalModuleObj = finalModuleObs.getJSONObject(i);
604             String moduleName = parseDistroModuleName(finalModuleObj);
605             if (moduleNames.contains(moduleName)) {
606                 LOG.error("ModuleJsonUtil:verifyModuleInPackInfo duplicated moduleName.");
607                 return false;
608             } else {
609                 moduleNames.add(moduleName);
610             }
611         }
612         for (int i = 0; i < srcModuleObs.size(); ++i) {
613             JSONObject srcModuleObj = srcModuleObs.getJSONObject(i);
614             String moduleName = parseDistroModuleName(srcModuleObj);
615             if (moduleNames.contains(moduleName)) {
616                 LOG.error("ModuleJsonUtil:verifyModuleInPackInfo duplicated moduleName.");
617                 return false;
618             } else {
619                 moduleNames.add(moduleName);
620             }
621         }
622         return true;
623     }
624 
625     /**
626      * verify package name in pack.info file.
627      *
628      * @param finalPackageObs is the final pack.info objects
629      * @param srcPackageObs is the src pack.info objects
630      * @return the result
631      */
verifyPackageName(JSONArray finalPackageObs, JSONArray srcPackageObs)632     public static boolean verifyPackageName(JSONArray finalPackageObs, JSONArray srcPackageObs) {
633         if (finalPackageObs == null || finalPackageObs.isEmpty() || srcPackageObs == null || srcPackageObs.isEmpty()) {
634             LOG.error("ModuleJsonUtil:verifyPackageName pack.info has empty packages.");
635             return false;
636         }
637         List<String> packageNames = new ArrayList<>();
638         for (int i = 0; i < finalPackageObs.size(); ++i) {
639             JSONObject packageObj = finalPackageObs.getJSONObject(i);
640             String packageName = packageObj.getString(NAME);
641             if (packageNames.contains(packageName)) {
642                 LOG.error("ModuleJsonUtil:verifyPackageName duplicated package name.");
643                 return false;
644             } else {
645                 packageNames.add(packageName);
646             }
647         }
648         for (int i = 0; i < srcPackageObs.size(); ++i) {
649             JSONObject packageObj = srcPackageObs.getJSONObject(i);
650             String packageName = packageObj.getString(NAME);
651             if (packageNames.contains(packageName)) {
652                 LOG.error("ModuleJsonUtil:verifyPackageName duplicated package name.");
653                 return false;
654             } else {
655                 packageNames.add(packageName);
656             }
657         }
658         return true;
659     }
660 
661     /**
662      * parse moduleName in pack.info file.
663      *
664      * @param moduleObj is the final pack.info objects
665      * @return the result
666      */
parseDistroModuleName(JSONObject moduleObj)667     public static String parseDistroModuleName(JSONObject moduleObj) throws BundleException {
668         String moduleName = "";
669         try {
670             if (moduleObj == null) {
671                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain module.");
672                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain module.");
673             }
674             JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
675             if (distroObj == null) {
676                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
677                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
678             }
679             if (!distroObj.containsKey(MODULE_NAME)) {
680                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain moduleName.");
681                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain moduleName.");
682             }
683             moduleName = distroObj.getString(MODULE_NAME);
684         } catch (BundleException e) {
685             LOG.error("ModuleJsonUtil:parseFaModuleName failed.");
686             throw new BundleException("ModuleJsonUtil:parseFaModuleName failed.");
687         }
688         return moduleName;
689     }
690 
691     /**
692      * merge pack.info file.
693      *
694      * @param finalPackinfoObj is the final pack.info objects
695      * @param srcPackinfoObj is the final pack.info objects
696      * @return the result
697      */
mergePackInfoObj(JSONObject finalPackinfoObj, JSONObject srcPackinfoObj)698     public static String mergePackInfoObj(JSONObject finalPackinfoObj, JSONObject srcPackinfoObj) throws BundleException {
699         if (finalPackinfoObj == null || srcPackinfoObj == null) {
700             String errMsg = "ModuleJsonUtil:mergePackInfoObj input an invalid json object.";
701             LOG.error(errMsg);
702             throw new BundleException(errMsg);
703         }
704         JSONObject finalSummaryObj = finalPackinfoObj.getJSONObject(SUMMARY);
705         JSONObject srcSummaryObj = srcPackinfoObj.getJSONObject(SUMMARY);
706         if (finalSummaryObj == null || srcSummaryObj == null) {
707             String errMsg = "ModuleJsonUtil:mergePackInfoObj input json file has empty summary.";
708             LOG.error(errMsg);
709             throw new BundleException(errMsg);
710         }
711         // merge modules
712         JSONArray finalModuleObs = finalSummaryObj.getJSONArray(MODULES);
713         JSONArray srcModuleObs = srcSummaryObj.getJSONArray(MODULES);
714         if (finalModuleObs == null || srcModuleObs == null) {
715             String errMsg = "ModuleJsonUtil:mergePackInfoObj input json file has empty module.";
716             LOG.error(errMsg);
717             throw new BundleException(errMsg);
718         }
719         finalModuleObs.addAll(srcModuleObs);
720         // merge packages
721         JSONArray finalPackageObs = finalPackinfoObj.getJSONArray(PACKAGES);
722         JSONArray srcPackageObs = srcPackinfoObj.getJSONArray(PACKAGES);
723         if (finalPackageObs == null || srcPackageObs == null) {
724             String errMsg = "ModuleJsonUtil:mergePackInfoObj input json file has empty packages.";
725             LOG.error(errMsg);
726             throw new BundleException(errMsg);
727         }
728         finalPackageObs.addAll(srcPackageObs);
729         return finalPackinfoObj.toString();
730     }
731 
732     /**
733      * merge two pack.info file into one pack.info file by packagePair.
734      *
735      * @param finalPackInfo is the final packInfo
736      * @param srcPackInfo is the packInfo to be merged
737      * @param packagePair is the selected packageName-moduleName pair map
738      * @return the result
739      * @throws BundleException Throws this exception if the json is not standard.
740      */
mergeTwoPackInfoByPackagePair(String finalPackInfo, String srcPackInfo, HashMap<String, String> packagePair)741     public static String mergeTwoPackInfoByPackagePair(String finalPackInfo, String srcPackInfo,
742                                                        HashMap<String, String> packagePair) throws BundleException {
743         JSONObject finalPackObj;
744         JSONObject srcPackObj;
745         try {
746             finalPackObj = JSON.parseObject(finalPackInfo);
747             srcPackObj = JSON.parseObject(srcPackInfo);
748         } catch (JSONException exception) {
749             String errMsg = "parse JSONobject failed.";
750             LOG.error(errMsg);
751             throw new BundleException(errMsg);
752         }
753         // verify app in pack.info
754         JSONObject finalSummaryObj = finalPackObj.getJSONObject(SUMMARY);
755         JSONObject finalAppObj = finalSummaryObj.getJSONObject(APP);
756         JSONObject srcSummaryObj = srcPackObj.getJSONObject(SUMMARY);
757         JSONObject srcAppObj = srcSummaryObj.getJSONObject(APP);
758         if (!verifyAppInPackInfo(finalAppObj, srcAppObj)) {
759             String errMsg = "verify pack.info failed, different version, bundleType or bundleName.";
760             LOG.error(errMsg);
761             throw new BundleException(errMsg);
762         }
763         for (HashMap.Entry<String, String> entry : packagePair.entrySet()) {
764             String packageName = entry.getKey().substring(0, entry.getKey().lastIndexOf(DOT));
765             mergeTwoPackInfoObjByPackagePair(finalPackObj, srcPackObj, packageName, entry.getValue());
766         }
767         return finalPackObj.toString();
768     }
769 
770     /**
771      * merge two pack.info file into one pack.info file by packagePair.
772      *
773      * @param finalPackObj is the final packInfo
774      * @param srcPackObj is the packInfo to be merged
775      * @param packageName is the selected packageName
776      * @param moduleName is the selected moduleName
777      * @throws BundleException Throws this exception if the json is not standard.
778      */
mergeTwoPackInfoObjByPackagePair(JSONObject finalPackObj, JSONObject srcPackObj, String packageName, String moduleName)779     public static void mergeTwoPackInfoObjByPackagePair(JSONObject finalPackObj, JSONObject srcPackObj,
780                                                         String packageName, String moduleName) throws BundleException {
781         if (finalPackObj == null || srcPackObj == null) {
782             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair failed: pack.info is not json object.";
783             LOG.error(errMsg);
784             throw new BundleException(errMsg);
785         }
786         // merge module
787         JSONObject finalSummaryObj = finalPackObj.getJSONObject(SUMMARY);
788         JSONObject srcSummaryObj = srcPackObj.getJSONObject(SUMMARY);
789         if (finalSummaryObj == null || srcSummaryObj == null) {
790             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair failed: pack.info do not contain summary.";
791             LOG.error(errMsg);
792             throw new BundleException(errMsg);
793         }
794         JSONArray finalModules = finalSummaryObj.getJSONArray(MODULES);
795         JSONArray srcModules = srcSummaryObj.getJSONArray(MODULES);
796         if (finalModules == null || srcModules == null) {
797             LOG.error("ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair input json file has empty module.");
798             throw new
799                 BundleException("ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair input json file has empty module.");
800         }
801         boolean findModule = false;
802         for (int index = 0; index < srcModules.size(); ++index) {
803             JSONObject moduleObj = srcModules.getJSONObject(index);
804             JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
805             if (distroObj.getString(MODULE_NAME).equals(moduleName)) {
806                 finalModules.add(moduleObj);
807                 findModule = true;
808                 break;
809             }
810         }
811         if (!findModule) {
812             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair" +
813                     " input json do not contain " + moduleName + ".";
814             LOG.error(errMsg);
815             throw new BundleException(errMsg);
816         }
817         // merge package
818         JSONArray finalPackages = finalPackObj.getJSONArray(PACKAGES);
819         JSONArray srcPackages = srcPackObj.getJSONArray(PACKAGES);
820         if (finalPackages == null || srcPackages == null) {
821             String errMsg =
822                 "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair failed: pack.info do not contain packages.";
823             LOG.error(errMsg);
824             throw new BundleException(errMsg);
825         }
826         boolean findPackage = false;
827         for (int index = 0; index < srcPackages.size(); ++index) {
828             JSONObject srcPackageObj = srcPackages.getJSONObject(index);
829             if (srcPackageObj.getString(NAME).equals(packageName)) {
830                 finalPackages.add(srcPackageObj);
831                 findPackage = true;
832                 break;
833             }
834         }
835         if (!findPackage) {
836             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair input json do not contain "
837                     + packageName + ".";
838             LOG.error(errMsg);
839             throw new BundleException(errMsg);
840         }
841     }
842 
843     /**
844      * parse FA hap is entry hap, if it is, return device type.
845      *
846      * @param hapPath is the path of hap.
847      *
848      * @throws BundleException FileNotFoundException|IOException.
849      */
parseFaEntry(String hapPath)850     public static List<String> parseFaEntry(String hapPath) throws BundleException {
851         String configJson = FileUtils.getJsonInZips(new File(hapPath), CONFIG_JSON);
852         JSONObject faObj;
853         try {
854             faObj = JSONObject.parseObject(configJson);
855         } catch (JSONException exception) {
856             String errMsg = "parse JSONobject failed.";
857             LOG.error(errMsg);
858             throw new BundleException(errMsg);
859         }
860         JSONObject moduleObj = faObj.getJSONObject(MODULE);
861         if (moduleObj == null) {
862             String errMSg = "ModuleJsonUtil::isFaEntry error, json do not contain module.";
863             LOG.error(errMSg);
864             throw new BundleException(errMSg);
865         }
866         List<String> deviceTypes = new ArrayList<>();
867         JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
868         if (distroObj == null) {
869             String errMSg = "ModuleJsonUtil::isFaEntry error, json do not contain distro.";
870             LOG.error(errMSg);
871             throw new BundleException(errMSg);
872         }
873         String moduleType = distroObj.getString(MODULE_TYPE);
874         if (ENTRY.equals(moduleType)) {
875             deviceTypes = getDeviceTypeFromFAModule(moduleObj);
876         }
877         return deviceTypes;
878     }
879 
880     /**
881      * parse stage hap is entry hap, if it is, record device type.
882      *
883      * @param hapPath is the path of hap.
884      * @throws BundleException FileNotFoundException|IOException.
885      */
parseStageEntry(String hapPath)886     public static List<String> parseStageEntry(String hapPath) throws BundleException {
887         String moduleJson = FileUtils.getJsonInZips(new File(hapPath), MODULE_JSON);
888         JSONObject stageObj;
889         try {
890             stageObj = JSONObject.parseObject(moduleJson);
891         } catch (JSONException exception) {
892             String errMsg = "parse JSONobject failed.";
893             LOG.error(errMsg);
894             throw new BundleException(errMsg);
895         }
896         JSONObject moduleObj = stageObj.getJSONObject(MODULE);
897         if (moduleObj == null) {
898             String errMSg = "ModuleJsonUtil::isFaEntry error, json do not contain module.";
899             LOG.error(errMSg);
900             throw new BundleException(errMSg);
901         }
902         List<String> deviceTypes = new ArrayList<>();
903         String type = moduleObj.getString(TYPE);
904         if (type != null && type.equals(ENTRY)) {
905             deviceTypes = getDeviceTypesFromStageModule(moduleObj);
906         }
907 
908         return deviceTypes;
909     }
910 
911     /**
912      * get deviceType from fa module.
913      *
914      * @param moduleObj is the object of module.
915      * @return true is for entry hap  and false is other kind of hap
916      */
getDeviceTypeFromFAModule(JSONObject moduleObj)917     public static List<String> getDeviceTypeFromFAModule(JSONObject moduleObj) {
918         List<String> deviceTypes = new ArrayList<>();
919         if (moduleObj == null) {
920             return deviceTypes;
921         }
922         deviceTypes = JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPE), String.class);
923         return deviceTypes;
924     }
925 
926     /**
927      * get deviceType from stage module.
928      *
929      * @param moduleObj is the object of module.
930      * @return true is for entry hap  and false is other kind of hap
931      */
getDeviceTypesFromStageModule(JSONObject moduleObj)932     public static List<String> getDeviceTypesFromStageModule(JSONObject moduleObj) {
933         List<String> deviceTypes = new ArrayList<>();
934         if (moduleObj == null) {
935             return deviceTypes;
936         }
937         deviceTypes = JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPES), String.class);
938         return deviceTypes;
939     }
940 
941     /**
942      * parse stage hapVerifyInfo.
943      *
944      * @param hapVerifyInfo is the parse result
945      * @throws BundleException Throws this exception if the json is not standard.
946      */
parseStageHapVerifyInfo(HapVerifyInfo hapVerifyInfo)947     public static void parseStageHapVerifyInfo(HapVerifyInfo hapVerifyInfo) throws BundleException {
948         if (hapVerifyInfo.getProfileStr().isEmpty()) {
949             throw new BundleException("ModuleJsonUtil::parseStageHapVerifyInfo failed, module.json is empty.");
950         }
951         String bundleName = parseBundleName(hapVerifyInfo.getProfileStr());
952         hapVerifyInfo.setBundleName(bundleName);
953         hapVerifyInfo.setVendor(parseVendor(hapVerifyInfo.getProfileStr()));
954         hapVerifyInfo.setVersion(parseStageVersion(hapVerifyInfo.getProfileStr()));
955         hapVerifyInfo.setApiVersion(parseStageModuleApiVersion(hapVerifyInfo.getProfileStr()));
956         hapVerifyInfo.setModuleName(parseStageModuleName(hapVerifyInfo.getProfileStr()));
957         List<ModuleMetadataInfo> moduleMetadataInfos =
958                 parseModuleAllMetadata(hapVerifyInfo.getProfileStr(), hapVerifyInfo.getResourceMap());
959         hapVerifyInfo.setDistroFilter(parseStageDistroFilter(moduleMetadataInfos));
960         hapVerifyInfo.setDeviceType(parseDeviceType(hapVerifyInfo.getProfileStr()));
961         hapVerifyInfo.setAbilityNames(parseAbilityNames(hapVerifyInfo.getProfileStr()));
962         List<String> extensionAbilityNames = parseExtensionAbilityName(hapVerifyInfo.getProfileStr());
963         hapVerifyInfo.addAbilityNames(extensionAbilityNames);
964         hapVerifyInfo.setModuleType(parseModuleType(hapVerifyInfo.getProfileStr()));
965         hapVerifyInfo.setDependencyItemList(parseDependencies(hapVerifyInfo.getProfileStr(), bundleName));
966         hapVerifyInfo.setInstallationFree(parseStageInstallation(hapVerifyInfo.getProfileStr()));
967         hapVerifyInfo.setBundleType(parseStageBundleType(hapVerifyInfo.getProfileStr()));
968         hapVerifyInfo.setPreloadItems(parseAtomicServicePreloads(hapVerifyInfo.getProfileStr()));
969         hapVerifyInfo.setTargetBundleName(parseTargetBundleName(hapVerifyInfo.getProfileStr()));
970         hapVerifyInfo.setTargetPriority(parseTargetPriority(hapVerifyInfo.getProfileStr()));
971         hapVerifyInfo.setTargetModuleName(parseTargetModuleName(hapVerifyInfo.getProfileStr()));
972         hapVerifyInfo.setTargetModulePriority(parseTargetModulePriority(hapVerifyInfo.getProfileStr()));
973         hapVerifyInfo.setDebug(getDebug(hapVerifyInfo.getProfileStr()));
974         hapVerifyInfo.setCompileSdkType(getCompileSdkType(hapVerifyInfo.getProfileStr()));
975         hapVerifyInfo.setCompileSdkVersion(getCompileSdkVersion(hapVerifyInfo.getProfileStr()));
976         hapVerifyInfo.setProxyDataUris(parseProxyDataUri(hapVerifyInfo.getProfileStr()));
977     }
978 
979     /**
980      * parse FA hapVerifyInfo.
981      *
982      * @param hapVerifyInfo is the parse result
983      * @throws BundleException Throws this exception if the json is not standard.
984      */
parseFAHapVerifyInfo(HapVerifyInfo hapVerifyInfo)985     public static void parseFAHapVerifyInfo(HapVerifyInfo hapVerifyInfo) throws BundleException {
986         if (hapVerifyInfo.getProfileStr().isEmpty()) {
987             LOG.error("ModuleJsonUtil::parseFAHapVerifyInfo failed, config.json is empty.");
988             throw new BundleException("ModuleJsonUtil::parseFAHapVerifyInfo failed, config.json is empty.");
989         }
990         String bundleName = parseBundleName(hapVerifyInfo.getProfileStr());
991         hapVerifyInfo.setBundleName(bundleName);
992         hapVerifyInfo.setBundleType(parseFaBundleType(hapVerifyInfo.getProfileStr()));
993         hapVerifyInfo.setVendor(parseVendor(hapVerifyInfo.getProfileStr()));
994         hapVerifyInfo.setVersion(parseFaVersion(hapVerifyInfo.getProfileStr()));
995         hapVerifyInfo.setApiVersion(parseFAModuleApiVersion(hapVerifyInfo.getProfileStr()));
996         hapVerifyInfo.setModuleName(parseFaModuleName(hapVerifyInfo.getProfileStr()));
997         hapVerifyInfo.setDistroFilter(parseFADistroFilter(hapVerifyInfo.getProfileStr()));
998         hapVerifyInfo.setDeviceType(parseDeviceType(hapVerifyInfo.getProfileStr()));
999         hapVerifyInfo.setAbilityNames(parseAbilityNames(hapVerifyInfo.getProfileStr()));
1000         hapVerifyInfo.setModuleType(parseFAIsEntry(hapVerifyInfo.getProfileStr()));
1001         hapVerifyInfo.setPackageName(parseFaPackageStr(hapVerifyInfo.getProfileStr()));
1002         hapVerifyInfo.setDependencyItemList(parseDependencies(hapVerifyInfo.getProfileStr(), bundleName));
1003         hapVerifyInfo.setInstallationFree(parseFAInstallationFree(hapVerifyInfo.getProfileStr()));
1004         hapVerifyInfo.setDebug(getFADebug(hapVerifyInfo.getProfileStr()));
1005         hapVerifyInfo.setCompileSdkType(getFACompileSdkType(hapVerifyInfo.getProfileStr()));
1006         hapVerifyInfo.setCompileSdkVersion(getFACompileSdkVersion(hapVerifyInfo.getProfileStr()));
1007     }
1008 
1009     /**
1010      * parse stage distroFilter.
1011      *
1012      * @param moduleMetadataInfos all metadata of module
1013      * @return DistroFilter is the result of parsed distroFilter
1014      */
parseStageDistroFilter(List<ModuleMetadataInfo> moduleMetadataInfos)1015     public static DistroFilter parseStageDistroFilter(List<ModuleMetadataInfo> moduleMetadataInfos) {
1016         DistroFilter distro = new DistroFilter();
1017         for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) {
1018             String resource = moduleMetadataInfo.resource;
1019             if (resource.isEmpty()) {
1020                 continue;
1021             }
1022             JSONObject distroFilter = JSONObject.parseObject(resource);
1023             if (distroFilter.containsKey(DISTRIBUTION_FILTER)) {
1024                 return JSONObject.parseObject(getJsonString(distroFilter, DISTRIBUTION_FILTER), DistroFilter.class);
1025             }
1026             if (distroFilter.containsKey(DISTRO_FILTER)) {
1027                 return JSONObject.parseObject(getJsonString(distroFilter, DISTRO_FILTER), DistroFilter.class);
1028             }
1029         }
1030         return distro;
1031     }
1032 
1033     /**
1034      * parse stage parseModuleAllMetadata.
1035      *
1036      * @param jsonString is the string of module.json
1037      * @param profileJson is the <fileName,fileString> of profile
1038      * @return DistroFilter is the result of parsed distroFilter
1039      */
parseModuleAllMetadata( String jsonString, HashMap<String, String> profileJson)1040     public static List<ModuleMetadataInfo> parseModuleAllMetadata(
1041             String jsonString, HashMap<String, String> profileJson) throws BundleException {
1042         JSONObject jsonObject;
1043         try {
1044             jsonObject = JSON.parseObject(jsonString);
1045         } catch (JSONException exception) {
1046             String errMsg = "parse JSONobject failed.";
1047             LOG.error(errMsg);
1048             throw new BundleException(errMsg);
1049         }
1050         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1051         if (moduleObj == null) {
1052             LOG.error("ModuleJsonUtil::parseModuleAllMetadata failed, module is null.");
1053             throw new BundleException("ModuleJsonUtil::parseModuleAllMetadata failed, module is null.");
1054         }
1055         List<ModuleMetadataInfo> moduleMetadataInfos = new ArrayList<>();
1056         if (moduleObj.containsKey(METADATA)) {
1057             JSONArray metadatas = moduleObj.getJSONArray(METADATA);
1058             for (int i = 0; i < metadatas.size(); ++i) {
1059                 JSONObject metadata = metadatas.getJSONObject(i);
1060                 moduleMetadataInfos.add(parseModuleMetadata(metadata, profileJson));
1061             }
1062         }
1063         return moduleMetadataInfos;
1064     }
1065 
1066     /**
1067      * parse metadata info
1068      *
1069      * @param jsonObject Json hap json Object
1070      * @param profileJson is the <fileName,fileString> of profile
1071      * @return the ModuleMetadataInfo result
1072      * @throws BundleException Throws this exception if the json is not standard.
1073      */
parseModuleMetadata(JSONObject jsonObject, HashMap<String, String> profileJson)1074     public static ModuleMetadataInfo parseModuleMetadata(JSONObject jsonObject, HashMap<String, String> profileJson)
1075             throws BundleException {
1076         if (jsonObject == null) {
1077             throw new BundleException("ModuleJsonUtil::parseModuleMetadata failed, jsonObject is null.");
1078         }
1079         ModuleMetadataInfo moduleMetadataInfo = new ModuleMetadataInfo();
1080         if (jsonObject.containsKey(NAME)) {
1081             moduleMetadataInfo.name = getJsonString(jsonObject, NAME);
1082         }
1083         if (jsonObject.containsKey(VALUE)) {
1084             moduleMetadataInfo.value = getJsonString(jsonObject, VALUE);
1085         }
1086         if (jsonObject.containsKey(RESOURCE)) {
1087             moduleMetadataInfo.resource = getJsonString(jsonObject, RESOURCE);
1088             String fileName = moduleMetadataInfo.resource;
1089             fileName = fileName.replace(PROFILE, "");
1090             fileName = fileName + JSON_PERFIX;
1091             moduleMetadataInfo.resource = profileJson.get(fileName);
1092         }
1093         return moduleMetadataInfo;
1094     }
1095 
1096     /**
1097      * parse metadata info
1098      *
1099      * @param jsonString Json string of config.json
1100      * @return the ModuleMetadataInfo result
1101      */
parseFADistroFilter(String jsonString)1102     public static DistroFilter parseFADistroFilter(String jsonString) throws BundleException {
1103         DistroFilter distroFilter = new DistroFilter();
1104         JSONObject jsonObj;
1105         try {
1106             jsonObj = JSON.parseObject(jsonString);
1107             if (jsonObj.containsKey(MODULE)) {
1108                 JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1109                 if (moduleObj.containsKey(DISTRO_FILTER)) {
1110                     distroFilter = JSONObject.parseObject(getJsonString(moduleObj,
1111                             DISTRO_FILTER), DistroFilter.class);
1112                 }
1113             }
1114         } catch (JSONException exception) {
1115             String errMsg = "parse JSONobject failed.";
1116             LOG.error(errMsg);
1117             throw new BundleException(errMsg);
1118         }
1119         return distroFilter;
1120     }
1121 
1122     /**
1123      * get deviceType from json file.
1124      *
1125      * @param jsonString is the json String of module.json or config.json
1126      */
parseDeviceType(String jsonString)1127     public static List<String> parseDeviceType(String jsonString) throws BundleException {
1128         JSONObject jsonObj;
1129         try {
1130             jsonObj = JSON.parseObject(jsonString);
1131         } catch (JSONException exception) {
1132             String errMsg = "parse JSONobject failed.";
1133             LOG.error(errMsg);
1134             throw new BundleException(errMsg);
1135         }
1136         List<String> deviceType = new ArrayList<>();
1137         if (jsonObj.containsKey(MODULE)) {
1138             JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1139             if (moduleObj.containsKey(DEVICE_TYPE)) {
1140                 return JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPE), String.class);
1141             } else if (moduleObj.containsKey(DEVICE_TYPES)) {
1142                 return JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPES), String.class);
1143             } else {
1144                 return deviceType;
1145             }
1146         }
1147         return deviceType;
1148     }
1149 
1150     /**
1151      * get ability names from json file.
1152      *
1153      * @param jsonString is the json String of module.json or config.json
1154      * @return ability names
1155      */
parseAbilityNames(String jsonString)1156     public static List<String> parseAbilityNames(String jsonString) throws BundleException {
1157         JSONObject jsonObj;
1158         try {
1159             jsonObj = JSON.parseObject(jsonString);
1160         } catch (JSONException exception) {
1161             String errMsg = "parse JSONobject failed.";
1162             LOG.error(errMsg);
1163             throw new BundleException(errMsg);
1164         }
1165         List<String> abilityNames = new ArrayList<>();
1166         JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1167         if (moduleObj != null && moduleObj.containsKey(ABILITIES)) {
1168             JSONArray abilityObjs = moduleObj.getJSONArray(ABILITIES);
1169             for (int i = 0; i < abilityObjs.size(); ++i) {
1170                 JSONObject abilityObj = abilityObjs.getJSONObject(i);
1171                 if (abilityObj.containsKey(NAME)) {
1172                     abilityNames.add(getJsonString(abilityObj, NAME));
1173                 }
1174             }
1175         }
1176 
1177         return abilityNames;
1178     }
1179 
1180     /**
1181      * parse stage ExtensionAbility names
1182      *
1183      * @param jsonString is the json String of module.json
1184      * @return extensionAbilityNames
1185      */
parseExtensionAbilityName(String jsonString)1186     public static List<String> parseExtensionAbilityName(String jsonString) throws BundleException {
1187         JSONObject jsonObj;
1188         try {
1189             jsonObj = JSON.parseObject(jsonString);
1190         } catch (JSONException exception) {
1191             String errMsg = "parse JSONobject failed.";
1192             LOG.error(errMsg);
1193             throw new BundleException(errMsg);
1194         }
1195         JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1196         if (moduleObj == null) {
1197             LOG.error("ModuleJsonUtil::parseExtensionAbilityName failed, module is null.");
1198             throw new BundleException("ModuleJsonUtil::parseExtensionAbilityName failed, module is null.");
1199         }
1200         List<String> extensionAbilityNames = new ArrayList<>();
1201         if (moduleObj.containsKey(EXTENSION_ABILITIES)) {
1202             JSONArray extensionAbilityObjs = moduleObj.getJSONArray(EXTENSION_ABILITIES);
1203             for (int i = 0; i < extensionAbilityObjs.size(); ++i) {
1204                 JSONObject extensionAbilityObj = extensionAbilityObjs.getJSONObject(i);
1205                 if (extensionAbilityObj.containsKey(NAME)) {
1206                     extensionAbilityNames.add(getJsonString(extensionAbilityObj, NAME));
1207                 }
1208             }
1209         }
1210         return extensionAbilityNames;
1211     }
1212 
1213     /**
1214      * parse stage module type.
1215      *
1216      * @param jsonString is the json String of module.json or config.json
1217      * @return is entry
1218      */
parseModuleType(String jsonString)1219     public static String parseModuleType(String jsonString) throws BundleException {
1220         JSONObject jsonObj;
1221         try {
1222             jsonObj = JSON.parseObject(jsonString);
1223         } catch (JSONException exception) {
1224             String errMsg = "parse JSONObject failed.";
1225             LOG.error(errMsg);
1226             throw new BundleException(errMsg);
1227         }
1228         if (jsonObj.containsKey(MODULE)) {
1229             JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1230             if (moduleObj.containsKey(TYPE)) {
1231                 return getJsonString(moduleObj, TYPE);
1232             }
1233         }
1234         return EMPTY_STRING;
1235     }
1236 
1237     /**
1238      * parse FA module type.
1239      *
1240      * @param jsonString is the json String of module.json or config.json
1241      * @return is entry
1242      */
parseFAIsEntry(String jsonString)1243     public static String parseFAIsEntry(String jsonString) throws BundleException {
1244         JSONObject jsonObj;
1245         try {
1246             jsonObj = JSON.parseObject(jsonString);
1247         } catch (JSONException exception) {
1248             String errMsg = "parse JSONobject failed.";
1249             LOG.error(errMsg);
1250             throw new BundleException(errMsg);
1251         }
1252         if (jsonObj.containsKey(MODULE)) {
1253             JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1254             if (moduleObj.containsKey(DISTRO)) {
1255                 JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
1256                 if (distroObj.containsKey(MODULE_TYPE)) {
1257                     return getJsonString(distroObj, MODULE_TYPE);
1258                 }
1259             }
1260         }
1261         return EMPTY_STRING;
1262     }
1263 
parseDependencies(String jsonString, String bundleName)1264     static List<DependencyItem> parseDependencies(String jsonString, String bundleName) throws BundleException {
1265         JSONObject jsonObj;
1266         try {
1267             jsonObj = JSON.parseObject(jsonString);
1268         } catch (JSONException exception) {
1269             String errMsg = "parse json object failed.";
1270             LOG.error(errMsg);
1271             throw new BundleException(errMsg);
1272         }
1273         JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1274         if (moduleObj == null) {
1275             LOG.error("parseDependencies failed: lack of module object.");
1276             throw new BundleException("parseDependencies failed: lack of module object.");
1277         }
1278         List<DependencyItem> dependencyItemList = new ArrayList<>();
1279         if (!moduleObj.containsKey(DEPENDENCIES)) {
1280             return dependencyItemList;
1281         }
1282         JSONArray dependencyObjList = moduleObj.getJSONArray(DEPENDENCIES);
1283         for (int i = 0; i < dependencyObjList.size(); ++i) {
1284             JSONObject object = dependencyObjList.getJSONObject(i);
1285             DependencyItem item = new DependencyItem();
1286             if (object.containsKey(BUNDLE_NAME)) {
1287                 item.setBundleName(object.getString(BUNDLE_NAME));
1288             } else {
1289                 item.setBundleName(bundleName);
1290             }
1291             if (object.containsKey(MODULE_NAME)) {
1292                 item.setModuleName(object.getString(MODULE_NAME));
1293             }
1294             dependencyItemList.add(item);
1295         }
1296         return dependencyItemList;
1297     }
1298 
parseStageInstallation(String jsonString)1299     static boolean parseStageInstallation(String jsonString) throws BundleException {
1300         JSONObject jsonObj;
1301         try {
1302             jsonObj = JSON.parseObject(jsonString);
1303         } catch (JSONException exception) {
1304             String errMsg = "parse JSONobject failed.";
1305             LOG.error(errMsg);
1306             throw new BundleException(errMsg);
1307         }
1308         JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1309         if (moduleObj == null) {
1310             LOG.error("ModuleJsonUtil::parseStageInstallation json do not contain module.");
1311             throw new BundleException("ModuleJsonUtil::parseStageInstallation json do not contain module.");
1312         }
1313         if (moduleObj.containsKey(INSTALLATION_FREE)) {
1314             return moduleObj.getBoolean(INSTALLATION_FREE);
1315         }
1316         return false;
1317     }
1318 
parseStageBundleType(String jsonString)1319     static String parseStageBundleType(String jsonString) throws BundleException {
1320         JSONObject jsonObject;
1321         try {
1322             jsonObject = JSON.parseObject(jsonString);
1323         } catch (JSONException exception) {
1324             LOG.error("parse JOSNObject failed in getStageAsanEnabled.");
1325             throw new BundleException("parse JOSNObject failed in getStageAsanEnabled.");
1326         }
1327         JSONObject appObj = jsonObject.getJSONObject(APP);
1328         if (appObj == null) {
1329             LOG.error("parse failed, input module.json is invalid, module.json has no app.");
1330             throw new BundleException("parse failed, input module.json is invalid, module.json has no app.");
1331         }
1332         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1333         if (moduleObj == null) {
1334             LOG.error("parse failed, input module.json is invalid, module.json has no module.");
1335             throw new BundleException("parse failed, input module.json is invalid, module.json has no module.");
1336         }
1337         if (!moduleObj.containsKey(TYPE)) {
1338             LOG.error("parse failed, input module.json is invalid, module.json has no type.");
1339             throw new BundleException("parse failed, input module.json is invalid, module.json has no type.");
1340         }
1341         boolean isShared = false;
1342         String type = moduleObj.getString(TYPE);
1343         if (type != null && SHARED.equals(type)) {
1344             isShared = true;
1345         }
1346         boolean installationFree = getJsonBooleanValue(moduleObj, INSTALLATION_FREE, false);
1347         if (!appObj.containsKey(BUNDLE_TYPE)) {
1348             if (installationFree) {
1349                 String errMessage = "The app.json5 file configuration does not match the installationFree:" +
1350                         " true settings. Add the bundleType field to the app.json5 file and set it atomicService.";
1351                 LOG.error(errMessage);
1352                 throw new BundleException(errMessage);
1353             }
1354             return APP;
1355         } else {
1356             String bundleType = getJsonString(appObj, BUNDLE_TYPE);
1357             if (bundleType.equals(APP)) {
1358                 if (installationFree) {
1359                     LOG.error("installationFree must be false when bundleType is app.");
1360                     throw new BundleException("installationFree must be false when bundleType is app.");
1361                 }
1362                 return APP;
1363             } else if (bundleType.equals(ATOMIC_SERVICE)) {
1364                 if (!installationFree) {
1365                     LOG.error("installationFree must be true when bundleType is atomicService.");
1366                     throw new BundleException("installationFree must be true when bundleType is atomicService.");
1367                 }
1368                 return ATOMIC_SERVICE;
1369             } else if (SHARED.equals(bundleType)) {
1370                 if (!isShared) {
1371                     LOG.error("type must be shared when bundleType is shared.");
1372                     throw new BundleException("type must be shared when bundleType is shared.");
1373                 }
1374                 return SHARED;
1375             } else {
1376                 LOG.error("bundleType is invalid in app.json.");
1377                 throw new BundleException("bundleType is invalid in app.json.");
1378             }
1379         }
1380     }
1381 
parseAtomicServicePreloads(String jsonString)1382     static List<PreloadItem> parseAtomicServicePreloads(String jsonString) throws BundleException {
1383         List<PreloadItem> preloadItems = new ArrayList<>();
1384         JSONObject jsonObject;
1385         try {
1386             jsonObject = JSON.parseObject(jsonString);
1387         } catch (JSONException exception) {
1388             LOG.error("parse JOSNObject failed in getStageAsanEnabled.");
1389             throw new BundleException("parse JOSNObject failed in getStageAsanEnabled.");
1390         }
1391         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1392         JSONObject atomicServiceObj = null;
1393         if (!moduleObj.containsKey(ATOMIC_SERVICE)) {
1394             return preloadItems;
1395         }
1396         atomicServiceObj = moduleObj.getJSONObject(ATOMIC_SERVICE);
1397         if (!atomicServiceObj.containsKey(PRELOADS)) {
1398             return preloadItems;
1399         }
1400         JSONArray preloadObjs = atomicServiceObj.getJSONArray(PRELOADS);
1401         for (int i = 0; i < preloadObjs.size(); ++i) {
1402             PreloadItem preloadItem = new PreloadItem();
1403             JSONObject itemObj = preloadObjs.getJSONObject(i);
1404             if (itemObj.containsKey(MODULE_NAME)) {
1405                 preloadItem.setModuleName(getJsonString(itemObj, MODULE_NAME));
1406             }
1407             preloadItems.add(preloadItem);
1408         }
1409         return preloadItems;
1410     }
1411 
parseProxyDataUri(String jsonString)1412     static List<String> parseProxyDataUri(String jsonString) throws BundleException {
1413         List<String> proxyDataUris = new ArrayList<>();
1414         JSONObject jsonObject;
1415         try {
1416             jsonObject = JSON.parseObject(jsonString);
1417         } catch (JSONException exception) {
1418             LOG.error("parse JOSNObject failed in parseProxyDataUri.");
1419             throw new BundleException("parse JOSNObject failed in parseProxyDataUri.");
1420         }
1421         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1422         if (!moduleObj.containsKey(PROXY_DATAS) && !moduleObj.containsKey(PROXY_DATA)) {
1423             return proxyDataUris;
1424         } else if (moduleObj.containsKey(PROXY_DATA)) {
1425             JSONArray proxyData = moduleObj.getJSONArray(PROXY_DATA);
1426             for (int i = 0; i < proxyData.size(); ++i) {
1427                 JSONObject itemObj = proxyData.getJSONObject(i);
1428                 if (!itemObj.containsKey(PROXY_URI)) {
1429                     LOG.error("parse JOSNObject failed in parseProxyDataUri.");
1430                     throw new BundleException("parse JOSNObject failed in parseProxyDataUri.");
1431                 }
1432                 String uri = itemObj.getString(PROXY_URI);
1433                 proxyDataUris.add(uri);
1434             }
1435         } else {
1436             JSONArray proxyDatas = moduleObj.getJSONArray(PROXY_DATAS);
1437             for (int i = 0; i < proxyDatas.size(); ++i) {
1438                 JSONObject itemObj = proxyDatas.getJSONObject(i);
1439                 if (!itemObj.containsKey(PROXY_URI)) {
1440                     LOG.error("parse JOSNObject failed in parseProxyDataUri.");
1441                     throw new BundleException("parse JOSNObject failed in parseProxyDataUri.");
1442                 }
1443                 String uri = itemObj.getString(PROXY_URI);
1444                 proxyDataUris.add(uri);
1445             }
1446         }
1447         return proxyDataUris;
1448     }
1449 
getAppObj(String jsonString)1450     static JSONObject getAppObj(String jsonString) throws BundleException {
1451         JSONObject jsonObject;
1452         try {
1453             jsonObject = JSON.parseObject(jsonString);
1454         } catch (JSONException exception) {
1455             String errMsg = "parse JSONobject failed.";
1456             LOG.error(errMsg);
1457             throw new BundleException(errMsg);
1458         }
1459         JSONObject appObj = jsonObject.getJSONObject(APP);
1460         if (appObj == null) {
1461             LOG.error("ModuleJsonUtil::parseStageInstallation json do not contain app.");
1462             throw new BundleException("ModuleJsonUtil::parseStageInstallation json do not contain app.");
1463         }
1464         return appObj;
1465     }
1466 
parseTargetBundleName(String jsonString)1467     static String parseTargetBundleName(String jsonString) throws BundleException {
1468         JSONObject jsonObject;
1469         try {
1470             jsonObject = JSON.parseObject(jsonString);
1471         } catch (JSONException exception) {
1472             String errMsg = "parse JSONobject failed.";
1473             LOG.error(errMsg);
1474             throw new BundleException(errMsg);
1475         }
1476         JSONObject appObject = jsonObject.getJSONObject(APP);
1477         if (appObject == null) {
1478             LOG.error("ModuleJsonUtil::parseTargetBundleName json object do not contain app.");
1479             throw new BundleException("ModuleJsonUtil::parseTargetBundleName json object do not contain app.");
1480         }
1481         String targetBundleName = "";
1482         if (appObject.containsKey(TARGET_BUNDLE_NAME)) {
1483             targetBundleName = appObject.getString(TARGET_BUNDLE_NAME);
1484         }
1485         return targetBundleName;
1486     }
1487 
parseTargetPriority(String jsonString)1488     static int parseTargetPriority(String jsonString) throws BundleException {
1489         JSONObject jsonObject;
1490         try {
1491             jsonObject = JSON.parseObject(jsonString);
1492         } catch (JSONException exception) {
1493             String errMsg = "parse JSONobject failed.";
1494             LOG.error(errMsg);
1495             throw new BundleException(errMsg);
1496         }
1497         JSONObject appObject = jsonObject.getJSONObject(APP);
1498         if (appObject == null) {
1499             LOG.error("ModuleJsonUtil::parseTargetPriority json object do not contain app.");
1500             throw new BundleException("ModuleJsonUtil::parseTargetPriority json object do not contain app.");
1501         }
1502         int targetPriority = 0;
1503         if (appObject.containsKey(TARGET_PRIORITY)) {
1504             targetPriority = appObject.getIntValue(TARGET_PRIORITY);
1505         }
1506         return targetPriority;
1507     }
1508 
parseTargetModuleName(String jsonString)1509     static String parseTargetModuleName(String jsonString) throws BundleException {
1510         JSONObject jsonObject;
1511         try {
1512             jsonObject = JSON.parseObject(jsonString);
1513         } catch (JSONException exception) {
1514             String errMsg = "parse JSONobject failed.";
1515             LOG.error(errMsg);
1516             throw new BundleException(errMsg);
1517         }
1518         JSONObject appObject = jsonObject.getJSONObject(MODULE);
1519         if (appObject == null) {
1520             LOG.error("ModuleJsonUtil::parseTargetBundleName json object do not contain app.");
1521             throw new BundleException("ModuleJsonUtil::parseTargetBundleName json object do not contain app.");
1522         }
1523         String targetModuleName = "";
1524         if (appObject.containsKey(TARGET_MODULE_NAME)) {
1525             targetModuleName = appObject.getString(TARGET_MODULE_NAME);
1526         }
1527         return targetModuleName;
1528     }
1529 
parseTargetModulePriority(String jsonString)1530     static int parseTargetModulePriority(String jsonString) throws BundleException {
1531         JSONObject jsonObject;
1532         try {
1533             jsonObject = JSON.parseObject(jsonString);
1534         } catch (JSONException exception) {
1535             String errMsg = "parse JSONobject failed.";
1536             LOG.error(errMsg);
1537             throw new BundleException(errMsg);
1538         }
1539         JSONObject appObject = jsonObject.getJSONObject(MODULE);
1540         if (appObject == null) {
1541             LOG.error("ModuleJsonUtil::parseTargetPriority json object do not contain app.");
1542             throw new BundleException("ModuleJsonUtil::parseTargetPriority json object do not contain app.");
1543         }
1544         int targetModulePriority = 0;
1545         if (appObject.containsKey(TARGET_PRIORITY)) {
1546             targetModulePriority = appObject.getIntValue(TARGET_PRIORITY);
1547         }
1548         return targetModulePriority;
1549     }
1550 
parseFAInstallationFree(String jsonString)1551     static boolean parseFAInstallationFree(String jsonString) throws BundleException {
1552         JSONObject jsonObject;
1553         try {
1554             jsonObject = JSON.parseObject(jsonString);
1555         } catch (JSONException exception) {
1556             String errMsg = "parse JSONobject failed.";
1557             LOG.error(errMsg);
1558             throw new BundleException(errMsg);
1559         }
1560         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1561         if (moduleObj == null) {
1562             LOG.error("ModuleJsonUtil::parseStageInstallation json do not contain module.");
1563             throw new BundleException("ModuleJsonUtil::parseStageInstallation json do not contain module.");
1564         }
1565         JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
1566         if (distroObj == null) {
1567             LOG.error("ModuleJsonUtil::parseStageInstallation json do not contain distro.");
1568             throw new BundleException("ModuleJsonUtil::parseStageInstallation json do not contain distro.");
1569         }
1570         if (distroObj.containsKey(INSTALLATION_FREE)) {
1571             return distroObj.getBoolean(INSTALLATION_FREE);
1572         }
1573         return false;
1574     }
1575 
1576     /**
1577      * get hqfVerifyINfo from hqf file
1578      *
1579      * @param hqfPath is the file path of hqf file
1580      * @return HQFVerifyInfo
1581      */
parseHQFInfo(String hqfPath)1582     static HQFInfo parseHQFInfo(String hqfPath) throws BundleException {
1583         File hqfFile = new File(hqfPath);
1584         String patchJson = FileUtils.getJsonInZips(hqfFile, PATCH_JSON);
1585         return parsePatch(patchJson);
1586     }
1587 
1588     /**
1589      * parse patch.json form json string.
1590      *
1591      * @param jsonString is the file path of hqf file
1592      * @return HQFVerifyInfo
1593      */
parsePatch(String jsonString)1594     static HQFInfo parsePatch(String jsonString) throws BundleException {
1595         JSONObject jsonObject;
1596         try {
1597             jsonObject = JSON.parseObject(jsonString);
1598         } catch (JSONException exception) {
1599             String errMsg = "parse JSONobject failed.";
1600             LOG.error(errMsg);
1601             throw new BundleException(errMsg);
1602         }
1603         JSONObject appObj = jsonObject.getJSONObject(APP);
1604         if (appObj == null) {
1605             LOG.error("parsePatch failed, input patch.json is invalid, patch.json has no app.");
1606             throw new BundleException("parsePatch failed, input patch.json is invalid.");
1607         }
1608         HQFInfo hqfVerifyInfo = new HQFInfo();
1609         if (appObj.containsKey(BUNDLE_NAME)) {
1610             hqfVerifyInfo.setBundleName(appObj.getString(BUNDLE_NAME));
1611         }
1612         if (appObj.containsKey(VERSIONCODE)) {
1613             hqfVerifyInfo.setVersionCode(appObj.getIntValue(VERSIONCODE));
1614         }
1615         if (appObj.containsKey(VERSIONNAME)) {
1616             hqfVerifyInfo.setVersionName(appObj.getString(VERSIONNAME));
1617         }
1618         if (appObj.containsKey(PATCH_VERSION_CODE)) {
1619             hqfVerifyInfo.setPatchVersionCode(appObj.getIntValue(PATCH_VERSION_CODE));
1620         }
1621         if (appObj.containsKey(PATCH_VERSION_NAME)) {
1622             hqfVerifyInfo.setPatchVersionName(appObj.getString(PATCH_VERSION_NAME));
1623         }
1624         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1625         if (moduleObj == null) {
1626             LOG.error("parse failed, input patch.json is invalid, patch.json has no module.");
1627             throw new BundleException("parse failed, input patch.json is invalid, patch.json has no module.");
1628         }
1629         if (moduleObj.containsKey(NAME)) {
1630             hqfVerifyInfo.setModuleName(moduleObj.getString(NAME));
1631         }
1632         if (moduleObj.containsKey(TYPE)) {
1633             hqfVerifyInfo.setType(moduleObj.getString(TYPE));
1634         }
1635         if (moduleObj.containsKey(DEVICE_TYPES)) {
1636             hqfVerifyInfo.setDeviceTypes(JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPES), String.class));
1637         }
1638         if (moduleObj.containsKey(ORIGINAL_MODULE_HASH)) {
1639             hqfVerifyInfo.setOriginalModuleHash(moduleObj.getString(ORIGINAL_MODULE_HASH));
1640         }
1641         return hqfVerifyInfo;
1642     }
1643 
1644     /**
1645      * determine whether it is a native compression libs.
1646      *
1647      * @param jsonString is the file path of hqf file
1648      * @return the result
1649      */
stageIsCompressNativeLibs(String jsonString)1650     public static boolean stageIsCompressNativeLibs(String jsonString) throws BundleException {
1651         JSONObject jsonObject;
1652         try {
1653             jsonObject = JSON.parseObject(jsonString);
1654         } catch (JSONException exception) {
1655             String errMsg = "parse JSONobject failed.";
1656             LOG.error(errMsg);
1657             throw new BundleException(errMsg);
1658         }
1659         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1660         if (moduleObj == null) {
1661             LOG.error("parse failed, input module.json is invalid, module.json has no module.");
1662             throw new BundleException("parse failed, input module.json is invalid, module.json has no module.");
1663         }
1664         if (moduleObj.containsKey(COMPRESS_NATIVE_LIBS)) {
1665             return moduleObj.getBoolean(COMPRESS_NATIVE_LIBS);
1666         }
1667 
1668         return true;
1669     }
1670 
1671     /**
1672      * get asanEnabled in module.json
1673      *
1674      * @param jsonString is the file content of module.json
1675      * @return the value of asanEnabled
1676      */
getStageAsanEnabled(String jsonString)1677     public static boolean getStageAsanEnabled(String jsonString) throws BundleException {
1678         JSONObject jsonObject;
1679         try {
1680             jsonObject = JSON.parseObject(jsonString);
1681         } catch (JSONException exception) {
1682             LOG.error("parse JOSNObject failed in getStageAsanEnabled.");
1683             throw new BundleException("parse JOSNObject failed in getStageAsanEnabled.");
1684         }
1685         JSONObject appObj = jsonObject.getJSONObject(APP);
1686         if (appObj == null) {
1687             LOG.error("parse failed, input module.json is invalid, module.json has no app.");
1688             throw new BundleException("parse failed, input module.json is invalid, module.json has no app.");
1689         }
1690         if (appObj.containsKey(ASAN_ENABLED)) {
1691             return appObj.getBoolean(ASAN_ENABLED);
1692         }
1693         return false;
1694     }
1695 
1696     /**
1697      * get apiReleaseType in module.json
1698      *
1699      * @param jsonString is the file content of module.json
1700      * @return the result
1701      */
getStageApiReleaseType(String jsonString)1702     public static String getStageApiReleaseType(String jsonString) throws BundleException {
1703         JSONObject jsonObject;
1704         try {
1705             jsonObject = JSON.parseObject(jsonString);
1706         } catch (JSONException exception) {
1707             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
1708             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
1709         }
1710         JSONObject appObj = jsonObject.getJSONObject(APP);
1711         if (appObj == null) {
1712             LOG.error("parse failed, input module.json is invalid, module.json has no app.");
1713             throw new BundleException("parse failed, input module.json is invalid, module.json has no app.");
1714         }
1715         return getJsonString(appObj, API_RELEASE_TYPE);
1716     }
1717 
1718     /**
1719      * get debug in module.json
1720      *
1721      * @param jsonString is the file content of module.json
1722      * @return the result
1723      */
getDebug(String jsonString)1724     public static boolean getDebug(String jsonString) throws BundleException {
1725         JSONObject jsonObject;
1726         try {
1727             jsonObject = JSON.parseObject(jsonString);
1728         } catch (JSONException exception) {
1729             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
1730             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
1731         }
1732         JSONObject appObj = jsonObject.getJSONObject(APP);
1733         if (appObj == null) {
1734             LOG.error("parse failed, input module.json is invalid, module.json has no app.");
1735             throw new BundleException("parse failed, input module.json is invalid, module.json has no app.");
1736         }
1737         return getJsonBooleanValue(appObj, DEBUG, false);
1738     }
1739 
1740     /**
1741      * get debug in config.json
1742      *
1743      * @param jsonString is the file content of module.json
1744      * @return the result
1745      */
getFADebug(String jsonString)1746     public static boolean getFADebug(String jsonString) throws BundleException {
1747         JSONObject jsonObject;
1748         try {
1749             jsonObject = JSON.parseObject(jsonString);
1750         } catch (JSONException exception) {
1751             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
1752             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
1753         }
1754         JSONObject deviceConfigObj = jsonObject.getJSONObject(DEVICE_CONFIG);
1755         if (deviceConfigObj == null) {
1756             return false;
1757         }
1758         JSONObject defaultObj = deviceConfigObj.getJSONObject(DEFAULT);
1759         if (defaultObj == null) {
1760             return false;
1761         }
1762 
1763         return getJsonBooleanValue(defaultObj, DEBUG, false);
1764     }
1765 
1766     /**
1767      * get compileSdkVersion in config.json
1768      *
1769      * @param jsonString is the file content of module.json
1770      * @return the result
1771      */
getFACompileSdkVersion(String jsonString)1772     public static String getFACompileSdkVersion(String jsonString) throws BundleException {
1773         JSONObject jsonObject;
1774         try {
1775             jsonObject = JSON.parseObject(jsonString);
1776         } catch (JSONException exception) {
1777             String errMsg = "parse JSONobject failed";
1778             LOG.error(errMsg);
1779             throw new BundleException(errMsg);
1780         }
1781         JSONObject appObj = jsonObject.getJSONObject(APP);
1782         if (appObj == null) {
1783             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain app.");
1784         }
1785         if (!appObj.containsKey(API_VERSION)) {
1786             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain apiVersion.");
1787         }
1788         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
1789         String compileSdkVersion = "";
1790         if (apiVersionObj.containsKey(COMPILE_SDK_VERSION)) {
1791             compileSdkVersion = apiVersionObj.getString(COMPILE_SDK_VERSION);
1792         }
1793         return compileSdkVersion;
1794     }
1795 
1796     /**
1797      * get compileSdkType in config.json
1798      *
1799      * @param jsonString is the file content of module.json
1800      * @return the result
1801      */
getFACompileSdkType(String jsonString)1802     public static String getFACompileSdkType(String jsonString) throws BundleException {
1803         JSONObject jsonObject;
1804         try {
1805             jsonObject = JSON.parseObject(jsonString);
1806         } catch (JSONException exception) {
1807             String errMsg = "parse JSONobject failed";
1808             LOG.error(errMsg);
1809             throw new BundleException(errMsg);
1810         }
1811         JSONObject appObj = jsonObject.getJSONObject(APP);
1812         if (appObj == null) {
1813             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain app.");
1814         }
1815         if (!appObj.containsKey(API_VERSION)) {
1816             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain apiVersion.");
1817         }
1818         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
1819         String compileSdkType = "";
1820         if (apiVersionObj.containsKey(COMPILE_SDK_TYPE)) {
1821             compileSdkType = apiVersionObj.getString(COMPILE_SDK_TYPE);
1822         }
1823         return compileSdkType;
1824     }
1825 
1826     /**
1827      * get compileSdkVersion in module.json
1828      *
1829      * @param jsonString is the file content of module.json
1830      * @return the result
1831      */
getCompileSdkVersion(String jsonString)1832     public static String getCompileSdkVersion(String jsonString) throws BundleException {
1833         String compileSdkVersion = "";
1834         JSONObject jsonObject;
1835         try {
1836             jsonObject = JSON.parseObject(jsonString);
1837             JSONObject appObj = jsonObject.getJSONObject(APP);
1838             if (appObj == null) {
1839                 LOG.error("ModuleJsonUtil:parseStageModuleName failed: json file do not contain app.");
1840                 throw new BundleException("ModuleJsonUtil:parseStageModuleName failed: json file do not contain app.");
1841             }
1842             if (appObj.containsKey(COMPILE_SDK_VERSION)) {
1843                 compileSdkVersion = appObj.getString(COMPILE_SDK_VERSION);
1844             } else {
1845                 LOG.warning(
1846                         "ModuleJsonUtil:getCompileSdkType failed: json file do not contain module compileSdkVersion.");
1847             }
1848         } catch (BundleException e) {
1849             LOG.error("ModuleJsonUtil:parseStageModuleName failed.");
1850             throw new BundleException("ModuleJsonUtil:parseStageModuleName failed.");
1851         }
1852         return compileSdkVersion;
1853     }
1854 
1855     /**
1856      * get compileSdkType in module.json
1857      *
1858      * @param jsonString is the file content of module.json
1859      * @return the result
1860      */
getCompileSdkType(String jsonString)1861     public static String getCompileSdkType(String jsonString) throws BundleException {
1862         String compileSdkType = "";
1863         JSONObject jsonObject;
1864         try {
1865             jsonObject = JSON.parseObject(jsonString);
1866             JSONObject appObj = jsonObject.getJSONObject(APP);
1867             if (appObj == null) {
1868                 LOG.error("ModuleJsonUtil:parseStageModuleName failed: json file do not contain app.");
1869                 throw new BundleException("ModuleJsonUtil:parseStageModuleName failed: json file do not contain app.");
1870             }
1871             if (appObj.containsKey(COMPILE_SDK_TYPE)) {
1872                 compileSdkType = appObj.getString(COMPILE_SDK_TYPE);
1873             } else {
1874                 LOG.warning(
1875                         "ModuleJsonUtil:getCompileSdkType failed: json file do not contain module compileSdkType.");
1876             }
1877         } catch (BundleException e) {
1878             LOG.error("ModuleJsonUtil:parseStageModuleName failed.");
1879             throw new BundleException("ModuleJsonUtil:parseStageModuleName failed.");
1880         }
1881         return compileSdkType;
1882     }
1883 
1884     /**
1885      * get targetModuleName in module.json
1886      *
1887      * @param jsonString is the file content of module.json
1888      * @return the result
1889      */
getStageTargetModuleName(String jsonString)1890     public static String getStageTargetModuleName(String jsonString) throws BundleException {
1891         JSONObject jsonObject;
1892         try {
1893             jsonObject = JSON.parseObject(jsonString);
1894         } catch (JSONException exception) {
1895             LOG.error("parse JOSNObject failed in getStageTargetModuleName.");
1896             throw new BundleException("parse JOSNObject failed in getStageTargetModuleName.");
1897         }
1898         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
1899         if (moduleObj == null) {
1900             LOG.error("parse failed, input module.json is invalid, module.json has no app.");
1901             throw new BundleException("parse failed, input module.json is invalid, module.json has no app.");
1902         }
1903         return getJsonString(moduleObj, TARGET_MODULE_NAME);
1904     }
1905 
1906     /**
1907      * get targetBundleName in module.json
1908      *
1909      * @param jsonString is the file content of module.json
1910      * @return the result
1911      */
getStageTargetBundleName(String jsonString)1912     public static String getStageTargetBundleName(String jsonString) throws BundleException {
1913         JSONObject jsonObject;
1914         try {
1915             jsonObject = JSON.parseObject(jsonString);
1916         } catch (JSONException exception) {
1917             LOG.error("parse JOSNObject failed in getStageTargetModuleName.");
1918             throw new BundleException("parse JOSNObject failed in getStageTargetModuleName.");
1919         }
1920         JSONObject appObj = jsonObject.getJSONObject(APP);
1921         if (appObj == null) {
1922             LOG.error("parse failed, input module.json is invalid, module.json has no app.");
1923             throw new BundleException("parse failed, input module.json is invalid, module.json has no app.");
1924         }
1925         return getJsonString(appObj, TARGET_BUNDLE_NAME);
1926     }
1927 
1928     /**
1929      * is existed requestPermission in module.json
1930      *
1931      * @param jsonString is the file content of module.json
1932      * @return the result
1933      */
isExistedStageRequestPermissions(String jsonString)1934     public static boolean isExistedStageRequestPermissions(String jsonString) throws BundleException {
1935         return isExistedProperty(jsonString, MODULE, REQUEST_PERMISSIONS);
1936     }
1937 
1938     /**
1939      * is existed targetPriority in module.json
1940      *
1941      * @param jsonString is the file content of module.json
1942      * @return the result
1943      */
isExistedStageModuleTargetPriority(String jsonString)1944     public static boolean isExistedStageModuleTargetPriority(String jsonString) throws BundleException {
1945         return isExistedProperty(jsonString, MODULE, TARGET_PRIORITY);
1946     }
1947 
1948     /**
1949      * is existed targetPriority in app.json
1950      *
1951      * @param jsonString is the file content of module.json
1952      * @return the result
1953      */
isExistedStageAppTargetPriority(String jsonString)1954     public static boolean isExistedStageAppTargetPriority(String jsonString) throws BundleException {
1955         return isExistedProperty(jsonString, APP, TARGET_PRIORITY);
1956     }
1957 
isExistedProperty(String jsonString, String fatherProperty, String childProperty)1958     private static boolean isExistedProperty(String jsonString, String fatherProperty,
1959                                              String childProperty) throws BundleException {
1960         JSONObject jsonObject;
1961         try {
1962             jsonObject = JSON.parseObject(jsonString);
1963         } catch (JSONException exception) {
1964             LOG.error("parse JOSNObject failed in isExistedProperty.");
1965             throw new BundleException("parse JOSNObject failed in isExistedProperty.");
1966         }
1967         JSONObject appObj = jsonObject.getJSONObject(fatherProperty);
1968         if (appObj == null) {
1969             LOG.error("parse failed, input module.json is invalid, module.json has no " + fatherProperty + ".");
1970             throw new BundleException("parse failed, input module.json is invalid, module.json has no " +
1971                     fatherProperty + ".");
1972         }
1973         return appObj.containsKey(childProperty);
1974     }
1975 
1976     /**
1977      * get asanEnabled in config.json
1978      *
1979      * @param jsonString is the file content of module.json
1980      * @return the value of asanEnabled
1981      */
getFAAsanEnabled(String jsonString)1982     public static boolean getFAAsanEnabled(String jsonString) throws BundleException {
1983         JSONObject jsonObject;
1984         try {
1985             jsonObject = JSON.parseObject(jsonString);
1986         } catch (JSONException exception) {
1987             LOG.error("parse JOSNObject failed in getStageAsanEnabled.");
1988             throw new BundleException("parse JOSNObject failed in getStageAsanEnabled.");
1989         }
1990         JSONObject appObj = jsonObject.getJSONObject(APP);
1991         if (appObj == null) {
1992             LOG.error("parse failed, input module.json is invalid, module.json has no app.");
1993             throw new BundleException("parse failed, input module.json is invalid, module.json has no app.");
1994         }
1995         if (appObj.containsKey(ASAN_ENABLED)) {
1996             return appObj.getBoolean(ASAN_ENABLED);
1997         }
1998         return false;
1999     }
2000 
2001     /**
2002      * get releaseType in config.json
2003      *
2004      * @param jsonString is the file content of config.json
2005      * @return the result
2006      */
getFAReleaseType(String jsonString)2007     public static String getFAReleaseType(String jsonString) throws BundleException {
2008         JSONObject jsonObject;
2009         try {
2010             jsonObject = JSON.parseObject(jsonString);
2011         } catch (JSONException exception) {
2012             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
2013             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
2014         }
2015         JSONObject appObj = jsonObject.getJSONObject(APP);
2016         if (appObj == null) {
2017             LOG.error("parse failed, input config.json is invalid, config.json has no app.");
2018             throw new BundleException("parse failed, input config.json is invalid, config.json has no app.");
2019         }
2020         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
2021         if (apiVersionObj == null) {
2022             return "";
2023         }
2024         return getJsonString(apiVersionObj, RELEASE_TYPE);
2025     }
2026 
2027     /**
2028      * check module atomic service is valid
2029      *
2030      * @param jsonString is the file content of config.json
2031      * @return the result
2032      * @throws BundleException Throws this exception if the json is not standard.
2033      */
isModuleAtomicServiceValid(String jsonString)2034     public static boolean isModuleAtomicServiceValid(String jsonString) throws BundleException {
2035         JSONObject jsonObject;
2036         try {
2037             jsonObject = JSON.parseObject(jsonString);
2038         } catch (JSONException exception) {
2039             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
2040             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
2041         }
2042         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
2043         if (moduleObj == null) {
2044             LOG.error("parse failed, input config.json is invalid, config.json has no module.");
2045             throw new BundleException("parse failed, input config.json is invalid, module.json has no app.");
2046         }
2047         if (!moduleObj.containsKey(ATOMIC_SERVICE)) {
2048             return true;
2049         }
2050         JSONObject appObj = jsonObject.getJSONObject(APP);
2051         if (appObj == null) {
2052             LOG.error("parse failed, input config.json is invalid, module.json has no app.");
2053             throw new BundleException("parse failed, input config.json is invalid, module.json has no app.");
2054         }
2055         if (moduleObj.containsKey(ATOMIC_SERVICE) && (!appObj.containsKey(BUNDLE_TYPE) ||
2056                 !getJsonString(appObj, BUNDLE_TYPE).equals(ATOMIC_SERVICE))) {
2057             LOG.error("module can not config atomicService when bundleType is not atomicService.");
2058             return false;
2059         }
2060         return true;
2061     }
2062 
2063     /**
2064      * check entry module must contain at least one ability.
2065      *
2066      * @param jsonString Indicates the jsonString.
2067      * @return Returns true if jsonString is valid.
2068      */
checkEntryInAtomicService(String jsonString)2069     public static boolean checkEntryInAtomicService(String jsonString) throws BundleException {
2070         JSONObject jsonObject;
2071         try {
2072             jsonObject = JSON.parseObject(jsonString);
2073         } catch (JSONException exception) {
2074             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
2075             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
2076         }
2077         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
2078         if (moduleObj == null) {
2079             LOG.error("parse failed, input config.json is invalid, config.json has no module.");
2080             throw new BundleException("parse failed, input config.json is invalid, module.json has no app.");
2081         }
2082         JSONObject appObj = jsonObject.getJSONObject(APP);
2083         if (appObj == null) {
2084             LOG.error("parse failed, input config.json is invalid, module.json has no app.");
2085             throw new BundleException("parse failed, input config.json is invalid, module.json has no app.");
2086         }
2087         if (!parseStageBundleType(jsonString).equals(ATOMIC_SERVICE)) {
2088             return true;
2089         }
2090         if (parseModuleType(jsonString).equals(ENTRY) && parseAbilityNames(jsonString).isEmpty()) {
2091             LOG.error("entry module must contain at least one ability.");
2092             return false;
2093         }
2094         return true;
2095     }
2096 
2097     /**
2098      * check module atomic installation free is valid
2099      *
2100      * @param jsonString is the file content of config.json
2101      * @return the result
2102      * @throws BundleException Throws this exception if the json is not standard.
2103      */
checkAtomicServiceInstallationFree(String jsonString)2104     public static boolean checkAtomicServiceInstallationFree(String jsonString) throws BundleException {
2105         JSONObject jsonObject;
2106         try {
2107             jsonObject = JSON.parseObject(jsonString);
2108         } catch (JSONException exception) {
2109             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
2110             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
2111         }
2112         JSONObject moduleObj = jsonObject.getJSONObject(MODULE);
2113         if (moduleObj == null) {
2114             LOG.error("parse failed, input config.json is invalid, config.json has no module.");
2115             throw new BundleException("parse failed, input config.json is invalid, module.json has no app.");
2116         }
2117         JSONObject appObj = jsonObject.getJSONObject(APP);
2118         if (appObj == null) {
2119             LOG.error("parse failed, input config.json is invalid, module.json has no app.");
2120             throw new BundleException("parse failed, input config.json is invalid, module.json has no app.");
2121         }
2122         boolean installationFree = getJsonBooleanValue(moduleObj, INSTALLATION_FREE, false);
2123         if (!appObj.containsKey(BUNDLE_TYPE)) {
2124             if (installationFree) {
2125                 String errMessage = "The app.json5 file configuration does not match the installationFree:" +
2126                         " true settings. Add the bundleType field to the app.json5 file and set it atomicService.";
2127                 LOG.error(errMessage);
2128                 return false;
2129             }
2130             return true;
2131         }
2132         String bundleType = getJsonString(appObj, BUNDLE_TYPE);
2133         if (bundleType.equals(APP)) {
2134             if (installationFree) {
2135                 LOG.error("installationFree must be false when bundleType is app.");
2136                 return false;
2137             }
2138         } else if (bundleType.equals(ATOMIC_SERVICE)) {
2139             if (!installationFree) {
2140                 LOG.error("installationFree must be true when bundleType is atomicService.");
2141                 return false;
2142             }
2143         } else if (SHARED.equals(bundleType)) {
2144             if (installationFree) {
2145                 LOG.error("installationFree must be false when bundleType is shared.");
2146                 return false;
2147             }
2148         } else {
2149             LOG.error("bundleType is invalid in app.json.");
2150             return false;
2151         }
2152         return true;
2153     }
2154 
2155     /**
2156      * get the String from JSONObject by the key.
2157      *
2158      * @param jsonObject uncompress json object
2159      * @param key value key
2160      * @return the result
2161      */
getJsonString(JSONObject jsonObject, String key)2162     private static String getJsonString(JSONObject jsonObject, String key) {
2163         String value = "";
2164         if (jsonObject != null && jsonObject.containsKey(key)) {
2165             value = jsonObject.get(key).toString();
2166         }
2167         return value;
2168     }
2169 
getJsonBooleanValue(JSONObject jsonObject, String key, boolean defaultValue)2170     private static boolean getJsonBooleanValue(JSONObject jsonObject, String key, boolean defaultValue) {
2171         boolean value = defaultValue;
2172         if (jsonObject != null && jsonObject.containsKey(key)) {
2173             value = jsonObject.getBooleanValue(key);
2174         }
2175         return value;
2176     }
2177 }
2178