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