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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2013 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.server.ui;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.License;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.platform.NewUserHandler;
import org.sonar.api.platform.PluginMetadata;
import org.sonar.api.platform.PluginRepository;
import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.profiles.ProfileImporter;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.rules.RuleRepository;
import org.sonar.api.test.MutableTestPlan;
import org.sonar.api.test.MutableTestable;
import org.sonar.api.test.TestPlan;
import org.sonar.api.test.Testable;
import org.sonar.api.utils.ValidationMessages;
import org.sonar.api.web.Footer;
import org.sonar.api.web.NavigationSection;
import org.sonar.api.web.Page;
import org.sonar.api.web.RubyRailsWebservice;
import org.sonar.api.web.Widget;
import org.sonar.core.component.SnapshotPerspectives;
import org.sonar.core.dryrun.DryRunCache;
import org.sonar.core.i18n.RuleI18nManager;
import org.sonar.core.measure.MeasureFilterEngine;
import org.sonar.core.measure.MeasureFilterResult;
import org.sonar.core.persistence.Database;
import org.sonar.core.purge.PurgeDao;
import org.sonar.core.resource.ResourceIndexerDao;
import org.sonar.core.resource.ResourceKeyUpdaterDao;
import org.sonar.core.timemachine.Periods;
import org.sonar.server.configuration.Backup;
import org.sonar.server.configuration.ProfilesManager;
import org.sonar.server.db.migrations.DatabaseMigrator;
import org.sonar.server.platform.Platform;
import org.sonar.server.platform.ServerIdGenerator;
import org.sonar.server.platform.ServerSettings;
import org.sonar.server.platform.SettingsChangeNotifier;
import org.sonar.server.plugins.DefaultServerPluginRepository;
import org.sonar.server.plugins.InstalledPluginReferentialFactory;
import org.sonar.server.plugins.PluginDeployer;
import org.sonar.server.plugins.PluginDownloader;
import org.sonar.server.plugins.UpdateCenterMatrixFactory;
import org.sonar.server.rules.ProfilesConsole;
import org.sonar.server.rules.RulesConsole;
import org.sonar.server.user.NewUserNotifier;
import org.sonar.updatecenter.common.PluginReferential;
import org.sonar.updatecenter.common.UpdateCenter;
import org.sonar.updatecenter.common.Version;
import javax.annotation.Nullable;
import java.net.InetAddress;
import java.sql.Connection;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.google.common.collect.Lists.newArrayList;
public final class JRubyFacade {
private static final JRubyFacade SINGLETON = new JRubyFacade();
private JRubyI18n i18n;
public static JRubyFacade getInstance() {
return SINGLETON;
}
<T> T get(Class<T> componentType) {
return getContainer().getComponentByType(componentType);
}
public MeasureFilterResult executeMeasureFilter(Map<String, Object> map, @Nullable Long userId) {
return get(MeasureFilterEngine.class).execute(map, userId);
}
public Collection<ResourceType> getResourceTypesForFilter() {
return get(ResourceTypes.class).getAll(ResourceTypes.AVAILABLE_FOR_FILTERS);
}
public Collection<ResourceType> getResourceTypes() {
return get(ResourceTypes.class).getAll();
}
public Collection<ResourceType> getResourceRootTypes() {
return get(ResourceTypes.class).getRoots();
}
public ResourceType getResourceType(String qualifier) {
return get(ResourceTypes.class).get(qualifier);
}
public String getResourceTypeStringProperty(String resourceTypeQualifier, String resourceTypeProperty) {
ResourceType resourceType = getResourceType(resourceTypeQualifier);
if (resourceType != null) {
return resourceType.getStringProperty(resourceTypeProperty);
}
return null;
}
public List<String> getQualifiersWithProperty(final String propertyKey) {
List<String> qualifiers = newArrayList();
for (ResourceType type : getResourceTypes()) {
if (type.getBooleanProperty(propertyKey) == Boolean.TRUE) {
qualifiers.add(type.getQualifier());
}
}
return qualifiers;
}
public Boolean getResourceTypeBooleanProperty(String resourceTypeQualifier, String resourceTypeProperty) {
ResourceType resourceType = getResourceType(resourceTypeQualifier);
if (resourceType != null) {
return resourceType.getBooleanProperty(resourceTypeProperty);
}
return null;
}
public Collection<String> getResourceLeavesQualifiers(String qualifier) {
return get(ResourceTypes.class).getLeavesQualifiers(qualifier);
}
public Collection<String> getResourceChildrenQualifiers(String qualifier) {
return get(ResourceTypes.class).getChildrenQualifiers(qualifier);
}
// UPDATE CENTER ------------------------------------------------------------
public void downloadPlugin(String pluginKey, String pluginVersion) {
get(PluginDownloader.class).download(pluginKey, Version.create(pluginVersion));
}
public void cancelPluginDownloads() {
get(PluginDownloader.class).cancelDownloads();
}
public List<String> getPluginDownloads() {
return get(PluginDownloader.class).getDownloads();
}
public void uninstallPlugin(String pluginKey) {
get(PluginDeployer.class).uninstall(pluginKey);
}
public void cancelPluginUninstalls() {
get(PluginDeployer.class).cancelUninstalls();
}
public List<String> getPluginUninstalls() {
return get(PluginDeployer.class).getUninstalls();
}
public UpdateCenter getUpdatePluginCenter(boolean forceReload) {
return get(UpdateCenterMatrixFactory.class).getUpdateCenter(forceReload);
}
public PluginReferential getInstalledPluginReferential() {
return get(InstalledPluginReferentialFactory.class).getInstalledPluginReferential();
}
// PLUGINS ------------------------------------------------------------------
public PropertyDefinitions getPropertyDefinitions() {
return get(PropertyDefinitions.class);
}
public boolean hasPlugin(String key) {
return get(PluginRepository.class).getPlugin(key) != null;
}
public Collection<PluginMetadata> getPluginsMetadata() {
return get(PluginRepository.class).getMetadata();
}
// SYNTAX HIGHLIGHTING ------------------------------------------------------
public String colorizeCode(String code, String language) {
try {
return get(CodeColorizers.class).toHtml(code, language);
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).error("Can not highlight the code, language= " + language, e);
return code;
}
}
public List<ViewProxy<Widget>> getWidgets(String resourceScope, String resourceQualifier, String resourceLanguage, Object[] availableMeasures) {
return get(Views.class).getWidgets(resourceScope, resourceQualifier, resourceLanguage, (String[]) availableMeasures);
}
public List<ViewProxy<Widget>> getWidgets() {
return get(Views.class).getWidgets();
}
public ViewProxy<Widget> getWidget(String id) {
return get(Views.class).getWidget(id);
}
public List<ViewProxy<Page>> getPages(String section, String resourceScope, String resourceQualifier, String resourceLanguage, Object[] availableMeasures) {
return get(Views.class).getPages(section, resourceScope, resourceQualifier, resourceLanguage, (String[]) availableMeasures);
}
public List<ViewProxy<Page>> getResourceTabs() {
return get(Views.class).getPages(NavigationSection.RESOURCE_TAB, null, null, null, null);
}
public List<ViewProxy<Page>> getResourceTabs(String scope, String qualifier, String language, Object[] availableMeasures) {
return get(Views.class).getPages(NavigationSection.RESOURCE_TAB, scope, qualifier, language, (String[]) availableMeasures);
}
public List<ViewProxy<Page>> getResourceTabsForMetric(String scope, String qualifier, String language, Object[] availableMeasures, String metric) {
return get(Views.class).getPagesForMetric(NavigationSection.RESOURCE_TAB, scope, qualifier, language, (String[]) availableMeasures, metric);
}
public ViewProxy<Page> getPage(String id) {
return get(Views.class).getPage(id);
}
public Collection<RubyRailsWebservice> getRubyRailsWebservices() {
return getContainer().getComponentsByType(RubyRailsWebservice.class);
}
public Collection<Language> getLanguages() {
return getContainer().getComponentsByType(Language.class);
}
public Database getDatabase() {
return get(Database.class);
}
public DatabaseMigrator databaseMigrator() {
return get(DatabaseMigrator.class);
}
/* PROFILES CONSOLE : RULES AND METRIC THRESHOLDS */
public List<RuleRepository> getRuleRepositories() {
return get(RulesConsole.class).getRepositories();
}
public RuleRepository getRuleRepository(String repositoryKey) {
return get(RulesConsole.class).getRepository(repositoryKey);
}
public Set<RuleRepository> getRuleRepositoriesByLanguage(String languageKey) {
return get(RulesConsole.class).getRepositoriesByLanguage(languageKey);
}
public String backupProfile(int profileId) {
return get(ProfilesConsole.class).backupProfile(profileId);
}
public ValidationMessages restoreProfile(String xmlBackup, boolean deleteExisting) {
return get(ProfilesConsole.class).restoreProfile(xmlBackup, deleteExisting);
}
public List<ProfileExporter> getProfileExportersForLanguage(String language) {
return get(ProfilesConsole.class).getProfileExportersForLanguage(language);
}
public List<ProfileImporter> getProfileImportersForLanguage(String language) {
return get(ProfilesConsole.class).getProfileImportersForLanguage(language);
}
/**
* @throws IllegalArgumentException if no such exporter
*/
public String exportProfile(int profileId, String exporterKey) {
return get(ProfilesConsole.class).exportProfile(profileId, exporterKey);
}
public ValidationMessages importProfile(String profileName, String language, String importerKey, String fileContent) {
return get(ProfilesConsole.class).importProfile(profileName, language, importerKey, fileContent);
}
public String getProfileExporterMimeType(String exporterKey) {
return get(ProfilesConsole.class).getProfileExporter(exporterKey).getMimeType();
}
public void copyProfile(long profileId, String newProfileName) {
getProfilesManager().copyProfile((int) profileId, newProfileName);
}
public ValidationMessages changeParentProfile(int profileId, String parentName, String userName) {
return getProfilesManager().changeParentProfile(profileId, parentName, userName);
}
public void ruleActivated(int parentProfileId, int activeRuleId, String userName) {
getProfilesManager().activated(parentProfileId, activeRuleId, userName);
}
public void ruleParamChanged(int parentProfileId, int activeRuleId, String paramKey, String oldValue, String newValue, String userName) {
getProfilesManager().ruleParamChanged(parentProfileId, activeRuleId, paramKey, oldValue, newValue, userName);
}
public void ruleSeverityChanged(int parentProfileId, int activeRuleId, int oldSeverityId, int newSeverityId, String userName) {
getProfilesManager().ruleSeverityChanged(parentProfileId, activeRuleId, RulePriority.values()[oldSeverityId],
RulePriority.values()[newSeverityId], userName);
}
public void ruleDeactivated(int parentProfileId, int deactivatedRuleId, String userName) {
getProfilesManager().deactivated(parentProfileId, deactivatedRuleId, userName);
}
public void revertRule(int profileId, int activeRuleId, String userName) {
getProfilesManager().revert(profileId, activeRuleId, userName);
}
public List<Footer> getWebFooters() {
return getContainer().getComponentsByType(Footer.class);
}
public Backup getBackup() {
return get(Backup.class);
}
private ProfilesManager getProfilesManager() {
return get(ProfilesManager.class);
}
public void setGlobalProperty(String key, @Nullable String value) {
get(ServerSettings.class).setProperty(key, value);
get(SettingsChangeNotifier.class).onGlobalPropertyChange(key, value);
}
public Settings getSettings() {
return get(Settings.class);
}
public String getConfigurationValue(String key) {
return get(Settings.class).getString(key);
}
public List<InetAddress> getValidInetAddressesForServerId() {
return get(ServerIdGenerator.class).getAvailableAddresses();
}
public String generateServerId(String organisation, String ipAddress) {
return get(ServerIdGenerator.class).generate(organisation, ipAddress);
}
public Connection getConnection() {
try {
return get(Database.class).getDataSource().getConnection();
} catch (Exception e) {
/* activerecord does not correctly manage exceptions when connection can not be opened. */
return null;
}
}
public Object getCoreComponentByClassname(String className) {
if (className == null) {
return null;
}
try {
return get(Class.forName(className));
} catch (ClassNotFoundException e) {
LoggerFactory.getLogger(getClass()).error("Component not found: " + className, e);
return null;
}
}
public Object getComponentByClassname(String pluginKey, String className) {
Object component = null;
Class<?> componentClass = get(DefaultServerPluginRepository.class).getClass(pluginKey, className);
if (componentClass != null) {
component = get(componentClass);
}
return component;
}
private JRubyI18n getJRubyI18n() {
if (i18n == null) {
i18n = get(JRubyI18n.class);
}
return i18n;
}
public String getMessage(String rubyLocale, String key, String defaultValue, Object... parameters) {
return getJRubyI18n().message(rubyLocale, key, defaultValue, parameters);
}
public String getRuleName(String rubyLocale, String repositoryKey, String key) {
return getJRubyI18n().getRuleName(rubyLocale, repositoryKey, key);
}
public String getRuleDescription(String rubyLocale, String repositoryKey, String key) {
return getJRubyI18n().getRuleDescription(rubyLocale, repositoryKey, key);
}
public String getRuleParamDescription(String rubyLocale, String repositoryKey, String key, String paramKey) {
return getJRubyI18n().getRuleParamDescription(rubyLocale, repositoryKey, key, paramKey);
}
public List<RuleI18nManager.RuleKey> searchRuleName(String rubyLocale, String searchText) {
return getJRubyI18n().searchRuleName(rubyLocale, searchText);
}
public String getJsL10nDictionnary(String rubyLocale) {
return getJRubyI18n().getJsDictionnary(rubyLocale);
}
public void indexProjects() {
get(ResourceIndexerDao.class).indexProjects();
}
public void indexResource(long resourceId) {
get(ResourceIndexerDao.class).indexResource(resourceId);
}
public void deleteResourceTree(long rootProjectId) {
try {
get(PurgeDao.class).deleteResourceTree(rootProjectId);
} catch (RuntimeException e) {
LoggerFactory.getLogger(JRubyFacade.class).error("Fail to delete resource with ID: " + rootProjectId, e);
throw e;
}
}
public void logError(String message) {
LoggerFactory.getLogger(getClass()).error(message);
}
public boolean hasSecretKey() {
return get(Settings.class).getEncryption().hasSecretKey();
}
public String encrypt(String clearText) {
return get(Settings.class).getEncryption().encrypt(clearText);
}
public String generateRandomSecretKey() {
return get(Settings.class).getEncryption().generateRandomSecretKey();
}
public License parseLicense(String base64) {
return License.readBase64(base64);
}
public String getServerHome() {
return get(Settings.class).getString(CoreProperties.SONAR_HOME);
}
public ComponentContainer getContainer() {
return Platform.getInstance().getContainer();
}
// UPDATE PROJECT KEY ------------------------------------------------------------------
public void updateResourceKey(long projectId, String newKey) {
get(ResourceKeyUpdaterDao.class).updateKey(projectId, newKey);
}
public Map<String, String> checkModuleKeysBeforeRenaming(long projectId, String stringToReplace, String replacementString) {
return get(ResourceKeyUpdaterDao.class).checkModuleKeysBeforeRenaming(projectId, stringToReplace, replacementString);
}
public void bulkUpdateKey(long projectId, String stringToReplace, String replacementString) {
get(ResourceKeyUpdaterDao.class).bulkUpdateKey(projectId, stringToReplace, replacementString);
}
// USERS
public void onNewUser(Map<String, String> fields) {
NewUserNotifier notifier = get(NewUserNotifier.class);
// notifier is null when creating the administrator in the migration script 011.
if (notifier != null) {
notifier.onNewUser(NewUserHandler.Context.builder()
.setLogin(fields.get("login"))
.setName(fields.get("name"))
.setEmail(fields.get("email"))
.build());
}
}
public byte[] createDatabaseForDryRun(@Nullable Long projectId) {
return get(DryRunCache.class).getDatabaseForDryRun(projectId);
}
public String getPeriodLabel(int periodIndex) {
return get(Periods.class).label(periodIndex);
}
public String getPeriodLabel(String mode, String param, Date date) {
return get(Periods.class).label(mode, param, date);
}
public String getPeriodLabel(String mode, String param, String date) {
return get(Periods.class).label(mode, param, date);
}
public String getPeriodAbbreviation(int periodIndex) {
return get(Periods.class).abbreviation(periodIndex);
}
public TestPlan testPlan(long snapshotId) {
return get(SnapshotPerspectives.class).as(MutableTestPlan.class, snapshotId);
}
public TestPlan testPlan(String componentKey) {
return get(SnapshotPerspectives.class).as(MutableTestPlan.class, componentKey);
}
public Testable testable(long snapshotId) {
return get(SnapshotPerspectives.class).as(MutableTestable.class, snapshotId);
}
public Testable testable(String componentKey) {
return get(SnapshotPerspectives.class).as(MutableTestable.class, componentKey);
}
}
|