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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 originalName;
  45. private final String description;
  46. private final Map<String, String> properties;
  47. private final String key;
  48. private final ProjectDefinition definition;
  49. private final Charset encoding;
  50. public AbstractProjectOrModule(ProjectDefinition definition, int scannerComponentId) {
  51. super(scannerComponentId);
  52. this.baseDir = initBaseDir(definition);
  53. this.workDir = initWorkingDir(definition);
  54. this.name = definition.getName();
  55. this.originalName = definition.getOriginalName();
  56. this.description = definition.getDescription();
  57. this.properties = Collections.unmodifiableMap(new HashMap<>(definition.properties()));
  58. this.definition = definition;
  59. this.key = definition.getKey();
  60. this.encoding = initEncoding(definition);
  61. }
  62. private static Charset initEncoding(ProjectDefinition module) {
  63. String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY);
  64. Charset result;
  65. if (StringUtils.isNotEmpty(encodingStr)) {
  66. result = Charset.forName(StringUtils.trim(encodingStr));
  67. } else {
  68. result = Charset.defaultCharset();
  69. }
  70. return result;
  71. }
  72. private static Path initBaseDir(ProjectDefinition module) {
  73. Path result;
  74. try {
  75. result = module.getBaseDir().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS);
  76. } catch (IOException e) {
  77. throw new IllegalStateException("Unable to resolve module baseDir", e);
  78. }
  79. return result;
  80. }
  81. private static Path initWorkingDir(ProjectDefinition module) {
  82. File workingDirAsFile = module.getWorkDir();
  83. Path workingDir = workingDirAsFile.getAbsoluteFile().toPath().normalize();
  84. if (SystemUtils.IS_OS_WINDOWS) {
  85. try {
  86. Files.createDirectories(workingDir);
  87. Files.setAttribute(workingDir, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
  88. } catch (IOException e) {
  89. LOGGER.warn("Failed to set working directory hidden: {}", e.getMessage());
  90. }
  91. }
  92. return workingDir;
  93. }
  94. @Override
  95. public String key() {
  96. return key;
  97. }
  98. @Override
  99. public boolean isFile() {
  100. return false;
  101. }
  102. public ProjectDefinition definition() {
  103. return definition;
  104. }
  105. public Path getBaseDir() {
  106. return baseDir;
  107. }
  108. public Path getWorkDir() {
  109. return workDir;
  110. }
  111. public Map<String, String> properties() {
  112. return properties;
  113. }
  114. @CheckForNull
  115. public String getOriginalName() {
  116. return originalName;
  117. }
  118. public String getName() {
  119. return name;
  120. }
  121. public String getDescription() {
  122. return description;
  123. }
  124. public Charset getEncoding() {
  125. return encoding;
  126. }
  127. }