KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > apache_tomcat > HttpSessionReplicationTestCase


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.cluster.apache_tomcat;
23
24 import java.io.IOException JavaDoc;
25 import java.net.HttpURLConnection JavaDoc;
26
27 import junit.framework.Test;
28
29 import org.apache.commons.httpclient.HttpClient;
30 import org.apache.commons.httpclient.HttpMethod;
31 import org.apache.commons.httpclient.HttpRecoverableException;
32 import org.apache.commons.httpclient.methods.GetMethod;
33 import org.jboss.test.JBossClusteredTestCase;
34 import org.jboss.test.JBossRMIAdaptorHelper;
35
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.MBeanInfo JavaDoc;
38
39 /**
40  * This testcase is written to test HttpSessionReplication with one apache webserver
41  * loadbalancing multiple tomcat/JBoss instances.
42  *
43  * @author <a HREF="mailto:anil.saldhana@jboss.com">Anil Saldhana</a>.
44  * @version $Revision: 1.0
45  * @see org.jboss.test.cluster.apache_tomcat.HttpSessionReplicationTestCase
46  */

47 public class HttpSessionReplicationTestCase
48    extends JBossClusteredTestCase
49 {
50    private String JavaDoc apacheurl = null;
51
52    public HttpSessionReplicationTestCase(String JavaDoc name)
53    {
54       super(name);
55 /* try{
56              this.getPropertiesFile();
57              String numin = prop.getProperty("NumOfInstances");
58              numInstances = Integer.parseInt( numin );
59              if( numInstances < 2 ) fail( "Atleast two nodes needed");
60              this.getLog().debug("Number of nodes="+numInstances);
61
62              //Lets build up the jndi server urls now
63              //this.setServerNames(servernames);
64             }catch( Exception e){
65                 fail( e.getMessage());
66             } */

67    }
68
69    public static Test suite() throws Exception JavaDoc
70    {
71       Test t1 = JBossClusteredTestCase.getDeploySetup(HttpSessionReplicationTestCase.class,
72          "http-sr.war");
73       return t1;
74    }
75
76    /**
77     * Tests connection to the Apache Server.
78     * Note: We deal with just one Apache Server. We can bounce the different
79     * JBoss/Tomcat servers and Apache will loadbalance.
80     *
81     * @throws Exception
82     */

83    public void testApacheConnection()
84       throws Exception JavaDoc
85    {
86       getLog().debug("Enter testApacheConnection");
87       try
88       {
89          /*String apacheurl = prop.getProperty("ApacheUrl");
90        getLog().debug(apacheurl);
91 assertTrue("Apache Up?", this.checkURL(apacheurl));*/

92          apacheurl = System.getProperty("apache.url");
93          getLog().debug(apacheurl);
94          assertTrue("Apache Up?", this.checkURL(apacheurl));
95       }
96       catch (Exception JavaDoc e)
97       {
98          getLog().debug(e.getMessage());
99       }
100       getLog().debug("Exit testApacheConnection");
101    }
102
103    /**
104     * Main method that deals with the Http Session Replication Test
105     *
106     * @throws Exception
107     */

108    public void testHttpSessionReplication()
109       throws Exception JavaDoc
110    {
111       String JavaDoc attr = "";
112       getLog().debug("Enter testHttpSessionReplication");
113
114       apacheurl = System.getProperty("apache.url");
115       String JavaDoc urlname = apacheurl + System.getProperty("apache.set.url");
116       String JavaDoc geturlname = apacheurl + System.getProperty("apache.get.url");
117
118       getLog().debug(urlname + ":::::::" + geturlname);
119
120 // Create an instance of HttpClient.
121
HttpClient client = new HttpClient();
122
123 // Create a method instance.
124
HttpMethod method = new GetMethod(geturlname);
125
126 // Get the Attribute set by testsessionreplication.jsp
127
attr = makeGet(client, method);
128
129 // Shut down the first tomcat instance
130
this.shutDownTomcatInstance(1);
131       getLog().debug("Brought down the first tomcat instance");
132
133       String JavaDoc[] httpURLs = super.getHttpURLs();
134       String JavaDoc httpurl = httpURLs[0];
135       String JavaDoc tmsg = "Is 1st Tomcat really down?Tomcat Up(" + httpurl + ")=";
136       getLog().debug(tmsg + checkURL(httpurl));
137
138 //Give 30 seconds for things to stabilize.
139
sleepThread(30);
140
141 // Make connection
142
method = new GetMethod(geturlname);
143       String JavaDoc attr2 = makeGet(client, method);
144       this.sleepThread(10);
145       getLog().debug("Will Start the Tomcat MBean back");
146       this.startTomcatInstance(1);
147       this.sleepThread(10);
148       getLog().debug("Tomcat Up=" + checkURL(httpurl));
149       String JavaDoc tstr = "attr1=" + attr + " and attr2=" + attr2;
150       if (!attr2.equals(attr)) fail("Http Session Replication failed with " + tstr);
151       getLog().debug("Http Session Replication has happened");
152       getLog().debug("Exit testHttpSessionReplication");
153    }
154
155
156    /**
157     * Starts the Tomcat MBean running on a particular node
158     *
159     * @param instancenum Instance Number of the node
160     * @throws Exception
161     */

162    private void startTomcatInstance(int instancenum)
163       throws Exception JavaDoc
164    {
165       String JavaDoc jndi = getJNDIUrl(instancenum);
166       getLog().debug("JNDI URL Obtained= " + jndi);
167       JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi);
168       //Get the MBeanInfo for the Tomcat MBean
169
ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.web:service=WebServer");
170       MBeanInfo JavaDoc info = server.getMBeanInfo(name);
171       System.out.println("Tomcat MBean:" + info.getClassName());
172
173       getLog().debug("Going to start tomcat ");
174       //Going to stop the Tomcat Instance
175
server.invokeOperation(name, "start", null, null);
176       this.sleepThread(10);
177       server.invokeOperation(name, "startConnectors", null, null);
178    }
179
180    /**
181     * Shuts down the Tomcat MBean running on a particular node
182     *
183     * @param instancenum Instance Number of the node
184     * @throws Exception
185     */

