001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 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 public class MessageDispatcher { 037 038 private Map<Check, Object> checkersByCheck; 039 private Map<Object, Check> checksByChecker; 040 private SensorContext context; 041 042 public MessageDispatcher(SensorContext context) { 043 this.context = context; 044 checkersByCheck = Maps.newIdentityHashMap(); 045 checksByChecker = Maps.newIdentityHashMap(); 046 } 047 048 public void registerChecker(Check check, Object checker) { 049 checkersByCheck.put(check, checker); 050 checksByChecker.put(checker, check); 051 } 052 053 public void registerCheckers(CheckerFactory factory) { 054 Map<Check, Object> map = factory.create(); 055 for (Map.Entry<Check, Object> entry : map.entrySet()) { 056 registerChecker(entry.getKey(), entry.getValue()); 057 } 058 } 059 060 public void registerCheckers(CheckProfile profile) { 061 for (Check check : profile.getChecks()) { 062 registerChecker(check, check); 063 } 064 } 065 066 public Object getChecker(Check check) { 067 return checkersByCheck.get(check); 068 } 069 070 public Check getCheck(Object checker) { 071 return checksByChecker.get(checker); 072 } 073 074 public Collection getCheckers() { 075 return checkersByCheck.values(); 076 } 077 078 public void unregisterCheck(Check check) { 079 Object checker = checkersByCheck.remove(check); 080 if (checker != null) { 081 checksByChecker.remove(checker); 082 } 083 } 084 085 public void unregisterChecks(CheckProfile profile) { 086 for (Check check : profile.getChecks()) { 087 unregisterCheck(check); 088 } 089 } 090 091 public void log(Resource resource, Message message) { 092 Object checker = message.getChecker(); 093 Check check = getCheck(checker); 094 Violation violation = new Violation(new Rule(check.getRepositoryKey(), check.getTemplateKey()), resource); 095 violation.setLineId(message.getLine()); 096 violation.setMessage(message.getText(Locale.ENGLISH)); 097 violation.setPriority(RulePriority.fromCheckPriority(check.getPriority())); 098 context.saveViolation(violation); 099 } 100 101 public void clear() { 102 checkersByCheck.clear(); 103 checksByChecker.clear(); 104 } 105 106 }