Hi Mukul, the problem with System.exit() can be in a library - dependency,
not only in your code and therefore the system manager has to be configured
in system property in order to find and confirm the hypothesis. T

Dňa pi 9. 10. 2020, 18:01 Enrico Olivelli <eolive...@gmail.com> napísal(a):

> Something like this:
>
> public class NoExitSecurityManager extends SecurityManager {
>
>     private final SecurityManager wrapped;
>
>     public static void setup() {
>         Policy.setPolicy(new Policy() {
>
>             final Permissions pc = new Permissions();
>
>             {
>                 pc.add(new AllPermission());
>             }
>
>             @Override
>             public boolean implies(ProtectionDomain domain, Permission
> permission) {
>                 return true;
>             }
>
>             @Override
>             public PermissionCollection getPermissions(ProtectionDomain
> domain) {
>                 return pc;
>             }
>
>             @Override
>             public PermissionCollection getPermissions(CodeSource
> codesource) {
>                 return pc;
>             }
>
>         });
>         System.setSecurityManager(new NoExitSecurityManager());
>     }
>
>     public NoExitSecurityManager() {
>         wrapped = new SecurityManager();
>     }
>
>     public NoExitSecurityManager(SecurityManager wrapped) {
>         this.wrapped = wrapped;
>     }
>
>     @Override
>     public void checkExit(int status) {
>         StackTraceElement[] stackTrace =
> Thread.currentThread().getStackTrace();
>         if (stackTrace != null) {
>             for (StackTraceElement el : stackTrace) {
>                 String className = el.getClassName();
>                 if (className != null
>                     &&
> className.startsWith("org.apache.maven.surefire.booter.ForkedBooter")
>                     && ("acknowledgedExit".equals(el.getMethodName())
>                     || "exit".equals(el.getMethodName()))) {
>                     return;
>                 }
>             }
>         }
>         throw new SecurityException("System.exit is disabled on unit
> tests");
>
>     }
>
>     @Override
>     public void checkPermission(Permission perm) {
>         if (wrapped == null) {
>             return;
>         }
>         if (perm instanceof FilePermission) {
>             // preveniamo errori su Jenkins
>             return;
>         }
>         if (perm instanceof ReflectPermission) {
>             return;
>         }
>         wrapped.checkPermission(perm);
>     }
>
> }
>
> Il giorno ven 9 ott 2020 alle ore 09:19 Mukul Gandhi <
> gandhi.mu...@gmail.com>
> ha scritto:
>
> > On Wed, Oct 7, 2020 at 9:09 PM Enrico Olivelli <eolive...@gmail.com>
> > wrote:
> >
> > > A good approach to workaround the presence of third party libraries
> that
> > > call System.exit is to set a SecurityManager that prevents calls to
> > > System.exit.
> >
> >
> > I'll find it helpful, if you may share any code sample how to do this?
> >
> >
> >
> > --
> > Regards,
> > Mukul Gandhi
> >
>

Reply via email to