1 22 package org.jboss.test.security.ejb; 23 24 import java.rmi.RemoteException ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import javax.ejb.CreateException ; 28 import javax.ejb.SessionBean ; 29 import javax.ejb.SessionContext ; 30 import javax.naming.InitialContext ; 31 32 import org.jboss.logging.Logger; 33 import org.jboss.test.security.interfaces.SecurityContext; 34 import org.jboss.test.security.interfaces.SecurityContextHome; 35 36 40 public class SecurityContextBean implements SessionBean 41 { 42 static Logger log = Logger.getLogger(SecurityContextBean.class); 43 44 private SessionContext sessionContext; 45 46 public void ejbCreate() throws CreateException 47 { 48 log.debug("ejbCreate() called"); 49 } 50 51 public void ejbActivate() 52 { 53 log.debug("ejbActivate() called"); 54 } 55 56 public void ejbPassivate() 57 { 58 log.debug("ejbPassivate() called"); 59 } 60 61 public void ejbRemove() 62 { 63 log.debug("ejbRemove() called"); 64 } 65 66 public void setSessionContext(SessionContext context) 67 { 68 sessionContext = context; 69 } 70 71 public void testDomainInteraction(Set expectedRoles) 72 { 73 validateRoles(expectedRoles, true); 75 try 77 { 78 InitialContext ctx = new InitialContext (); 79 SecurityContextHome home = (SecurityContextHome)ctx.lookup("java:comp/env/ejb/CalledBean"); 80 SecurityContext bean = home.create(); 81 SecurityContext thisBean = (SecurityContext) sessionContext.getEJBObject(); 82 bean.nestedInteraction(thisBean, expectedRoles); 83 } 84 catch(Exception e) 85 { 86 SecurityException se = new SecurityException ("DataSource connection failed"); 87 se.initCause(e); 88 throw se; 89 } 90 validateRoles(expectedRoles, true); 92 } 93 94 public void nestedInteraction(SecurityContext caller, Set expectedRoles) 95 throws RemoteException 96 { 97 validateRoles(expectedRoles, false); 98 } 99 100 109 private void validateRoles(Set expectedRoles, boolean isCallerInRoleFlag) 110 throws SecurityException 111 { 112 Iterator names = expectedRoles.iterator(); 113 while( names.hasNext() ) 114 { 115 String name = (String ) names.next(); 116 boolean hasRole = sessionContext.isCallerInRole(name); 117 if( hasRole != isCallerInRoleFlag ) 118 throw new SecurityException ("Caller does not have role: "+name); 119 } 120 } 121 } 122 | Popular Tags |