日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

防止System.exit调用

發布時間:2023/12/3 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 防止System.exit调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在開發運行其他開發人員編寫的代碼的容器時,請謹慎防范System.exit調用。 如果開發人員無意間調用了System.exit并將其代碼部署為由您的容器運行,則它將完全降低容器進程。 可以使用SecurityManager中的checkExit函數調用來控制。

根據SecurityManager checkExit的參考 :

當前的安全管理器通過Runtime類的exit方法調用此方法。 狀態0表示成功;狀態0表示成功。 其他值表示各種錯誤。

因此,任何對exit的調用都將調用此方法,并且如果我們不希望繼續進行處理,則只需拋出異常。 我們將SecurityManager定義如下:

public class StopExitSecurityManager extends SecurityManager{private SecurityManager _prevMgr = System.getSecurityManager();public void checkPermission(Permission perm){}public void checkExit(int status){super.checkExit(status);throw new ExitTrappedException(); //This throws an exception if an exit is called.}public SecurityManager getPreviousMgr() { return _prevMgr; }}

現在,我們可以提供一個易于使用的CodeControl類,如下所示:

public class CodeControl {public CodeControl(){super();}public void disableSystemExit(){SecurityManager securityManager = new StopExitSecurityManager();System.setSecurityManager(securityManager) ;} public void enableSystemExit(){SecurityManager mgr = System.getSecurityManager();if ((mgr != null) && (mgr instanceof StopExitSecurityManager)){StopExitSecurityManager smgr = (StopExitSecurityManager)mgr;System.setSecurityManager(smgr.getPreviousMgr());}elseSystem.setSecurityManager(null);} }

現在可以按以下方式使用CodeControl:

CodeControl control = new CodeControl(); try {control.disableSystemExit();//invoke the methods and other classes that are not allowed to call System.exit.Object ret = invokeExecute(_method, runWith, parms); } finally {//finally enable exitcontrol.enableSystemExit(); }

這樣可以防止在disable中調用方法,并允許調用System.exit,但允許您的代碼毫無問題地調用它。

參考: 防止我們的JCG合作伙伴 Raji Sankar 致電System.exit,該郵件來自Reflections博客。

翻譯自: https://www.javacodegeeks.com/2013/11/preventing-system-exit-calls.html

總結

以上是生活随笔為你收集整理的防止System.exit调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。