• 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 APP_SERVICE = "appService";
86     private static final String REQUEST_PERMISSIONS = "requestPermissions";
87     private static final String TARGET_MODULE_NAME = "targetModuleName";
88     private static final String TARGET_PRIORITY = "targetPriority";
89     private static final String TARGET_BUNDLE_NAME = "targetBundleName";
90     private static final String DEVICE_CONFIG = "deviceConfig";
91     private static final String DEFAULT = "default";
92     private static final String COMPILE_SDK_VERSION = "compileSdkVersion";
93     private static final String COMPILE_SDK_TYPE = "compileSdkType";
94     private static final String PROXY_DATAS = "proxyDatas";
95     private static final String PROXY_DATA = "proxyData";
96     private static final String PROXY_URI = "uri";
97 
98     private static final Log LOG = new Log(ModuleJsonUtil.class.toString());
99 
100     /**
101      * get the version from json file for stage module.
102      *
103      * @param jsonString uncompress json object
104      * @return the result
105      * @throws BundleException Throws this exception if the json is not standard.
106      */
parseStageVersion(String jsonString)107     public static Version parseStageVersion(String jsonString) throws BundleException {
108         Version version = new Version();
109         JSONObject appObj = getAppObj(jsonString);
110         if (appObj.containsKey(VERSIONCODE) && appObj.containsKey(VERSIONNAME)) {
111             version.versionCode = appObj.getIntValue(VERSIONCODE);
112             version.versionName = appObj.getString(VERSIONNAME);
113         } else {
114             String errMsg = "ModuleJsonUtil:parseStageVersion json file do not contain version.";
115             LOG.error(errMsg);
116             throw new BundleException(errMsg);
117         }
118         if (appObj.containsKey(MIN_COMPATIBLE_VERSION_CODE)) {
119             version.minCompatibleVersionCode = appObj.getIntValue(MIN_COMPATIBLE_VERSION_CODE);
120         } else {
121             version.minCompatibleVersionCode = version.versionCode;
122         }
123         return version;
124     }
125 
126     /**
127      * get the version from json file for fa module.
128      *
129      * @param jsonString uncompress json object
130      * @return the result
131      * @throws BundleException Throws this exception if the json is not standard.
132      */
parseFaVersion(String jsonString)133     public static Version parseFaVersion(String jsonString) throws BundleException {
134         JSONObject appObj = getAppObj(jsonString);
135         JSONObject versionObj = appObj.getJSONObject(VERSION);
136         if (versionObj == null) {
137             LOG.error("ModuleJsonUtil:parseFaVersion failed : json file do not version.");
138             throw new BundleException("ModuleJsonUtil:parseFaVersion failed : json file do not version.");
139         }
140         Version version = new Version();
141         if (versionObj.containsKey(CODE) && versionObj.containsKey(NAME)) {
142             version.versionName = versionObj.getString(NAME);
143             version.versionCode = versionObj.getIntValue(CODE);
144         } else {
145             LOG.error("ModuleJsonUtil:parseFaVersion failed : json file do not version name or version code.");
146             throw new BundleException(
147                     "ModuleJsonUtil:parseFaVersion failed : json file do not version name or version code.");
148         }
149         if (versionObj.containsKey(MIN_COMPATIBLE_VERSION_CODE)) {
150             version.minCompatibleVersionCode = versionObj.getIntValue(MIN_COMPATIBLE_VERSION_CODE);
151         } else {
152             version.minCompatibleVersionCode = version.versionCode;
153         }
154         return version;
155     }
156 
157     /**
158      * get the bundleType fa module.
159      *
160      * @param jsonString uncompress json object
161      * @return the result
162      * @throws BundleException Throws this exception if the json is not standard.
163      */
parseFaBundleType(String jsonString)164     public static String parseFaBundleType(String jsonString) throws BundleException {
165         boolean installationFree = parseFAInstallationFree(jsonString);
166         if (installationFree) {
167             return ATOMIC_SERVICE;
168         }
169         return APP;
170     }
171 
172     /**
173      * get the apiVersion from json file for stage module.
174      *
175      * @param jsonString uncompress json object
176      * @return the result
177      * @throws BundleException Throws this exception if the json is not standard.
178      */
parseStageModuleApiVersion(String jsonString)179     public static ModuleApiVersion parseStageModuleApiVersion(String jsonString) throws BundleException {
180         JSONObject appObj = getAppObj(jsonString);
181         ModuleApiVersion moduleApiVersion = new ModuleApiVersion();
182         if (appObj.containsKey(MIN_API_VERSION)) {
183             moduleApiVersion.setCompatibleApiVersion(appObj.getIntValue(MIN_API_VERSION));
184         }
185         if (appObj.containsKey(TARGET_API_VERSION)) {
186             moduleApiVersion.setTargetApiVersion(appObj.getIntValue(TARGET_API_VERSION));
187         }
188         if (appObj.containsKey(API_RELEASE_TYPE)) {
189             moduleApiVersion.setReleaseType(appObj.getString(API_RELEASE_TYPE));
190         }
191         return moduleApiVersion;
192     }
193 
194     /**
195      * get the apiVersion from json file for fa module.
196      *
197      * @param jsonString uncompress json object
198      * @return the result
199      * @throws BundleException Throws this exception if the json is not standard.
200      */
parseFAModuleApiVersion(String jsonString)201     public static ModuleApiVersion parseFAModuleApiVersion(String jsonString) throws BundleException {
202         JSONObject appObj = getAppObj(jsonString);
203         if (!appObj.containsKey(API_VERSION)) {
204             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain apiVersion.");
205         }
206         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
207         ModuleApiVersion moduleApiVersion = new ModuleApiVersion();
208         if (apiVersionObj.containsKey(COMPATIBLE)) {
209             moduleApiVersion.setCompatibleApiVersion(apiVersionObj.getIntValue(COMPATIBLE));
210         }
211         if (apiVersionObj.containsKey(RELEASE_TYPE)) {
212             moduleApiVersion.setReleaseType(apiVersionObj.getString(RELEASE_TYPE));
213         }
214         if (apiVersionObj.containsKey(TARGET)) {
215             moduleApiVersion.setTargetApiVersion(apiVersionObj.getIntValue(TARGET));
216         }
217         return moduleApiVersion;
218     }
219 
220     /**
221      * get the module name from json file for stage module.
222      *
223      * @param jsonString uncompress json object
224      * @return the result
225      * @throws BundleException Throws this exception if the json is not standard.
226      */
parseStageModuleName(String jsonString)227     public static String parseStageModuleName(String jsonString) throws BundleException {
228         JSONObject moduleObj = getModuleObj(jsonString);
229         String moduleName;
230         if (moduleObj.containsKey(NAME)) {
231             moduleName = moduleObj.getString(NAME);
232         } else {
233             LOG.error("ModuleJsonUtil:parseStageModuleName failed: json file do not contain module name.");
234             throw new BundleException("ModuleJsonUtil:parseStageModuleName failed: json file do not contain module name.");
235         }
236         return moduleName;
237     }
238 
239     /**
240      * get the moduleName from json file for stage module.
241      *
242      * @param jsonString uncompress json object
243      * @return the result
244      * @throws BundleException Throws this exception if the json is not standard.
245      */
parseFaModuleName(String jsonString)246     public static String parseFaModuleName(String jsonString) throws BundleException {
247         String moduleName;
248         JSONObject moduleObj = getModuleObj(jsonString);
249         JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
250         if (distroObj == null) {
251             LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
252             throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
253         }
254         if (!distroObj.containsKey(MODULE_NAME)) {
255             LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain moduleName.");
256             throw new BundleException(
257                     "ModuleJsonUtil:parseFaModuleName failed: json file do not contain moduleName.");
258         }
259         moduleName = distroObj.getString(MODULE_NAME);
260         return moduleName;
261     }
262 
263     /**
264      * get the moduleName from json file for hqf.
265      *
266      * @param jsonString uncompress json object
267      * @return the result
268      * @throws BundleException Throws this exception if the json is not standard.
269      */
parsePatchModuleName(String jsonString)270     public static String parsePatchModuleName(String jsonString) throws BundleException {
271         String moduleName;
272         JSONObject moduleObj = getModuleObj(jsonString);
273         if (!moduleObj.containsKey(NAME)) {
274             LOG.error("ModuleJsonUtil:parsePatchModuleName failed: json file do not contain moduleName.");
275             throw new BundleException(
276                     "ModuleJsonUtil:parsePatchModuleName failed: json file do not contain moduleName.");
277         }
278         moduleName = moduleObj.getString(NAME);
279         return moduleName;
280     }
281 
282     /**
283      * get the package from json file for stage module.
284      *
285      * @param jsonString uncompress json object
286      * @return the result
287      * @throws BundleException Throws this exception if the json is not standard.
288      */
parseFaPackageStr(String jsonString)289     public static String parseFaPackageStr(String jsonString) throws BundleException {
290         String packageStr = "";
291         JSONObject moduleObj = getModuleObj(jsonString);
292         if (moduleObj.containsKey(PACKAGE)) {
293             packageStr = moduleObj.getString(PACKAGE);
294         } else {
295             LOG.error("ModuleJsonUtil:parseFaPackageStr failed: json file do not contain package.");
296             throw new BundleException("ModuleJsonUtil:parseFaPackageStr failed: json file do not contain package.");
297         }
298         return packageStr;
299     }
300 
301     /**
302      * get the bundleName from json file.
303      *
304      * @param jsonString uncompress json object
305      * @return the result
306      * @throws BundleException Throws this exception if the json is not standard.
307      */
parseBundleName(String jsonString)308     public static String parseBundleName(String jsonString) throws BundleException {
309         JSONObject appObject = getAppObj(jsonString);
310         String bundleName = "";
311         if (appObject.containsKey(BUNDLE_NAME)) {
312             bundleName = appObject.getString(BUNDLE_NAME);
313         } else {
314             LOG.error("ModuleJsonUtil::parseStageBundleName json object do not contain bundleNames.");
315             throw new BundleException("ModuleJsonUtil::parseStageBundleName json object do not contain bundleNames.");
316         }
317         return bundleName;
318     }
319 
320     /**
321      * get the vendor from json file.
322      *
323      * @param jsonString uncompress json object
324      * @return the result
325      * @throws BundleException Throws this exception if the json is not standard.
326      */
parseVendor(String jsonString)327     public static String parseVendor(String jsonString) throws BundleException {
328         JSONObject appObject = getAppObj(jsonString);
329         String vendor = "";
330         if (appObject.containsKey(VENDOR)) {
331             vendor = appObject.getString(VENDOR);
332         }
333         return vendor;
334     }
335 
336     /**
337      * merge two pack.info file into one pack.info file.
338      *
339      * @param finalPackInfo is the final packInfo
340      * @param srcPackInfo is the packInfo to be merged
341      * @return the result
342      * @throws BundleException Throws this exception if the json is not standard.
343      */
mergeTwoPackInfo(String finalPackInfo, String srcPackInfo)344     public static String mergeTwoPackInfo(String finalPackInfo, String srcPackInfo) throws BundleException {
345         String desPackInfo = "";
346         JSONObject finalPackObj;
347         try {
348             finalPackObj = JSON.parseObject(finalPackInfo);
349             JSONObject srcPackObj = JSON.parseObject(srcPackInfo);
350             if (!verifyPackInfo(finalPackObj, srcPackObj)) {
351                 LOG.error("ModuleJsonUtil:mergeTwoPackInfo verify pack.info failed.");
352                 throw new BundleException("ModuleJsonUtil:mergeTwoPackInfo verify pack.info failed.");
353             }
354             desPackInfo = mergePackInfoObj(finalPackObj, srcPackObj);
355         } catch (BundleException | JSONException e) {
356             LOG.error("ModuleJsonUtil:mergeTwoPackInfo merge pack.info failed.");
357             throw new BundleException("ModuleJsonUtil:mergeTwoPackInfo merge pack.info failed.");
358         }
359         return desPackInfo;
360     }
361 
362     /**
363      * verify pack.info file.
364      *
365      * @param finalPackObj is the final pack.info object
366      * @param srcPackObj is the src pack.info object
367      * @return the result
368      */
verifyPackInfo(JSONObject finalPackObj, JSONObject srcPackObj)369     public static boolean verifyPackInfo(JSONObject finalPackObj, JSONObject srcPackObj) throws BundleException {
370         if (finalPackObj == null || srcPackObj == null) {
371             LOG.error("ModuleJsonUtil:verifyPackInfo fail to read pack.info.");
372             return false;
373         }
374         JSONObject finalSummaryObj = finalPackObj.getJSONObject(SUMMARY);
375         JSONObject srcSummaryObj = srcPackObj.getJSONObject(SUMMARY);
376         if (finalSummaryObj == null || srcSummaryObj == null) {
377             LOG.error("ModuleJsonUtil:verifyPackInfo pack.info do not contain summary.");
378             return false;
379         }
380         // check app info
381         JSONObject finalAppObj = finalSummaryObj.getJSONObject(APP);
382         JSONObject srcAppObj = srcSummaryObj.getJSONObject(APP);
383         if (finalAppObj == null || srcAppObj == null) {
384             LOG.error("ModuleJsonUtil:verifyPackInfo pack.info do not contain app.");
385             return false;
386         }
387         if (!verifyAppInPackInfo(finalAppObj, srcAppObj)) {
388             LOG.error("ModuleJsonUtil:verifyPackInfo verify app failed.");
389             return false;
390         }
391 
392         return true;
393     }
394 
395     /**
396      * verify app in pack.info file.
397      *
398      * @param finalAppObj is the final pack.info app object
399      * @param srcAppObj is the src pack.info app object
400      * @return the result
401      */
verifyAppInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj)402     public static boolean verifyAppInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj) {
403         if (finalAppObj == null || srcAppObj == null) {
404             LOG.error("ModuleJsonUtil:verifyAppInPackInfo input null json object.");
405             return false;
406         }
407         // check bundleName
408         String finalBundleName = finalAppObj.getString(BUNDLE_NAME);
409         String srcBundleName = srcAppObj.getString(BUNDLE_NAME);
410         if (!finalBundleName.equals(srcBundleName)) {
411             LOG.error("ModuleJsonUtil:verifyAppInPackInfo bundleName is different.");
412             return false;
413         }
414         // check bundleType
415         if (!checkBundleTypeInPackInfo(finalAppObj, srcAppObj)) {
416             LOG.error("ModuleJsonUtil:verifyAppInPackInfo bundleType is different.");
417             return false;
418         }
419         // check version
420         JSONObject finalVersionObj = finalAppObj.getJSONObject(VERSION);
421         JSONObject srcVersionObj = srcAppObj.getJSONObject(VERSION);
422         if (finalVersionObj == null || srcVersionObj == null) {
423             LOG.error("ModuleJsonUtil:verifyAppInPackInfo version object is empty.");
424             return false;
425         }
426         String finalVersionName = finalVersionObj.getString(NAME);
427         String srcVersionName = srcVersionObj.getString(NAME);
428         if (!finalVersionName.equals(srcVersionName)) {
429             LOG.error("ModuleJsonUtil:verifyAppInPackInfo versionName is different.");
430             return false;
431         }
432         int finalVersionCode = finalVersionObj.getIntValue(CODE);
433         int srcVersionCode = srcVersionObj.getIntValue(CODE);
434         if (finalVersionCode != srcVersionCode) {
435             LOG.error("ModuleJsonUtil:verifyAppInPackInfo versionCode is different.");
436             return false;
437         }
438         return true;
439     }
440 
441     /**
442      * verify bundleType in pack.info file.
443      *
444      * @param finalAppObj is the final pack.info app objects
445      * @param srcAppObj is the src pack.info app objects
446      * @return the result
447      */
checkBundleTypeInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj)448     public static boolean checkBundleTypeInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj) {
449         if (finalAppObj.isEmpty() || srcAppObj.isEmpty()) {
450             LOG.error("ModuleJsonUtil:checkBundleTypeInPackInfo pack.info has empty module.");
451             return false;
452         }
453         String finalBundleType = "app";
454         String srcBundleType = "app";
455         if (finalAppObj.containsKey(BUNDLE_TYPE)) {
456             finalBundleType = getJsonString(finalAppObj, BUNDLE_TYPE);
457         }
458         if (srcAppObj.containsKey(BUNDLE_TYPE)) {
459             srcBundleType = getJsonString(srcAppObj, BUNDLE_TYPE);
460         }
461         if (!finalBundleType.equals(srcBundleType)) {
462             LOG.error("bundleType in pack.info is not same.");
463             return false;
464         }
465         return true;
466     }
467 
468     /**
469      * verify module in pack.info file.
470      *
471      * @param finalModuleObs is the final pack.info module objects
472      * @param srcModuleObs is the src pack.info module objects
473      * @return the result
474      */
verifyModuleInPackInfo(JSONArray finalModuleObs, JSONArray srcModuleObs)475     public static boolean verifyModuleInPackInfo(JSONArray finalModuleObs, JSONArray srcModuleObs)
476             throws BundleException {
477         if (finalModuleObs.isEmpty() || srcModuleObs.isEmpty()) {
478             LOG.error("ModuleJsonUtil:verifyModuleInPackInfo pack.info has empty module.");
479             throw new BundleException("ModuleJsonUtil:verifyModuleInPackInfo pack.info has empty module.");
480         }
481         List<String> moduleNames = new ArrayList<>();
482         for (int i = 0; i < finalModuleObs.size(); ++i) {
483             JSONObject finalModuleObj = finalModuleObs.getJSONObject(i);
484             String moduleName = parseDistroModuleName(finalModuleObj);
485             if (moduleNames.contains(moduleName)) {
486                 LOG.error("ModuleJsonUtil:verifyModuleInPackInfo duplicated moduleName.");
487                 return false;
488             } else {
489                 moduleNames.add(moduleName);
490             }
491         }
492         for (int i = 0; i < srcModuleObs.size(); ++i) {
493             JSONObject srcModuleObj = srcModuleObs.getJSONObject(i);
494             String moduleName = parseDistroModuleName(srcModuleObj);
495             if (moduleNames.contains(moduleName)) {
496                 LOG.error("ModuleJsonUtil:verifyModuleInPackInfo duplicated moduleName.");
497                 return false;
498             } else {
499                 moduleNames.add(moduleName);
500             }
501         }
502         return true;
503     }
504 
505     /**
506      * verify package name in pack.info file.
507      *
508      * @param finalPackageObs is the final pack.info objects
509      * @param srcPackageObs is the src pack.info objects
510      * @return the result
511      */
verifyPackageName(JSONArray finalPackageObs, JSONArray srcPackageObs)512     public static boolean verifyPackageName(JSONArray finalPackageObs, JSONArray srcPackageObs) {
513         if (finalPackageObs == null || finalPackageObs.isEmpty() || srcPackageObs == null || srcPackageObs.isEmpty()) {
514             LOG.error("ModuleJsonUtil:verifyPackageName pack.info has empty packages.");
515             return false;
516         }
517         List<String> packageNames = new ArrayList<>();
518         for (int i = 0; i < finalPackageObs.size(); ++i) {
519             JSONObject packageObj = finalPackageObs.getJSONObject(i);
520             String packageName = packageObj.getString(NAME);
521             if (packageNames.contains(packageName)) {
522                 LOG.error("ModuleJsonUtil:verifyPackageName duplicated package name.");
523                 return false;
524             } else {
525                 packageNames.add(packageName);
526             }
527         }
528         for (int i = 0; i < srcPackageObs.size(); ++i) {
529             JSONObject packageObj = srcPackageObs.getJSONObject(i);
530             String packageName = packageObj.getString(NAME);
531             if (packageNames.contains(packageName)) {
532                 LOG.error("ModuleJsonUtil:verifyPackageName duplicated package name.");
533                 return false;
534             } else {
535                 packageNames.add(packageName);
536             }
537         }
538         return true;
539     }
540 
541     /**
542      * parse moduleName in pack.info file.
543      *
544      * @param moduleObj is the final pack.info objects
545      * @return the result
546      */
parseDistroModuleName(JSONObject moduleObj)547     public static String parseDistroModuleName(JSONObject moduleObj) throws BundleException {
548         String moduleName = "";
549         try {
550             if (moduleObj == null) {
551                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain module.");
552                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain module.");
553             }
554             JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
555             if (distroObj == null) {
556                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
557                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed: json file do not contain distro.");
558             }
559             if (!distroObj.containsKey(MODULE_NAME)) {
560                 LOG.error("ModuleJsonUtil:parseFaModuleName failed: json file do not contain moduleName.");
561                 throw new BundleException("ModuleJsonUtil:parseFaModuleName failed:" +
562                         "json file do not contain moduleName.");
563             }
564             moduleName = distroObj.getString(MODULE_NAME);
565         } catch (BundleException e) {
566             LOG.error("ModuleJsonUtil:parseFaModuleName failed.");
567             throw new BundleException("ModuleJsonUtil:parseFaModuleName failed.");
568         }
569         return moduleName;
570     }
571 
572     /**
573      * merge pack.info file.
574      *
575      * @param finalPackinfoObj is the final pack.info objects
576      * @param srcPackinfoObj is the final pack.info objects
577      * @return the result
578      */
mergePackInfoObj(JSONObject finalPackinfoObj, JSONObject srcPackinfoObj)579     public static String mergePackInfoObj(JSONObject finalPackinfoObj, JSONObject srcPackinfoObj) throws BundleException {
580         if (finalPackinfoObj == null || srcPackinfoObj == null) {
581             String errMsg = "ModuleJsonUtil:mergePackInfoObj input an invalid json object.";
582             LOG.error(errMsg);
583             throw new BundleException(errMsg);
584         }
585         JSONObject finalSummaryObj = finalPackinfoObj.getJSONObject(SUMMARY);
586         JSONObject srcSummaryObj = srcPackinfoObj.getJSONObject(SUMMARY);
587         if (finalSummaryObj == null || srcSummaryObj == null) {
588             String errMsg = "ModuleJsonUtil:mergePackInfoObj input json file has empty summary.";
589             LOG.error(errMsg);
590             throw new BundleException(errMsg);
591         }
592         // merge modules
593         JSONArray finalModuleObs = finalSummaryObj.getJSONArray(MODULES);
594         JSONArray srcModuleObs = srcSummaryObj.getJSONArray(MODULES);
595         if (finalModuleObs == null || srcModuleObs == null) {
596             String errMsg = "ModuleJsonUtil:mergePackInfoObj input json file has empty module.";
597             LOG.error(errMsg);
598             throw new BundleException(errMsg);
599         }
600         finalModuleObs.addAll(srcModuleObs);
601         // merge packages
602         JSONArray finalPackageObs = finalPackinfoObj.getJSONArray(PACKAGES);
603         JSONArray srcPackageObs = srcPackinfoObj.getJSONArray(PACKAGES);
604         if (finalPackageObs == null || srcPackageObs == null) {
605             String errMsg = "ModuleJsonUtil:mergePackInfoObj input json file has empty packages.";
606             LOG.error(errMsg);
607             throw new BundleException(errMsg);
608         }
609         finalPackageObs.addAll(srcPackageObs);
610         return finalPackinfoObj.toString();
611     }
612 
613     /**
614      * merge two pack.info file into one pack.info file by packagePair.
615      *
616      * @param finalPackInfo is the final packInfo
617      * @param srcPackInfo is the packInfo to be merged
618      * @param packagePair is the selected packageName-moduleName pair map
619      * @return the result
620      * @throws BundleException Throws this exception if the json is not standard.
621      */
mergeTwoPackInfoByPackagePair(String finalPackInfo, String srcPackInfo, HashMap<String, String> packagePair)622     public static String mergeTwoPackInfoByPackagePair(String finalPackInfo, String srcPackInfo,
623                                                        HashMap<String, String> packagePair) throws BundleException {
624         JSONObject finalPackObj;
625         JSONObject srcPackObj;
626         try {
627             finalPackObj = JSON.parseObject(finalPackInfo);
628             srcPackObj = JSON.parseObject(srcPackInfo);
629         } catch (JSONException exception) {
630             String errMsg = "parse JSONobject failed.";
631             LOG.error(errMsg);
632             throw new BundleException(errMsg);
633         }
634         // verify app in pack.info
635         JSONObject finalSummaryObj = finalPackObj.getJSONObject(SUMMARY);
636         JSONObject finalAppObj = finalSummaryObj.getJSONObject(APP);
637         JSONObject srcSummaryObj = srcPackObj.getJSONObject(SUMMARY);
638         JSONObject srcAppObj = srcSummaryObj.getJSONObject(APP);
639         if (!verifyAppInPackInfo(finalAppObj, srcAppObj)) {
640             String errMsg = "verify pack.info failed, different version, bundleType or bundleName.";
641             LOG.error(errMsg);
642             throw new BundleException(errMsg);
643         }
644         for (HashMap.Entry<String, String> entry : packagePair.entrySet()) {
645             String packageName = entry.getKey().substring(0, entry.getKey().lastIndexOf(DOT));
646             mergeTwoPackInfoObjByPackagePair(finalPackObj, srcPackObj, packageName, entry.getValue());
647         }
648         return finalPackObj.toString();
649     }
650 
651     /**
652      * merge two pack.info file into one pack.info file by packagePair.
653      *
654      * @param finalPackObj is the final packInfo
655      * @param srcPackObj is the packInfo to be merged
656      * @param packageName is the selected packageName
657      * @param moduleName is the selected moduleName
658      * @throws BundleException Throws this exception if the json is not standard.
659      */
mergeTwoPackInfoObjByPackagePair(JSONObject finalPackObj, JSONObject srcPackObj, String packageName, String moduleName)660     public static void mergeTwoPackInfoObjByPackagePair(JSONObject finalPackObj, JSONObject srcPackObj,
661                                                         String packageName, String moduleName) throws BundleException {
662         if (finalPackObj == null || srcPackObj == null) {
663             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair failed: pack.info is not json object.";
664             LOG.error(errMsg);
665             throw new BundleException(errMsg);
666         }
667         // merge module
668         JSONObject finalSummaryObj = finalPackObj.getJSONObject(SUMMARY);
669         JSONObject srcSummaryObj = srcPackObj.getJSONObject(SUMMARY);
670         if (finalSummaryObj == null || srcSummaryObj == null) {
671             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair failed: pack.info do not contain summary.";
672             LOG.error(errMsg);
673             throw new BundleException(errMsg);
674         }
675         JSONArray finalModules = finalSummaryObj.getJSONArray(MODULES);
676         JSONArray srcModules = srcSummaryObj.getJSONArray(MODULES);
677         if (finalModules == null || srcModules == null) {
678             LOG.error("ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair input json file has empty module.");
679             throw new
680                 BundleException("ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair input json file has empty module.");
681         }
682         boolean findModule = false;
683         for (int index = 0; index < srcModules.size(); ++index) {
684             JSONObject moduleObj = srcModules.getJSONObject(index);
685             JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
686             if (distroObj.getString(MODULE_NAME).equals(moduleName)) {
687                 finalModules.add(moduleObj);
688                 findModule = true;
689                 break;
690             }
691         }
692         if (!findModule) {
693             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair" +
694                     " input json do not contain " + moduleName + ".";
695             LOG.error(errMsg);
696             throw new BundleException(errMsg);
697         }
698         // merge package
699         JSONArray finalPackages = finalPackObj.getJSONArray(PACKAGES);
700         JSONArray srcPackages = srcPackObj.getJSONArray(PACKAGES);
701         if (finalPackages == null || srcPackages == null) {
702             String errMsg =
703                     "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair failed: pack.info do not contain packages.";
704             LOG.error(errMsg);
705             throw new BundleException(errMsg);
706         }
707         boolean findPackage = false;
708         for (int index = 0; index < srcPackages.size(); ++index) {
709             JSONObject srcPackageObj = srcPackages.getJSONObject(index);
710             if (srcPackageObj.getString(NAME).equals(packageName)) {
711                 finalPackages.add(srcPackageObj);
712                 findPackage = true;
713                 break;
714             }
715         }
716         if (!findPackage) {
717             String errMsg = "ModuleJsonUtil:mergeTwoPackInfoObjByPackagePair input json do not contain "
718                     + packageName + ".";
719             LOG.error(errMsg);
720             throw new BundleException(errMsg);
721         }
722     }
723 
724     /**
725      * parse FA hap is entry hap, if it is, return device type.
726      *
727      * @param hapPath is the path of hap.
728      *
729      * @throws BundleException FileNotFoundException|IOException.
730      */
parseFaEntry(String hapPath)731     public static List<String> parseFaEntry(String hapPath) throws BundleException {
732         String configJson = FileUtils.getJsonInZips(new File(hapPath), CONFIG_JSON);
733         JSONObject faObj;
734         try {
735             faObj = JSONObject.parseObject(configJson);
736         } catch (JSONException exception) {
737             String errMsg = "parse JSONobject failed.";
738             LOG.error(errMsg);
739             throw new BundleException(errMsg);
740         }
741         JSONObject moduleObj = faObj.getJSONObject(MODULE);
742         if (moduleObj == null) {
743             String errMSg = "ModuleJsonUtil::isFaEntry error, json do not contain module.";
744             LOG.error(errMSg);
745             throw new BundleException(errMSg);
746         }
747         List<String> deviceTypes = new ArrayList<>();
748         JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
749         if (distroObj == null) {
750             String errMSg = "ModuleJsonUtil::isFaEntry error, json do not contain distro.";
751             LOG.error(errMSg);
752             throw new BundleException(errMSg);
753         }
754         String moduleType = distroObj.getString(MODULE_TYPE);
755         if (ENTRY.equals(moduleType)) {
756             deviceTypes = getDeviceTypeFromFAModule(moduleObj);
757         }
758         return deviceTypes;
759     }
760 
761     /**
762      * parse stage hap is entry hap, if it is, record device type.
763      *
764      * @param hapPath is the path of hap.
765      * @throws BundleException FileNotFoundException|IOException.
766      */
parseStageEntry(String hapPath)767     public static List<String> parseStageEntry(String hapPath) throws BundleException {
768         String moduleJson = FileUtils.getJsonInZips(new File(hapPath), MODULE_JSON);
769         JSONObject stageObj;
770         try {
771             stageObj = JSONObject.parseObject(moduleJson);
772         } catch (JSONException exception) {
773             String errMsg = "parse JSONobject failed.";
774             LOG.error(errMsg);
775             throw new BundleException(errMsg);
776         }
777         JSONObject moduleObj = stageObj.getJSONObject(MODULE);
778         if (moduleObj == null) {
779             String errMSg = "ModuleJsonUtil::isFaEntry error, json do not contain module.";
780             LOG.error(errMSg);
781             throw new BundleException(errMSg);
782         }
783         List<String> deviceTypes = new ArrayList<>();
784         String type = moduleObj.getString(TYPE);
785         if (type != null && type.equals(ENTRY)) {
786             deviceTypes = getDeviceTypesFromStageModule(moduleObj);
787         }
788 
789         return deviceTypes;
790     }
791 
792     /**
793      * get deviceType from fa module.
794      *
795      * @param moduleObj is the object of module.
796      * @return true is for entry hap  and false is other kind of hap
797      */
getDeviceTypeFromFAModule(JSONObject moduleObj)798     public static List<String> getDeviceTypeFromFAModule(JSONObject moduleObj) {
799         List<String> deviceTypes = new ArrayList<>();
800         if (moduleObj == null) {
801             return deviceTypes;
802         }
803         deviceTypes = JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPE), String.class);
804         return deviceTypes;
805     }
806 
807     /**
808      * get deviceType from stage module.
809      *
810      * @param moduleObj is the object of module.
811      * @return true is for entry hap  and false is other kind of hap
812      */
getDeviceTypesFromStageModule(JSONObject moduleObj)813     public static List<String> getDeviceTypesFromStageModule(JSONObject moduleObj) {
814         List<String> deviceTypes = new ArrayList<>();
815         if (moduleObj == null) {
816             return deviceTypes;
817         }
818         deviceTypes = JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPES), String.class);
819         return deviceTypes;
820     }
821 
822     /**
823      * parse stage hapVerifyInfo.
824      *
825      * @param hapVerifyInfo is the parse result
826      * @throws BundleException Throws this exception if the json is not standard.
827      */
parseStageHapVerifyInfo(HapVerifyInfo hapVerifyInfo)828     public static void parseStageHapVerifyInfo(HapVerifyInfo hapVerifyInfo) throws BundleException {
829         if (hapVerifyInfo.getProfileStr().isEmpty()) {
830             throw new BundleException("ModuleJsonUtil::parseStageHapVerifyInfo failed, module.json is empty.");
831         }
832         String bundleName = parseBundleName(hapVerifyInfo.getProfileStr());
833         hapVerifyInfo.setBundleName(bundleName);
834         hapVerifyInfo.setVendor(parseVendor(hapVerifyInfo.getProfileStr()));
835         hapVerifyInfo.setVersion(parseStageVersion(hapVerifyInfo.getProfileStr()));
836         hapVerifyInfo.setApiVersion(parseStageModuleApiVersion(hapVerifyInfo.getProfileStr()));
837         hapVerifyInfo.setModuleName(parseStageModuleName(hapVerifyInfo.getProfileStr()));
838         List<ModuleMetadataInfo> moduleMetadataInfos =
839                 parseModuleAllMetadata(hapVerifyInfo.getProfileStr(), hapVerifyInfo.getResourceMap());
840         hapVerifyInfo.setDistroFilter(parseStageDistroFilter(moduleMetadataInfos));
841         hapVerifyInfo.setDeviceType(parseDeviceType(hapVerifyInfo.getProfileStr()));
842         hapVerifyInfo.setAbilityNames(parseAbilityNames(hapVerifyInfo.getProfileStr()));
843         List<String> extensionAbilityNames = parseExtensionAbilityName(hapVerifyInfo.getProfileStr());
844         hapVerifyInfo.addAbilityNames(extensionAbilityNames);
845         hapVerifyInfo.setModuleType(parseModuleType(hapVerifyInfo.getProfileStr()));
846         hapVerifyInfo.setDependencyItemList(parseDependencies(hapVerifyInfo.getProfileStr(), bundleName));
847         hapVerifyInfo.setInstallationFree(parseStageInstallation(hapVerifyInfo.getProfileStr()));
848         hapVerifyInfo.setBundleType(parseStageBundleType(hapVerifyInfo.getProfileStr()));
849         hapVerifyInfo.setPreloadItems(parseAtomicServicePreloads(hapVerifyInfo.getProfileStr()));
850         hapVerifyInfo.setTargetBundleName(parseTargetBundleName(hapVerifyInfo.getProfileStr()));
851         hapVerifyInfo.setTargetPriority(parseTargetPriority(hapVerifyInfo.getProfileStr()));
852         hapVerifyInfo.setTargetModuleName(parseTargetModuleName(hapVerifyInfo.getProfileStr()));
853         hapVerifyInfo.setTargetModulePriority(parseTargetModulePriority(hapVerifyInfo.getProfileStr()));
854         hapVerifyInfo.setDebug(getDebug(hapVerifyInfo.getProfileStr()));
855         hapVerifyInfo.setCompileSdkType(getCompileSdkType(hapVerifyInfo.getProfileStr()));
856         hapVerifyInfo.setCompileSdkVersion(getCompileSdkVersion(hapVerifyInfo.getProfileStr()));
857         hapVerifyInfo.setProxyDataUris(parseProxyDataUri(hapVerifyInfo.getProfileStr()));
858     }
859 
860     /**
861      * parse FA hapVerifyInfo.
862      *
863      * @param hapVerifyInfo is the parse result
864      * @throws BundleException Throws this exception if the json is not standard.
865      */
parseFAHapVerifyInfo(HapVerifyInfo hapVerifyInfo)866     public static void parseFAHapVerifyInfo(HapVerifyInfo hapVerifyInfo) throws BundleException {
867         if (hapVerifyInfo.getProfileStr().isEmpty()) {
868             LOG.error("ModuleJsonUtil::parseFAHapVerifyInfo failed, config.json is empty.");
869             throw new BundleException("ModuleJsonUtil::parseFAHapVerifyInfo failed, config.json is empty.");
870         }
871         String bundleName = parseBundleName(hapVerifyInfo.getProfileStr());
872         hapVerifyInfo.setBundleName(bundleName);
873         hapVerifyInfo.setBundleType(parseFaBundleType(hapVerifyInfo.getProfileStr()));
874         hapVerifyInfo.setVendor(parseVendor(hapVerifyInfo.getProfileStr()));
875         hapVerifyInfo.setVersion(parseFaVersion(hapVerifyInfo.getProfileStr()));
876         hapVerifyInfo.setApiVersion(parseFAModuleApiVersion(hapVerifyInfo.getProfileStr()));
877         hapVerifyInfo.setModuleName(parseFaModuleName(hapVerifyInfo.getProfileStr()));
878         hapVerifyInfo.setDistroFilter(parseFADistroFilter(hapVerifyInfo.getProfileStr()));
879         hapVerifyInfo.setDeviceType(parseDeviceType(hapVerifyInfo.getProfileStr()));
880         hapVerifyInfo.setAbilityNames(parseAbilityNames(hapVerifyInfo.getProfileStr()));
881         hapVerifyInfo.setModuleType(parseFAIsEntry(hapVerifyInfo.getProfileStr()));
882         hapVerifyInfo.setPackageName(parseFaPackageStr(hapVerifyInfo.getProfileStr()));
883         hapVerifyInfo.setDependencyItemList(parseDependencies(hapVerifyInfo.getProfileStr(), bundleName));
884         hapVerifyInfo.setInstallationFree(parseFAInstallationFree(hapVerifyInfo.getProfileStr()));
885         hapVerifyInfo.setDebug(getFADebug(hapVerifyInfo.getProfileStr()));
886         hapVerifyInfo.setCompileSdkType(getFACompileSdkType(hapVerifyInfo.getProfileStr()));
887         hapVerifyInfo.setCompileSdkVersion(getFACompileSdkVersion(hapVerifyInfo.getProfileStr()));
888     }
889 
890     /**
891      * parse stage distroFilter.
892      *
893      * @param moduleMetadataInfos all metadata of module
894      * @return DistroFilter is the result of parsed distroFilter
895      */
parseStageDistroFilter(List<ModuleMetadataInfo> moduleMetadataInfos)896     public static DistroFilter parseStageDistroFilter(List<ModuleMetadataInfo> moduleMetadataInfos) {
897         for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) {
898             String resource = moduleMetadataInfo.resource;
899             if (resource.isEmpty()) {
900                 continue;
901             }
902             JSONObject distroFilter = JSONObject.parseObject(resource);
903             if (distroFilter.containsKey(DISTRIBUTION_FILTER)) {
904                 return JSONObject.parseObject(getJsonString(distroFilter, DISTRIBUTION_FILTER), DistroFilter.class);
905             }
906             if (distroFilter.containsKey(DISTRO_FILTER)) {
907                 return JSONObject.parseObject(getJsonString(distroFilter, DISTRO_FILTER), DistroFilter.class);
908             }
909         }
910         return new DistroFilter();
911     }
912 
913     /**
914      * parse stage parseModuleAllMetadata.
915      *
916      * @param jsonString is the string of module.json
917      * @param profileJson is the <fileName,fileString> of profile
918      * @return DistroFilter is the result of parsed distroFilter
919      */
parseModuleAllMetadata( String jsonString, HashMap<String, String> profileJson)920     public static List<ModuleMetadataInfo> parseModuleAllMetadata(
921             String jsonString, HashMap<String, String> profileJson) throws BundleException {
922         JSONObject moduleObj = getModuleObj(jsonString);
923         List<ModuleMetadataInfo> moduleMetadataInfos = new ArrayList<>();
924         if (moduleObj.containsKey(METADATA)) {
925             JSONArray metadatas = moduleObj.getJSONArray(METADATA);
926             for (int i = 0; i < metadatas.size(); ++i) {
927                 JSONObject metadata = metadatas.getJSONObject(i);
928                 moduleMetadataInfos.add(parseModuleMetadata(metadata, profileJson));
929             }
930         }
931         return moduleMetadataInfos;
932     }
933 
934     /**
935      * parse metadata info
936      *
937      * @param jsonObject Json hap json Object
938      * @param profileJson is the <fileName,fileString> of profile
939      * @return the ModuleMetadataInfo result
940      * @throws BundleException Throws this exception if the json is not standard.
941      */
parseModuleMetadata(JSONObject jsonObject, HashMap<String, String> profileJson)942     public static ModuleMetadataInfo parseModuleMetadata(JSONObject jsonObject, HashMap<String, String> profileJson)
943             throws BundleException {
944         if (jsonObject == null) {
945             throw new BundleException("ModuleJsonUtil::parseModuleMetadata failed, jsonObject is null.");
946         }
947         ModuleMetadataInfo moduleMetadataInfo = new ModuleMetadataInfo();
948         if (jsonObject.containsKey(NAME)) {
949             moduleMetadataInfo.name = getJsonString(jsonObject, NAME);
950         }
951         if (jsonObject.containsKey(VALUE)) {
952             moduleMetadataInfo.value = getJsonString(jsonObject, VALUE);
953         }
954         if (jsonObject.containsKey(RESOURCE)) {
955             moduleMetadataInfo.resource = getJsonString(jsonObject, RESOURCE);
956             String fileName = moduleMetadataInfo.resource;
957             fileName = fileName.replace(PROFILE, "");
958             fileName = fileName + JSON_PERFIX;
959             moduleMetadataInfo.resource = profileJson.get(fileName);
960         }
961         return moduleMetadataInfo;
962     }
963 
964     /**
965      * parse metadata info
966      *
967      * @param jsonString Json string of config.json
968      * @return the ModuleMetadataInfo result
969      */
parseFADistroFilter(String jsonString)970     public static DistroFilter parseFADistroFilter(String jsonString) throws BundleException {
971         DistroFilter distroFilter = new DistroFilter();
972         JSONObject jsonObj;
973         try {
974             jsonObj = JSON.parseObject(jsonString);
975             if (jsonObj.containsKey(MODULE)) {
976                 JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
977                 if (moduleObj.containsKey(DISTRO_FILTER)) {
978                     distroFilter = JSONObject.parseObject(getJsonString(moduleObj,
979                             DISTRO_FILTER), DistroFilter.class);
980                 }
981             }
982         } catch (JSONException exception) {
983             String errMsg = "parse JSONobject failed.";
984             LOG.error(errMsg);
985             throw new BundleException(errMsg);
986         }
987         return distroFilter;
988     }
989 
990     /**
991      * get deviceType from json file.
992      *
993      * @param jsonString is the json String of module.json or config.json
994      */
parseDeviceType(String jsonString)995     public static List<String> parseDeviceType(String jsonString) throws BundleException {
996         List<String> deviceType = new ArrayList<>();
997         JSONObject moduleObj = getModuleObj(jsonString);
998         if (moduleObj.containsKey(DEVICE_TYPE)) {
999             return JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPE), String.class);
1000         } else if (moduleObj.containsKey(DEVICE_TYPES)) {
1001             return JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPES), String.class);
1002         } else {
1003             return deviceType;
1004         }
1005     }
1006 
1007     /**
1008      * get ability names from json file.
1009      *
1010      * @param jsonString is the json String of module.json or config.json
1011      * @return ability names
1012      */
parseAbilityNames(String jsonString)1013     public static List<String> parseAbilityNames(String jsonString) throws BundleException {
1014         List<String> abilityNames = new ArrayList<>();
1015         JSONObject moduleObj = getModuleObj(jsonString);
1016         if (moduleObj.containsKey(ABILITIES)) {
1017             JSONArray abilityObjs = moduleObj.getJSONArray(ABILITIES);
1018             for (int i = 0; i < abilityObjs.size(); ++i) {
1019                 JSONObject abilityObj = abilityObjs.getJSONObject(i);
1020                 if (abilityObj.containsKey(NAME)) {
1021                     abilityNames.add(getJsonString(abilityObj, NAME));
1022                 }
1023             }
1024         }
1025 
1026         return abilityNames;
1027     }
1028 
1029     /**
1030      * parse stage ExtensionAbility names
1031      *
1032      * @param jsonString is the json String of module.json
1033      * @return extensionAbilityNames
1034      */
parseExtensionAbilityName(String jsonString)1035     public static List<String> parseExtensionAbilityName(String jsonString) throws BundleException {
1036         JSONObject moduleObj = getModuleObj(jsonString);
1037         List<String> extensionAbilityNames = new ArrayList<>();
1038         if (moduleObj.containsKey(EXTENSION_ABILITIES)) {
1039             JSONArray extensionAbilityObjs = moduleObj.getJSONArray(EXTENSION_ABILITIES);
1040             for (int i = 0; i < extensionAbilityObjs.size(); ++i) {
1041                 JSONObject extensionAbilityObj = extensionAbilityObjs.getJSONObject(i);
1042                 if (extensionAbilityObj.containsKey(NAME)) {
1043                     extensionAbilityNames.add(getJsonString(extensionAbilityObj, NAME));
1044                 }
1045             }
1046         }
1047         return extensionAbilityNames;
1048     }
1049 
1050     /**
1051      * parse stage module type.
1052      *
1053      * @param jsonString is the json String of module.json or config.json
1054      * @return is entry
1055      */
parseModuleType(String jsonString)1056     public static String parseModuleType(String jsonString) throws BundleException {
1057         JSONObject moduleObj = getModuleObj(jsonString);
1058         if (moduleObj.containsKey(TYPE)) {
1059             return getJsonString(moduleObj, TYPE);
1060         }
1061         return EMPTY_STRING;
1062     }
1063 
1064     /**
1065      * parse FA module type.
1066      *
1067      * @param jsonString is the json String of module.json or config.json
1068      * @return is entry
1069      */
parseFAIsEntry(String jsonString)1070     public static String parseFAIsEntry(String jsonString) throws BundleException {
1071         JSONObject moduleObj = getModuleObj(jsonString);
1072         if (moduleObj.containsKey(DISTRO)) {
1073             JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
1074             if (distroObj.containsKey(MODULE_TYPE)) {
1075                 return getJsonString(distroObj, MODULE_TYPE);
1076             }
1077         }
1078         return EMPTY_STRING;
1079     }
1080 
parseDependencies(String jsonString, String bundleName)1081     static List<DependencyItem> parseDependencies(String jsonString, String bundleName) throws BundleException {
1082         JSONObject moduleObj = getModuleObj(jsonString);
1083 
1084         List<DependencyItem> dependencyItemList = new ArrayList<>();
1085         if (!moduleObj.containsKey(DEPENDENCIES)) {
1086             return dependencyItemList;
1087         }
1088         JSONArray dependencyObjList = moduleObj.getJSONArray(DEPENDENCIES);
1089         for (int i = 0; i < dependencyObjList.size(); ++i) {
1090             JSONObject object = dependencyObjList.getJSONObject(i);
1091             DependencyItem item = new DependencyItem();
1092             if (object.containsKey(BUNDLE_NAME)) {
1093                 item.setBundleName(object.getString(BUNDLE_NAME));
1094             } else {
1095                 item.setBundleName(bundleName);
1096             }
1097             if (object.containsKey(MODULE_NAME)) {
1098                 item.setModuleName(object.getString(MODULE_NAME));
1099             }
1100             dependencyItemList.add(item);
1101         }
1102         return dependencyItemList;
1103     }
1104 
parseStageInstallation(String jsonString)1105     static boolean parseStageInstallation(String jsonString) throws BundleException {
1106         JSONObject moduleObj = getModuleObj(jsonString);
1107 
1108         if (moduleObj.containsKey(INSTALLATION_FREE)) {
1109             return moduleObj.getBoolean(INSTALLATION_FREE);
1110         }
1111         return false;
1112     }
1113 
parseStageBundleType(String jsonString)1114     static String parseStageBundleType(String jsonString) throws BundleException {
1115         JSONObject moduleObj = getModuleObj(jsonString);
1116         String moduleName = parseStageModuleName(jsonString);
1117         if (!moduleObj.containsKey(TYPE)) {
1118             String errMsg = "parse failed, input module.json is invalid, " +
1119                     "module.json has no type in module: " + moduleName;
1120             LOG.error(errMsg);
1121             throw new BundleException(errMsg);
1122         }
1123         String type = moduleObj.getString(TYPE);
1124         boolean installationFree = getJsonBooleanValue(moduleObj, INSTALLATION_FREE, false);
1125         JSONObject appObj = getAppObj(jsonString);
1126         if (!appObj.containsKey(BUNDLE_TYPE)) {
1127             if (installationFree) {
1128                 String errMessage = "The app.json5 file configuration does not match the installationFree:" +
1129                         " true settings. Add the bundleType field to the app.json5 file and set it atomicService.";
1130                 LOG.error(errMessage);
1131                 throw new BundleException(errMessage);
1132             }
1133             return APP;
1134         } else {
1135             String bundleType = getJsonString(appObj, BUNDLE_TYPE);
1136             if (bundleType.equals(APP)) {
1137                 if (installationFree) {
1138                     String errMsg = "installationFree must be false in module(" +
1139                             moduleName + ") when bundleType is app.";
1140                     LOG.error(errMsg);
1141                     throw new BundleException(errMsg);
1142                 }
1143                 return APP;
1144             } else if (bundleType.equals(ATOMIC_SERVICE)) {
1145                 if (!installationFree) {
1146                     String errMsg = "installationFree must be true in module(" +
1147                             moduleName + ") when bundleType is atomicService.";
1148                     LOG.error(errMsg);
1149                     throw new BundleException(errMsg);
1150                 }
1151                 return ATOMIC_SERVICE;
1152             } else if (SHARED.equals(bundleType)) {
1153                 if (!SHARED.equals(type)) {
1154                     String errMsg = "type must be shared in module(" + moduleName + ") when bundleType is shared.";
1155                     LOG.error(errMsg);
1156                     throw new BundleException(errMsg);
1157                 }
1158                 return SHARED;
1159             } else if (APP_SERVICE.equals(bundleType)) {
1160                 return APP_SERVICE;
1161             } else {
1162                 LOG.error("bundleType is invalid in app.json.");
1163                 throw new BundleException("bundleType is invalid in app.json.");
1164             }
1165         }
1166     }
1167 
parseAtomicServicePreloads(String jsonString)1168     static List<PreloadItem> parseAtomicServicePreloads(String jsonString) throws BundleException {
1169         List<PreloadItem> preloadItems = new ArrayList<>();
1170         JSONObject moduleObj = getModuleObj(jsonString);
1171         JSONObject atomicServiceObj = null;
1172         if (!moduleObj.containsKey(ATOMIC_SERVICE)) {
1173             return preloadItems;
1174         }
1175         atomicServiceObj = moduleObj.getJSONObject(ATOMIC_SERVICE);
1176         if (!atomicServiceObj.containsKey(PRELOADS)) {
1177             return preloadItems;
1178         }
1179         JSONArray preloadObjs = atomicServiceObj.getJSONArray(PRELOADS);
1180         for (int i = 0; i < preloadObjs.size(); ++i) {
1181             PreloadItem preloadItem = new PreloadItem();
1182             JSONObject itemObj = preloadObjs.getJSONObject(i);
1183             if (itemObj.containsKey(MODULE_NAME)) {
1184                 preloadItem.setModuleName(getJsonString(itemObj, MODULE_NAME));
1185             }
1186             preloadItems.add(preloadItem);
1187         }
1188         return preloadItems;
1189     }
1190 
parseProxyDataUri(String jsonString)1191     static List<String> parseProxyDataUri(String jsonString) throws BundleException {
1192         List<String> proxyDataUris = new ArrayList<>();
1193         JSONObject moduleObj = getModuleObj(jsonString);
1194         if (!moduleObj.containsKey(PROXY_DATAS) && !moduleObj.containsKey(PROXY_DATA)) {
1195             return proxyDataUris;
1196         } else if (moduleObj.containsKey(PROXY_DATA)) {
1197             JSONArray proxyData = moduleObj.getJSONArray(PROXY_DATA);
1198             for (int i = 0; i < proxyData.size(); ++i) {
1199                 JSONObject itemObj = proxyData.getJSONObject(i);
1200                 if (!itemObj.containsKey(PROXY_URI)) {
1201                     LOG.error("parse JOSNObject failed in parseProxyDataUri.");
1202                     throw new BundleException("parse JOSNObject failed in parseProxyDataUri.");
1203                 }
1204                 String uri = itemObj.getString(PROXY_URI);
1205                 proxyDataUris.add(uri);
1206             }
1207         } else {
1208             JSONArray proxyDatas = moduleObj.getJSONArray(PROXY_DATAS);
1209             for (int i = 0; i < proxyDatas.size(); ++i) {
1210                 JSONObject itemObj = proxyDatas.getJSONObject(i);
1211                 if (!itemObj.containsKey(PROXY_URI)) {
1212                     LOG.error("parse JOSNObject failed in parseProxyDataUri.");
1213                     throw new BundleException("parse JOSNObject failed in parseProxyDataUri.");
1214                 }
1215                 String uri = itemObj.getString(PROXY_URI);
1216                 proxyDataUris.add(uri);
1217             }
1218         }
1219         return proxyDataUris;
1220     }
1221 
getAppObj(String jsonString)1222     static JSONObject getAppObj(String jsonString) throws BundleException {
1223         JSONObject jsonObject;
1224         try {
1225             jsonObject = JSON.parseObject(jsonString);
1226         } catch (JSONException exception) {
1227             String errMsg = "parse JSONobject failed.";
1228             LOG.error(errMsg);
1229             throw new BundleException(errMsg);
1230         }
1231         JSONObject appObj = jsonObject.getJSONObject(APP);
1232         if (appObj == null) {
1233             LOG.error("ModuleJsonUtil::parseStageInstallation json do not contain app.");
1234             throw new BundleException("ModuleJsonUtil::parseStageInstallation json do not contain app.");
1235         }
1236         return appObj;
1237     }
1238 
getModuleObj(String jsonString)1239     static JSONObject getModuleObj(String jsonString) throws BundleException {
1240         JSONObject jsonObj;
1241         try {
1242             jsonObj = JSON.parseObject(jsonString);
1243         } catch (JSONException exception) {
1244             String errMsg = "parse JSONobject failed.";
1245             LOG.error(errMsg);
1246             throw new BundleException(errMsg);
1247         }
1248         JSONObject moduleObj = jsonObj.getJSONObject(MODULE);
1249         if (moduleObj == null) {
1250             LOG.error("ModuleJsonUtil::parseStageInstallation json do not contain module.");
1251             throw new BundleException("ModuleJsonUtil::parseStageInstallation json do not contain module.");
1252         }
1253         return moduleObj;
1254     }
1255 
parseTargetBundleName(String jsonString)1256     static String parseTargetBundleName(String jsonString) throws BundleException {
1257         JSONObject appObject = getAppObj(jsonString);
1258         String targetBundleName = "";
1259         if (appObject.containsKey(TARGET_BUNDLE_NAME)) {
1260             targetBundleName = appObject.getString(TARGET_BUNDLE_NAME);
1261         }
1262         return targetBundleName;
1263     }
1264 
parseTargetPriority(String jsonString)1265     static int parseTargetPriority(String jsonString) throws BundleException {
1266         JSONObject appObject = getAppObj(jsonString);
1267         int targetPriority = 0;
1268         if (appObject.containsKey(TARGET_PRIORITY)) {
1269             targetPriority = appObject.getIntValue(TARGET_PRIORITY);
1270         }
1271         return targetPriority;
1272     }
1273 
parseTargetModuleName(String jsonString)1274     static String parseTargetModuleName(String jsonString) throws BundleException {
1275         JSONObject moduleObj = getModuleObj(jsonString);
1276         String targetModuleName = "";
1277         if (moduleObj.containsKey(TARGET_MODULE_NAME)) {
1278             targetModuleName = moduleObj.getString(TARGET_MODULE_NAME);
1279         }
1280         return targetModuleName;
1281     }
1282 
parseTargetModulePriority(String jsonString)1283     static int parseTargetModulePriority(String jsonString) throws BundleException {
1284         JSONObject moduleObj = getModuleObj(jsonString);
1285         int targetModulePriority = 0;
1286         if (moduleObj.containsKey(TARGET_PRIORITY)) {
1287             targetModulePriority = moduleObj.getIntValue(TARGET_PRIORITY);
1288         }
1289         return targetModulePriority;
1290     }
1291 
parseFAInstallationFree(String jsonString)1292     static boolean parseFAInstallationFree(String jsonString) throws BundleException {
1293         JSONObject moduleObj = getModuleObj(jsonString);
1294         JSONObject distroObj = moduleObj.getJSONObject(DISTRO);
1295         if (distroObj == null) {
1296             LOG.error("ModuleJsonUtil::parseStageInstallation json do not contain distro.");
1297             throw new BundleException("ModuleJsonUtil::parseStageInstallation json do not contain distro.");
1298         }
1299         if (distroObj.containsKey(INSTALLATION_FREE)) {
1300             return distroObj.getBoolean(INSTALLATION_FREE);
1301         }
1302         return false;
1303     }
1304 
1305     /**
1306      * get hqfVerifyINfo from hqf file
1307      *
1308      * @param hqfPath is the file path of hqf file
1309      * @return HQFVerifyInfo
1310      */
parseHQFInfo(String hqfPath)1311     static HQFInfo parseHQFInfo(String hqfPath) throws BundleException {
1312         File hqfFile = new File(hqfPath);
1313         String patchJson = FileUtils.getJsonInZips(hqfFile, PATCH_JSON);
1314         return parsePatch(patchJson);
1315     }
1316 
1317     /**
1318      * parse patch.json form json string.
1319      *
1320      * @param jsonString is the file path of hqf file
1321      * @return HQFVerifyInfo
1322      */
parsePatch(String jsonString)1323     static HQFInfo parsePatch(String jsonString) throws BundleException {
1324         JSONObject appObj = getAppObj(jsonString);
1325         HQFInfo hqfVerifyInfo = new HQFInfo();
1326         if (appObj.containsKey(BUNDLE_NAME)) {
1327             hqfVerifyInfo.setBundleName(appObj.getString(BUNDLE_NAME));
1328         }
1329         if (appObj.containsKey(VERSIONCODE)) {
1330             hqfVerifyInfo.setVersionCode(appObj.getIntValue(VERSIONCODE));
1331         }
1332         if (appObj.containsKey(VERSIONNAME)) {
1333             hqfVerifyInfo.setVersionName(appObj.getString(VERSIONNAME));
1334         }
1335         if (appObj.containsKey(PATCH_VERSION_CODE)) {
1336             hqfVerifyInfo.setPatchVersionCode(appObj.getIntValue(PATCH_VERSION_CODE));
1337         }
1338         if (appObj.containsKey(PATCH_VERSION_NAME)) {
1339             hqfVerifyInfo.setPatchVersionName(appObj.getString(PATCH_VERSION_NAME));
1340         }
1341 
1342         JSONObject moduleObj = getModuleObj(jsonString);
1343         if (moduleObj.containsKey(NAME)) {
1344             hqfVerifyInfo.setModuleName(moduleObj.getString(NAME));
1345         }
1346         if (moduleObj.containsKey(TYPE)) {
1347             hqfVerifyInfo.setType(moduleObj.getString(TYPE));
1348         }
1349         if (moduleObj.containsKey(DEVICE_TYPES)) {
1350             hqfVerifyInfo.setDeviceTypes(JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPES), String.class));
1351         }
1352         if (moduleObj.containsKey(ORIGINAL_MODULE_HASH)) {
1353             hqfVerifyInfo.setOriginalModuleHash(moduleObj.getString(ORIGINAL_MODULE_HASH));
1354         }
1355         return hqfVerifyInfo;
1356     }
1357 
1358     /**
1359      * determine whether it is a native compression libs.
1360      *
1361      * @param jsonString is the file path of hqf file
1362      * @return the result
1363      */
stageIsCompressNativeLibs(String jsonString)1364     public static boolean stageIsCompressNativeLibs(String jsonString) throws BundleException {
1365         JSONObject moduleObj = getModuleObj(jsonString);
1366         if (moduleObj.containsKey(COMPRESS_NATIVE_LIBS)) {
1367             return moduleObj.getBoolean(COMPRESS_NATIVE_LIBS);
1368         }
1369 
1370         return false;
1371     }
1372 
1373     /**
1374      * get asanEnabled in module.json
1375      *
1376      * @param jsonString is the file content of module.json
1377      * @return the value of asanEnabled
1378      */
getStageAsanEnabled(String jsonString)1379     public static boolean getStageAsanEnabled(String jsonString) throws BundleException {
1380         JSONObject appObj = getAppObj(jsonString);
1381         if (appObj.containsKey(ASAN_ENABLED)) {
1382             return appObj.getBoolean(ASAN_ENABLED);
1383         }
1384         return false;
1385     }
1386 
1387     /**
1388      * get apiReleaseType in module.json
1389      *
1390      * @param jsonString is the file content of module.json
1391      * @return the result
1392      */
getStageApiReleaseType(String jsonString)1393     public static String getStageApiReleaseType(String jsonString) throws BundleException {
1394         JSONObject appObj = getAppObj(jsonString);
1395         return getJsonString(appObj, API_RELEASE_TYPE);
1396     }
1397 
1398     /**
1399      * get debug in module.json
1400      *
1401      * @param jsonString is the file content of module.json
1402      * @return the result
1403      */
getDebug(String jsonString)1404     public static boolean getDebug(String jsonString) throws BundleException {
1405         JSONObject appObj = getAppObj(jsonString);
1406 
1407         return getJsonBooleanValue(appObj, DEBUG, false);
1408     }
1409 
1410     /**
1411      * get debug in config.json
1412      *
1413      * @param jsonString is the file content of module.json
1414      * @return the result
1415      */
getFADebug(String jsonString)1416     public static boolean getFADebug(String jsonString) throws BundleException {
1417         JSONObject jsonObject;
1418         try {
1419             jsonObject = JSON.parseObject(jsonString);
1420         } catch (JSONException exception) {
1421             LOG.error("parse JOSNObject failed in getStageApiReleaseType.");
1422             throw new BundleException("parse JOSNObject failed in getStageApiReleaseType.");
1423         }
1424         JSONObject deviceConfigObj = jsonObject.getJSONObject(DEVICE_CONFIG);
1425         if (deviceConfigObj == null) {
1426             return false;
1427         }
1428         JSONObject defaultObj = deviceConfigObj.getJSONObject(DEFAULT);
1429         if (defaultObj == null) {
1430             return false;
1431         }
1432 
1433         return getJsonBooleanValue(defaultObj, DEBUG, false);
1434     }
1435 
1436     /**
1437      * get compileSdkVersion in config.json
1438      *
1439      * @param jsonString is the file content of module.json
1440      * @return the result
1441      */
getFACompileSdkVersion(String jsonString)1442     public static String getFACompileSdkVersion(String jsonString) throws BundleException {
1443         JSONObject appObj = getAppObj(jsonString);
1444         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
1445         String compileSdkVersion = "";
1446         if (apiVersionObj.containsKey(COMPILE_SDK_VERSION)) {
1447             compileSdkVersion = apiVersionObj.getString(COMPILE_SDK_VERSION);
1448         }
1449         return compileSdkVersion;
1450     }
1451 
1452     /**
1453      * get compileSdkType in config.json
1454      *
1455      * @param jsonString is the file content of module.json
1456      * @return the result
1457      */
getFACompileSdkType(String jsonString)1458     public static String getFACompileSdkType(String jsonString) throws BundleException {
1459         JSONObject appObj = getAppObj(jsonString);
1460         if (!appObj.containsKey(API_VERSION)) {
1461             throw new BundleException("ModuleJsonUtil::parseFAAPIVersion json file do not contain apiVersion.");
1462         }
1463         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
1464         String compileSdkType = "";
1465         if (apiVersionObj.containsKey(COMPILE_SDK_TYPE)) {
1466             compileSdkType = apiVersionObj.getString(COMPILE_SDK_TYPE);
1467         }
1468         return compileSdkType;
1469     }
1470 
1471     /**
1472      * get compileSdkVersion in module.json
1473      *
1474      * @param jsonString is the file content of module.json
1475      * @return the result
1476      */
getCompileSdkVersion(String jsonString)1477     public static String getCompileSdkVersion(String jsonString) throws BundleException {
1478         String compileSdkVersion = "";
1479         JSONObject appObj = getAppObj(jsonString);
1480         if (appObj.containsKey(COMPILE_SDK_VERSION)) {
1481             compileSdkVersion = appObj.getString(COMPILE_SDK_VERSION);
1482         } else {
1483             LOG.warning("getCompileSdkType failed: json file do not contain module compileSdkVersion.");
1484         }
1485         return compileSdkVersion;
1486     }
1487 
1488     /**
1489      * get compileSdkType in module.json
1490      *
1491      * @param jsonString is the file content of module.json
1492      * @return the result
1493      */
getCompileSdkType(String jsonString)1494     public static String getCompileSdkType(String jsonString) throws BundleException {
1495         String compileSdkType = "";
1496         JSONObject appObj = getAppObj(jsonString);
1497         if (appObj.containsKey(COMPILE_SDK_TYPE)) {
1498             compileSdkType = appObj.getString(COMPILE_SDK_TYPE);
1499         } else {
1500             LOG.warning("getCompileSdkType failed: json file do not contain module compileSdkType.");
1501         }
1502         return compileSdkType;
1503     }
1504 
1505     /**
1506      * get targetModuleName in module.json
1507      *
1508      * @param jsonString is the file content of module.json
1509      * @return the result
1510      */
getStageTargetModuleName(String jsonString)1511     public static String getStageTargetModuleName(String jsonString) throws BundleException {
1512         JSONObject moduleObj = getModuleObj(jsonString);
1513         return getJsonString(moduleObj, TARGET_MODULE_NAME);
1514     }
1515 
1516     /**
1517      * get targetBundleName in module.json
1518      *
1519      * @param jsonString is the file content of module.json
1520      * @return the result
1521      */
getStageTargetBundleName(String jsonString)1522     public static String getStageTargetBundleName(String jsonString) throws BundleException {
1523         JSONObject appObj = getAppObj(jsonString);
1524         return getJsonString(appObj, TARGET_BUNDLE_NAME);
1525     }
1526 
1527     /**
1528      * is existed requestPermission in module.json
1529      *
1530      * @param jsonString is the file content of module.json
1531      * @return the result
1532      */
isExistedStageRequestPermissions(String jsonString)1533     public static boolean isExistedStageRequestPermissions(String jsonString) throws BundleException {
1534         return isExistedProperty(jsonString, MODULE, REQUEST_PERMISSIONS);
1535     }
1536 
1537     /**
1538      * is existed targetPriority in module.json
1539      *
1540      * @param jsonString is the file content of module.json
1541      * @return the result
1542      */
isExistedStageModuleTargetPriority(String jsonString)1543     public static boolean isExistedStageModuleTargetPriority(String jsonString) throws BundleException {
1544         return isExistedProperty(jsonString, MODULE, TARGET_PRIORITY);
1545     }
1546 
1547     /**
1548      * is existed targetPriority in app.json
1549      *
1550      * @param jsonString is the file content of module.json
1551      * @return the result
1552      */
isExistedStageAppTargetPriority(String jsonString)1553     public static boolean isExistedStageAppTargetPriority(String jsonString) throws BundleException {
1554         return isExistedProperty(jsonString, APP, TARGET_PRIORITY);
1555     }
1556 
isExistedProperty(String jsonString, String fatherProperty, String childProperty)1557     private static boolean isExistedProperty(String jsonString, String fatherProperty,
1558                                              String childProperty) throws BundleException {
1559         JSONObject jsonObject;
1560         try {
1561             jsonObject = JSON.parseObject(jsonString);
1562         } catch (JSONException exception) {
1563             LOG.error("parse JOSNObject failed in isExistedProperty.");
1564             throw new BundleException("parse JOSNObject failed in isExistedProperty.");
1565         }
1566         JSONObject appObj = jsonObject.getJSONObject(fatherProperty);
1567         if (appObj == null) {
1568             LOG.error("parse failed, input module.json is invalid, module.json has no " + fatherProperty + ".");
1569             throw new BundleException("parse failed, input module.json is invalid, module.json has no " +
1570                     fatherProperty + ".");
1571         }
1572         return appObj.containsKey(childProperty);
1573     }
1574 
1575     /**
1576      * get asanEnabled in config.json
1577      *
1578      * @param jsonString is the file content of module.json
1579      * @return the value of asanEnabled
1580      */
getFAAsanEnabled(String jsonString)1581     public static boolean getFAAsanEnabled(String jsonString) throws BundleException {
1582         JSONObject appObj = getAppObj(jsonString);
1583         if (appObj.containsKey(ASAN_ENABLED)) {
1584             return appObj.getBoolean(ASAN_ENABLED);
1585         }
1586         return false;
1587     }
1588 
1589     /**
1590      * get releaseType in config.json
1591      *
1592      * @param jsonString is the file content of config.json
1593      * @return the result
1594      */
getFAReleaseType(String jsonString)1595     public static String getFAReleaseType(String jsonString) throws BundleException {
1596         JSONObject appObj = getAppObj(jsonString);
1597         JSONObject apiVersionObj = appObj.getJSONObject(API_VERSION);
1598         if (apiVersionObj == null) {
1599             return "";
1600         }
1601         return getJsonString(apiVersionObj, RELEASE_TYPE);
1602     }
1603 
1604     /**
1605      * check module atomic service is valid
1606      *
1607      * @param jsonString is the file content of config.json
1608      * @return the result
1609      * @throws BundleException Throws this exception if the json is not standard.
1610      */
isModuleAtomicServiceValid(String jsonString)1611     public static boolean isModuleAtomicServiceValid(String jsonString) throws BundleException {
1612         JSONObject moduleObj = getModuleObj(jsonString);
1613         if (!moduleObj.containsKey(ATOMIC_SERVICE)) {
1614             return true;
1615         }
1616         JSONObject appObj = getAppObj(jsonString);
1617 
1618         if (moduleObj.containsKey(ATOMIC_SERVICE) && (!appObj.containsKey(BUNDLE_TYPE) ||
1619                 !getJsonString(appObj, BUNDLE_TYPE).equals(ATOMIC_SERVICE))) {
1620             LOG.error("module can not config atomicService when bundleType is not atomicService.");
1621             return false;
1622         }
1623         return true;
1624     }
1625 
1626     /**
1627      * check entry module must contain at least one ability.
1628      *
1629      * @param jsonString Indicates the jsonString.
1630      * @return Returns true if jsonString is valid.
1631      */
checkEntryInAtomicService(String jsonString)1632     public static boolean checkEntryInAtomicService(String jsonString) throws BundleException {
1633         if (!parseStageBundleType(jsonString).equals(ATOMIC_SERVICE)) {
1634             return true;
1635         }
1636         if (parseModuleType(jsonString).equals(ENTRY) && parseAbilityNames(jsonString).isEmpty()) {
1637             LOG.error("entry module must contain at least one ability.");
1638             return false;
1639         }
1640         return true;
1641     }
1642 
1643     /**
1644      * check module atomic installation free is valid
1645      *
1646      * @param jsonString is the file content of config.json
1647      * @return the result
1648      * @throws BundleException Throws this exception if the json is not standard.
1649      */
checkAtomicServiceInstallationFree(String jsonString)1650     public static boolean checkAtomicServiceInstallationFree(String jsonString) throws BundleException {
1651         JSONObject moduleObj = getModuleObj(jsonString);
1652         JSONObject appObj = getAppObj(jsonString);
1653 
1654         boolean installationFree = getJsonBooleanValue(moduleObj, INSTALLATION_FREE, false);
1655         if (!appObj.containsKey(BUNDLE_TYPE)) {
1656             if (installationFree) {
1657                 String errMessage = "The app.json5 file configuration does not match the installationFree:" +
1658                         " true settings. Add the bundleType field to the app.json5 file and set it atomicService.";
1659                 LOG.error(errMessage);
1660                 return false;
1661             }
1662             return true;
1663         }
1664         String bundleType = getJsonString(appObj, BUNDLE_TYPE);
1665         if (bundleType.equals(APP)) {
1666             if (installationFree) {
1667                 LOG.error("installationFree must be false when bundleType is app.");
1668                 return false;
1669             }
1670         } else if (bundleType.equals(ATOMIC_SERVICE)) {
1671             if (!installationFree) {
1672                 LOG.error("installationFree must be true when bundleType is atomicService.");
1673                 return false;
1674             }
1675         } else if (SHARED.equals(bundleType)) {
1676             if (installationFree) {
1677                 LOG.error("installationFree must be false when bundleType is shared.");
1678                 return false;
1679             }
1680         } else if (APP_SERVICE.equals(bundleType)) {
1681             if (installationFree) {
1682                 LOG.error("installationFree must be false when bundleType is appService.");
1683                 return false;
1684             }
1685         } else {
1686             LOG.error("bundleType is invalid in app.json.");
1687             return false;
1688         }
1689         return true;
1690     }
1691 
1692     /**
1693      * get the String from JSONObject by the key.
1694      *
1695      * @param jsonObject uncompress json object
1696      * @param key value key
1697      * @return the result
1698      */
getJsonString(JSONObject jsonObject, String key)1699     private static String getJsonString(JSONObject jsonObject, String key) {
1700         String value = "";
1701         if (jsonObject != null && jsonObject.containsKey(key)) {
1702             value = jsonObject.get(key).toString();
1703         }
1704         return value;
1705     }
1706 
getJsonBooleanValue(JSONObject jsonObject, String key, boolean defaultValue)1707     private static boolean getJsonBooleanValue(JSONObject jsonObject, String key, boolean defaultValue) {
1708         boolean value = defaultValue;
1709         if (jsonObject != null && jsonObject.containsKey(key)) {
1710             value = jsonObject.getBooleanValue(key);
1711         }
1712         return value;
1713     }
1714 }
1715