1 22 package org.jboss.test.cluster.apache_tomcat; 23 24 import java.io.IOException ; 25 import java.net.HttpURLConnection ; 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 ; 37 import javax.management.MBeanInfo ; 38 39 47 public class HttpSessionReplicationTestCase 48 extends JBossClusteredTestCase 49 { 50 private String apacheurl = null; 51 52 public HttpSessionReplicationTestCase(String name) 53 { 54 super(name); 55 67 } 68 69 public static Test suite() throws Exception 70 { 71 Test t1 = JBossClusteredTestCase.getDeploySetup(HttpSessionReplicationTestCase.class, 72 "http-sr.war"); 73 return t1; 74 } 75 76 83 public void testApacheConnection() 84 throws Exception 85 { 86 getLog().debug("Enter testApacheConnection"); 87 try 88 { 89 92 apacheurl = System.getProperty("apache.url"); 93 getLog().debug(apacheurl); 94 assertTrue("Apache Up?", this.checkURL(apacheurl)); 95 } 96 catch (Exception e) 97 { 98 getLog().debug(e.getMessage()); 99 } 100 getLog().debug("Exit testApacheConnection"); 101 } 102 103 108 public void testHttpSessionReplication() 109 throws Exception 110 { 111 String attr = ""; 112 getLog().debug("Enter testHttpSessionReplication"); 113 114 apacheurl = System.getProperty("apache.url"); 115 String urlname = apacheurl + System.getProperty("apache.set.url"); 116 String geturlname = apacheurl + System.getProperty("apache.get.url"); 117 118 getLog().debug(urlname + ":::::::" + geturlname); 119 120 HttpClient client = new HttpClient(); 122 123 HttpMethod method = new GetMethod(geturlname); 125 126 attr = makeGet(client, method); 128 129 this.shutDownTomcatInstance(1); 131 getLog().debug("Brought down the first tomcat instance"); 132 133 String [] httpURLs = super.getHttpURLs(); 134 String httpurl = httpURLs[0]; 135 String tmsg = "Is 1st Tomcat really down?Tomcat Up(" + httpurl + ")="; 136 getLog().debug(tmsg + checkURL(httpurl)); 137 138 sleepThread(30); 140 141 method = new GetMethod(geturlname); 143 String 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 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 162 private void startTomcatInstance(int instancenum) 163 throws Exception 164 { 165 String jndi = getJNDIUrl(instancenum); 166 getLog().debug("JNDI URL Obtained= " + jndi); 167 JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi); 168 ObjectName name = new ObjectName ("jboss.web:service=WebServer"); 170 MBeanInfo info = server.getMBeanInfo(name); 171 System.out.println("Tomcat MBean:" + info.getClassName()); 172 173 getLog().debug("Going to start tomcat "); 174 server.invokeOperation(name, "start", null, null); 176 this.sleepThread(10); 177 server.invokeOperation(name, "startConnectors", null, null); 178 } 179 180 186 private void shutDownTomcatInstance(int instancenum) 187 throws Exception 188 { 189 String jndi = getJNDIUrl(instancenum); 190 getLog().debug("JNDI URL Obtained= " + jndi); 191 JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi); 192 ObjectName name = new ObjectName ("jboss.web:service=WebServer"); 194 MBeanInfo info = server.getMBeanInfo(name); 195 System.out.println("Tomcat MBean:" + info.getClassName()); 196 197 getLog().debug("Going to stop tomcat "); 198 server.invokeOperation(name, "stop", null, null); 200 } 201 202 203 209 private String getJNDIUrl(int instancenum) 210 { 211 String jndi = ""; 212 try 213 { 214 int num = instancenum - 1; String key = "node" + num + ".jndi.url"; jndi = System.getProperty(key); 217 } 218 catch (Exception e) 219 { 220 fail("getJNDIUrl Failed with:" + e.getMessage()); 221 } 222 223 return jndi; 224 } 225 226 232 private void sleepThread(long secs) 233 throws Exception 234 { 235 Thread.sleep(1000 * secs); 236 } 237 238 239 249 private String 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 e) 261 { 262 log.debug(e); 263 e.printStackTrace(); 264 System.exit(-1); 265 } 266 267 byte[] responseBody = method.getResponseBody(); 269 270 method.releaseConnection(); 272 273 return new String (responseBody); 276 } 277 278 284 private boolean checkURL(String 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 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 |