Skip to content

Commit 7cd94b1

Browse files
committed
Improve quality
1 parent c08b60b commit 7cd94b1

14 files changed

Lines changed: 396 additions & 277 deletions

api/src/main/java/org/sonarsource/scanner/api/Utils.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,26 @@ public static void deleteQuietly(Path f) {
108108
private static class DeleteQuietlyFileVisitor extends SimpleFileVisitor<Path> {
109109
@Override
110110
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
111-
try {
112-
Files.delete(file);
113-
} catch (IOException e) {
114-
// ignore
115-
}
116-
return FileVisitResult.CONTINUE;
111+
return deleteAndContinue(file);
117112
}
118113

119114
@Override
120115
public FileVisitResult visitFileFailed(Path file, IOException exc) {
121-
try {
122-
Files.delete(file);
123-
} catch (IOException e) {
124-
// ignore
125-
}
126-
return FileVisitResult.CONTINUE;
116+
return deleteAndContinue(file);
127117
}
128118

129119
@Override
130120
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
121+
return deleteAndContinue(dir);
122+
}
123+
124+
private static FileVisitResult deleteAndContinue(Path path) {
131125
try {
132-
Files.delete(dir);
126+
Files.delete(path);
133127
} catch (IOException e) {
134128
// ignore
135129
}
136130
return FileVisitResult.CONTINUE;
137131
}
138132
}
139-
140133
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* SonarQube Scanner API
3+
* Copyright (C) 2011-2017 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.sonarsource.scanner.api.internal;
21+
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import org.sonarsource.scanner.api.internal.cache.Logger;
25+
26+
class BootstrapIndexDownloader {
27+
private final ServerConnection conn;
28+
private final Logger logger;
29+
30+
BootstrapIndexDownloader(ServerConnection conn, Logger logger) {
31+
this.conn = conn;
32+
this.logger = logger;
33+
}
34+
35+
Collection<JarEntry> getIndex() {
36+
String index;
37+
try {
38+
logger.debug("Get bootstrap index...");
39+
index = conn.downloadString("/batch/index");
40+
logger.debug("Get bootstrap completed");
41+
} catch (Exception e) {
42+
throw new IllegalStateException("Fail to get bootstrap index from server", e);
43+
}
44+
return parse(index);
45+
}
46+
47+
private static Collection<JarEntry> parse(String index) {
48+
Collection<JarEntry> entries = new ArrayList<>();
49+
50+
String[] lines = index.split("[\r\n]+");
51+
for (String line : lines) {
52+
try {
53+
line = line.trim();
54+
String[] libAndHash = line.split("\\|");
55+
String filename = libAndHash[0];
56+
String hash = libAndHash[1];
57+
entries.add(new JarEntry(filename, hash));
58+
} catch (Exception e) {
59+
throw new IllegalStateException("Fail to parse entry in bootstrap index: " + line);
60+
}
61+
}
62+
63+
return entries;
64+
}
65+
66+
static class JarEntry {
67+
private String filename;
68+
private String hash;
69+
70+
JarEntry(String filename, String hash) {
71+
this.filename = filename;
72+
this.hash = hash;
73+
}
74+
75+
public String getFilename() {
76+
return filename;
77+
}
78+
79+
public String getHash() {
80+
return hash;
81+
}
82+
}
83+
}

api/src/main/java/org/sonarsource/scanner/api/internal/IsolatedLauncherFactory.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,24 @@ public IsolatedLauncher createLauncher(Map<String, String> props, ClassloadRules
6262
return new SimulatedLauncher(version, logger);
6363
}
6464
ServerConnection serverConnection = ServerConnection.create(props, logger);
65-
JarDownloader jarDownloader = new JarDownloader(serverConnection, logger, props.get("sonar.userHome"));
65+
JarDownloader jarDownloader = new JarDownloaderFactory(serverConnection, logger, props.get("sonar.userHome")).create();
6666

6767
return createLauncher(jarDownloader, rules);
6868
}
6969

