1 7 package jfox.test.ejb3.env; 8 9 import javax.ejb.Stateless ; 10 import javax.ejb.Remote ; 11 import javax.ejb.Local ; 12 import javax.ejb.SessionContext ; 13 import javax.ejb.EJBException ; 14 import javax.annotation.Resource; 15 import javax.naming.Context ; 16 17 import org.jfox.ejb3.naming.JNDIContextHelper; 18 19 @Stateless (name = "env.CalculatorBean") 20 @Remote 21 @Local 22 public class CalculatorBean implements CalculatorRemote, CalculatorLocal { 23 24 @Resource 25 SessionContext sessionContext; 26 27 public void remember(int number) { 28 try { 29 Context ctx = (Context )JNDIContextHelper.getInitalContext().lookup("java:comp/env"); 30 ctx.bind("memory", number); 31 } 32 catch (Exception e) { 33 throw new EJBException (e); 34 } 35 } 36 37 public int takeout() { 38 try { 39 int n1 = (Integer )JNDIContextHelper.getInitalContext().lookup("java:comp/env/memory"); 40 int n2 = (Integer )sessionContext.lookup("memory"); 41 if (n1 != n2) { 42 throw new EJBException ("number not equals"); 43 } 44 return n1; 45 46 } 47 catch (Exception e) { 48 throw new EJBException (e); 49 } 50 } 51 52 public void clear() { 53 try { 54 Context ctx = (Context )JNDIContextHelper.getInitalContext().lookup("java:comp/env"); 55 ctx.unbind("memory"); 56 } 57 catch (Exception e) { 58 throw new EJBException (e); 59 } 60 } 61 62 } 63 | Popular Tags |