BaseThreadDumpManager.java
3.37 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
*/
package com.adobe.granite.threaddump.impl;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Dictionary;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
abstract class BaseThreadDumpManager {
private static final String THREADDUMPS_DIRECTORY_NAME = "threaddumps";
private static final String CRX_WORKSPACE_DIRECTORY_NAME = "crx-quickstart";
private static final String SLING_HOME_PROPERTY = "sling.home";
private static final String USER_DIR_SYSTEM_PROPERTY = "user.dir";
private static final int DEFAULT_BUFFER_SIZE = 1024;
private File storingDirectory;
BaseThreadDumpManager() {
}
protected void activate(ComponentContext context) {
File userDir;
Dictionary contextProperties = context.getProperties();
String storingDirectoryName = OsgiUtil.toString(contextProperties.get("sling.home"), (String)null);
File home = storingDirectoryName == null || storingDirectoryName.length() == 0 ? ("crx-quickstart".equals((userDir = new File(System.getProperty("user.dir"))).getName()) ? userDir : new File(userDir, "crx-quickstart")) : new File(storingDirectoryName);
this.storingDirectory = new File(home, "threaddumps");
}
protected final File getStoringDirectory() {
return this.storingDirectory;
}
protected static void addToZip(ZipOutputStream os, File base, File file) throws IOException {
String name = base.toURI().relativize(file.toURI()).getPath();
if (file.isDirectory()) {
name = name.endsWith("/") ? name : name + "/";
}
ZipEntry entry = new ZipEntry(name);
entry.setSize(file.length());
os.putNextEntry(entry);
if (file.isDirectory()) {
for (File kid : file.listFiles()) {
BaseThreadDumpManager.addToZip(os, base, kid);
}
} else {
BaseThreadDumpManager.copyContent(file, os, false);
}
os.closeEntry();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected static void copyContent(File source, OutputStream targetStream, boolean closeTargetStream) throws IOException {
FileInputStream sourceStream = null;
try {
int bytesRead;
sourceStream = new FileInputStream(source);
byte[] buffer = new byte[1024];
while ((bytesRead = sourceStream.read(buffer)) > 0) {
targetStream.write(buffer, 0, bytesRead);
}
targetStream.flush();
}
finally {
BaseThreadDumpManager.closeQuietly(sourceStream);
if (closeTargetStream) {
BaseThreadDumpManager.closeQuietly(targetStream);
}
}
}
protected static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
}
catch (IOException e) {
// empty catch block
}
}
}
}