1 22 package org.jboss.test.jca.ejb; 23 24 import org.jboss.logging.Logger; 25 import org.jboss.mx.util.MBeanServerLocator; 26 import org.jboss.test.jca.interfaces.HAConnectionSession; 27 import org.jboss.test.jca.adapter.MockedXADataSource; 28 import org.jboss.system.ServiceMBean; 29 30 import javax.ejb.SessionBean ; 31 import javax.ejb.SessionContext ; 32 import javax.ejb.EJBException ; 33 import javax.ejb.CreateException ; 34 import javax.sql.DataSource ; 35 import javax.naming.InitialContext ; 36 import javax.management.MBeanServer ; 37 import javax.management.ObjectName ; 38 import javax.management.InstanceNotFoundException ; 39 import javax.management.MBeanException ; 40 import javax.management.ReflectionException ; 41 import javax.management.AttributeNotFoundException ; 42 import java.rmi.RemoteException ; 43 import java.sql.Connection ; 44 45 54 public class HAConnectionSessionBean 55 implements SessionBean 56 { 57 58 private static final long serialVersionUID = 1L; 59 60 private static final Logger log = Logger.getLogger(HAConnectionSessionBean.class); 61 62 private SessionContext ctx; 63 64 68 public void testHaLocalConnection() throws Exception 69 { 70 String port1 = "1702"; 73 String port2 = "1703"; 74 ObjectName db1 = new ObjectName ("jboss:service=Hypersonic,database=haLocalDB1"); 75 ObjectName db2 = new ObjectName ("jboss:service=Hypersonic,database=haLocalDB2"); 76 77 MBeanServer server = MBeanServerLocator.locateJBoss(); 78 79 waitForState(server, db1, ServiceMBean.STARTED); 80 waitForState(server, db2, ServiceMBean.STARTED); 81 82 DataSource ds = (DataSource)new InitialContext ().lookup("java:/TestHADefaultDS"); 83 84 stopDb(server, db2); 85 String expectedPort = port1; 86 87 for(int i = 0; i < 10; ++i) 88 { 89 HAConnectionSession me = (HAConnectionSession)ctx.getEJBObject(); 90 String conUrl = me.getHAConnectionURL(ds); 91 log.debug("got connection to: " + conUrl); 92 93 if(!conUrl.endsWith(expectedPort)) 94 { 95 throw new Exception ("Expected " + expectedPort + " but got " + conUrl); 96 } 97 98 if(conUrl.endsWith(port1)) 99 { 100 stopDb(server, db1); 101 startDb(server, db2); 102 expectedPort = port2; 103 } 104 else if(conUrl.endsWith(port2)) 105 { 106 stopDb(server, db2); 107 startDb(server, db1); 108 expectedPort = port1; 109 } 110 else 111 { 112 throw new Exception ("Unexpected connection url: " + conUrl); 113 } 114 } 115 } 116 117 121 public void testHaXaConnection() throws Exception 122 { 123 String [] urls = MockedXADataSource.getUrls(); 124 for(int i = 1; i < urls.length; ++i) 125 { 126 MockedXADataSource.stop(urls[i]); 127 } 128 129 DataSource ds = (DataSource)new InitialContext ().lookup("java:/MockedHaXaDS"); 130 HAConnectionSession facade = (HAConnectionSession)ctx.getEJBObject(); 131 132 for(int i = 0; i < 3*urls.length; ++i) 133 { 134 String url = facade.getHAConnectionURL(ds); 135 int urlIndex = i % urls.length; 136 137 if(!url.equals(urls[urlIndex])) 138 { 139 throw new IllegalStateException ("Connected to a wrong database: " + url + ", expected " + urls[urlIndex]); 140 } 141 142 MockedXADataSource.stop(url); 143 144 urlIndex = (i + 1) % urls.length; 145 MockedXADataSource.start(urls[urlIndex]); 146 } 147 } 148 149 153 public String getHAConnectionURL(DataSource ds) throws Exception 154 { 155 Connection con = null; 156 try 157 { 158 con = ds.getConnection(); 159 return con.getMetaData().getURL(); 160 } 161 finally 162 { 163 if(con != null) 164 { 165 con.close(); 166 } 167 } 168 } 169 170 private void startDb(MBeanServer server, ObjectName db2) 171 throws InstanceNotFoundException , MBeanException , ReflectionException , AttributeNotFoundException 172 { 173 server.invoke(db2, "start", null, null); 174 waitForState(server, db2, ServiceMBean.STARTED); 175 } 176 177 private void stopDb(MBeanServer server, ObjectName db2) 178 throws InstanceNotFoundException , 179 MBeanException , 180 ReflectionException , 181 AttributeNotFoundException 182 { 183 server.invoke(db2, "stop", null, null); 184 waitForState(server, db2, ServiceMBean.STOPPED); 185 } 186 187 private void waitForState(MBeanServer server, ObjectName db2, int state) 188 throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , ReflectionException 189 { 190 Integer stateValue = (Integer )server.getAttribute(db2, "State"); 191 while(stateValue.intValue() != state) 192 { 193 try 194 { 195 Thread.sleep(500); 196 } 197 catch(InterruptedException e) 198 { 199 } 200 stateValue = (Integer )server.getAttribute(db2, "State"); 201 } 202 if (stateValue.intValue() == ServiceMBean.STARTED) 206 { 207 try 208 { 209 Thread.sleep(5000); 210 } 211 catch (InterruptedException e) 212 { 213 } 214 } 215 } 216 217 221 public void ejbCreate() throws CreateException 222 { 223 } 224 225 public void setSessionContext(SessionContext ctx) throws EJBException , RemoteException 226 { 227 this.ctx = ctx; 228 } 229 230 public void ejbRemove() throws EJBException , RemoteException 231 { 232 } 233 234 public void ejbActivate() throws EJBException , RemoteException 235 { 236 } 237 238 public void ejbPassivate() throws EJBException , RemoteException 239 { 240 } 241 } 242 | Popular Tags |