1 22 package org.jboss.ejb3.test.dd.web.ejb; 23 24 import java.security.Principal ; 25 import java.util.StringTokenizer ; 26 import javax.ejb.CreateException ; 27 import javax.ejb.SessionContext ; 28 import javax.ejb.EJBException ; 29 import javax.naming.InitialContext ; 30 import javax.naming.Context ; 31 import javax.naming.NamingException ; 32 33 import org.jboss.logging.Logger; 34 35 40 public class RunAsTargetBean 41 { 42 static Logger log = Logger.getLogger(RunAsTargetBean.class); 43 44 private SessionContext sessionContext; 45 46 public void setSessionContext(SessionContext context) 47 { 48 sessionContext = context; 49 } 50 51 54 public void checkRunAs() 55 { 56 Principal caller = sessionContext.getCallerPrincipal(); 57 String callerName = caller.getName(); 58 log.debug("checkRunAs, caller="+caller); 59 try 60 { 61 InitialContext ctx = new InitialContext (); 63 Context enc = (Context ) ctx.lookup("java:comp/env"); 64 String name = (String ) enc.lookup("runAsName"); 65 if( name.equals(callerName) == false ) 66 throw new EJBException ("runAsName mismatch, "+name+"!="+callerName); 67 String roles = (String ) enc.lookup("runAsRoles"); 69 StringTokenizer st = new StringTokenizer (roles, ","); 70 while( st.hasMoreTokens() ) 71 { 72 String role = st.nextToken(); 73 boolean inRole = sessionContext.isCallerInRole(role); 74 String msg = "isCallerInRole("+role+"): "+inRole; 75 log.debug(msg); 76 if( inRole == false ) 77 throw new EJBException ("Failed check: "+msg); 78 } 79 } 80 catch(NamingException e) 81 { 82 throw new EJBException ("Failed to access enc", e); 83 } 84 } 85 86 } 87 | Popular Tags |