Skip to content

Commit 5a018c6

Browse files
author
goktug@google.com
committed
Changes StackTraceDeobfuscator to abstract and provide various factory methods.
This change will remove the need for extending StackTraceObfusctor most of the time as in almost all scenarios it is extended to get the symbol map from jar through the class loader (which makes sense). Also a factory is provided to get the symbol map from the file system. For all other cases, developers can construct the deobfuscator with a URL path or extend the class to override openInputStream method. Change-Id: I562e052caef8da7f3434319cf11b8984bc347fe5 Review-Link: https://gwt-review.googlesource.com/#/c/2270/ Review by: skybrian@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@11565 8db76d5a-ed1c-0410-87a9-c151d255dfc7
1 parent b5e96c3 commit 5a018c6

File tree

2 files changed

+65
-50
lines changed

2 files changed

+65
-50
lines changed

user/src/com/google/gwt/core/server/impl/StackTraceDeobfuscator.java

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.InputStreamReader;
28+
import java.net.URL;
2829
import java.util.HashMap;
2930
import java.util.HashSet;
3031
import java.util.Map;
@@ -45,7 +46,48 @@
4546
* directory is written. By default, the final <code>symbolMaps</code> directory is
4647
* <code>war/WEB-INF/deploy/<i>yourmodulename</i>/symbolMaps/</code>.
4748
*/
48-
public class StackTraceDeobfuscator {
49+
public abstract class StackTraceDeobfuscator {
50+
51+
/**
52+
* Creates a deobfuscator that loads symbol and source map files under given resource path. Uses
53+
* StackTraceObfuscator's {@link ClassLoader}.
54+
*/
55+
public static StackTraceDeobfuscator fromResource(String symbolMapsPath) {
56+
final String basePath = symbolMapsPath.endsWith("/") ? symbolMapsPath : symbolMapsPath + "/";
57+
final ClassLoader classLoader = StackTraceDeobfuscator.class.getClassLoader();
58+
return new StackTraceDeobfuscator() {
59+
protected InputStream openInputStream(String fileName) throws IOException {
60+
String filePath = basePath + fileName;
61+
InputStream inputStream = classLoader.getResourceAsStream(filePath);
62+
if (inputStream == null) {
63+
throw new IOException("Missing resource: " + filePath);
64+
}
65+
return inputStream;
66+
}
67+
};
68+
}
69+
70+
/**
71+
* Creates a deobfuscator that loads symbol and source map files from the given directory.
72+
*/
73+
public static StackTraceDeobfuscator fromFileSystem(final String symbolMapsDirectory) {
74+
return new StackTraceDeobfuscator() {
75+
protected InputStream openInputStream(String fileName) throws IOException {
76+
return new FileInputStream(new File(symbolMapsDirectory, fileName));
77+
}
78+
};
79+
}
80+
81+
/**
82+
* Creates a deobfuscator that loads symbol and source map files beneath the given URL.
83+
*/
84+
public static StackTraceDeobfuscator fromUrl(final URL urlPath) {
85+
return new StackTraceDeobfuscator() {
86+
protected InputStream openInputStream(String fileName) throws IOException {
87+
return new URL(urlPath, fileName).openStream();
88+
}
89+
};
90+
}
4991

5092
/**
5193
* A cache that maps obfuscated symbols to arbitrary non-null string values. The cache can assume
@@ -105,36 +147,16 @@ Map<String, String> getAll(String strongName, Set<String> symbols) {
105147
private static final int LINE_NUMBER_UNKNOWN = -1;
106148
private static final String SYMBOL_DATA_UNKNOWN = "";
107149

108-
private final boolean lazyLoad;
109-
private final File symbolMapsDirectory;
110150
private final Map<String, SourceMapping> sourceMaps = new HashMap<String, SourceMapping>();
111151
private final SymbolCache symbolCache = new SymbolCache();
152+
private boolean lazyLoad = false;
112153

113154
/**
114-
* Creates a deobfuscator that loads symbol map files from the given directory. Symbol maps are
115-
* generated into the location specified by the GWT compiler <code>-deploy</code> command line
116-
* argument.
117-
*
118-
* @param symbolMapsDirectory the <code>symbolMaps</code> directory, with or without trailing
119-
* directory separator character
155+
* If set to {@code true}, only symbols requested to be deobfuscated are cached and the rest is
156+
* discarded. This provides a large memory savings at the expense of occasional extra disk reads.
157+
* Note that, this will only have effect on symbol maps that haven't been fully loaded yet.
120158
*/
121-
public StackTraceDeobfuscator(String symbolMapsDirectory) {
122-
this(symbolMapsDirectory, false);
123-
}
124-
125-
/**
126-
* Creates a deobfuscator that loads symbol map files from the given directory. Symbol maps are
127-
* generated into the location specified by the GWT compiler <code>-deploy</code> command line
128-
* argument.
129-
*
130-
* @param symbolMapsDirectory the <code>symbolMaps</code> directory, with or without trailing
131-
* directory separator character. Usually this directory will include files with .symbolmap
132-
or .json extensions.
133-
* @param lazyLoad if true, only symbols requested to be deobfuscated are cached. This provides a
134-
* large memory savings at the expense of occasional extra disk reads.
135-
*/
136-
public StackTraceDeobfuscator(String symbolMapsDirectory, boolean lazyLoad) {
137-
this.symbolMapsDirectory = new File(symbolMapsDirectory);
159+
public void setLazyLoad(boolean lazyLoad) {
138160
this.lazyLoad = lazyLoad;
139161
}
140162

@@ -293,26 +315,30 @@ public final StackTraceElement resymbolize(StackTraceElement ste, String strongN
293315

294316
protected InputStream getSourceMapInputStream(String permutationStrongName, int fragmentNumber)
295317
throws IOException {
296-
String filename = symbolMapsDirectory.getCanonicalPath()
297-
+ File.separatorChar + permutationStrongName + "_sourceMap" + fragmentNumber + ".json";
298-
return new FileInputStream(filename);
318+
return openInputStream(permutationStrongName + "_sourceMap" + fragmentNumber + ".json");
299319
}
300320

301321
/**
302322
* Retrieves a new {@link InputStream} for the given permutation strong name. This implementation,
303323
* which subclasses may override, returns a {@link InputStream} for the <code>
304-
* <i>permutation-strong-name</i>.symbolMap</code> file in the <code>symbolMaps</code> directory.
324+
* <i>permutation-strong-name</i>.symbolMap</code> file.
305325
*
306326
* @param permutationStrongName the GWT permutation strong name
307327
* @return a new {@link InputStream}
308328
*/
309-
protected InputStream getSymbolMapInputStream(String permutationStrongName)
310-
throws IOException {
311-
String filename = symbolMapsDirectory.getCanonicalPath()
312-
+ File.separatorChar + permutationStrongName + ".symbolMap";
313-
return new FileInputStream(filename);
329+
protected InputStream getSymbolMapInputStream(String permutationStrongName) throws IOException {
330+
return openInputStream(permutationStrongName + ".symbolMap");
314331
}
315332

333+
/**
334+
* Opens a new {@link InputStream} for a symbol or source map file.
335+
*
336+
* @param fileName name of the symbol or source map file
337+
* @return an input stream for reading the file (doesn't need to be buffered).
338+
* @exception IOException if an I/O error occurs while creating the input stream.
339+
*/
340+
protected abstract InputStream openInputStream(String fileName) throws IOException;
341+
316342
private SourceMapping loadSourceMap(String permutationStrongName, int fragmentId) {
317343
SourceMapping toReturn = sourceMaps.get(permutationStrongName + fragmentId);
318344
if (toReturn == null) {

user/src/com/google/gwt/logging/server/StackTraceDeobfuscator.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public class StackTraceDeobfuscator extends com.google.gwt.core.server.impl.Stac
3939
* directory separator character
4040
*/
4141
public StackTraceDeobfuscator(String symbolMapsDirectory) {
42-
super(symbolMapsDirectory);
43-
setSymbolMapsDirectory(symbolMapsDirectory);
42+
this(symbolMapsDirectory, false);
4443
}
4544

4645
/**
@@ -54,7 +53,7 @@ public StackTraceDeobfuscator(String symbolMapsDirectory) {
5453
* a large memory savings at the expense of occasional extra disk reads.
5554
*/
5655
public StackTraceDeobfuscator(String symbolMapsDirectory, boolean lazyLoad) {
57-
super(symbolMapsDirectory, lazyLoad);
56+
setLazyLoad(lazyLoad);
5857
setSymbolMapsDirectory(symbolMapsDirectory);
5958
}
6059

@@ -96,17 +95,7 @@ public void setSymbolMapsDirectory(String symbolMapsDirectory) {
9695
this.symbolMapsDirectory = new File(symbolMapsDirectory);
9796
}
9897

99-
protected InputStream getSourceMapInputStream(String permutationStrongName, int fragmentNumber)
100-
throws IOException {
101-
String filename = symbolMapsDirectory.getCanonicalPath()
102-
+ File.separatorChar + permutationStrongName + "_sourceMap" + fragmentNumber + ".json";
103-
return new FileInputStream(filename);
104-
}
105-
106-
protected InputStream getSymbolMapInputStream(String permutationStrongName)
107-
throws IOException {
108-
String filename = symbolMapsDirectory.getCanonicalPath()
109-
+ File.separatorChar + permutationStrongName + ".symbolMap";
110-
return new FileInputStream(filename);
98+
protected InputStream openInputStream(String fileName) throws IOException {
99+
return new FileInputStream(new File(symbolMapsDirectory, fileName));
111100
}
112101
}

0 commit comments

Comments
 (0)