1 22 package org.jboss.test.security.ejb; 23 24 import java.security.Principal ; 25 import javax.ejb.CreateException ; 26 import javax.ejb.EJBException ; 27 import javax.ejb.SessionBean ; 28 import javax.ejb.SessionContext ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.rmi.PortableRemoteObject ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.test.security.interfaces.StatelessSessionLocal; 35 import org.jboss.test.security.interfaces.StatelessSessionLocalHome; 36 import org.jboss.test.security.interfaces.CalledSessionHome; 37 import org.jboss.test.security.interfaces.CalledSession; 38 import org.jboss.test.security.interfaces.CalledSessionLocalHome; 39 import org.jboss.test.security.interfaces.CalledSessionLocal; 40 41 46 public class CallerBean implements SessionBean 47 { 48 private static Logger log = Logger.getLogger(CallerBean.class); 49 private SessionContext sessionContext; 50 51 public void ejbCreate() throws CreateException 52 { 53 log.debug("ejbCreate() called"); 54 } 55 56 public void ejbActivate() 57 { 58 log.debug("ejbActivate() called"); 59 } 60 61 public void ejbPassivate() 62 { 63 log.debug("ejbPassivate() called"); 64 } 65 66 public void ejbRemove() 67 { 68 log.debug("ejbRemove() called"); 69 } 70 71 public void setSessionContext(SessionContext context) 72 { 73 sessionContext = context; 74 } 75 76 80 public String invokeEcho(String arg) 81 { 82 log.debug("echo, arg=" + arg); 83 Principal p = sessionContext.getCallerPrincipal(); 84 log.debug("echo, callerPrincipal=" + p); 85 boolean isEchoCaller = sessionContext.isCallerInRole("EchoCaller"); 86 log.debug("echo, isCallerInRole('EchoCaller')=" + isEchoCaller); 87 boolean isInternalRole = sessionContext.isCallerInRole("InternalRole"); 88 log.debug("echo, isCallerInRole('InternalRole')=" + isInternalRole); 89 90 if (isEchoCaller == false && isInternalRole == false) 91 throw new SecurityException ("isEchoCaller == false && isInternalRole == false"); 92 try 93 { 94 InitialContext ic = new InitialContext (); 95 Context enc = (Context ) ic.lookup("java:comp/env"); 96 Object ref = enc.lookup("ejb/local/CalleeHome"); 97 StatelessSessionLocalHome localHome = (StatelessSessionLocalHome) PortableRemoteObject.narrow(ref, 98 StatelessSessionLocalHome.class); 99 StatelessSessionLocal localBean = localHome.create(); 100 String echo2 = localBean.echo(arg); 101 log.debug("echo#1, callee.echo=" + echo2); 102 echo2 = localBean.echo(arg); 103 log.debug("echo#2, callee.echo=" + echo2); 104 } 105 catch (Exception e) 106 { 107 log.error("Failed to invoke Callee.echo", e); 108 throw new EJBException ("Failed to invoke Callee.echo", e); 109 } 110 111 isEchoCaller = sessionContext.isCallerInRole("EchoCaller"); 112 log.debug("echo, isCallerInRole#2('EchoCaller')=" + isEchoCaller); 113 isInternalRole = sessionContext.isCallerInRole("InternalRole"); 114 log.debug("echo, isCallerInRole#2('InternalRole')=" + isInternalRole); 115 116 if (isEchoCaller == false && isInternalRole == false) 117 throw new SecurityException ("isEchoCaller == false && isInternalRole == false post calls"); 118 119 return arg; 120 } 121 122 125 public void callEcho() 126 { 127 try 128 { 129 InitialContext ic = new InitialContext (); 130 Context enc = (Context ) ic.lookup("java:comp/env"); 131 Object ref = enc.lookup("ejb/CallerHome"); 132 CalledSessionHome home = (CalledSessionHome) PortableRemoteObject.narrow(ref, 133 CalledSessionHome.class); 134 CalledSession bean = home.create(); 135 String echo = bean.invokeEcho("Level1"); 136 log.debug("echo, callee.invokeEcho=" + echo); 137 } 138 catch (Exception e) 139 { 140 log.error("Failed to invoke Callee.invokeEcho", e); 141 throw new EJBException ("Failed to invoke Callee.invokeEcho", e); 142 } 143 144 } 145 146 149 public String callLocalEcho(String arg) 150 { 151 try 152 { 153 InitialContext ic = new InitialContext (); 154 Context enc = (Context ) ic.lookup("java:comp/env"); 155 Object ref = enc.lookup("ejb/CallerHome"); 156 CalledSessionLocalHome home = (CalledSessionLocalHome) PortableRemoteObject.narrow(ref, 157 CalledSessionLocalHome.class); 158 CalledSessionLocal bean = home.create(); 159 String echo2 = bean.invokeEcho(arg + "Level1"); 160 log.debug("echo, callee.invokeEcho=" + echo2); 161 return echo2; 162 } 163 catch (Exception e) 164 { 165 log.error("Failed to invoke Callee.invokeEcho", e); 166 throw new EJBException ("Failed to invoke Callee.invokeEcho", e); 167 } 168 } 169 170 public void noop() 171 { 172 log.debug("noop"); 173 } 174 175 } 176 | Popular Tags |