forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ins-test/src/main/java/com/navercorp/pinpoint/test/plugin/util/ThreadContextExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.navercorp.pinpoint.test.plugin.util; | ||
|
||
import java.util.Objects; | ||
|
||
public class ThreadContextExecutor { | ||
|
||
private final ClassLoader context; | ||
|
||
public ThreadContextExecutor(ClassLoader context) { | ||
// @Nullable | ||
this.context = context; | ||
} | ||
|
||
public ClassLoader getClassLoader() { | ||
return context; | ||
} | ||
|
||
public void execute(Executable executable) { | ||
Objects.requireNonNull(executable, "executable"); | ||
|
||
final Thread thread = Thread.currentThread(); | ||
final ClassLoader before = thread.getContextClassLoader(); | ||
thread.setContextClassLoader(context); | ||
try { | ||
executable.execute(); | ||
} finally { | ||
thread.setContextClassLoader(before); | ||
} | ||
} | ||
|
||
|
||
public <V> V call(Callable<V> callable) { | ||
Objects.requireNonNull(callable, "callable"); | ||
|
||
final Thread thread = Thread.currentThread(); | ||
final ClassLoader before = thread.getContextClassLoader(); | ||
thread.setContextClassLoader(context); | ||
try { | ||
return callable.call(); | ||
} finally { | ||
thread.setContextClassLoader(before); | ||
} | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Executable { | ||
void execute(); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Callable<V> { | ||
V call(); | ||
} | ||
} |