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
|
/*
* 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.ce.container;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
import org.sonar.api.Plugin;
import org.sonar.api.Startable;
import org.slf4j.LoggerFactory;
import org.sonar.core.platform.ExplodedPlugin;
import org.sonar.core.platform.PluginClassLoader;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginRepository;
import org.sonar.server.platform.ServerFileSystem;
import org.sonar.server.plugins.PluginRequirementsValidator;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format;
/**
* Entry point to load plugins on startup. It assumes that plugins
* have been correctly installed/uninstalled/updated during web server startup
*/
public class CePluginRepository implements PluginRepository, Startable {
private static final String[] JAR_FILE_EXTENSIONS = new String[] {"jar"};
private static final String NOT_STARTED_YET = "not started yet";
private final ServerFileSystem fs;
private final PluginClassLoader loader;
private final CePluginJarExploder cePluginJarExploder;
private final AtomicBoolean started = new AtomicBoolean(false);
// following fields are available after startup
private final Map<String, PluginInfo> pluginInfosByKeys = new HashMap<>();
private final Map<String, Plugin> pluginInstancesByKeys = new HashMap<>();
public CePluginRepository(ServerFileSystem fs, PluginClassLoader loader, CePluginJarExploder cePluginJarExploder) {
this.fs = fs;
this.loader = loader;
this.cePluginJarExploder = cePluginJarExploder;
}
@Override
public void start() {
LoggerFactory.getLogger(getClass()).info("Load plugins");
registerPluginsFromDir(fs.getInstalledBundledPluginsDir());
registerPluginsFromDir(fs.getInstalledExternalPluginsDir());
PluginRequirementsValidator.unloadIncompatiblePlugins(pluginInfosByKeys);
Map<String, ExplodedPlugin> explodedPluginsByKey = extractPlugins(pluginInfosByKeys);
pluginInstancesByKeys.putAll(loader.load(explodedPluginsByKey));
started.set(true);
}
private void registerPluginsFromDir(File pluginsDir) {
for (File file : listJarFiles(pluginsDir)) {
PluginInfo info = PluginInfo.create(file);
pluginInfosByKeys.put(info.getKey(), info);
}
}
private Map<String, ExplodedPlugin> extractPlugins(Map<String, PluginInfo> pluginsByKey) {
return pluginsByKey.values().stream()
.map(cePluginJarExploder::explode)
.collect(Collectors.toMap(ExplodedPlugin::getKey, p -> p));
}
@Override
public void stop() {
// close classloaders
loader.unload(pluginInstancesByKeys.values());
pluginInstancesByKeys.clear();
pluginInfosByKeys.clear();
started.set(false);
}
@Override
public Collection<PluginInfo> getPluginInfos() {
checkState(started.get(), NOT_STARTED_YET);
return Set.copyOf(pluginInfosByKeys.values());
}
@Override
public PluginInfo getPluginInfo(String key) {
checkState(started.get(), NOT_STARTED_YET);
PluginInfo info = pluginInfosByKeys.get(key);
if (info == null) {
throw new IllegalArgumentException(format("Plugin [%s] does not exist", key));
}
return info;
}
@Override
public Plugin getPluginInstance(String key) {
checkState(started.get(), NOT_STARTED_YET);
Plugin plugin = pluginInstancesByKeys.get(key);
checkArgument(plugin != null, "Plugin [%s] does not exist", key);
return plugin;
}
@Override
public Collection<Plugin> getPluginInstances() {
return pluginInstancesByKeys.values();
}
@Override
public boolean hasPlugin(String key) {
checkState(started.get(), NOT_STARTED_YET);
return pluginInfosByKeys.containsKey(key);
}
private static Collection<File> listJarFiles(File dir) {
if (dir.exists()) {
return FileUtils.listFiles(dir, JAR_FILE_EXTENSIONS, false);
}
return Collections.emptyList();
}
}
|