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