All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.java.JavaClasspath Maven / Gradle / Ivy

There is a newer version: 3.2
Show newest version
/*
 * SonarQube Java
 * Copyright (C) 2012 SonarSource
 * [email protected]
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.java;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FalseFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.SonarException;
import org.sonar.api.utils.WildcardPattern;

import javax.annotation.Nullable;
import java.io.File;
import java.io.FileFilter;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import static org.apache.commons.io.filefilter.FileFilterUtils.suffixFileFilter;

public class JavaClasspath implements BatchExtension {

  private static final char SEPARATOR = ',';
  private static final Logger LOG = LoggerFactory.getLogger(JavaClasspath.class);
  private final Project project;
  private final Settings settings;
  private final FileSystem fs;
  @Nullable
  private final MavenProject pom;

  private List binaries;
  private List elements;
  private boolean validateLibraries;
  private boolean hasJavaSources;
  private boolean initalized;

  public JavaClasspath(Project project, Settings settings, FileSystem fs) {
    this(project, settings, fs, null);
  }

  public JavaClasspath(Project project, Settings settings, FileSystem fs, @Nullable MavenProject pom) {
    this.project = project;
    this.settings = settings;
    this.fs = fs;
    this.pom = pom;
    initalized = false;
  }

  private void init() {
    if (!initalized) {
      initalized = true;
      validateLibraries = project.getModules().isEmpty();
      FilePredicates predicates = fs.predicates();
      hasJavaSources = fs.hasFiles(predicates.and(predicates.hasLanguage("java"), predicates.hasType(InputFile.Type.MAIN)));
      File baseDir = fs.baseDir();
      binaries = getFilesFromProperty(JavaClasspathProperties.SONAR_JAVA_BINARIES, settings, baseDir);
      List libraries = getFilesFromProperty(JavaClasspathProperties.SONAR_JAVA_LIBRARIES, settings, baseDir);
      boolean useDeprecatedProperties = binaries.isEmpty() && libraries.isEmpty();
      if (useDeprecatedProperties) {
        binaries = getFilesFromProperty("sonar.binaries", settings, baseDir);
        libraries = getFilesFromProperty("sonar.libraries", settings, baseDir);
      }
      if (pom != null && libraries.isEmpty()) {
        //check mojo
        elements = getLibrariesFromMaven(pom);
      } else {
        elements = Lists.newArrayList(binaries);
        elements.addAll(libraries);
        if (useDeprecatedProperties && !elements.isEmpty()) {
          LOG.warn("sonar.binaries and sonar.libraries are deprecated since version 2.5 of sonar-java-plugin, please use sonar.java.binaries and sonar.java.libraries instead");
        }
      }
    }
  }

  private List getLibrariesFromMaven(MavenProject pom) {
    try {
      List files = Lists.newArrayList();
      if (pom.getCompileClasspathElements() != null) {
        for (String classPathString : (List) pom.getCompileClasspathElements()) {
          files.add(new File(classPathString));
        }
      }
      if (pom.getBuild().getOutputDirectory() != null) {
        File outputDirectoryFile = new File(pom.getBuild().getOutputDirectory());
        if (outputDirectoryFile.exists()) {
          files.add(outputDirectoryFile);
        }
      }
      return files;
    } catch (DependencyResolutionRequiredException e) {
      throw new SonarException("Fail to create the project classloader", e);
    }
  }


  private List getFilesFromProperty(String property, Settings settings, File baseDir) {
    List result = Lists.newArrayList();
    String fileList = settings.getString(property);
    if (StringUtils.isNotEmpty(fileList)) {
      Iterable fileNames = Splitter.on(SEPARATOR).omitEmptyStrings().split(fileList);
      for (String pathPattern : fileNames) {
        boolean isLibraryProperty = property.endsWith("libraries");
        List libraryFilesForPattern = getFilesForPattern(baseDir, pathPattern, isLibraryProperty);
        if (validateLibraries && libraryFilesForPattern.isEmpty() && hasJavaSources) {
          LOG.error("Invalid value for " + property);
          String message = "No files nor directories matching '" + pathPattern + "'";
          throw new IllegalStateException(message);
        }
        result.addAll(libraryFilesForPattern);
      }
    }
    return result;
  }


  private List getFilesForPattern(File baseDir, String pathPattern, boolean libraryProperty) {
    String dirPath = pathPattern;
    String filePattern;
    int wildcardIndex = pathPattern.indexOf('*');
    if (wildcardIndex >= 0) {
      dirPath = pathPattern.substring(0, wildcardIndex);
    }
    int lastPathSeparator = Math.max(dirPath.lastIndexOf('/'), dirPath.lastIndexOf('\\'));
    if (lastPathSeparator == -1) {
      dirPath = ".";
      filePattern = pathPattern;
    } else {
      dirPath = pathPattern.substring(0, lastPathSeparator);
      filePattern = pathPattern.substring(lastPathSeparator + 1);
    }
    File dir = resolvePath(baseDir, dirPath);
    if (!dir.isDirectory()) {
      return Lists.newArrayList();
    }
    return getMatchingFiles(filePattern, dir, libraryProperty);
  }

  private List getMatchingFiles(String pattern, File dir, boolean libraryProperty) {
    WilcardPatternFileFilter wilcardPatternFileFilter = new WilcardPatternFileFilter(dir, pattern);
    FileFilter fileFilter = wilcardPatternFileFilter;
    List files = Lists.newArrayList();
    if (libraryProperty) {
      if (pattern.endsWith("*")) {
        fileFilter = new AndFileFilter((IOFileFilter) fileFilter,
            new OrFileFilter(Lists.newArrayList(suffixFileFilter(".jar", IOCase.INSENSITIVE), suffixFileFilter(".zip", IOCase.INSENSITIVE))));
      }
      //find jar and zip files
      files.addAll(Lists.newArrayList(FileUtils.listFiles(dir, (IOFileFilter) fileFilter, TrueFileFilter.TRUE)));
    }
    //find directories matching pattern.
    IOFileFilter subdirectories = pattern.isEmpty() ? FalseFileFilter.FALSE : TrueFileFilter.TRUE;
    Collection dirs = FileUtils.listFilesAndDirs(dir, new AndFileFilter(wilcardPatternFileFilter, DirectoryFileFilter.DIRECTORY), subdirectories);
    //remove searching dir from matching as listFilesAndDirs always includes it in the list see https://issues.apache.org/jira/browse/IO-328
    if (!pattern.isEmpty()) {
      dirs.remove(dir);
      //remove subdirectories that were included during search
      Iterator iterator = dirs.iterator();
      while (iterator.hasNext()) {
        File matchingDir = iterator.next();
        if (!wilcardPatternFileFilter.accept(matchingDir)) {
          iterator.remove();
        }
      }
    }

    if (libraryProperty) {
      for (File directory : dirs) {
        files.addAll(getMatchingFiles("**/*.jar", directory, true));
        files.addAll(getMatchingFiles("**/*.zip", directory, true));
      }
    }
    files.addAll(dirs);
    return files;
  }

  private File resolvePath(File baseDir, String fileName) {
    File file = new File(fileName);
    if (!file.isAbsolute()) {
      file = new File(baseDir, fileName);
    }
    return file;
  }

  public List getElements() {
    init();
    return elements;
  }

  public List getBinaryDirs() {
    init();
    return binaries;
  }

  private static class WilcardPatternFileFilter implements IOFileFilter {
    private File baseDir;
    private WildcardPattern wildcardPattern;

    public WilcardPatternFileFilter(File baseDir, String wildcardPattern) {
      this.baseDir = baseDir;
      this.wildcardPattern = WildcardPattern.create(FilenameUtils.separatorsToSystem(wildcardPattern), File.separator);
    }

    @Override
    public boolean accept(File dir, String name) {
      return accept(new File(dir, name));
    }

    @Override
    public boolean accept(File file) {
      String path = file.getAbsolutePath();
      path = path.substring(baseDir.getAbsolutePath().length() + 1);
      return wildcardPattern.match(path);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy