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.batch; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 import org.sonar.api.batch.BatchExtensionDictionnary; 026 import org.sonar.api.batch.Decorator; 027 import org.sonar.api.batch.DecoratorContext; 028 import org.sonar.api.batch.SensorContext; 029 import org.sonar.api.database.DatabaseSession; 030 import org.sonar.api.resources.Project; 031 import org.sonar.api.resources.Resource; 032 import org.sonar.batch.indexer.DefaultSonarIndex; 033 034 import java.util.ArrayList; 035 import java.util.Collection; 036 import java.util.List; 037 038 public class DecoratorsExecutor implements CoreJob { 039 040 private DecoratorsSelector decoratorsSelector; 041 private DatabaseSession session; 042 private ViolationsDao violationsDao; 043 private static final Logger LOG = LoggerFactory.getLogger(DecoratorsExecutor.class); 044 private DefaultSonarIndex index; 045 046 public DecoratorsExecutor(BatchExtensionDictionnary extensionDictionnary, DefaultSonarIndex index, DatabaseSession session, ViolationsDao violationsDao) { 047 this.decoratorsSelector = new DecoratorsSelector(extensionDictionnary); 048 this.session = session; 049 this.violationsDao = violationsDao; 050 this.index = index; 051 } 052 053 054 public void execute(Project project, SensorContext sensorContext) { 055 LoggerFactory.getLogger(DecoratorsExecutor.class).info("Execute decorators..."); 056 Collection<Decorator> decorators = decoratorsSelector.select(project); 057 058 if (LOG.isDebugEnabled()) { 059 LOG.debug("Decorators: {}", StringUtils.join(decorators, " -> ")); 060 } 061 062 decorateResource(project, decorators, true); 063 } 064 065 private DecoratorContext decorateResource(Resource resource, Collection<Decorator> decorators, boolean executeDecorators) { 066 List<DecoratorContext> childrenContexts = new ArrayList<DecoratorContext>(); 067 for (Resource child : index.getChildren(resource)) { 068 boolean isModule = (child instanceof Project); 069 DefaultDecoratorContext childContext = (DefaultDecoratorContext) decorateResource(child, decorators, !isModule); 070 childrenContexts.add(childContext.setReadOnly(true)); 071 } 072 073 DefaultDecoratorContext context = new DefaultDecoratorContext(resource, index, childrenContexts, session, violationsDao); 074 if (executeDecorators) { 075 for (Decorator decorator : decorators) { 076 decorator.decorate(resource, context); 077 } 078 } 079 return context; 080 } 081 }