0 Replies Latest reply: May 11, 2011 1:57 PM by opher535 RSS

    doPrivilegedWithCombiner vs doPrivileged(action, context)

    opher535
      Hi all,
      I was reading the api for AccessController and noticed that in JavaSE6 the method doPrivilegedWithCombiner was added.
      I'm wondering what is the visible difference between calling doPrivilegedWithCombiner(action) and doPrivileged(action, context) ?
              Subject.doAs(myS, new PrivilegedAction<Object>() {
                  public Object run()
                  {
                      printSubject("Inside doAs");
      
                      AccessController.doPrivileged(new PrivilegedAction<Object>() {
                          public Object run()
                          {
                              printSubject("In nested doPrivileged(action)");
                              return null;
                          }
                      });
      
                      AccessControlContext acc = AccessController.getContext();
                      AccessController.doPrivileged(new PrivilegedAction<Object>() {
                          public Object run()
                          {
                              printSubject("In nested doPrivileged(action, context)");
                              return null;
                          }
                      }, acc);
      
                      AccessController.doPrivilegedWithCombiner(new PrivilegedAction<Object>() {
                          public Object run()
                          {
                              printSubject("In nested doPrivilegedWithCombiner");
                              return null;
                          }
                      });
      
                      return null;
                  }
              });
      
          private static void printSubject(String prompt) {
              Subject s = Subject.getSubject(AccessController.getContext());
              System.out.print(prompt + " Subject is: ");
              System.out.println(s);
          }
      Running the previous code produces output similar to:
      Inside doAs Subject is: Subject:
           Principal: Opher@HOME
      
      In nested doPrivileged(action) Subject is: null
      In nested doPrivileged(action, context) Subject is: Subject:
           Principal: Opher@HOME
      
      In nested doPrivilegedWithCombiner Subject is: Subject:
           Principal: Opher@HOME