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