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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/*
* SonarQube
* Copyright (C) 2009-2025 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.sonar.scanner.bootstrap;
import static java.util.stream.Collectors.toMap;
import static org.sonar.api.utils.Preconditions.checkState;
import static org.sonar.core.config.ScannerProperties.PLUGIN_LOADING_OPTIMIZATION_KEY;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Plugin;
import org.sonar.api.Startable;
import org.sonar.api.config.Configuration;
import org.sonar.core.platform.ExplodedPlugin;
import org.sonar.core.platform.PluginClassLoader;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginJarExploder;
import org.sonar.core.platform.PluginRepository;
import org.sonar.core.plugin.PluginType;
import org.sonar.scanner.mediumtest.LocalPlugin;
/**
* Orchestrates the installation and loading of plugins
*/
public class ScannerPluginRepository implements PluginRepository, Startable {
private static final Logger LOG = LoggerFactory.getLogger(ScannerPluginRepository.class);
private final PluginInstaller installer;
private final PluginJarExploder pluginJarExploder;
private final PluginClassLoader loader;
private final Configuration properties;
private Map<String, Plugin> pluginInstancesByKeys;
private Map<String, ScannerPlugin> pluginsByKeys;
private Map<ClassLoader, String> keysByClassLoader;
private boolean shouldLoadOnlyRequiredPluginsOnStart;
public ScannerPluginRepository(PluginInstaller installer, PluginJarExploder pluginJarExploder, PluginClassLoader loader, Configuration properties) {
this.installer = installer;
this.pluginJarExploder = pluginJarExploder;
this.loader = loader;
this.properties = properties;
}
@Override
public void start() {
shouldLoadOnlyRequiredPluginsOnStart = properties.getBoolean(PLUGIN_LOADING_OPTIMIZATION_KEY).orElse(true);
if (!shouldLoadOnlyRequiredPluginsOnStart) {
LOG.warn("{} is false, so ALL available plugins will be downloaded", PLUGIN_LOADING_OPTIMIZATION_KEY);
pluginsByKeys = new HashMap<>(installer.installAllPlugins());
} else {
pluginsByKeys = new HashMap<>(installer.installRequiredPlugins());
}
Map<String, ExplodedPlugin> explodedPluginsByKey = pluginsByKeys.entrySet().stream()
.collect(toMap(Map.Entry::getKey, e -> pluginJarExploder.explode(e.getValue().getInfo())));
pluginInstancesByKeys = new HashMap<>(loader.load(explodedPluginsByKey));
// this part is only used by medium tests
for (LocalPlugin localPlugin : installer.installLocals()) {
ScannerPlugin scannerPlugin = localPlugin.toScannerPlugin();
String pluginKey = localPlugin.pluginInfo().getKey();
pluginsByKeys.put(pluginKey, scannerPlugin);
pluginInstancesByKeys.put(pluginKey, localPlugin.pluginInstance());
}
keysByClassLoader = new HashMap<>();
for (Map.Entry<String, Plugin> e : pluginInstancesByKeys.entrySet()) {
keysByClassLoader.put(e.getValue().getClass().getClassLoader(), e.getKey());
}
logPlugins(pluginsByKeys.values());
}
public Collection<PluginInfo> installPluginsForLanguages(Set<String> languageKeys) {
if (!shouldLoadOnlyRequiredPluginsOnStart) {
return Collections.emptySet();
}
var languagePluginsByKeys = new HashMap<>(installer.installPluginsForLanguages(languageKeys));
pluginsByKeys.putAll(languagePluginsByKeys);
Map<String, ExplodedPlugin> explodedPluginsByKey = languagePluginsByKeys.entrySet().stream()
.collect(toMap(Map.Entry::getKey, e -> pluginJarExploder.explode(e.getValue().getInfo())));
pluginInstancesByKeys.putAll(new HashMap<>(loader.load(explodedPluginsByKey)));
// this part is only used by medium tests
for (LocalPlugin localPlugin : installer.installOptionalLocals(languageKeys)) {
ScannerPlugin scannerPlugin = localPlugin.toScannerPlugin();
String pluginKey = localPlugin.pluginInfo().getKey();
languagePluginsByKeys.put(pluginKey, scannerPlugin);
pluginsByKeys.put(pluginKey, scannerPlugin);
pluginInstancesByKeys.put(pluginKey, localPlugin.pluginInstance());
}
keysByClassLoader = new HashMap<>();
for (Map.Entry<String, Plugin> e : pluginInstancesByKeys.entrySet()) {
keysByClassLoader.put(e.getValue().getClass().getClassLoader(), e.getKey());
}
logPlugins(languagePluginsByKeys.values());
return languagePluginsByKeys.values().stream().map(ScannerPlugin::getInfo).toList();
}
@CheckForNull
public String getPluginKey(ClassLoader cl) {
return keysByClassLoader.get(cl);
}
private static void logPlugins(Collection<ScannerPlugin> plugins) {
if (plugins.isEmpty()) {
LOG.debug("No plugins loaded");
} else {
LOG.debug("Plugins loaded:");
for (ScannerPlugin p : plugins) {
LOG.debug(" * {} {} ({})", p.getName(), p.getVersion(), p.getKey());
}
}
}
@Override
public void stop() {
// close plugin classloaders
loader.unload(pluginInstancesByKeys.values());
pluginInstancesByKeys.clear();
pluginsByKeys.clear();
keysByClassLoader.clear();
}
public Map<String, ScannerPlugin> getPluginsByKey() {
return pluginsByKeys;
}
@Override
public Collection<PluginInfo> getPluginInfos() {
return pluginsByKeys.values().stream().map(ScannerPlugin::getInfo).toList();
}
public Collection<PluginInfo> getExternalPluginsInfos() {
return pluginsByKeys.values().stream().filter(p -> p.getType() == PluginType.EXTERNAL).map(ScannerPlugin::getInfo).toList();
}
public Collection<PluginInfo> getBundledPluginsInfos() {
return pluginsByKeys.values().stream().filter(p -> p.getType() == PluginType.BUNDLED).map(ScannerPlugin::getInfo).toList();
}
@Override
public PluginInfo getPluginInfo(String key) {
ScannerPlugin info = pluginsByKeys.get(key);
checkState(info != null, "Plugin [%s] does not exist", key);
return info.getInfo();
}
@Override
public Plugin getPluginInstance(String key) {
Plugin instance = pluginInstancesByKeys.get(key);
checkState(instance != null, "Plugin [%s] does not exist", key);
return instance;
}
@Override
public Collection<Plugin> getPluginInstances() {
return pluginInstancesByKeys.values();
}
@Override
public boolean hasPlugin(String key) {
return pluginsByKeys.containsKey(key);
}
}
|