001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.server.ui; 021 022 import com.google.common.collect.Lists; 023 import org.apache.commons.configuration.Configuration; 024 import org.slf4j.LoggerFactory; 025 import org.sonar.api.CoreProperties; 026 import org.sonar.api.config.License; 027 import org.sonar.api.config.PropertyDefinitions; 028 import org.sonar.api.config.Settings; 029 import org.sonar.api.platform.ComponentContainer; 030 import org.sonar.api.platform.PluginMetadata; 031 import org.sonar.api.platform.PluginRepository; 032 import org.sonar.api.profiles.ProfileExporter; 033 import org.sonar.api.profiles.ProfileImporter; 034 import org.sonar.api.resources.Language; 035 import org.sonar.api.resources.ResourceType; 036 import org.sonar.api.resources.ResourceTypes; 037 import org.sonar.api.rules.RulePriority; 038 import org.sonar.api.rules.RuleRepository; 039 import org.sonar.api.utils.ValidationMessages; 040 import org.sonar.api.web.*; 041 import org.sonar.core.i18n.RuleI18nManager; 042 import org.sonar.core.persistence.Database; 043 import org.sonar.core.persistence.DatabaseMigrator; 044 import org.sonar.core.purge.PurgeDao; 045 import org.sonar.core.resource.ResourceIndexerDao; 046 import org.sonar.markdown.Markdown; 047 import org.sonar.server.configuration.Backup; 048 import org.sonar.server.configuration.ProfilesManager; 049 import org.sonar.server.filters.Filter; 050 import org.sonar.server.filters.FilterExecutor; 051 import org.sonar.server.filters.FilterResult; 052 import org.sonar.server.notifications.reviews.ReviewsNotificationManager; 053 import org.sonar.server.platform.GlobalSettingsUpdater; 054 import org.sonar.server.platform.Platform; 055 import org.sonar.server.platform.ServerIdGenerator; 056 import org.sonar.server.plugins.*; 057 import org.sonar.server.rules.ProfilesConsole; 058 import org.sonar.server.rules.RulesConsole; 059 import org.sonar.updatecenter.common.Version; 060 061 import javax.annotation.Nullable; 062 import java.net.InetAddress; 063 import java.sql.Connection; 064 import java.util.Collection; 065 import java.util.List; 066 import java.util.Set; 067 068 public final class JRubyFacade { 069 070 private static final JRubyFacade SINGLETON = new JRubyFacade(); 071 private JRubyI18n i18n; 072 073 public static JRubyFacade getInstance() { 074 return SINGLETON; 075 } 076 077 public FilterResult executeFilter(Filter filter) { 078 return getContainer().getComponentByType(FilterExecutor.class).execute(filter); 079 } 080 081 public Collection<ResourceType> getResourceTypesForFilter() { 082 return getContainer().getComponentByType(ResourceTypes.class).getAll(ResourceTypes.AVAILABLE_FOR_FILTERS); 083 } 084 085 public Collection<ResourceType> getResourceTypes() { 086 return getContainer().getComponentByType(ResourceTypes.class).getAll(); 087 } 088 089 public ResourceType getResourceType(String qualifier) { 090 return getContainer().getComponentByType(ResourceTypes.class).get(qualifier); 091 } 092 093 public String getResourceTypeStringProperty(String resourceTypeQualifier, String resourceTypeProperty) { 094 ResourceType resourceType = getResourceType(resourceTypeQualifier); 095 if (resourceType != null) { 096 return resourceType.getStringProperty(resourceTypeProperty); 097 } 098 return null; 099 } 100 101 public List<String> getQualifiersWithProperty(final String propertyKey) { 102 List<String> qualifiers = Lists.newArrayList(); 103 for (ResourceType type : getResourceTypes()) { 104 if (type.getBooleanProperty(propertyKey) == Boolean.TRUE) { 105 qualifiers.add(type.getQualifier()); 106 } 107 } 108 return qualifiers; 109 } 110 111 public Boolean getResourceTypeBooleanProperty(String resourceTypeQualifier, String resourceTypeProperty) { 112 ResourceType resourceType = getResourceType(resourceTypeQualifier); 113 if (resourceType != null) { 114 return resourceType.getBooleanProperty(resourceTypeProperty); 115 } 116 return null; 117 } 118 119 public Collection<String> getResourceLeavesQualifiers(String qualifier) { 120 return getContainer().getComponentByType(ResourceTypes.class).getLeavesQualifiers(qualifier); 121 } 122 123 public Collection<String> getResourceChildrenQualifiers(String qualifier) { 124 return getContainer().getComponentByType(ResourceTypes.class).getChildrenQualifiers(qualifier); 125 } 126 127 // UPDATE CENTER ------------------------------------------------------------ 128 129 public void downloadPlugin(String pluginKey, String pluginVersion) { 130 getContainer().getComponentByType(PluginDownloader.class).download(pluginKey, Version.create(pluginVersion)); 131 } 132 133 public void cancelPluginDownloads() { 134 getContainer().getComponentByType(PluginDownloader.class).cancelDownloads(); 135 } 136 137 public List<String> getPluginDownloads() { 138 return getContainer().getComponentByType(PluginDownloader.class).getDownloads(); 139 } 140 141 public void uninstallPlugin(String pluginKey) { 142 getContainer().getComponentByType(PluginDeployer.class).uninstall(pluginKey); 143 } 144 145 public void cancelPluginUninstalls() { 146 getContainer().getComponentByType(PluginDeployer.class).cancelUninstalls(); 147 } 148 149 public List<String> getPluginUninstalls() { 150 return getContainer().getComponentByType(PluginDeployer.class).getUninstalls(); 151 } 152 153 public UpdateCenterMatrix getUpdateCenterMatrix(boolean forceReload) { 154 return getContainer().getComponentByType(UpdateCenterMatrixFactory.class).getMatrix(forceReload); 155 } 156 157 // PLUGINS ------------------------------------------------------------------ 158 159 public PropertyDefinitions getPropertyDefinitions() { 160 return getContainer().getComponentByType(PropertyDefinitions.class); 161 } 162 163 public boolean hasPlugin(String key) { 164 return getContainer().getComponentByType(PluginRepository.class).getPlugin(key) != null; 165 } 166 167 public Collection<PluginMetadata> getPluginsMetadata() { 168 return getContainer().getComponentByType(PluginRepository.class).getMetadata(); 169 } 170 171 172 // SYNTAX HIGHLIGHTING ------------------------------------------------------ 173 174 public String colorizeCode(String code, String language) { 175 try { 176 return getContainer().getComponentByType(CodeColorizers.class).toHtml(code, language); 177 178 } catch (Exception e) { 179 LoggerFactory.getLogger(getClass()).error("Can not highlight the code, language= " + language, e); 180 return code; 181 } 182 } 183 184 public static String markdownToHtml(String input) { 185 return Markdown.convertToHtml(input); 186 } 187 188 189 public List<ViewProxy<Widget>> getWidgets(String resourceScope, String resourceQualifier, String resourceLanguage, Object[] availableMeasures) { 190 return getContainer().getComponentByType(Views.class).getWidgets(resourceScope, resourceQualifier, resourceLanguage, (String[]) availableMeasures); 191 } 192 193 public List<ViewProxy<Widget>> getWidgets() { 194 return getContainer().getComponentByType(Views.class).getWidgets(); 195 } 196 197 public ViewProxy<Widget> getWidget(String id) { 198 return getContainer().getComponentByType(Views.class).getWidget(id); 199 } 200 201 public List<ViewProxy<Page>> getPages(String section, String resourceScope, String resourceQualifier, String resourceLanguage, Object[] availableMeasures) { 202 return getContainer().getComponentByType(Views.class).getPages(section, resourceScope, resourceQualifier, resourceLanguage, (String[]) availableMeasures); 203 } 204 205 public List<ViewProxy<Page>> getResourceTabs() { 206 return getContainer().getComponentByType(Views.class).getPages(NavigationSection.RESOURCE_TAB, null, null, null, null); 207 } 208 209 public List<ViewProxy<Page>> getResourceTabs(String scope, String qualifier, String language, Object[] availableMeasures) { 210 return getContainer().getComponentByType(Views.class).getPages(NavigationSection.RESOURCE_TAB, scope, qualifier, language, (String[]) availableMeasures); 211 } 212 213 public List<ViewProxy<Page>> getResourceTabsForMetric(String scope, String qualifier, String language, Object[] availableMeasures, String metric) { 214 return getContainer().getComponentByType(Views.class).getPagesForMetric(NavigationSection.RESOURCE_TAB, scope, qualifier, language, (String[]) availableMeasures, metric); 215 } 216 217 public ViewProxy<Page> getPage(String id) { 218 return getContainer().getComponentByType(Views.class).getPage(id); 219 } 220 221 public Collection<RubyRailsWebservice> getRubyRailsWebservices() { 222 return getContainer().getComponentsByType(RubyRailsWebservice.class); 223 } 224 225 public Collection<Language> getLanguages() { 226 return getContainer().getComponentsByType(Language.class); 227 } 228 229 public Database getDatabase() { 230 return getContainer().getComponentByType(Database.class); 231 } 232 233 public boolean createDatabase() { 234 return getContainer().getComponentByType(DatabaseMigrator.class).createDatabase(); 235 } 236 237 /* PROFILES CONSOLE : RULES AND METRIC THRESHOLDS */ 238 239 public List<RuleRepository> getRuleRepositories() { 240 return getContainer().getComponentByType(RulesConsole.class).getRepositories(); 241 } 242 243 public RuleRepository getRuleRepository(String repositoryKey) { 244 return getContainer().getComponentByType(RulesConsole.class).getRepository(repositoryKey); 245 } 246 247 public Set<RuleRepository> getRuleRepositoriesByLanguage(String languageKey) { 248 return getContainer().getComponentByType(RulesConsole.class).getRepositoriesByLanguage(languageKey); 249 } 250 251 public String backupProfile(int profileId) { 252 return getContainer().getComponentByType(ProfilesConsole.class).backupProfile(profileId); 253 } 254 255 public ValidationMessages restoreProfile(String xmlBackup) { 256 return getContainer().getComponentByType(ProfilesConsole.class).restoreProfile(xmlBackup); 257 } 258 259 public List<ProfileExporter> getProfileExportersForLanguage(String language) { 260 return getContainer().getComponentByType(ProfilesConsole.class).getProfileExportersForLanguage(language); 261 } 262 263 public List<ProfileImporter> getProfileImportersForLanguage(String language) { 264 return getContainer().getComponentByType(ProfilesConsole.class).getProfileImportersForLanguage(language); 265 } 266 267 /** 268 * @throws IllegalArgumentException if no such exporter 269 */ 270 public String exportProfile(int profileId, String exporterKey) { 271 return getContainer().getComponentByType(ProfilesConsole.class).exportProfile(profileId, exporterKey); 272 } 273 274 public ValidationMessages importProfile(String profileName, String language, String importerKey, String fileContent) { 275 return getContainer().getComponentByType(ProfilesConsole.class).importProfile(profileName, language, importerKey, fileContent); 276 } 277 278 public String getProfileExporterMimeType(String exporterKey) { 279 return getContainer().getComponentByType(ProfilesConsole.class).getProfileExporter(exporterKey).getMimeType(); 280 } 281 282 public void renameProfile(int profileId, String newProfileName) { 283 getProfilesManager().renameProfile(profileId, newProfileName); 284 } 285 286 public void copyProfile(long profileId, String newProfileName) { 287 getProfilesManager().copyProfile((int) profileId, newProfileName); 288 } 289 290 public void deleteProfile(long profileId) { 291 getProfilesManager().deleteProfile((int) profileId); 292 } 293 294 public ValidationMessages changeParentProfile(int profileId, String parentName, String userName) { 295 return getProfilesManager().changeParentProfile(profileId, parentName, userName); 296 } 297 298 public void ruleActivated(int parentProfileId, int activeRuleId, String userName) { 299 getProfilesManager().activated(parentProfileId, activeRuleId, userName); 300 } 301 302 public void ruleParamChanged(int parentProfileId, int activeRuleId, String paramKey, String oldValue, String newValue, String userName) { 303 getProfilesManager().ruleParamChanged(parentProfileId, activeRuleId, paramKey, oldValue, newValue, userName); 304 } 305 306 public void ruleSeverityChanged(int parentProfileId, int activeRuleId, int oldSeverityId, int newSeverityId, String userName) { 307 getProfilesManager().ruleSeverityChanged(parentProfileId, activeRuleId, RulePriority.values()[oldSeverityId], 308 RulePriority.values()[newSeverityId], userName); 309 } 310 311 public void ruleDeactivated(int parentProfileId, int deactivatedRuleId, String userName) { 312 getProfilesManager().deactivated(parentProfileId, deactivatedRuleId, userName); 313 } 314 315 public void revertRule(int profileId, int activeRuleId, String userName) { 316 getProfilesManager().revert(profileId, activeRuleId, userName); 317 } 318 319 public List<Footer> getWebFooters() { 320 return getContainer().getComponentsByType(Footer.class); 321 } 322 323 public Backup getBackup() { 324 return getContainer().getComponentByType(Backup.class); 325 } 326 327 private ProfilesManager getProfilesManager() { 328 return getContainer().getComponentByType(ProfilesManager.class); 329 } 330 331 public void setGlobalProperty(String key, @Nullable String value) { 332 getContainer().getComponentByType(GlobalSettingsUpdater.class).setProperty(key, value); 333 } 334 335 public Settings getSettings() { 336 return getContainer().getComponentByType(Settings.class); 337 } 338 339 public String getConfigurationValue(String key) { 340 return getContainer().getComponentByType(Configuration.class).getString(key, null); 341 } 342 343 public List<InetAddress> getValidInetAddressesForServerId() { 344 return getContainer().getComponentByType(ServerIdGenerator.class).getAvailableAddresses(); 345 } 346 347 public String generateServerId(String organisation, String ipAddress) { 348 return getContainer().getComponentByType(ServerIdGenerator.class).generate(organisation, ipAddress); 349 } 350 351 public Connection getConnection() { 352 try { 353 return getContainer().getComponentByType(Database.class).getDataSource().getConnection(); 354 } catch (Exception e) { 355 /* activerecord does not correctly manage exceptions when connection can not be opened. */ 356 return null; 357 } 358 } 359 360 public Object getCoreComponentByClassname(String className) { 361 if (className == null) { 362 return null; 363 } 364 365 try { 366 Class aClass = Class.forName(className); 367 return getContainer().getComponentByType(aClass); 368 369 } catch (ClassNotFoundException e) { 370 LoggerFactory.getLogger(getClass()).error("Component not found: " + className, e); 371 return null; 372 } 373 } 374 375 public Object getComponentByClassname(String pluginKey, String className) { 376 Object component = null; 377 ComponentContainer container = getContainer(); 378 Class componentClass = container.getComponentByType(DefaultServerPluginRepository.class).getClass(pluginKey, className); 379 if (componentClass != null) { 380 component = container.getComponentByType(componentClass); 381 } 382 return component; 383 } 384 385 public String getMessage(String rubyLocale, String key, String defaultValue, Object... parameters) { 386 if (i18n == null) { 387 i18n = getContainer().getComponentByType(JRubyI18n.class); 388 } 389 return i18n.message(rubyLocale, key, defaultValue, parameters); 390 } 391 392 public String getRuleName(String rubyLocale, String repositoryKey, String key) { 393 if (i18n == null) { 394 i18n = getContainer().getComponentByType(JRubyI18n.class); 395 } 396 return i18n.getRuleName(rubyLocale, repositoryKey, key); 397 } 398 399 public String getRuleDescription(String rubyLocale, String repositoryKey, String key) { 400 if (i18n == null) { 401 i18n = getContainer().getComponentByType(JRubyI18n.class); 402 } 403 return i18n.getRuleDescription(rubyLocale, repositoryKey, key); 404 } 405 406 public String getRuleParamDescription(String rubyLocale, String repositoryKey, String key, String paramKey) { 407 if (i18n == null) { 408 i18n = getContainer().getComponentByType(JRubyI18n.class); 409 } 410 return i18n.getRuleParamDescription(rubyLocale, repositoryKey, key, paramKey); 411 } 412 413 public List<RuleI18nManager.RuleKey> searchRuleName(String rubyLocale, String searchText) { 414 if (i18n == null) { 415 i18n = getContainer().getComponentByType(JRubyI18n.class); 416 } 417 return i18n.searchRuleName(rubyLocale, searchText); 418 } 419 420 public String getJsL10nDictionnary(String rubyLocale) { 421 if (i18n == null) { 422 i18n = getContainer().getComponentByType(JRubyI18n.class); 423 } 424 return i18n.getJsDictionnary(rubyLocale); 425 } 426 427 public void indexProjects() { 428 getContainer().getComponentByType(ResourceIndexerDao.class).indexProjects(); 429 } 430 431 public void indexResource(long resourceId) { 432 getContainer().getComponentByType(ResourceIndexerDao.class).indexResource(resourceId); 433 } 434 435 public void deleteResourceTree(long rootProjectId) { 436 getContainer().getComponentByType(PurgeDao.class).deleteResourceTree(rootProjectId); 437 } 438 439 public void logError(String message) { 440 LoggerFactory.getLogger(getClass()).error(message); 441 } 442 443 public boolean hasSecretKey() { 444 return getContainer().getComponentByType(Settings.class).getEncryption().hasSecretKey(); 445 } 446 447 public String encrypt(String clearText) { 448 return getContainer().getComponentByType(Settings.class).getEncryption().encrypt(clearText); 449 } 450 451 public String generateRandomSecretKey() { 452 return getContainer().getComponentByType(Settings.class).getEncryption().generateRandomSecretKey(); 453 } 454 455 public License parseLicense(String base64) { 456 return License.readBase64(base64); 457 } 458 459 public String getServerHome() { 460 return getContainer().getComponentByType(Settings.class).getString(CoreProperties.SONAR_HOME); 461 } 462 463 public ReviewsNotificationManager getReviewsNotificationManager() { 464 return getContainer().getComponentByType(ReviewsNotificationManager.class); 465 } 466 467 public ComponentContainer getContainer() { 468 return Platform.getInstance().getContainer(); 469 } 470 }