1 22 package org.jboss.ejb3.test.security; 23 24 import java.security.Principal ; 25 import javax.ejb.EJBException ; 26 import javax.ejb.Remote ; 27 import javax.ejb.SessionContext ; 28 import javax.ejb.Stateless ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 32 import javax.ejb.EJB ; 33 import javax.ejb.EJBs ; 34 import javax.annotation.Resource; 35 import javax.annotation.security.RolesAllowed; 36 37 import org.jboss.logging.Logger; 38 import org.jboss.annotation.ejb.RemoteBinding; 39 import org.jboss.annotation.security.SecurityDomain; 40 import org.jboss.ejb3.Container; 41 42 47 @Stateless (name="CallerBean2") 48 @Remote (CalledSession.class) 49 @RemoteBinding(jndiBinding = "spec.Level2CallerBean") 50 @RolesAllowed({"InternalRole"}) 51 @SecurityDomain("spec-test") 52 @EJBs ({@EJB (name="Callee", beanInterface=org.jboss.ejb3.test.security.StatelessSessionLocal.class, beanName="CalleeBean")}) 53 public class CallerBean2 implements CalledSession 54 { 55 private static Logger log = Logger.getLogger(CallerBean2.class); 56 @Resource SessionContext sessionContext; 57 58 62 public String invokeEcho(String arg) 63 { 64 log.info("echo, arg=" + arg); 65 Principal p = sessionContext.getCallerPrincipal(); 66 log.info("echo, callerPrincipal=" + p); 67 boolean isEchoCaller = sessionContext.isCallerInRole("Echo"); 68 log.info("echo, isCallerInRole('Echo')=" + isEchoCaller); 69 boolean isInternalRole = sessionContext.isCallerInRole("InternalRole"); 70 log.info("echo, isCallerInRole('InternalRole')=" + isInternalRole); 71 72 if (isEchoCaller == false && isInternalRole == false) 73 throw new SecurityException ("isEchoCaller == false && isInternalRole == false"); 74 try 75 { 76 InitialContext ic = new InitialContext (); 77 StatelessSessionLocal localBean = (StatelessSessionLocal)ic.lookup("spec.CalleeBean"); } 83 catch (Exception e) 84 { 85 log.error("Failed to invoke Callee.echo", e); 86 throw new EJBException ("Failed to invoke Callee.echo", e); 87 } 88 89 isEchoCaller = sessionContext.isCallerInRole("Echo"); 90 log.info("echo, isCallerInRole#2('Echo')=" + isEchoCaller); 91 isInternalRole = sessionContext.isCallerInRole("InternalRole"); 92 log.info("echo, isCallerInRole#2('InternalRole')=" + isInternalRole); 93 94 if (isEchoCaller == false && isInternalRole == false) 95 throw new SecurityException ("isEchoCaller == false && isInternalRole == false post calls "); 96 97 return arg; 98 } 99 100 103 public String callEcho() 104 { 105 try 106 { 107 InitialContext ic = new InitialContext (); 108 109 javax.naming.NamingEnumeration list = ic.list(Container.ENC_CTX_NAME + "/env/ejb"); 110 111 CalledSession bean = (CalledSession)ic.lookup(Container.ENC_CTX_NAME + "/env/ejb/Caller"); 112 String echo = bean.invokeEcho("Level2"); 113 log.info("echo, callee.invokeEcho=" + echo); 114 115 String principal = sessionContext.getCallerPrincipal().getName(); 116 return principal; 117 } 118 catch (Exception e) 119 { 120 log.error("Failed to invoke Callee.invokeEcho", e); 121 throw new EJBException ("Failed to invoke Callee.invokeEcho", e); 122 } 123 124 } 125 126 129 public String callLocalEcho(String arg) 130 { 131 try 132 { 133 InitialContext ic = new InitialContext (); 134 Context enc = (Context ) ic.lookup("java:comp/env"); 135 CalledSessionLocal bean = (CalledSessionLocal)enc.lookup("ejb/Caller"); 136 String echo2 = bean.invokeEcho(arg + "Level1"); 137 log.info("echo, callee.invokeEcho=" + echo2); 138 return echo2; 139 } 140 catch (Exception e) 141 { 142 log.error("Failed to invoke Callee.invokeEcho", e); 143 throw new EJBException ("Failed to invoke Callee.invokeEcho", e); 144 } 145 } 146 147 public void noop() 148 { 149 log.info("noop"); 150 } 151 152 } 153 | Popular Tags |