001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 */ 020package org.sonar.api.batch.fs.internal; 021 022import java.io.File; 023import java.io.IOException; 024import java.nio.file.LinkOption; 025import java.nio.file.Path; 026import java.util.ArrayList; 027import java.util.Collections; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031import javax.annotation.CheckForNull; 032import javax.annotation.concurrent.Immutable; 033import org.sonar.api.batch.bootstrap.ProjectDefinition; 034import org.sonar.api.batch.fs.InputModule; 035 036/** 037 * @since 5.2 038 */ 039@Immutable 040public class DefaultInputModule extends DefaultInputComponent implements InputModule { 041 private final Path baseDir; 042 private final Path workDir; 043 private final String name; 044 private final String version; 045 private final String originalName; 046 private final String originalVersion; 047 private final String description; 048 private final String keyWithBranch; 049 private final String branch; 050 private final List<String> sources; 051 private final List<String> tests; 052 private final Map<String, String> properties; 053 054 private final String moduleKey; 055 private final ProjectDefinition definition; 056 057 /** 058 * For testing only! 059 */ 060 public DefaultInputModule(ProjectDefinition definition) { 061 this(definition, TestInputFileBuilder.nextBatchId()); 062 } 063 064 public DefaultInputModule(ProjectDefinition definition, int batchId) { 065 super(batchId); 066 this.baseDir = initBaseDir(definition); 067 this.workDir = initWorkingDir(definition); 068 this.name = definition.getName(); 069 this.originalName = definition.getOriginalName(); 070 this.version = definition.getVersion(); 071 this.originalVersion = definition.getOriginalVersion(); 072 this.description = definition.getDescription(); 073 this.keyWithBranch = definition.getKeyWithBranch(); 074 this.branch = definition.getBranch(); 075 this.sources = Collections.unmodifiableList(new ArrayList<>(definition.sources())); 076 this.tests = Collections.unmodifiableList(new ArrayList<>(definition.tests())); 077 this.properties = Collections.unmodifiableMap(new HashMap<>(definition.properties())); 078 079 this.definition = definition; 080 this.moduleKey = definition.getKey(); 081 } 082 083 private static Path initBaseDir(ProjectDefinition module) { 084 Path result; 085 try { 086 result = module.getBaseDir().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS); 087 } catch (IOException e) { 088 throw new IllegalStateException("Unable to resolve module baseDir", e); 089 } 090 return result; 091 } 092 093 private static Path initWorkingDir(ProjectDefinition module) { 094 File workingDirAsFile = module.getWorkDir(); 095 return workingDirAsFile.getAbsoluteFile().toPath().normalize(); 096 } 097 098 /** 099 * Module key without branch 100 */ 101 @Override 102 public String key() { 103 return moduleKey; 104 } 105 106 @Override 107 public boolean isFile() { 108 return false; 109 } 110 111 public ProjectDefinition definition() { 112 return definition; 113 } 114 115 public Path getBaseDir() { 116 return baseDir; 117 } 118 119 public Path getWorkDir() { 120 return workDir; 121 } 122 123 public String getKeyWithBranch() { 124 return keyWithBranch; 125 } 126 127 @CheckForNull 128 public String getBranch() { 129 return branch; 130 } 131 132 public Map<String, String> properties() { 133 return properties; 134 } 135 136 @CheckForNull 137 public String getOriginalVersion() { 138 return originalVersion; 139 } 140 141 public String getVersion() { 142 return version; 143 } 144 145 @CheckForNull 146 public String getOriginalName() { 147 return originalName; 148 } 149 150 public String getName() { 151 return name; 152 } 153 154 public String getDescription() { 155 return description; 156 } 157 158 /** 159 * @return Source files and folders. 160 */ 161 public List<String> sources() { 162 return sources; 163 } 164 165 public List<String> tests() { 166 return tests; 167 } 168 169}