001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test.stress;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServlet;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: public class StressTestServlet extends HttpServlet {
030:
031: private static final long serialVersionUID = 1588315166209073945L;
032:
033: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
034: .getLogger(StressTestServlet.class);
035:
036: private static final long DEFAULT_POLL_INTERVAL = 10000;
037: public static final String POLL_INTERVAL = "pollInterval";
038:
039: public static final String RANDOM = "random";
040: public static final String UTILITY = "utility";
041:
042: public void init() throws ServletException {
043: super .init();
044:
045: TestRepository testRepo = TestRepository.getInstance();
046: testRepo.addInitiators("tkirkend");
047: testRepo
048: .addXml(new XmlBean("StressTestDocument", "route1.xml"));
049: }
050:
051: protected void doGet(HttpServletRequest request,
052: HttpServletResponse response) throws ServletException,
053: IOException {
054: try {
055: String pollingIntervalValue = request
056: .getParameter(POLL_INTERVAL);
057: long pollingInterval = (pollingIntervalValue == null ? DEFAULT_POLL_INTERVAL
058: : Long.parseLong(pollingIntervalValue));
059: Test test = determineTest(request);
060: Map parameters = new HashMap();
061: Enumeration enumeration = request.getParameterNames();
062: while (enumeration.hasMoreElements()) {
063: String name = (String) enumeration.nextElement();
064: parameters.put(name, request.getParameter(name));
065: }
066: test.setParameters(parameters);
067: while (!test.doWork()) {
068: Thread.sleep(pollingInterval);
069: }
070: response.setContentType("text/html");
071: response
072: .getWriter()
073: .write(
074: "<html><head><title>SUCCESS</title></head><body>"
075: + "Successfully completed test. Calls to server "
076: + TestInfo.getServerCalls()
077: + "<br>"
078: + "Documents routed "
079: + TestInfo.getRouteHeaderIds()
080: .size() + "</body></html>");
081: } catch (Exception e) {
082: LOG.error("Exception thrown from test!.", e);
083: throw new ServletException(e);
084: //response.getWriter().write("<html><head>ERROR: Stress Test</head><body>STRESS TEST ERROR!!!<br><br>");
085: //e.printStackTrace(response.getWriter());
086: //response.getWriter().write("</body></html>");
087: //response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
088: }
089: }
090:
091: protected void doPost(HttpServletRequest request,
092: HttpServletResponse response) throws ServletException,
093: IOException {
094: doGet(request, response);
095: }
096:
097: protected Test determineTest(HttpServletRequest request)
098: throws Exception {
099: String test = request.getParameter("test");
100: if (test == null)
101: test = "";
102: test = test.trim();
103: if (test == null || "".equals(test)) {
104: throw new Exception("Must specifiy a 'test' parameter!");
105: } else if (RANDOM.equals(test)) {
106: return new RandomTest();
107: } else if (test.equals(UTILITY)) {
108: return new WorkflowUtilityTest();
109: } else {
110: return (Test) Class.forName(test).newInstance();
111: }
112: }
113:
114: }
|