001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.apache_tomcat;
023:
024: import java.io.IOException;
025: import java.net.HttpURLConnection;
026:
027: import junit.framework.Test;
028:
029: import org.apache.commons.httpclient.HttpClient;
030: import org.apache.commons.httpclient.HttpMethod;
031: import org.apache.commons.httpclient.HttpRecoverableException;
032: import org.apache.commons.httpclient.methods.GetMethod;
033: import org.jboss.test.JBossClusteredTestCase;
034: import org.jboss.test.JBossRMIAdaptorHelper;
035:
036: import javax.management.ObjectName;
037: import javax.management.MBeanInfo;
038:
039: /**
040: * This testcase is written to test HttpSessionReplication with one apache webserver
041: * loadbalancing multiple tomcat/JBoss instances.
042: *
043: * @author <a href="mailto:anil.saldhana@jboss.com">Anil Saldhana</a>.
044: * @version $Revision: 1.0
045: * @see org.jboss.test.cluster.apache_tomcat.HttpSessionReplicationTestCase
046: */
047: public class HttpSessionReplicationTestCase extends
048: JBossClusteredTestCase {
049: private String apacheurl = null;
050:
051: public HttpSessionReplicationTestCase(String name) {
052: super (name);
053: /* try{
054: this.getPropertiesFile();
055: String numin = prop.getProperty("NumOfInstances");
056: numInstances = Integer.parseInt( numin );
057: if( numInstances < 2 ) fail( "Atleast two nodes needed");
058: this.getLog().debug("Number of nodes="+numInstances);
059:
060: //Lets build up the jndi server urls now
061: //this.setServerNames(servernames);
062: }catch( Exception e){
063: fail( e.getMessage());
064: } */
065: }
066:
067: public static Test suite() throws Exception {
068: Test t1 = JBossClusteredTestCase.getDeploySetup(
069: HttpSessionReplicationTestCase.class, "http-sr.war");
070: return t1;
071: }
072:
073: /**
074: * Tests connection to the Apache Server.
075: * Note: We deal with just one Apache Server. We can bounce the different
076: * JBoss/Tomcat servers and Apache will loadbalance.
077: *
078: * @throws Exception
079: */
080: public void testApacheConnection() throws Exception {
081: getLog().debug("Enter testApacheConnection");
082: try {
083: /*String apacheurl = prop.getProperty("ApacheUrl");
084: getLog().debug(apacheurl);
085: assertTrue("Apache Up?", this.checkURL(apacheurl));*/
086: apacheurl = System.getProperty("apache.url");
087: getLog().debug(apacheurl);
088: assertTrue("Apache Up?", this .checkURL(apacheurl));
089: } catch (Exception e) {
090: getLog().debug(e.getMessage());
091: }
092: getLog().debug("Exit testApacheConnection");
093: }
094:
095: /**
096: * Main method that deals with the Http Session Replication Test
097: *
098: * @throws Exception
099: */
100: public void testHttpSessionReplication() throws Exception {
101: String attr = "";
102: getLog().debug("Enter testHttpSessionReplication");
103:
104: apacheurl = System.getProperty("apache.url");
105: String urlname = apacheurl
106: + System.getProperty("apache.set.url");
107: String geturlname = apacheurl
108: + System.getProperty("apache.get.url");
109:
110: getLog().debug(urlname + ":::::::" + geturlname);
111:
112: // Create an instance of HttpClient.
113: HttpClient client = new HttpClient();
114:
115: // Create a method instance.
116: HttpMethod method = new GetMethod(geturlname);
117:
118: // Get the Attribute set by testsessionreplication.jsp
119: attr = makeGet(client, method);
120:
121: // Shut down the first tomcat instance
122: this .shutDownTomcatInstance(1);
123: getLog().debug("Brought down the first tomcat instance");
124:
125: String[] httpURLs = super .getHttpURLs();
126: String httpurl = httpURLs[0];
127: String tmsg = "Is 1st Tomcat really down?Tomcat Up(" + httpurl
128: + ")=";
129: getLog().debug(tmsg + checkURL(httpurl));
130:
131: //Give 30 seconds for things to stabilize.
132: sleepThread(30);
133:
134: // Make connection
135: method = new GetMethod(geturlname);
136: String attr2 = makeGet(client, method);
137: this .sleepThread(10);
138: getLog().debug("Will Start the Tomcat MBean back");
139: this .startTomcatInstance(1);
140: this .sleepThread(10);
141: getLog().debug("Tomcat Up=" + checkURL(httpurl));
142: String tstr = "attr1=" + attr + " and attr2=" + attr2;
143: if (!attr2.equals(attr))
144: fail("Http Session Replication failed with " + tstr);
145: getLog().debug("Http Session Replication has happened");
146: getLog().debug("Exit testHttpSessionReplication");
147: }
148:
149: /**
150: * Starts the Tomcat MBean running on a particular node
151: *
152: * @param instancenum Instance Number of the node
153: * @throws Exception
154: */
155: private void startTomcatInstance(int instancenum) throws Exception {
156: String jndi = getJNDIUrl(instancenum);
157: getLog().debug("JNDI URL Obtained= " + jndi);
158: JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi);
159: //Get the MBeanInfo for the Tomcat MBean
160: ObjectName name = new ObjectName("jboss.web:service=WebServer");
161: MBeanInfo info = server.getMBeanInfo(name);
162: System.out.println("Tomcat MBean:" + info.getClassName());
163:
164: getLog().debug("Going to start tomcat ");
165: //Going to stop the Tomcat Instance
166: server.invokeOperation(name, "start", null, null);
167: this .sleepThread(10);
168: server.invokeOperation(name, "startConnectors", null, null);
169: }
170:
171: /**
172: * Shuts down the Tomcat MBean running on a particular node
173: *
174: * @param instancenum Instance Number of the node
175: * @throws Exception
176: */
177: private void shutDownTomcatInstance(int instancenum)
178: throws Exception {
179: String jndi = getJNDIUrl(instancenum);
180: getLog().debug("JNDI URL Obtained= " + jndi);
181: JBossRMIAdaptorHelper server = new JBossRMIAdaptorHelper(jndi);
182: //Get the MBeanInfo for the Tomcat MBean
183: ObjectName name = new ObjectName("jboss.web:service=WebServer");
184: MBeanInfo info = server.getMBeanInfo(name);
185: System.out.println("Tomcat MBean:" + info.getClassName());
186:
187: getLog().debug("Going to stop tomcat ");
188: //Going to stop the Tomcat Instance
189: server.invokeOperation(name, "stop", null, null);
190: }
191:
192: /**
193: * Generate the JNDI Url for the JBoss Instance with instance number
194: *
195: * @param instancenum
196: * @return
197: */
198: private String getJNDIUrl(int instancenum) {
199: String jndi = "";
200: try {
201: int num = instancenum - 1; //node0,node1 etc
202: String key = "node" + num + ".jndi.url"; // node0.jndiurl
203: jndi = System.getProperty(key);
204: } catch (Exception e) {
205: fail("getJNDIUrl Failed with:" + e.getMessage());
206: }
207:
208: return jndi;
209: }
210:
211: /**
212: * Sleep for specified time
213: *
214: * @param secs
215: * @throws Exception
216: */
217: private void sleepThread(long secs) throws Exception {
218: Thread.sleep(1000 * secs);
219: }
220:
221: /**
222: * Makes a http call to the jsp that retrieves the attribute stored on the
223: * session. When the attribute values mathes with the one retrieved earlier,
224: * we have HttpSessionReplication.
225: * Makes use of commons-httpclient library of Apache
226: *
227: * @param client
228: * @param method
229: * @return session attribute
230: */
231: private String makeGet(HttpClient client, HttpMethod method) {
232: try {
233: client.executeMethod(method);
234:
235: // Read the response body.
236: byte[] responseBody = method.getResponseBody();
237:
238: // Release the connection.
239: method.releaseConnection();
240:
241: // Deal with the response.
242: // Use caution: ensure correct character encoding and is not binary data
243: return new String(responseBody);
244: } catch (HttpRecoverableException e) {
245: log.debug("A recoverable exception occurred, retrying."
246: + e.getMessage());
247: } catch (IOException e) {
248: log.debug(e);
249: e.printStackTrace();
250: System.exit(-1);
251: }
252:
253: return null;
254: }
255:
256: /**
257: * Checks whether the url is valid or not
258: *
259: * @param url The URL which should be checked
260: * @return whether the url is up or not
261: */
262: private boolean checkURL(String url) {
263: boolean ok = false;
264: if (url != null)
265: url = url.trim();
266: try {
267: HttpClient httpConn = new HttpClient();
268: GetMethod g = new GetMethod(url);
269: int responseCode = httpConn.executeMethod(g);
270: log.debug("Response Code for " + url + " is="
271: + responseCode);
272: ok = responseCode == HttpURLConnection.HTTP_OK;
273: } catch (Exception e) {
274: log.debug("Exception for checking url=" + url);
275: log.debug(e);
276: ok = false;
277: }
278: return ok;
279: }
280:
281: }
|