1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "Bootstrap"
16 #include "bootstrap.h"
17
18 #include <dlfcn.h>
19
20 #include "backup_manager.h"
21 #include "backuprule/backup_rule_manager.h"
22 #include "checker/checker_manager.h"
23 #include "config_factory.h"
24 #include "directory/directory_manager.h"
25 #include "log_print.h"
26 namespace OHOS {
27 namespace DistributedData {
GetInstance()28 Bootstrap &Bootstrap::GetInstance()
29 {
30 static Bootstrap bootstrap;
31 return bootstrap;
32 }
33
GetProcessLabel()34 std::string Bootstrap::GetProcessLabel()
35 {
36 auto *global = ConfigFactory::GetInstance().GetGlobalConfig();
37 if (global == nullptr || global->processLabel.empty()) {
38 return DEFAULT_LABEL;
39 }
40 return global->processLabel;
41 }
42
GetMetaDBName()43 std::string Bootstrap::GetMetaDBName()
44 {
45 auto *global = ConfigFactory::GetInstance().GetGlobalConfig();
46 if (global == nullptr || global->metaData.empty()) {
47 return DEFAULT_META;
48 }
49 return global->metaData;
50 }
51
LoadComponents()52 void Bootstrap::LoadComponents()
53 {
54 auto *comps = ConfigFactory::GetInstance().GetComponentConfig();
55 if (comps == nullptr) {
56 return;
57 }
58 for (auto &comp : *comps) {
59 if (comp.lib.empty()) {
60 continue;
61 }
62 // no need to close the component, so we don't keep the handles
63 auto handle = dlopen(comp.lib.c_str(), RTLD_LAZY);
64 if (handle == nullptr) {
65 ZLOGE("dlopen(%{public}s) failed(%{public}d)!", comp.lib.c_str(), errno);
66 continue;
67 }
68
69 if (comp.constructor.empty()) {
70 continue;
71 }
72
73 auto ctor = reinterpret_cast<Constructor>(dlsym(handle, comp.constructor.c_str()));
74 if (ctor == nullptr) {
75 ZLOGE("dlsym(%{public}s) failed(%{public}d)!", comp.constructor.c_str(), errno);
76 continue;
77 }
78 ctor(comp.params.c_str());
79 }
80 }
81
LoadCheckers()82 void Bootstrap::LoadCheckers()
83 {
84 auto *checkers = ConfigFactory::GetInstance().GetCheckerConfig();
85 if (checkers == nullptr) {
86 return;
87 }
88 CheckerManager::GetInstance().LoadCheckers(checkers->checkers);
89 for (const auto &trust : checkers->trusts) {
90 auto *checker = CheckerManager::GetInstance().GetChecker(trust.checker);
91 if (checker == nullptr) {
92 continue;
93 }
94 checker->SetTrustInfo(trust);
95 }
96 }
97
LoadBackup(std::shared_ptr<ExecutorPool> executors)98 void Bootstrap::LoadBackup(std::shared_ptr<ExecutorPool> executors)
99 {
100 auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig();
101 if (backupRules == nullptr) {
102 return;
103 }
104 BackupRuleManager::GetInstance().LoadBackupRules(backupRules->rules);
105
106 BackupManager::BackupParam backupParam = { backupRules->schedularDelay,
107 backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber};
108 BackupManager::GetInstance().SetBackupParam(backupParam);
109 BackupManager::GetInstance().Init();
110 BackupManager::GetInstance().BackSchedule(std::move(executors));
111 }
112
LoadNetworks()113 void Bootstrap::LoadNetworks()
114 {
115 }
LoadDirectory()116 void Bootstrap::LoadDirectory()
117 {
118 auto *config = ConfigFactory::GetInstance().GetDirectoryConfig();
119 if (config == nullptr) {
120 return;
121 }
122 std::vector<DirectoryManager::Strategy> strategies(config->strategy.size());
123 for (size_t i = 0; i < config->strategy.size(); ++i) {
124 strategies[i] = config->strategy[i];
125 }
126 DirectoryManager::GetInstance().Initialize(strategies);
127 }
128 } // namespace DistributedData
129 } // namespace OHOS
130