1 22 package org.jboss.test.web.ejb; 23 24 import java.security.Principal ; 25 import java.util.StringTokenizer ; 26 import javax.ejb.CreateException ; 27 import javax.ejb.SessionBean ; 28 import javax.ejb.SessionContext ; 29 import javax.ejb.EJBException ; 30 import javax.naming.InitialContext ; 31 import javax.naming.Context ; 32 import javax.naming.NamingException ; 33 34 import org.jboss.logging.Logger; 35 36 41 public class RunAsTargetBean implements SessionBean 42 { 43 static Logger log = Logger.getLogger(RunAsTargetBean.class); 44 45 private SessionContext sessionContext; 46 47 public void ejbCreate() throws CreateException 48 { 49 log.debug("ejbCreate() called"); 50 } 51 52 public void ejbActivate() 53 { 54 log.debug("ejbActivate() called"); 55 } 56 57 public void ejbPassivate() 58 { 59 log.debug("ejbPassivate() called"); 60 } 61 62 public void ejbRemove() 63 { 64 log.debug("ejbRemove() called"); 65 } 66 67 public void setSessionContext(SessionContext context) 68 { 69 sessionContext = context; 70 } 71 72 75 public void checkRunAs() 76 { 77 Principal caller = sessionContext.getCallerPrincipal(); 78 String callerName = caller.getName(); 79 log.debug("checkRunAs, caller="+caller); 80 try 81 { 82 InitialContext ctx = new InitialContext (); 84 Context enc = (Context ) ctx.lookup("java:comp/env"); 85 String name = (String ) enc.lookup("runAsName"); 86 if( name.equals(callerName) == false ) 87 throw new EJBException ("runAsName mismatch, "+name+"!="+callerName); 88 String roles = (String ) enc.lookup("runAsRoles"); 90 StringTokenizer st = new StringTokenizer (roles, ","); 91 while( st.hasMoreTokens() ) 92 { 93 String role = st.nextToken(); 94 boolean inRole = sessionContext.isCallerInRole(role); 95 String msg = "isCallerInRole("+role+"): "+inRole; 96 log.debug(msg); 97 if( inRole == false ) 98 throw new EJBException ("Failed check: "+msg); 99 } 100 } 101 catch(NamingException e) 102 { 103 throw new EJBException ("Failed to access enc", e); 104 } 105 } 106 107 } 108 | Popular Tags |