aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2021-11-14 19:10:46 +0000
committerLuck <git@lucko.me>2021-11-14 19:10:46 +0000
commit49fc0ba47fac077a2fe965c44fd382a1ab02089c (patch)
tree2261bfdb4fd50b56a51fc1386eb4ac77d2fee939 /spark-common/src/main/java/me/lucko/spark/common
parenteed8b8d38260fb16f114ab856e486904ae6dc937 (diff)
downloadspark-49fc0ba47fac077a2fe965c44fd382a1ab02089c.tar.gz
spark-49fc0ba47fac077a2fe965c44fd382a1ab02089c.tar.bz2
spark-49fc0ba47fac077a2fe965c44fd382a1ab02089c.zip
Improve Velocity ClassSourceLookup
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java6
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java2
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java46
3 files changed, 23 insertions, 31 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
index d67c092..414db57 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
@@ -95,7 +95,6 @@ public class SparkPlatform {
private final ActivityLog activityLog;
private final TickHook tickHook;
private final TickReporter tickReporter;
- private final ClassSourceLookup classSourceLookup;
private final TickStatistics tickStatistics;
private Map<String, GarbageCollectorStatistics> startupGcStatistics = ImmutableMap.of();
private long serverNormalOperationStartTime;
@@ -132,7 +131,6 @@ public class SparkPlatform {
this.tickHook = plugin.createTickHook();
this.tickReporter = plugin.createTickReporter();
- this.classSourceLookup = plugin.createClassSourceLookup();
this.tickStatistics = this.tickHook != null ? new TickStatistics() : null;
}
@@ -212,8 +210,8 @@ public class SparkPlatform {
return this.tickReporter;
}
- public ClassSourceLookup getClassSourceLookup() {
- return this.classSourceLookup;
+ public ClassSourceLookup createClassSourceLookup() {
+ return this.plugin.createClassSourceLookup();
}
public TickStatistics getTickStatistics() {
diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java
index d45c7af..2dd07c9 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java
@@ -305,7 +305,7 @@ public class SamplerModule implements CommandModule {
}
private void handleUpload(SparkPlatform platform, CommandResponseHandler resp, Sampler sampler, ThreadNodeOrder threadOrder, String comment, MergeMode mergeMode, boolean saveToFileFlag) {
- SparkProtos.SamplerData output = sampler.toProto(platform.getPlugin().getPlatformInfo(), resp.sender(), threadOrder, comment, mergeMode, platform.getClassSourceLookup());
+ SparkProtos.SamplerData output = sampler.toProto(platform.getPlugin().getPlatformInfo(), resp.sender(), threadOrder, comment, mergeMode, platform.createClassSourceLookup());
boolean saveToFile = false;
if (saveToFileFlag) {
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java b/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java
index 9f58c7f..42a04f7 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java
@@ -83,9 +83,27 @@ public interface ClassSourceLookup {
}
/**
+ * A {@link ClassSourceLookup} which identifies classes based on URL.
+ */
+ interface ByUrl extends ClassSourceLookup {
+
+ default String identifyUrl(URL url) throws URISyntaxException {
+ return url.getProtocol().equals("file") ? identifyFile(Paths.get(url.toURI())) : null;
+ }
+
+ default String identifyFile(Path path) {
+ return identifyFileName(path.getFileName().toString());
+ }
+
+ default String identifyFileName(String fileName) {
+ return fileName.endsWith(".jar") ? fileName.substring(0, fileName.length() - 4) : null;
+ }
+ }
+
+ /**
* A {@link ClassSourceLookup} which identifies classes based on the first URL in a {@link URLClassLoader}.
*/
- class ByFirstUrlSource extends ByClassLoader {
+ class ByFirstUrlSource extends ByClassLoader implements ByUrl {
@Override
public @Nullable String identify(ClassLoader loader) throws IOException, URISyntaxException {
if (loader instanceof URLClassLoader) {
@@ -98,24 +116,12 @@ public interface ClassSourceLookup {
}
return null;
}
-
- protected String identifyUrl(URL url) throws URISyntaxException {
- return url.getProtocol().equals("file") ? identifyFile(Paths.get(url.toURI())) : null;
- }
-
- protected String identifyFile(Path path) {
- return identifyFileName(path.getFileName().toString());
- }
-
- protected String identifyFileName(String fileName) {
- return fileName.endsWith(".jar") ? fileName.substring(0, fileName.length() - 4) : null;
- }
}
/**
* A {@link ClassSourceLookup} which identifies classes based on their {@link ProtectionDomain#getCodeSource()}.
*/
- class ByCodeSource implements ClassSourceLookup {
+ class ByCodeSource implements ClassSourceLookup, ByUrl {
@Override
public @Nullable String identify(Class<?> clazz) throws URISyntaxException {
ProtectionDomain protectionDomain = clazz.getProtectionDomain();
@@ -130,18 +136,6 @@ public interface ClassSourceLookup {
URL url = codeSource.getLocation();
return url == null ? null : identifyUrl(url);
}
-
- protected String identifyUrl(URL url) throws URISyntaxException {
- return url.getProtocol().equals("file") ? identifyFile(Paths.get(url.toURI())) : null;
- }
-
- protected String identifyFile(Path path) {
- return identifyFileName(path.getFileName().toString());
- }
-
- protected String identifyFileName(String fileName) {
- return fileName.endsWith(".jar") ? fileName.substring(0, fileName.length() - 4) : null;
- }
}
interface Visitor {