186    private void shutDownTomcatInstance(int instancenum)
187       throws Exception JavaDoc
188    {
189       String JavaDoc jndi = getJNDIUrl(instancenum);
190       getLog().debug("JNDI URL Obtained= " + jndi);
191       JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi);
192       //Get the MBeanInfo for the Tomcat MBean
193
ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.web:service=WebServer");
194       MBeanInfo JavaDoc info = server.getMBeanInfo(name);
195       System.out.println("Tomcat MBean:" + info.getClassName());
196
197       getLog().debug("Going to stop tomcat ");
198       //Going to stop the Tomcat Instance
199
server.invokeOperation(name, "stop", null, null);
200    }
201
202
203    /**
204     * Generate the JNDI Url for the JBoss Instance with instance number
205     *
206     * @param instancenum
207     * @return
208     */

209    private String JavaDoc getJNDIUrl(int instancenum)
210    {
211       String JavaDoc jndi = "";
212       try
213       {
214          int num = instancenum - 1; //node0,node1 etc
215
String JavaDoc key = "node" + num + ".jndi.url"; // node0.jndiurl
216
jndi = System.getProperty(key);
217       }
218       catch (Exception JavaDoc e)
219       {
220          fail("getJNDIUrl Failed with:" + e.getMessage());
221       }
222
223       return jndi;
224    }
225
226    /**
227     * Sleep for specified time
228     *
229     * @param secs
230     * @throws Exception
231     */

232    private void sleepThread(long secs)
233       throws Exception JavaDoc
234    {
235       Thread.sleep(1000 * secs);
236    }
237
238
239    /**
240     * Makes a http call to the jsp that retrieves the attribute stored on the
241     * session. When the attribute values mathes with the one retrieved earlier,
242     * we have HttpSessionReplication.
243     * Makes use of commons-httpclient library of Apache
244     *
245     * @param client
246     * @param method
247     * @return session attribute
248     */

249    private String JavaDoc makeGet(HttpClient client, HttpMethod method)
250    {
251       try
252       {
253          client.executeMethod(method);
254       }
255       catch (HttpRecoverableException e)
256       {
257          log.debug("A recoverable exception occurred, retrying." +
258             e.getMessage());
259       }
260       catch (IOException JavaDoc e)
261       {
262          log.debug(e);
263          e.printStackTrace();
264          System.exit(-1);
265       }
266
267       // Read the response body.
268
byte[] responseBody = method.getResponseBody();
269
270       // Release the connection.
271
method.releaseConnection();
272
273       // Deal with the response.
274
// Use caution: ensure correct character encoding and is not binary data
275
return new String JavaDoc(responseBody);
276    }
277
278    /**
279     * Checks whether the url is valid or not
280     *
281     * @param url The URL which should be checked
282     * @return whether the url is up or not
283     */

284    private boolean checkURL(String JavaDoc url)
285    {
286       boolean ok = false;
287       if (url != null) url = url.trim();
288       try
289       {
290          HttpClient httpConn = new HttpClient();
291          GetMethod g = new GetMethod(url);
292          int responseCode = httpConn.executeMethod(g);
293          log.debug("Response Code for " + url + " is=" + responseCode);
294          ok = responseCode == HttpURLConnection.HTTP_OK;
295       }
296       catch (Exception JavaDoc e)
297       {
298          log.debug("Exception for checking url=" + url);
299          log.debug(e);
300          ok = false;
301       }
302       return ok;
303    }
304
305 }
306
Popular Tags