• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cloud/cloud_config_manager.h"
24 #include "config_factory.h"
25 #include "directory/directory_manager.h"
26 #include "log_print.h"
27 namespace OHOS {
28 namespace DistributedData {
GetInstance()29 Bootstrap &Bootstrap::GetInstance()
30 {
31     static Bootstrap bootstrap;
32     return bootstrap;
33 }
34 
GetProcessLabel()35 std::string Bootstrap::GetProcessLabel()
36 {
37     auto *global = ConfigFactory::GetInstance().GetGlobalConfig();
38     if (global == nullptr || global->processLabel.empty()) {
39         return DEFAULT_LABEL;
40     }
41     return global->processLabel;
42 }
43 
GetMetaDBName()44 std::string Bootstrap::GetMetaDBName()
45 {
46     auto *global = ConfigFactory::GetInstance().GetGlobalConfig();
47     if (global == nullptr || global->metaData.empty()) {
48         return DEFAULT_META;
49     }
50     return global->metaData;
51 }
52 
LoadComponents()53 void Bootstrap::LoadComponents()
54 {
55     auto *comps = ConfigFactory::GetInstance().GetComponentConfig();
56     if (comps == nullptr) {
57         return;
58     }
59     for (auto &comp : *comps) {
60         if (comp.lib.empty()) {
61             continue;
62         }
63         // no need to close the component, so we don't keep the handles
64         auto handle = dlopen(comp.lib.c_str(), RTLD_LAZY);
65         if (handle == nullptr) {
66             ZLOGE("dlopen(%{public}s) failed(%{public}d)!", comp.lib.c_str(), errno);
67             continue;
68         }
69 
70         if (comp.constructor.empty()) {
71             continue;
72         }
73 
74         auto ctor = reinterpret_cast<Constructor>(dlsym(handle, comp.constructor.c_str()));
75         if (ctor == nullptr) {
76             ZLOGE("dlsym(%{public}s) failed(%{public}d)!", comp.constructor.c_str(), errno);
77             continue;
78         }
79         ctor(comp.params.c_str());
80     }
81 }
82 
LoadCheckers()83 void Bootstrap::LoadCheckers()
84 {
85     auto *checkers = ConfigFactory::GetInstance().GetCheckerConfig();
86     if (checkers == nullptr) {
87         return;
88     }
89     CheckerManager::GetInstance().LoadCheckers(checkers->checkers);
90     for (const auto &trust : checkers->trusts) {
91         auto *checker = CheckerManager::GetInstance().GetChecker(trust.checker);
92         if (checker == nullptr) {
93             continue;
94         }
95         checker->SetTrustInfo(trust);
96     }
97     for (const auto &distrust : checkers->distrusts) {
98         auto *checker = CheckerManager::GetInstance().GetChecker(distrust.checker);
99         if (checker == nullptr) {
100             continue;
101         }
102         checker->SetDistrustInfo(distrust);
103     }
104     for (const auto &switches : checkers->switches) {
105         auto *checker = CheckerManager::GetInstance().GetChecker(switches.checker);
106         if (checker == nullptr) {
107             continue;
108         }
109         checker->SetSwitchesInfo(switches);
110     }
111     for (const auto &dynamicStore : checkers->dynamicStores) {
112         auto *checker = CheckerManager::GetInstance().GetChecker(dynamicStore.checker);
113         if (checker == nullptr) {
114             continue;
115         }
116         checker->AddDynamicStore(dynamicStore);
117     }
118     for (const auto &staticStore : checkers->staticStores) {
119         auto *checker = CheckerManager::GetInstance().GetChecker(staticStore.checker);
120         if (checker == nullptr) {
121             continue;
122         }
123         checker->AddStaticStore(staticStore);
124     }
125 }
126 
LoadBackup(std::shared_ptr<ExecutorPool> executors)127 void Bootstrap::LoadBackup(std::shared_ptr<ExecutorPool> executors)
128 {
129     auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig();
130     if (backupRules == nullptr) {
131         return;
132     }
133     BackupRuleManager::GetInstance().LoadBackupRules(backupRules->rules);
134 
135     BackupManager::BackupParam backupParam = { backupRules->schedularDelay,
136         backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber};
137     BackupManager::GetInstance().SetBackupParam(backupParam);
138     BackupManager::GetInstance().Init();
139     BackupManager::GetInstance().BackSchedule(std::move(executors));
140 }
141 
LoadNetworks()142 void Bootstrap::LoadNetworks()
143 {
144 }
LoadDirectory()145 void Bootstrap::LoadDirectory()
146 {
147     auto *config = ConfigFactory::GetInstance().GetDirectoryConfig();
148     if (config == nullptr) {
149         return;
150     }
151     std::vector<DirectoryManager::Strategy> strategies(config->strategy.size());
152     for (size_t i = 0; i < config->strategy.size(); ++i) {
153         strategies[i] = config->strategy[i];
154     }
155     DirectoryManager::GetInstance().Initialize(strategies);
156 }
157 
LoadCloud()158 void Bootstrap::LoadCloud()
159 {
160     auto *config = ConfigFactory::GetInstance().GetCloudConfig();
161     if (config == nullptr) {
162         return;
163     }
164     std::vector<CloudConfigManager::Info> infos;
165     for (auto &info : config->mapper) {
166         infos.push_back({ info.localBundle, info.cloudBundle });
167     }
168     CloudConfigManager::GetInstance().Initialize(infos);
169 }
170 } // namespace DistributedData
171 } // namespace OHOS
172