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.api.checks.checkers; 021 022 import com.google.common.collect.Maps; 023 import org.sonar.api.batch.SensorContext; 024 import org.sonar.api.checks.profiles.Check; 025 import org.sonar.api.checks.profiles.CheckProfile; 026 import org.sonar.api.resources.Resource; 027 import org.sonar.api.rules.Rule; 028 import org.sonar.api.rules.RulePriority; 029 import org.sonar.api.rules.Violation; 030 import org.sonar.check.Message; 031 032 import java.util.Collection; 033 import java.util.Locale; 034 import java.util.Map; 035 036 /** 037 * @since 2.1 (experimental) 038 * @deprecated since 2.3 039 */ 040 @Deprecated 041 public class MessageDispatcher { 042 043 private Map<Check, Object> checkersByCheck; 044 private Map<Object, Check> checksByChecker; 045 private SensorContext context; 046 047 public MessageDispatcher(SensorContext context) { 048 this.context = context; 049 checkersByCheck = Maps.newIdentityHashMap(); 050 checksByChecker = Maps.newIdentityHashMap(); 051 } 052 053 public void registerChecker(Check check, Object checker) { 054 checkersByCheck.put(check, checker); 055 checksByChecker.put(checker, check); 056 } 057 058 public void registerCheckers(CheckerFactory factory) { 059 Map<Check, Object> map = factory.create(); 060 for (Map.Entry<Check, Object> entry : map.entrySet()) { 061 registerChecker(entry.getKey(), entry.getValue()); 062 } 063 } 064 065 public void registerCheckers(CheckProfile profile) { 066 for (Check check : profile.getChecks()) { 067 registerChecker(check, check); 068 } 069 } 070 071 public Object getChecker(Check check) { 072 return checkersByCheck.get(check); 073 } 074 075 public Check getCheck(Object checker) { 076 return checksByChecker.get(checker); 077 } 078 079 public Collection getCheckers() { 080 return checkersByCheck.values(); 081 } 082 083 public void unregisterCheck(Check check) { 084 Object checker = checkersByCheck.remove(check); 085 if (checker != null) { 086 checksByChecker.remove(checker); 087 } 088 } 089 090 public void unregisterChecks(CheckProfile profile) { 091 for (Check check : profile.getChecks()) { 092 unregisterCheck(check); 093 } 094 } 095 096 public void log(Resource resource, Message message) { 097 Object checker = message.getChecker(); 098 Check check = getCheck(checker); 099 Violation violation = Violation.create(new Rule(check.getRepositoryKey(), check.getTemplateKey()), resource); 100 violation.setLineId(message.getLine()); 101 violation.setMessage(message.getText(Locale.ENGLISH)); 102 violation.setSeverity(RulePriority.fromCheckPriority(check.getPriority())); 103 context.saveViolation(violation); 104 } 105 106 public void clear() { 107 checkersByCheck.clear(); 108 checksByChecker.clear(); 109 } 110 111 }