1 22 package org.jboss.test.cmp2.cacheinvalidation.ejb; 23 24 25 import org.jboss.logging.Logger; 26 27 import javax.ejb.SessionBean ; 28 import javax.ejb.SessionContext ; 29 import javax.ejb.CreateException ; 30 import javax.ejb.FinderException ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 34 38 public class FacadeSessionBean 39 implements SessionBean 40 { 41 private static final Logger log = Logger.getLogger(FacadeSessionBean.class); 42 SessionContext ctx; 43 44 private transient CLocalHome ch; 45 private transient ALocalHome ah; 46 47 49 53 public void setup() throws Exception 54 { 55 CLocal c = getCLocalHome("CRWLocal").create(new Long (1)); 56 c.setFirstName("Avoka"); 57 58 ALocal a = getALocalHome("ARWLocal").create(new Long (2), "Ataka"); 59 a.setC(c); 60 } 61 62 66 public void tearDown() throws Exception 67 { 68 try 69 { 70 CLocal c = getCLocalHome("CRWLocal").findByPrimaryKey(new Long (1)); 71 c.remove(); 72 } 73 catch(FinderException e) 74 { 75 } 76 77 try 78 { 79 ALocal a = getALocalHome("ARWLocal").findByPrimaryKey(new Long (2)); 80 a.remove(); 81 } 82 catch(FinderException e) 83 { 84 } 85 } 86 87 91 public String readFirstName(String jndiName, Long id) throws Exception 92 { 93 final CLocalHome ch = (CLocalHome) getHome(jndiName); 94 CLocal c = ch.findByPrimaryKey(id); 95 96 final String firstName = c.getFirstName(); 97 log.debug(jndiName + ".name=" + firstName); 98 return firstName; 99 } 100 101 105 public void writeFirstName(String jndiName, Long id, String name) throws Exception 106 { 107 final CLocalHome ch = (CLocalHome) getHome(jndiName); 108 CLocal c = ch.findByPrimaryKey(id); 109 110 c.setFirstName(name); 111 log.debug(jndiName + ".name=" + c.getFirstName()); 112 } 113 114 118 public String readRelatedAFirstName(String jndiName, Long id) throws Exception 119 { 120 final CLocalHome ch = (CLocalHome) getHome(jndiName); 121 CLocal c = ch.findByPrimaryKey(id); 122 123 final String firstName = c.getA() == null ? null : c.getA().getName(); 124 log.debug(jndiName + ".a.name=" + firstName); 125 return firstName; 126 } 127 128 132 public void removeA(String jndiName, Long id) throws Exception 133 { 134 final ALocalHome ah = (ALocalHome) getHome(jndiName); 135 ALocal a = ah.findByPrimaryKey(id); 136 a.remove(); 137 } 138 139 141 145 public void ejbCreate() throws CreateException 146 { 147 } 148 149 public void ejbActivate() 150 { 151 } 152 153 public void ejbPassivate() 154 { 155 } 156 157 public void ejbRemove() 158 { 159 } 160 161 public void setSessionContext(SessionContext ctx) 162 { 163 this.ctx = ctx; 164 } 165 166 private CLocalHome getCLocalHome(String jndiName) 167 { 168 if(ch == null) 169 { 170 ch = (CLocalHome) getHome(jndiName); 171 } 172 return ch; 173 } 174 175 private ALocalHome getALocalHome(String jndiName) 176 { 177 if(ah == null) 178 { 179 ah = (ALocalHome) getHome(jndiName); 180 } 181 return ah; 182 } 183 184 private Object getHome(String jndiName) 185 { 186 try 187 { 188 InitialContext ctx = new InitialContext (); 189 return ctx.lookup(jndiName); 190 } 191 catch(NamingException e) 192 { 193 throw new IllegalStateException ("Failed to look up home " + jndiName + ": " + e.getMessage()); 194 } 195 } 196 } 197 | Popular Tags |