• 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.devtools.datasources.utils.plugin.service;
17 
18 import ohos.devtools.datasources.utils.common.util.FileUtils;
19 import ohos.devtools.datasources.utils.device.entity.DeviceType;
20 import ohos.devtools.datasources.utils.plugin.IPluginConfig;
21 import ohos.devtools.datasources.utils.plugin.entity.AnalysisType;
22 import ohos.devtools.datasources.utils.plugin.entity.PluginConf;
23 import ohos.devtools.datasources.utils.plugin.entity.PluginMode;
24 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
25 import ohos.devtools.datasources.utils.session.service.SessionManager;
26 import ohos.devtools.views.layout.chartview.ProfilerMonitorItem;
27 import org.apache.commons.collections.map.MultiValueMap;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 
32 import java.io.File;
33 import java.lang.reflect.InvocationTargetException;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Comparator;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Objects;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
42 
43 /**
44  * PlugManager
45  *
46  * @since 2021/5/20
47  */
48 public class PlugManager {
49     private static final Logger LOGGER = LogManager.getLogger(PlugManager.class);
50     private static volatile PlugManager singleton;
51 
52     private final MultiValueMap profilerConfigMap = new MultiValueMap();
53     private final List<PluginConf> confLists = new ArrayList<>();
54     private String systemOsName = "";
55 
PlugManager()56     private PlugManager() {
57     }
58 
59     /**
60      * getInstance
61      *
62      * @return PlugManager
63      */
getInstance()64     public static PlugManager getInstance() {
65         if (singleton == null) {
66             synchronized (PlugManager.class) {
67                 if (singleton == null) {
68                     singleton = new PlugManager();
69                 }
70             }
71         }
72         return singleton;
73     }
74 
75     /**
76      * get Plugin Config
77      *
78      * @param deviceType deviceType
79      * @param pluginMode pluginMode
80      * @param analysisType analysisType
81      * @return List <PluginConf>
82      */
getPluginConfig(DeviceType deviceType, PluginMode pluginMode, AnalysisType analysisType)83     public List<PluginConf> getPluginConfig(DeviceType deviceType, PluginMode pluginMode, AnalysisType analysisType) {
84         if (ProfilerLogManager.isInfoEnabled()) {
85             LOGGER.info("getPluginConfig");
86         }
87         List<PluginConf> collect;
88         if (Objects.nonNull(analysisType)) {
89             collect = confLists.stream().filter(pluginConf -> pluginConf.getAnalysisTypes().contains(analysisType))
90                 .collect(Collectors.toList());
91         } else {
92             collect = confLists;
93         }
94         if (Objects.isNull(pluginMode)) {
95             if (Objects.isNull(deviceType)) {
96                 return collect;
97             }
98             return collect.stream().filter(hiProfilerPluginConf -> {
99                 List<DeviceType> supportDeviceTypes = hiProfilerPluginConf.getSupportDeviceTypes();
100                 if (supportDeviceTypes.isEmpty()) {
101                     return hiProfilerPluginConf.isEnable();
102                 } else {
103                     return supportDeviceTypes.contains(deviceType) && hiProfilerPluginConf.isEnable();
104                 }
105             }).collect(Collectors.toList());
106         }
107         return collect.stream().filter(hiProfilerPluginConf -> {
108             List<DeviceType> supportDeviceTypes = hiProfilerPluginConf.getSupportDeviceTypes();
109             if (supportDeviceTypes.isEmpty()) {
110                 return hiProfilerPluginConf.isEnable() && hiProfilerPluginConf.getPluginMode() == pluginMode;
111             } else {
112                 return supportDeviceTypes.contains(deviceType) && hiProfilerPluginConf.isEnable()
113                     && hiProfilerPluginConf.getPluginMode() == pluginMode;
114             }
115         }).collect(Collectors.toList());
116     }
117 
118     /**
119      * loadingPlug
120      *
121      * @param pluginConfigs pluginConfigs
122      */
123     public void loadingPlugs(List<Class<? extends IPluginConfig>> pluginConfigs) {
124         if (ProfilerLogManager.isInfoEnabled()) {
125             LOGGER.info("loadingPlugs");
126         }
127         if (pluginConfigs != null && pluginConfigs.size() > 0) {
128             for (Class<? extends IPluginConfig> pluginConfigPackage : pluginConfigs) {
129                 IPluginConfig config;
130                 try {
131                     if (pluginConfigPackage != null) {
132                         config = pluginConfigPackage.getConstructor().newInstance();
133                         config.registerPlugin();
134                     }
135                 } catch (InstantiationException | IllegalAccessException
136                     | InvocationTargetException | NoSuchMethodException exception) {
137                     if (ProfilerLogManager.isErrorEnabled()) {
138                         LOGGER.error("loadingPlugs exception {}", exception.getMessage());
139                     }
140                     continue;
141                 }
142             }
143         }
144     }
145 
146     /**
147      * loadingPlug
148      *
149      * @param pluginConfig pluginConfig
150      */
151     public void loadingPlug(Class<? extends IPluginConfig> pluginConfig) {
152         if (ProfilerLogManager.isInfoEnabled()) {
153             LOGGER.info("loadingPlug");
154         }
155         IPluginConfig config;
156         try {
157             if (pluginConfig != null) {
158                 config = pluginConfig.getConstructor().newInstance();
159                 config.registerPlugin();
160             }
161         } catch (InstantiationException | IllegalAccessException
162             | InvocationTargetException | NoSuchMethodException exception) {
163             if (ProfilerLogManager.isErrorEnabled()) {
164                 LOGGER.error("loadingPlug exception {}", exception.getMessage());
165             }
166         }
167     }
168 
169     /**
170      * registerPlugin
171      *
172      * @param pluginConf pluginConf
173      */
174     public void registerPlugin(PluginConf pluginConf) {
175         if (ProfilerLogManager.isInfoEnabled()) {
176             LOGGER.info("registerPlugin");
177         }
178         if (confLists.contains(pluginConf)) {
179             return;
180         }
181         confLists.add(pluginConf);
182     }
183 
184     /**
185      * Add a plug-in that started successfully
186      *
187      * @param sessionId sessionId
188      * @param pluginConf hiProfilerPluginConf
189      */
190     public void addPluginStartSuccess(long sessionId, PluginConf pluginConf) {
191         if (ProfilerLogManager.isInfoEnabled()) {
192             LOGGER.info("addPluginStartSuccess");
193         }
194         boolean isRegister = profilerConfigMap.containsValue(sessionId, pluginConf);
195         if (isRegister) {
196             return;
197         }
198         profilerConfigMap.put(sessionId, pluginConf);
199     }
200 
201     /**
202      * getProfilerMonitorItemMap
203      *
204      * @param sessionId sessionId
205      * @return List <PluginConf>
206      */
207     public List<PluginConf> getProfilerPlugConfig(long sessionId) {
208         if (ProfilerLogManager.isInfoEnabled()) {
209             LOGGER.info("getProfilerPlugConfig");
210         }
211         Collection<PluginConf> collection = profilerConfigMap.getCollection(sessionId);
212         if (Objects.nonNull(collection)) {
213             return new ArrayList<>(collection);
214         }
215         return new ArrayList<>();
216     }
217 
218     /**
219      * getProfilerMonitorItemMap
220      *
221      * @param sessionId sessionId
222      * @return List <ProfilerMonitorItem>
223      */
224     public List<ProfilerMonitorItem> getProfilerMonitorItemList(long sessionId) {
225         if (ProfilerLogManager.isInfoEnabled()) {
226             LOGGER.info("getProfilerMonitorItemList");
227         }
228         Collection<PluginConf> collection = profilerConfigMap.getCollection(sessionId);
229         if (Objects.nonNull(collection)) {
230             List<ProfilerMonitorItem> itemList = collection.stream().filter(
231                 hiProfilerPluginConf -> hiProfilerPluginConf.isChartPlugin() && Objects
232                     .nonNull(hiProfilerPluginConf.getMonitorItem())).map(PluginConf::getMonitorItem)
233                 .collect(Collectors.toList());
234             return itemList.stream().sorted(Comparator.comparingInt(ProfilerMonitorItem::getIndex))
235                 .collect(Collectors.toList());
236         }
237         return new ArrayList<>();
238     }
239 
240     /**
241      * getPluginConfigByName
242      *
243      * @param configFileName configFileName
244      * @return Optional<PluginConf
245      */
246     public Optional<PluginConf> getPluginConfigByName(String configFileName) {
247         if (StringUtils.isNotBlank(configFileName)) {
248             List<PluginConf> collect =
249                 confLists.stream().filter(pluginConf -> pluginConf.getPluginFileName().equals(configFileName))
250                     .collect(Collectors.toList());
251             if (!collect.isEmpty()) {
252                 return Optional.ofNullable(collect.get(0));
253             }
254         }
255         return Optional.empty();
256     }
257 
258     /**
259      * unzip StdDevelopTools
260      *
261      * @return boolean
262      */
263     public boolean unzipStdDevelopTools() {
264         if (ProfilerLogManager.isInfoEnabled()) {
265             LOGGER.info("unzipStdDevelopTools");
266         }
267         String pluginPath = SessionManager.getInstance().getPluginPath() + "stddeveloptools.tar";
268         File stdFile = new File(pluginPath);
269         if (stdFile.exists()) {
270             return FileUtils.unzipTarFile(stdFile).size() > 0;
271         }
272         return false;
273     }
274 
275     /**
276      * getSystemOsName
277      *
278      * @return String
279      */
280     public String getSystemOsName() {
281         if (StringUtils.isBlank(systemOsName)) {
282             systemOsName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
283         }
284         return systemOsName;
285     }
286 
287     /**
288      * clear Profiler Monitor ItemMap
289      */
290     public void clearProfilerMonitorItemMap() {
291         profilerConfigMap.clear();
292     }
293 
294     /**
295      * clear PluginConf List
296      */
297     public void clearPluginConfList() {
298         confLists.clear();
299     }
300 }
301