-
Notifications
You must be signed in to change notification settings - Fork 49
/
PluginCacheLoader.java
97 lines (83 loc) · 3.71 KB
/
PluginCacheLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* SonarLint Core - Implementation
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.sonarlint.core.plugin;
import com.google.common.collect.ImmutableSet;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler;
import org.sonarsource.sonarlint.core.client.api.exceptions.StorageException;
import org.sonarsource.sonarlint.core.plugin.PluginIndex.PluginReference;
import org.sonarsource.sonarlint.core.plugin.cache.PluginCache;
public class PluginCacheLoader {
private static final ImmutableSet<String> PLUGIN_WHITELIST = ImmutableSet.of("xoo", "java", "javascript", "php", "python", "cobol", "abap", "plsql", "swift",
"rpg", "cpp", "pli", "typescript", "web");
private static final String IMPLEMENTED_SQ_API = "7.0";
private static final Logger LOG = Loggers.get(PluginCacheLoader.class);
private final PluginCache fileCache;
private final PluginIndex pluginIndex;
public PluginCacheLoader(PluginCache fileCache, PluginIndex pluginIndex) {
this.fileCache = fileCache;
this.pluginIndex = pluginIndex;
}
public Map<String, PluginInfo> load() {
return loadPlugins(pluginIndex.references());
}
private Map<String, PluginInfo> loadPlugins(List<PluginReference> pluginReferences) {
Map<String, PluginInfo> infosByKey = new HashMap<>();
Profiler profiler = Profiler.create(LOG).startDebug("Load plugins");
for (PluginReference ref : pluginReferences) {
Path jarFilePath = getFromCache(ref);
PluginInfo info = PluginInfo.create(jarFilePath);
Boolean sonarLintSupported = info.isSonarLintSupported();
if (!info.isCompatibleWith(IMPLEMENTED_SQ_API)) {
LOG.debug("Plugin {} need SonarQube plugin API {} while SonarLint supports only up to {}. Skip it.", info.getKey(), info.getMinimalSqVersion(), IMPLEMENTED_SQ_API);
break;
}
if ((sonarLintSupported != null && sonarLintSupported.booleanValue()) || isWhitelisted(info.getKey())) {
infosByKey.put(info.getKey(), info);
} else {
LOG.debug("Plugin {} is not compatible with SonarLint. Skip it.", info.getKey());
}
}
profiler.stopDebug();
return infosByKey;
}
public static boolean isWhitelisted(String pluginKey) {
return PLUGIN_WHITELIST.contains(pluginKey);
}
private Path getFromCache(final PluginReference pluginReference) {
Path jar;
try {
jar = fileCache.get(pluginReference.getFilename(), pluginReference.getHash());
} catch (StorageException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Fail to find plugin " + pluginReference.getFilename() + " in the cache", e);
}
if (jar == null) {
throw new StorageException("Couldn't find plugin '" + pluginReference.getFilename() + "' in the cache. Please update the binding", false);
}
return jar;
}
}