1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.util.HashSet ; 4 import java.util.Set ; 5 import java.security.AccessController ; 6 import java.security.PrivilegedExceptionAction ; 7 import java.security.PrivilegedActionException ; 8 import java.io.Serializable ; 9 10 import javax.security.auth.Subject ; 11 12 import com.tirsen.nanning.Invocation; 13 import org.apache.commons.collections.CollectionUtils; 14 import org.apache.commons.collections.Predicate; 15 16 public class AuthenticatedCall extends Call { 17 static final long serialVersionUID = 8405907347881334801L; 18 19 protected Set principals; 20 protected Set privateCredentials; 21 protected Set publicCredentials; 22 23 private static final Predicate isSerializable = new Predicate() { 24 public boolean evaluate(Object o) { 25 return o instanceof Serializable ; 26 } 27 }; 28 29 public AuthenticatedCall() { 30 Subject subject = Subject.getSubject(AccessController.getContext()); 31 if (subject != null) { 32 principals = new HashSet (subject.getPrincipals()); 33 CollectionUtils.filter(principals, isSerializable); 34 privateCredentials = new HashSet (subject.getPrivateCredentials()); 35 CollectionUtils.filter(privateCredentials, isSerializable); 36 publicCredentials = new HashSet (subject.getPublicCredentials()); 37 CollectionUtils.filter(publicCredentials, isSerializable); 38 } 39 } 40 41 public AuthenticatedCall(Invocation invocation) throws Exception { 42 this(); 43 setInvocation(invocation); 44 } 45 46 public Subject getSubject() { 47 if (principals == null && publicCredentials == null && privateCredentials == null) { 48 return null; 49 } else { 50 return new Subject (false, principals == null ? new HashSet () : principals, publicCredentials == null ? new HashSet () : publicCredentials, privateCredentials == null ? new HashSet () : privateCredentials); 51 } 52 } 53 54 public Object invoke() throws Exception { 55 try { 56 return Subject.doAs(getSubject(), new PrivilegedExceptionAction () { 57 public Object run() throws Exception { 58 return AuthenticatedCall.super.invoke(); 59 } 60 }); 61 } catch (PrivilegedActionException e) { 62 throw e.getException(); 63 } 64 } 65 } 66 | Popular Tags |