7070
IsolatedLauncher createLauncher(final JarDownloader jarDownloader, final ClassloadRules rules) {
71-
return AccessController.doPrivileged(new PrivilegedAction<IsolatedLauncher>() {
72-
@Override
73-
public IsolatedLauncher run() {
74-
try {
75-
List<File> jarFiles = jarDownloader.download();
76-
logger.debug("Create isolated classloader...");
77-
ClassLoader cl = createClassLoader(jarFiles, rules);
78-
IsolatedLauncher objProxy = IsolatedLauncherProxy.create(cl, IsolatedLauncher.class, launcherImplClassName, logger);
79-
tempCleaning.clean();
71+
return AccessController.doPrivileged((PrivilegedAction<IsolatedLauncher>) () -> {
72+
try {
73+
List<File> jarFiles = jarDownloader.download();
74+
logger.debug("Create isolated classloader...");
75+
ClassLoader cl = createClassLoader(jarFiles, rules);
76+
IsolatedLauncher objProxy = IsolatedLauncherProxy.create(cl, IsolatedLauncher.class, launcherImplClassName, logger);
77+
tempCleaning.clean();
8078

81-
return objProxy;
82-
} catch (Exception e) {
83-
// Catch all other exceptions, which relates to reflection
84-
throw new ScannerException("Unable to execute SonarQube", e);
85-
}
79+
return objProxy;
80+
} catch (Exception e) {
81+
// Catch all other exceptions, which relates to reflection
82+
throw new ScannerException("Unable to execute SonarQube", e);
8683
}
8784
});
8885
}

api/src/main/java/org/sonarsource/scanner/api/internal/JarDownloader.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,57 @@
2020
package org.sonarsource.scanner.api.internal;
2121

2222
import java.io.File;
23+
import java.io.IOException;
24+
import java.util.ArrayList;
25+
import java.util.Collection;
2326
import java.util.List;
24-
import javax.annotation.Nullable;
27+
import java.util.stream.Collectors;
28+
import org.sonarsource.scanner.api.internal.BootstrapIndexDownloader.JarEntry;
29+
import org.sonarsource.scanner.api.internal.cache.FileCache;
2530
import org.sonarsource.scanner.api.internal.cache.Logger;
2631

32+
import static java.lang.String.format;
33+
2734
class JarDownloader {
28-
private final ServerConnection serverConnection;
35+
private final FileCache fileCache;
36+
private final JarExtractor jarExtractor;
2937
private final Logger logger;
30-
private final String userHome;
38+
private final ScannerFileDownloader scannerFileDownloader;
39+
private final BootstrapIndexDownloader bootstrapIndexDownloader;
3140

32-
JarDownloader(ServerConnection conn, Logger logger, @Nullable String userHome) {
33-
this.serverConnection = conn;
41+
JarDownloader(ScannerFileDownloader scannerFileDownloader, BootstrapIndexDownloader bootstrapIndexDownloader, FileCache fileCache, JarExtractor jarExtractor, Logger logger) {
42+
this.scannerFileDownloader = scannerFileDownloader;
43+
this.bootstrapIndexDownloader = bootstrapIndexDownloader;
3444
this.logger = logger;
35-
this.userHome = userHome;
45+
this.fileCache = fileCache;
46+
this.jarExtractor = jarExtractor;
3647
}
3748

3849
List<File> download() {
39-
return new Jars(serverConnection, new JarExtractor(), logger, userHome).download();
50+
List<File> files = new ArrayList<>();
51+
logger.debug("Extract sonar-scanner-api-batch in temp...");
52+
files.add(jarExtractor.extractToTemp("sonar-scanner-api-batch").toFile());
53+
files.addAll(getScannerEngineFiles());
54+
return files;
55+
}
56+
57+
private List<File> getScannerEngineFiles() {
58+
Collection<JarEntry> index = bootstrapIndexDownloader.getIndex();
59+
return index.stream()
60+
.map(jar -> fileCache.get(jar.getFilename(), jar.getHash(), scannerFileDownloader))
61+
.collect(Collectors.toList());
62+
}
63+
64+
static class ScannerFileDownloader implements FileCache.Downloader {
65+
private final ServerConnection connection;
66+
67+
ScannerFileDownloader(ServerConnection conn) {
68+
this.connection = conn;
69+
}
70+
71+
@Override
72+
public void download(String filename, File toFile) throws IOException {
73+
connection.downloadFile(format("/batch/file?name=%s", filename), toFile.toPath());
74+
}
4075
}
4176
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* SonarQube Scanner API
3+
* Copyright (C) 2011-2017 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.sonarsource.scanner.api.internal;
21+
22+
import javax.annotation.Nullable;
23+
import org.sonarsource.scanner.api.internal.JarDownloader.ScannerFileDownloader;
24+
import org.sonarsource.scanner.api.internal.cache.FileCache;
25+
import org.sonarsource.scanner.api.internal.cache.FileCacheBuilder;
26+
import org.sonarsource.scanner.api.internal.cache.Logger;
27+
28+
class JarDownloaderFactory {
29+
private final ServerConnection serverConnection;
30+
private final Logger logger;
31+
private final String userHome;
32+
33+
JarDownloaderFactory(ServerConnection conn, Logger logger, @Nullable String userHome) {
34+
this.serverConnection = conn;
35+
this.logger = logger;
36+
this.userHome = userHome;
37+
}
38+
39+
JarDownloader create() {
40+
FileCache fileCache = new FileCacheBuilder(logger)
41+
.setUserHome(userHome)
42+
.build();
43+
BootstrapIndexDownloader bootstrapIndexDownloader = new BootstrapIndexDownloader(serverConnection, logger);
44+
ScannerFileDownloader scannerFileDownloader = new ScannerFileDownloader(serverConnection);
45+
JarExtractor jarExtractor = new JarExtractor();
46+
return new JarDownloader(scannerFileDownloader, bootstrapIndexDownloader, fileCache, jarExtractor, logger);
47+
}
48+
}

api/src/main/java/org/sonarsource/scanner/api/internal/Jars.java

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)