aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/AbstractProjectOrModule.java
blob: de2aad6fc546aff711d99f4450a38fc3d39a2cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * 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  02110-1301, USA.
 */
package org.sonar.api.batch.fs.internal;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;

@Immutable
public abstract class AbstractProjectOrModule extends DefaultInputComponent {
  private static final Logger LOGGER = LoggerFactory.getLogger(AbstractProjectOrModule.class);
  private final Path baseDir;
  private final Path workDir;
  private final String name;
  private final String originalName;
  private final String description;
  private final Map<String, String> properties;

  private final String key;
  private final ProjectDefinition definition;
  private final Charset encoding;

  public AbstractProjectOrModule(ProjectDefinition definition, int scannerComponentId) {
    super(scannerComponentId);
    this.baseDir = initBaseDir(definition);
    this.workDir = initWorkingDir(definition);
    this.name = definition.getName();
    this.originalName = definition.getOriginalName();
    this.description = definition.getDescription();
    this.properties = Collections.unmodifiableMap(new HashMap<>(definition.properties()));

    this.definition = definition;
    this.key = definition.getKey();
    this.encoding = initEncoding(definition);
  }

  private static Charset initEncoding(ProjectDefinition module) {
    String encodingStr = module.properties().get(CoreProperties.ENCODING_PROPERTY);
    Charset result;
    if (StringUtils.isNotEmpty(encodingStr)) {
      result = Charset.forName(StringUtils.trim(encodingStr));
    } else {
      result = Charset.defaultCharset();
    }
    return result;
  }

  private static Path initBaseDir(ProjectDefinition module) {
    Path result;
    try {
      result = module.getBaseDir().toPath().toRealPath(LinkOption.NOFOLLOW_LINKS);
    } catch (IOException e) {
      throw new IllegalStateException("Unable to resolve module baseDir", e);
    }
    return result;
  }

  private static Path initWorkingDir(ProjectDefinition module) {
    File workingDirAsFile = module.getWorkDir();
    Path workingDir = workingDirAsFile.getAbsoluteFile().toPath().normalize();
    if (SystemUtils.IS_OS_WINDOWS) {
      try {
        Files.createDirectories(workingDir);
        Files.setAttribute(workingDir, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
      } catch (IOException e) {
        LOGGER.warn("Failed to set working directory hidden: {}", e.getMessage());
      }
    }
    return workingDir;
  }

  @Override
  public String key() {
    return key;
  }

  @Override
  public boolean isFile() {
    return false;
  }

  public ProjectDefinition definition() {
    return definition;
  }

  public Path getBaseDir() {
    return baseDir;
  }

  public Path getWorkDir() {
    return workDir;
  }

  public Map<String, String> properties() {
    return properties;
  }

  @CheckForNull
  public String getOriginalName() {
    return originalName;
  }

  public String getName() {
    return name;
  }

  public String getDescription() {
    return description;
  }

  public Charset getEncoding() {
    return encoding;
  }
}