• 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 "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     for (const auto &distrust : checkers->distrusts) {
97         auto *checker = CheckerManager::GetInstance().GetChecker(distrust.checker);
98         if (checker == nullptr) {
99             continue;
100         }
101         checker->SetDistrustInfo(distrust);
102     }
103 }
104 
LoadBackup(std::shared_ptr<ExecutorPool> executors)105 void Bootstrap::LoadBackup(std::shared_ptr<ExecutorPool> executors)
106 {
107     auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig();
108     if (backupRules == nullptr) {
109         return;
110     }
111     BackupRuleManager::GetInstance().LoadBackupRules(backupRules->rules);
112 
113     BackupManager::BackupParam backupParam = { backupRules->schedularDelay,
114         backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber};
115     BackupManager::GetInstance().SetBackupParam(backupParam);
116     BackupManager::GetInstance().Init();
117     BackupManager::GetInstance().BackSchedule(std::move(executors));
118 }
119 
LoadNetworks()120 void Bootstrap::LoadNetworks()
121 {
122 }
LoadDirectory()123 void Bootstrap::LoadDirectory()
124 {
125     auto *config = ConfigFactory::GetInstance().GetDirectoryConfig();
126     if (config == nullptr) {
127         return;
128     }
129     std::vector<DirectoryManager::Strategy> strategies(config->strategy.size());
130     for (size_t i = 0; i < config->strategy.size(); ++i) {
131         strategies[i] = config->strategy[i];
132     }
133     DirectoryManager::GetInstance().Initialize(strategies);
134 }
135 } // namespace DistributedData
136 } // namespace OHOS
137