forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockServlet.java
More file actions
158 lines (150 loc) · 5.6 KB
/
Copy pathStockServlet.java
File metadata and controls
158 lines (150 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sdo.stock.StockPriceHistory;
import net.sdo.stock.StockPriceUtils;
public class StockServlet extends HttpServlet {
@EJB
private StockSessionBeanLocal stockSessionBean;
private boolean doMock = System.getProperty("MockEntityManager") != null;
private ThreadLocal<DateFormat> localDf = new ThreadLocal<DateFormat>() {
@Override
public DateFormat initialValue() {
return DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
}
};
Set<SoftReference<StockPriceHistory>> softCache = new HashSet();
Set<WeakReference<StockPriceHistory>> weakCache = new HashSet();
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
String symbol = request.getParameter("symbol");
if (symbol == null) {
symbol = StockPriceUtils.getRandomSymbol();
}
String startDate = request.getParameter("startDate");
if (startDate == null) {
startDate = "1/1/01";
}
String endDate = request.getParameter("endDate");
if (endDate == null) {
endDate = "12/31/25";
}
String log = request.getParameter("log");
int impl = StockPriceHistory.STANDARD;
if ("true".equals(log)) {
impl = StockPriceHistory.LOGGING;
}
StockPriceHistory sph;
DateFormat df = localDf.get();
sph = stockSessionBean.getHistory(symbol, df.parse(startDate),
df.parse(endDate), doMock, impl);
String saveSession = request.getParameter("save");
if (saveSession != null) {
int saveCount = Integer.parseInt(saveSession);
HttpSession session = request.getSession(true);
ArrayList<StockPriceHistory> al =
(ArrayList<StockPriceHistory>) session.getAttribute("saveHistory");
if (al == null) {
al = new ArrayList<StockPriceHistory>(100);
}
al.add(sph);
if (al.size() > saveCount) {
al.remove(0);
}
ArrayList<Date> ald = (ArrayList<Date>) session.getAttribute("date");
if (ald == null) {
ald = new ArrayList<Date>(100);
}
ald.add(new Date());
if (ald.size() > saveCount * 4) {
ald.remove(0);
}
session.setAttribute("saveHistory", al);
session.setAttribute("last", ald);
}
String doSoftCache = request.getParameter("saveSoft");
if ("true".equals(doSoftCache)) {
softCache.add(new SoftReference(sph));
}
String doWeakCache = request.getParameter("saveWeak");
if ("true".equals(doWeakCache)) {
weakCache.add(new WeakReference(sph));
}
request.setAttribute("history", sph);
String doLong = request.getParameter("long");
if ("true".equals(doLong)) {
request.getRequestDispatcher("longhistory.jsp").forward(request, response);
} else {
request.getRequestDispatcher("history.jsp").forward(request, response);
}
} catch (Exception e) {
request.setAttribute("exception", e);
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Sample StockPrice Servlet";
}
}