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