001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.scan.filesystem;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import org.apache.commons.lang.StringUtils;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    import org.sonar.api.BatchComponent;
027    import org.sonar.api.scan.filesystem.PathResolver;
028    
029    import java.io.File;
030    import java.nio.charset.Charset;
031    import java.util.Iterator;
032    import java.util.List;
033    import java.util.Locale;
034    
035    public class FileSystemLogger implements BatchComponent {
036    
037      private final DefaultModuleFileSystem fs;
038    
039      public FileSystemLogger(DefaultModuleFileSystem fs) {
040        this.fs = fs;
041      }
042    
043      public void log() {
044        doLog(LoggerFactory.getLogger(getClass()));
045      }
046    
047      @VisibleForTesting
048      void doLog(Logger logger) {
049        logDir(logger, "Base dir: ", fs.baseDir());
050        logDir(logger, "Working dir: ", fs.workDir());
051        logPaths(logger, "Source paths: ", fs.baseDir(), fs.sources());
052        logPaths(logger, "Test paths: ", fs.baseDir(), fs.tests());
053        logPaths(logger, "Binary dirs: ", fs.baseDir(), fs.binaryDirs());
054        logEncoding(logger, fs.encoding());
055      }
056    
057      private void logEncoding(Logger logger, Charset charset) {
058        if (!fs.isDefaultJvmEncoding()) {
059          logger.info("Source encoding: " + charset.displayName() + ", default locale: " + Locale.getDefault());
060        } else {
061          logger.warn("Source encoding is platform dependent (" + charset.displayName() + "), default locale: " + Locale.getDefault());
062        }
063      }
064    
065      private void logPaths(Logger logger, String label, File baseDir, List<File> paths) {
066        if (!paths.isEmpty()) {
067          PathResolver resolver = new PathResolver();
068          StringBuilder sb = new StringBuilder(label);
069          for (Iterator<File> it = paths.iterator(); it.hasNext();) {
070            File file = it.next();
071            String relativePathToBaseDir = resolver.relativePath(baseDir, file);
072            if (relativePathToBaseDir == null) {
073              sb.append(file);
074            } else if (StringUtils.isBlank(relativePathToBaseDir)) {
075              sb.append(".");
076            } else {
077              sb.append(relativePathToBaseDir);
078            }
079            if (it.hasNext()) {
080              sb.append(", ");
081            }
082          }
083          logger.info(sb.toString());
084        }
085      }
086    
087      private void logDir(Logger logger, String label, File dir) {
088        if (dir != null) {
089          logger.info(label + dir.getAbsolutePath());
090        }
091      }
092    }