# RoleCheckerBean.ejb Sample

This topic inludes the source code for the RoleCheckerBean.ejb Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/EJBs/security/

## Sample Source Code

`01 package security; 02 03 import javax.ejb.*; 04 import javax.naming.InitialContext; 05 import javax.naming.NamingException; 06 import weblogic.ejb.*; 07 08 /** 09 * @ejbgen:session run-as-identity-principal="weblogic" default-transaction="Supports" use-caller-identity="false" run-as="engineer" 10 * ejb-name = "RoleChecker" 11 * 12 * @ejbgen:jndi-name local="ejb.RoleCheckerLocalHome" 13 * @ejbgen:file-generation remote-class="false" remote-class-name = "RoleChecker" remote-home="false" remote-home-name = "RoleCheckerHome" local-class="true" local-class-name = "RoleCheckerLocal" local-home="true" local-home-name = "RoleCheckerLocalHome" 14 * 15 * @ejbgen:ejb-local-ref link="Invoked" 16 * 17 * @ejbgen:security-role-ref role-link="manager" role-name="manager" 18 * @ejbgen:security-role-ref role-link="engineer" role-name="engineer" 19 * @ejbgen:role-mapping global-role="true" role-name="manager" 20 * @ejbgen:role-mapping principals="weblogic, engineer" role-name="engineer" 21 * @ejbgen:role-mapping principals="ceo" role-name="ceo" 22 */ 23 public class RoleCheckerBean 24 extends GenericSessionBean 25 implements SessionBean 26 { 27 private javax.ejb.SessionContext context; 28 private InvokedLocalHome invokedHome; 29 30 public void ejbCreate() { 31 try { 32 javax.naming.Context ic = new InitialContext(); 33 invokedHome = (InvokedLocalHome)ic.lookup("java:comp/env/ejb/Invoked"); 34 } 35 catch (NamingException ne) { 36 throw new EJBException(ne); 37 } 38 } 39 40 public void setSessionContext(SessionContext ctx) { 41 context = ctx; 42 } 43 44 /** 45 * @ejbgen:local-method 46 */ 47 public String discoverCallerRole() 48 { 49 50 if(context.isCallerInRole("manager")) { 51 return "You are a manager"; 52 } 53 else if(context.isCallerInRole("engineer")) { 54 return "You are an engineer"; 55 } 56 else { 57 return "I cannot determine your security role"; 58 } 59 } 60 61 /** 62 * @ejbgen:local-method roles="ceo" 63 */ 64 public String ceoOnly() 65 { 66 return "You are a CEO"; 67 } 68 69 /** 70 * @ejbgen:local-method roles="manager" 71 */ 72 public String managerOnly() 73 { 74 return "You are a manager"; 75 } 76 77 /** 78 * @ejbgen:local-method 79 */ 80 public String discoverRoleCheckersRole() 81 { 82 InvokedLocal invokedEJB; 83 84 try { 85 invokedEJB = invokedHome.create(); 86 return invokedEJB.discoverRole(); 87 } 88 catch(CreateException ce) { 89 return "invokedEJB instance could not be created"; 90 } 91 92 93 } 94 } 95`