gembin

          OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

          HBase, Hadoop, ZooKeeper, Cassandra

          Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

          There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

          About Me

           

          java.security AccessController

          AccessController.doPrivileged是一個在AccessController類中的靜態方法,允許在一個類實例中的代碼 通知這個AccessController:它的代碼主體是享受"privileged(特權的)",它單獨負責對它的可得的資源的訪問請求,而不管這個 請求是由什么代碼所引發的。

          這就是說,一個調用者在調用doPrivileged方法時,可被標識為 "特權"。在做訪問控制決策時,如果checkPermission方法遇到一個通過doPrivileged調用而被表示為 "特權"的調用者,并且沒有上下文自變量,checkPermission方法則將終止檢查。如果那個調用者的域具有特定的許可,則不做進一步檢查, checkPermission安靜地返回,表示那個訪問請求是被允許的;如果那個域沒有特定的許可,則象通常一樣,一個異常被拋出。

          一、"特權"特性的正常使用如下所示:

          1、如果你不需要從"特權"塊內返回一個值,按下列代碼去做:

          somemethod() {

                ...normal code here...

                AccessController.doPrivileged(
          new PrivilegedAction() {
                      
          public Object run() {
                              // privileged code goes here, for example:
                              System.loadLibrary("awt");
                              return null// nothing to return
                      }
                });

                ...normal code here...


          PrivilegedAction是一個接口,它帶有一個被稱為run的方法,這個方法返回一個Object。上述例子顯示了一個用來實現那個接口的匿名內類的創建,并提供了一個run方法的具體實現。

          當做一個doPrivileged調用時,一個PrivilegedAction實現的實例被傳遞給它。doPrivileged方法在使特權生效 后,從PrivilegedAction實現中調用run方法,并返回run方法的返回值以作為doPrivileged的返回值,這一點在本例中被忽 略。

          2、如果你需要返回一個值,你可按如下方法去做:

          somemethod() {

                  ...normal code here...

                  String user 
          = (String) AccessController.doPrivileged(new PrivilegedAction() {
                            public Object run() {
                                   return System.getProperty("user.name");
                           }
                  });

                  ...normal code here...

          }


          3、如果用你的run方法執行的動作可能扔出一個"檢查"的異常(包括在一個方法的throws子句列表中),則你需要使用PrivilegedExceptionAction接口,而不是使用PrivilegedAction接口:

          somemethod() throws FileNotFoundException {

                  ...normal code here...

                  try {
                         FileInputStream fis 
          = (FileInputStream)
                         AccessController.doPrivileged(
          new PrivilegedExceptionAction() {
                               public Object run() throws FileNotFoundException {
                                      return new FileInputStream("someFile");
                               }
                         });
                  } 
          catch (PrivilegedActionException e) {
                           // e.getException() should be an instance of
                           // FileNotFoundException,
                           // as only "checked" exceptions will be "wrapped" in a
                           // PrivilegedActionException.
                          throw (FileNotFoundException) e.getException();
                  }

                 ...normal code here...

          }

          有關被授予特權的一些重要事項:
          首先,這個概念僅存在于一個單獨線程內。一旦特權代碼完成了任務,特權將被保證清除或作廢。

          第二,在這個例子中,在run方法中的代碼體被授予了特權。然而,如果它調用無特權的不可信代碼,則那個代碼將不會獲得任何特權;只有在特權代碼具有許可并且在直到checkPermission調用的調用鏈中的所有隨后的調用者也具有許可時, 一個許可才能被準予。

          二、使用事例:

          final String name = MyClass.class.getName();
          String className 
          = AccessController.doPrivileged(new PrivilegedAction<String>() {
                          
          public String run() {
                              
          return System.getProperty(name);
                          }
          });

           

          posted on 2008-03-12 18:47 gembin 閱讀(1003) 評論(0)  編輯  收藏 所屬分類: JavaSE

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(440)

          隨筆檔案(378)

          文章檔案(6)

          新聞檔案(1)

          相冊

          收藏夾(9)

          Adobe

          Android

          AS3

          Blog-Links

          Build

          Design Pattern

          Eclipse

          Favorite Links

          Flickr

          Game Dev

          HBase

          Identity Management

          IT resources

          JEE

          Language

          OpenID

          OSGi

          SOA

          Version Control

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          free counters
          主站蜘蛛池模板: 赤城县| 黔东| 阳泉市| 江西省| 长海县| 攀枝花市| 蒙山县| 依兰县| 于田县| 江西省| 邵阳县| 日喀则市| 峨边| 肥乡县| 潜山县| 亚东县| 万荣县| 凤城市| 呼玛县| 湄潭县| 淮滨县| 新密市| 富裕县| 阿克陶县| 家居| 甘德县| 许昌市| 绥棱县| 道孚县| 宜兴市| 甘孜县| 永兴县| 平定县| 泸西县| 泰宁县| 五指山市| 汉中市| 兴安盟| 大足县| 金平| 唐海县|