Skip to content

Commit

Permalink
if using Tomcat, display Tomcat's sources like for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
evernat committed Apr 12, 2020
1 parent 554e20f commit 9907e93
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Expand Up @@ -67,6 +67,8 @@ public final class MavenArtifact implements Serializable {
private static final File LOCAL_REPO = new File(
System.getProperty("user.home") + "/.m2/repository");

private static final String TOMCAT_ARCHIVES = "https://archive.apache.org/dist/tomcat/";

private static Map<String, String> sourceFilePathsByJarFileNames;

private static String webappVersion;
Expand Down Expand Up @@ -618,6 +620,41 @@ private static File getMavenArtifact(String filePath) throws IOException {
return null;
}

public static File getTomcatSrcZipFile() throws IOException {
final String serverInfo = Parameters.getServletContext().getServerInfo();
if (!serverInfo.matches("Apache Tomcat/\\d+\\.\\d+\\.\\d+")) {
// si pas Tomcat ou si Tomcat version 0.0.0.Mx, tant pis
return null;
}
final String version = serverInfo.substring(serverInfo.lastIndexOf('/') + 1);
final String fileName = "apache-tomcat-" + version + "-src.zip";
final File storageDirectory = Parameters
.getStorageDirectory(Parameters.getCurrentApplication());
final String subDirectory = "sources";
final File file = new File(storageDirectory, subDirectory + '/' + fileName);
if (!file.exists() || file.length() == 0) {
final String majorVersion = version.substring(0, version.indexOf('.'));
final String url = TOMCAT_ARCHIVES + "tomcat-" + majorVersion + "/v" + version + "/src/"
+ fileName;
mkdirs(file.getParentFile());
final OutputStream output = new FileOutputStream(file);
try {
final LabradorRetriever labradorRetriever = new LabradorRetriever(new URL(url));
labradorRetriever.downloadTo(output);
} catch (final IOException e) {
output.close();
InputOutput.deleteFile(file);
// si non trouvé, on continue avec le repo suivant s'il y en a un
} finally {
output.close();
}
}
if (file.exists()) {
return file;
}
return null;
}

private static void mkdirs(File directory) {
if (!directory.exists() && !directory.mkdirs()) {
throw new IllegalStateException("Can't create directory " + directory.getPath());
Expand Down
Expand Up @@ -26,7 +26,10 @@
import java.io.Writer;
import java.nio.charset.Charset;
import java.security.CodeSource;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -39,6 +42,9 @@
*/
class HtmlSourceReport extends HtmlAbstractReport {
private static final File JDK_SRC_FILE = getJdkSrcFile();
private static final List<String> TOMCAT_PACKAGES = Collections.unmodifiableList(
Arrays.asList("org.apache.tomcat", "org.apache.catalina", "org.apache.coyote",
"org.apache.jasper", "org.apache.el", "org.apache.juli", "org.apache.naming"));

private final String source;

Expand Down Expand Up @@ -78,6 +84,19 @@ private String getSource(String className) throws IOException {
final File sourceJarFile = MavenArtifact.getSourceJarFile(codeSource.getLocation());
if (sourceJarFile != null) {
return getSourceFromZip(sourceFilePath, sourceJarFile);
} else if (clazz.getName().startsWith("org.apache.")) {
for (final String tomcatPackage : TOMCAT_PACKAGES) {
if (clazz.getName().startsWith(tomcatPackage + '.')) {
final File tomcatSrcFile = MavenArtifact.getTomcatSrcZipFile();
if (tomcatSrcFile != null) {
assert tomcatSrcFile.getName().endsWith(".zip");
final String entryName = tomcatSrcFile.getName().substring(0,
tomcatSrcFile.getName().length() - ".zip".length()) + "/java/"
+ sourceFilePath;
return getSourceFromZip(entryName, tomcatSrcFile);
}
}
}
}
}
return null;
Expand Down

0 comments on commit 9907e93

Please sign in to comment.