-
Notifications
You must be signed in to change notification settings - Fork 70
ExpectedSystemExit improvement in multi-threads environment #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 10 commits
e69de84
deebe0d
52c2795
26e344d
cc6afe4
03ddfec
368b566
cea8cfc
a4d86e1
b3ad88f
6ecda12
ccb090b
1b3c0ee
557a17d
104514c
a63c648
ef13c1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,37 +3,45 @@ | |
| import java.io.FileDescriptor; | ||
| import java.net.InetAddress; | ||
| import java.security.Permission; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * A {@code NoExitSecurityManager} throws a {@link CheckExitCalled} exception | ||
| * whenever {@link #checkExit(int)} is called. All other method calls are | ||
| * delegated to the original security manager. | ||
| */ | ||
| public class NoExitSecurityManager extends SecurityManager { | ||
|
|
||
| private final SecurityManager originalSecurityManager; | ||
| private final long timeout; | ||
|
|
||
| private final CountDownLatch synchLatch = new CountDownLatch(1); | ||
| private Integer statusOfFirstExitCall = null; | ||
|
|
||
| public NoExitSecurityManager(SecurityManager originalSecurityManager) { | ||
| public NoExitSecurityManager(SecurityManager originalSecurityManager, long timeout) { | ||
| this.originalSecurityManager = originalSecurityManager; | ||
| this.timeout = timeout; | ||
| } | ||
|
|
||
| @Override | ||
| public void checkExit(int status) { | ||
| if (statusOfFirstExitCall == null) | ||
| statusOfFirstExitCall = status; | ||
| synchLatch.countDown(); | ||
| throw new CheckExitCalled(status); | ||
| } | ||
|
|
||
| public boolean isCheckExitCalled() { | ||
| return statusOfFirstExitCall != null; | ||
| public boolean isCheckExitCalled() throws InterruptedException { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add the timeout as a parameter to the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stefanbirkner done |
||
| boolean wasCountDown = synchLatch.await(timeout, TimeUnit.MILLISECONDS); | ||
| return statusOfFirstExitCall != null && wasCountDown; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't have to check
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stefanbirkner agreed. Also it's possible to change type of WDYT? |
||
| } | ||
|
|
||
| public int getStatusOfFirstCheckExitCall() { | ||
| public int getStatusOfFirstCheckExitCall() throws InterruptedException { | ||
| if (isCheckExitCalled()) | ||
| return statusOfFirstExitCall; | ||
| else | ||
| throw new IllegalStateException( | ||
| "checkExit(int) has not been called."); | ||
| throw new IllegalStateException("checkExit(int) has not been called."); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't catch that exception. By rethrowing it, the developer gets the full stacktrace of that exception which can be valuable. Additionally it is less code on our side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Fixed.