You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractProjectOrModule.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.batch.fs.internal;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.charset.Charset;
  24. import java.nio.file.Files;
  25. import java.nio.file.LinkOption;
  26. import java.nio.file.Path;
  27. import java.util.Collections;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import javax.annotation.CheckForNull;
  31. import javax.annotation.concurrent.Immutable;
  32. import org.apache.commons.lang.StringUtils;
  33. import org.apache.commons.lang.SystemUtils;
  34. import org.sonar.api.CoreProperties;
  35. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  36. import org.sonar.api.utils.log.Logger;
  37. import org.sonar.api.utils.log.Loggers;
  38. @Immutable
  39. public abstract class AbstractProjectOrModule extends DefaultInputComponent {
  40. private static final Logger LOGGER = Loggers.get(AbstractProjectOrModule.class);
  41. private final Path baseDir;
  42. private final Path workDir;
  43. private final String name;
  44. private final String version;
  45. private final String originalName;
  46. private final String originalVersion;
  47. private final String description;
  48. private final String keyWithBranch;
  49. private final String branch;
  50. private final Map<String, String> properties;
  51. private final String key;
  52. private final ProjectDefinition definition;
  53. private final Charset encoding;
  54. public AbstractProjectOrModule(ProjectDefinition definition, int scannerComponentId) {
  55. super(scannerComponentId);
  56. this.baseDir = initBaseDir(definition);
  57. this.workDir = initWorkingDir(definition);
  58. this.name = definition.getName();
  59. this.originalName = definition.getOriginalName();
  60. this.version = definition.getVersion();
  61. this.originalVersion = definition.getOriginalVersion();
  62. this.description = definition.getDescription();
  63. this.keyWithBranch = definition.getKeyWithBranch();
  64. this.branch = definition.getBranch();
  65. this.properties = Collections.unmodifiableMap(new HashMap<>(definition.properties()));
  66. this.definition = definition;
  67. this.key = definition.getKey();
  68. this.encoding = initEncoding(definition);
  69. }
  70. private static Charset initEncoding(ProjectDefinition module) {
  71. String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY);
  72. Charset result;
  73. if (StringUtils.isNotEmpty(encodingStr)) {
  74. result = Charset.forName(StringUtils.trim(encodingStr));
  75. } else {
  76. result = Charset.defaultCharset();
  77. }
  78. return result;
  79. }
  80. private static Path initBaseDir(ProjectDefinition module) {
  81. Path result;
  82. try {
  83. result = module.getBaseDir().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS);
  84. } catch (IOException e) {
  85. throw new IllegalStateException("Unable to resolve module baseDir", e);
  86. }
  87. return result;
  88. }
  89. private static Path initWorkingDir(ProjectDefinition module) {
  90. File workingDirAsFile = module.getWorkDir();
  91. Path workingDir = workingDirAsFile.getAbsoluteFile().toPath().normalize();
  92. if (SystemUtils.IS_OS_WINDOWS) {
  93. try {
  94. Files.setAttribute(workingDir, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
  95. } catch (IOException e) {
  96. LOGGER.warn("Failed to set working directory hidden: {}", e.getMessage());
  97. }
  98. }
  99. return workingDir;
  100. }
  101. /**
  102. * Module key without branch
  103. */
  104. @Override
  105. public String key() {
  106. return key;
  107. }
  108. @Override
  109. public boolean isFile() {
  110. return false;
  111. }
  112. public ProjectDefinition definition() {
  113. return definition;
  114. }
  115. public Path getBaseDir() {
  116. return baseDir;
  117. }
  118. public Path getWorkDir() {
  119. return workDir;
  120. }
  121. public String getKeyWithBranch() {
  122. return keyWithBranch;
  123. }
  124. @CheckForNull
  125. public String getBranch() {
  126. return branch;
  127. }
  128. public Map<String, String> properties() {
  129. return properties;
  130. }
  131. @CheckForNull
  132. public String getOriginalVersion() {
  133. return originalVersion;
  134. }
  135. public String getVersion() {
  136. return version;
  137. }
  138. @CheckForNull
  139. public String getOriginalName() {
  140. return originalName;
  141. }
  142. public String getName() {
  143. return name;
  144. }
  145. public String getDescription() {
  146. return description;
  147. }
  148. public Charset getEncoding() {
  149. return encoding;
  150. }
  151. }