KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > ejb > HAConnectionSessionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.CreateException JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.InstanceNotFoundException JavaDoc;
39 import javax.management.MBeanException JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41 import javax.management.AttributeNotFoundException JavaDoc;
42 import java.rmi.RemoteException JavaDoc;
43 import java.sql.Connection JavaDoc;
44
45 /**
46  * @ejb.bean
47  * name="HAConnectionSession"
48  * view-type="remote"
49  * type="Stateless"
50  *
51  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
52  * @version <tt>$Revision: 37406 $</tt>
53  */

54 public class HAConnectionSessionBean
55    implements SessionBean JavaDoc
56 {
57    /** The serialVersionUID */
58    private static final long serialVersionUID = 1L;
59
60    private static final Logger log = Logger.getLogger(HAConnectionSessionBean.class);
61
62    private SessionContext JavaDoc ctx;
63
64    /**
65     * @ejb.interface-method
66     * @ejb.transaction type="NotSupported"
67     */

68    public void testHaLocalConnection() throws Exception JavaDoc
69    {
70       //String url1 = "jdbc:hsqldb:hsql://localhost:1702";
71
//String url2 = "jdbc:hsqldb:hsql://localhost:1703";
72
String JavaDoc port1 = "1702";
73       String JavaDoc port2 = "1703";
74       ObjectName JavaDoc db1 = new ObjectName JavaDoc("jboss:service=Hypersonic,database=haLocalDB1");
75       ObjectName JavaDoc db2 = new ObjectName JavaDoc("jboss:service=Hypersonic,database=haLocalDB2");
76
77       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
78
79       waitForState(server, db1, ServiceMBean.STARTED);
80       waitForState(server, db2, ServiceMBean.STARTED);
81
82       DataSource ds = (DataSource)new InitialContext JavaDoc().lookup("java:/TestHADefaultDS");
83
84       stopDb(server, db2);
85       String JavaDoc expectedPort = port1;
86
87       for(int i = 0; i < 10; ++i)
88       {
89          HAConnectionSession me = (HAConnectionSession)ctx.getEJBObject();
90          String JavaDoc conUrl = me.getHAConnectionURL(ds);
91          log.debug("got connection to: " + conUrl);
92
93          if(!conUrl.endsWith(expectedPort))
94          {
95             throw new Exception JavaDoc("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 JavaDoc("Unexpected connection url: " + conUrl);
113          }
114       }
115    }
116
117    /**
118     * @ejb.interface-method
119     * @ejb.transaction type="NotSupported"
120     */

121    public void testHaXaConnection() throws Exception JavaDoc
122    {
123       String JavaDoc[] 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 JavaDoc().lookup("java:/MockedHaXaDS");
130       HAConnectionSession facade = (HAConnectionSession)ctx.getEJBObject();
131
132       for(int i = 0; i < 3*urls.length; ++i)
133       {
134          String JavaDoc url = facade.getHAConnectionURL(ds);
135          int urlIndex = i % urls.length;
136
137          if(!url.equals(urls[urlIndex]))
138          {
139             throw new IllegalStateException JavaDoc("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    /**
150     * @ejb.interface-method
151     * @ejb.transaction type="RequiresNew"
152     */

153    public String JavaDoc getHAConnectionURL(DataSource ds) throws Exception JavaDoc
154    {
155       Connection JavaDoc 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 JavaDoc server, ObjectName JavaDoc db2)
171       throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc, AttributeNotFoundException JavaDoc
172    {
173       server.invoke(db2, "start", null, null);
174       waitForState(server, db2, ServiceMBean.STARTED);
175    }
176
177    private void stopDb(MBeanServer JavaDoc server, ObjectName JavaDoc db2)
178       throws InstanceNotFoundException JavaDoc,
179       MBeanException JavaDoc,
180       ReflectionException JavaDoc,
181       AttributeNotFoundException JavaDoc
182    {
183       server.invoke(db2, "stop", null, null);
184       waitForState(server, db2, ServiceMBean.STOPPED);
185    }
186
187    private void waitForState(MBeanServer JavaDoc server, ObjectName JavaDoc db2, int state)
188       throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
189    {
190       Integer JavaDoc stateValue = (Integer JavaDoc)server.getAttribute(db2, "State");
191       while(stateValue.intValue() != state)
192       {
193          try
194          {
195             Thread.sleep(500);
196          }
197          catch(InterruptedException JavaDoc e)
198          {
199          }
200          stateValue = (Integer JavaDoc)server.getAttribute(db2, "State");
201       }
202       // The hypersonic MBean is not implementated properly
203
// It goes into state STARTED when in reality there is
204
// a background thread still recovering the server
205
if (stateValue.intValue() == ServiceMBean.STARTED)
206       {
207          try
208          {
209             Thread.sleep(5000);
210          }
211          catch (InterruptedException JavaDoc e)
212          {
213          }
214       }
215    }
216
217    /**
218     * @throws javax.ejb.CreateException Description of Exception
219     * @ejb.create-method
220     */

221    public void ejbCreate() throws CreateException JavaDoc
222    {
223    }
224
225    public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc, RemoteException JavaDoc
226    {
227       this.ctx = ctx;
228    }
229
230    public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc
231    {
232    }
233
234    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
235    {
236    }
237
238    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
239    {
240    }
241 }
242
Popular Tags