KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > servlets > OrganicObjectGraphServlet


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.performance.servlets;
5
6 import com.tctest.performance.sampledata.OrganicObjectGraph;
7 import com.tctest.performance.sampledata.OrganicObjectGraphManager;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Random JavaDoc;
14
15 import javax.servlet.http.HttpServlet JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import javax.servlet.http.HttpSession JavaDoc;
19
20 public final class OrganicObjectGraphServlet extends HttpServlet JavaDoc {
21
22   private static final String JavaDoc CHANGES = "changes"; // # of changes to randomly selected graph
23
private static final String JavaDoc CREATE_GRAPH = "create"; // 0 = no, > 0 = # of nodes
24
private static final String JavaDoc FINISHED = "finished"; // test complete, serialize data
25
private static final String JavaDoc ERROR = "error";
26   private static final String JavaDoc HOST_SYSPROP = "__TEST_HOSTNAME__"; // system property uniquely identifies a host
27
private static final int DEFAULT_GRAPH_SIZE = 10;
28   private final Random JavaDoc random = new Random JavaDoc(0);
29
30   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
31     HttpSession JavaDoc session = request.getSession(true);
32     response.setContentType("text/html");
33     PrintWriter JavaDoc out = response.getWriter();
34     
35     String JavaDoc envKey = System.getProperty(HOST_SYSPROP);
36     if (envKey == null) throw new IOException JavaDoc("System Property \"" + HOST_SYSPROP + "\" Not Set");
37
38     if (request.getParameter(FINISHED) != null) {
39       try {
40         writeData(session);
41       } catch (Exception JavaDoc e) {
42         throw new IOException JavaDoc("Unable to write graph data to filesystem.");
43       }
44       out.println("Wrote Data");
45       return;
46     }
47
48     int graphCount = graphCount(session);
49     int changes = 0;
50     if (request.getParameter(CHANGES) == null) {
51       out.println(ERROR);
52       return;
53     } else {
54       changes = Integer.parseInt(request.getParameter(CHANGES));
55     }
56     int createNewGraph = 0;
57     if (request.getParameter(CREATE_GRAPH) == null) {
58       out.println(ERROR);
59       return;
60     } else {
61       createNewGraph = Integer.parseInt(request.getParameter(CREATE_GRAPH));
62     }
63
64     if (createNewGraph > 0 || session.isNew()) {
65       if (session.isNew()) createNewGraph = DEFAULT_GRAPH_SIZE;
66       try {
67         OrganicObjectGraph graph = OrganicObjectGraphManager.createOrganicGraph(createNewGraph, envKey);
68         session.setAttribute("graph_" + graphCount, graph);
69       } catch (Exception JavaDoc e) {
70         e.printStackTrace();
71         throw new IOException JavaDoc("Unable to create data graph");
72       }
73     }
74
75     if (changes > 0) {
76       int rand = getRandom(graphCount);
77       OrganicObjectGraph graph = (OrganicObjectGraph) session.getAttribute("graph_" + rand);
78       graph.mutateRandom(changes);
79       session.setAttribute("graph_" + rand, graph); // put the session back (not needed by DSO)
80
}
81
82     out.println("Session Updated");
83     out.flush();
84     return;
85   }
86
87   private void writeData(HttpSession JavaDoc session) throws Exception JavaDoc {
88     Enumeration JavaDoc names = session.getAttributeNames();
89     String JavaDoc name;
90     while (names.hasMoreElements()) {
91       name = (String JavaDoc) names.nextElement();
92       File JavaDoc file = new File JavaDoc("results" + File.separator + session.getId() + "_" + name + ".obj");
93       System.out.println("Writing File: " + file.getAbsolutePath());
94       OrganicObjectGraphManager.serializeGraph(((OrganicObjectGraph) session.getAttribute(name)), file);
95     }
96   }
97
98   private int graphCount(HttpSession JavaDoc session) {
99     Enumeration JavaDoc names = session.getAttributeNames();
100     int count = 0;
101     while (names.hasMoreElements()) {
102       if (((String JavaDoc) names.nextElement()).startsWith("graph_")) count++;
103     }
104     return count;
105   }
106
107   private int getRandom(int bound) {
108     return new Long JavaDoc(Math.round(Math.floor(bound * random.nextDouble()))).intValue();
109   }
110 }
111
Popular Tags