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