123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
- package org.sonar.plugins.checkstyle;
-
- import com.puppycrawl.tools.checkstyle.Checker;
- import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
- import com.puppycrawl.tools.checkstyle.XMLLogger;
- import org.apache.commons.io.IOUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.sonar.api.BatchExtension;
- import org.sonar.api.batch.ProjectClasspath;
- import org.sonar.api.utils.TimeProfiler;
-
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import java.net.URLClassLoader;
-
- public class CheckstyleExecutor implements BatchExtension {
- private static Logger LOG = LoggerFactory.getLogger(CheckstyleExecutor.class);
-
- private CheckstyleConfiguration configuration;
- private URLClassLoader projectClassloader;
- private CheckstyleAuditListener listener;
-
- public CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, ProjectClasspath classpath) {
- this.configuration = configuration;
- this.listener = listener;
- this.projectClassloader = classpath.getClassloader();
- }
-
-
- CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, URLClassLoader projectClassloader) {
- this.configuration = configuration;
- this.listener = listener;
- this.projectClassloader = projectClassloader;
- }
-
- /**
- * Execute Checkstyle and return the generated XML report.
- */
- public void execute() {
- TimeProfiler profiler = new TimeProfiler().start("Execute Checkstyle " + CheckstyleVersion.getVersion());
- ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
- Thread.currentThread().setContextClassLoader(PackageNamesLoader.class.getClassLoader());
-
- Checker checker = null;
- OutputStream xmlOutput = null;
- try {
-
- checker = new Checker();
- checker.setClassloader(projectClassloader);
- checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
- checker.addListener(listener);
-
- File xmlReport = configuration.getTargetXMLReport();
- if (xmlReport != null) {
- LOG.info("Checkstyle output report: " + xmlReport.getAbsolutePath());
- xmlOutput = new FileOutputStream(xmlReport);
- checker.addListener(new XMLLogger(xmlOutput, true));
- }
-
- checker.setCharset(configuration.getCharset().name());
- checker.configure(configuration.getCheckstyleConfiguration());
- checker.process(configuration.getSourceFiles());
-
- profiler.stop();
-
- } catch (Exception e) {
- throw new RuntimeException("Can not execute Checkstyle", e);
-
- } finally {
- if (checker != null) {
- checker.destroy();
- }
- IOUtils.closeQuietly(xmlOutput);
- Thread.currentThread().setContextClassLoader(initialClassLoader);
- }
- }
-
-
- }
|