1 22 package org.jboss.test.cmp2.jbas979; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import javax.ejb.SessionBean ; 27 import javax.ejb.SessionContext ; 28 import javax.ejb.CreateException ; 29 import javax.ejb.EJBException ; 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 import javax.naming.NamingException ; 33 import javax.naming.InitialContext ; 34 import javax.sql.DataSource ; 35 import org.jboss.mx.util.MBeanServerLocator; 36 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil; 37 38 42 public class FacadeSessionBean 43 implements SessionBean 44 { 45 47 public void create(String ejbJndiName, Integer pk, String name) throws Exception 48 { 49 ALocalHome ah = getALocalHome(ejbJndiName); 50 ah.create(pk, name); 51 } 52 53 public void remove(String ejbJndiName, Integer pk) throws Exception 54 { 55 ALocalHome ah = getALocalHome(ejbJndiName); 56 ah.remove(pk); 57 } 58 59 public String getName(String ejbJndiName, Integer pk) throws Exception 60 { 61 ALocalHome ah = getALocalHome(ejbJndiName); 62 return ah.findByPrimaryKey(pk).getName(); 63 } 64 65 public String getNameFlushCacheGetName(String ejbJndiName, Integer pk) throws Exception 66 { 67 ALocalHome ah = getALocalHome(ejbJndiName); 68 String nameBeforeFlush = ah.findByPrimaryKey(pk).getName(); 69 flushCache(ejbJndiName); 70 String nameAfterFlush = ah.findByPrimaryKey(pk).getName(); 71 if(!nameBeforeFlush.equals(nameAfterFlush)) 72 { 73 throw new EJBException ( 74 "The value of the name field before flush (" + nameBeforeFlush + 75 ") is not equal to the value after flush (" + nameAfterFlush + ")!"); 76 } 77 return nameAfterFlush; 78 } 79 80 public String getNameFlushCacheSetName(String ejbJndiName, Integer pk, String value) throws Exception 81 { 82 ALocalHome ah = getALocalHome(ejbJndiName); 83 ah.findByPrimaryKey(pk).getName(); 84 flushCache(ejbJndiName); 85 ALocal a = ah.findByPrimaryKey(pk); 86 a.setName(value); 87 String name = a.getName(); 88 if(!name.equals(value)) 89 { 90 throw new EJBException ("setName(" + value + ") was ignored: " + name); 91 } 92 return name; 93 } 94 95 public void updateDB(String tableName, Integer pk, String value) throws Exception 96 { 97 DataSource ds = (DataSource )lookup("java:/DefaultDS"); 98 Connection con = null; 99 PreparedStatement st = null; 100 try 101 { 102 con = ds.getConnection(); 103 st = con.prepareStatement("update " + tableName + " set name=? where id=?"); 104 st.setString(1, value); 105 st.setInt(2, pk.intValue()); 106 int rowsAffected = st.executeUpdate(); 107 if(rowsAffected != 1) 108 { 109 throw new EJBException ("Failed to update column name in the row with pk " + pk + 110 " in the table " + tableName + ": expected 1 updated row but got " + rowsAffected); 111 } 112 } 113 finally 114 { 115 JDBCUtil.safeClose(st); 116 JDBCUtil.safeClose(con); 117 } 118 } 119 120 public void flushCache(String ejbJndiName) throws Exception 121 { 122 MBeanServer server = MBeanServerLocator.locateJBoss(); 123 ObjectName container = new ObjectName ("jboss.j2ee:service=EJB,jndiName=" + ejbJndiName); 124 server.invoke(container, "flushCache", null, null); 125 } 126 127 public void longTx(String ejbJndiName, Integer pk, long ms) throws Exception 128 { 129 ALocalHome ah = getALocalHome(ejbJndiName); 130 ah.findByPrimaryKey(pk).longTx(); 131 try 132 { 133 Thread.sleep(ms); 134 } 135 catch(InterruptedException e) 136 { 137 } 138 } 139 140 142 146 public void ejbCreate() throws CreateException 147 { 148 } 149 150 public void ejbActivate() 151 { 152 } 153 154 public void ejbPassivate() 155 { 156 } 157 158 public void ejbRemove() 159 { 160 } 161 162 public void setSessionContext(SessionContext ctx) 163 { 164 } 165 166 168 private ALocalHome getALocalHome(String name) 169 throws NamingException 170 { 171 return (ALocalHome)lookup(name); 172 } 173 174 private Object lookup(String name) throws NamingException 175 { 176 InitialContext ic = null; 177 try 178 { 179 ic = new InitialContext (); 180 return ic.lookup(name); 181 } 182 finally 183 { 184 if(ic != null) 185 { 186 ic.close(); 187 } 188 } 189 } 190 } 191 | Popular Tags |