1 |
torben |
659 |
package dk.thoerup.marketstats;
|
2 |
|
|
|
3 |
|
|
import java.io.IOException;
|
4 |
|
|
import java.net.InetSocketAddress;
|
5 |
torben |
662 |
import java.util.ArrayList;
|
6 |
torben |
659 |
import java.util.Locale;
|
7 |
torben |
762 |
import java.util.concurrent.TimeUnit;
|
8 |
torben |
659 |
import java.util.logging.Logger;
|
9 |
|
|
|
10 |
|
|
import javax.servlet.ServletException;
|
11 |
|
|
import javax.servlet.http.HttpServlet;
|
12 |
|
|
import javax.servlet.http.HttpServletRequest;
|
13 |
|
|
import javax.servlet.http.HttpServletResponse;
|
14 |
|
|
|
15 |
|
|
import net.spy.memcached.MemcachedClient;
|
16 |
|
|
|
17 |
|
|
import com.gc.android.market.api.MarketSession;
|
18 |
|
|
import com.gc.android.market.api.model.Market.AppsRequest;
|
19 |
|
|
import com.gc.android.market.api.model.Market.CommentsRequest;
|
20 |
|
|
|
21 |
|
|
|
22 |
torben |
664 |
|
23 |
torben |
659 |
public class ShowStats extends HttpServlet {
|
24 |
|
|
private static final long serialVersionUID = 1L;
|
25 |
|
|
|
26 |
torben |
950 |
static final int MAXCOMMENTS = 30;
|
27 |
|
|
static final int APP_TIMEOUT = 30*60;
|
28 |
|
|
static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4;
|
29 |
torben |
949 |
|
30 |
torben |
659 |
static final Logger log = Logger.getLogger(ShowStats.class.getName());
|
31 |
|
|
|
32 |
|
|
String login;
|
33 |
|
|
String password;
|
34 |
torben |
667 |
|
35 |
|
|
MemcachedClient memcache = null;
|
36 |
torben |
659 |
|
37 |
|
|
|
38 |
|
|
@Override
|
39 |
|
|
public void init() throws ServletException {
|
40 |
|
|
super.init();
|
41 |
|
|
|
42 |
|
|
login = getServletContext().getInitParameter("login");
|
43 |
|
|
password = getServletContext().getInitParameter("password");
|
44 |
torben |
667 |
|
45 |
|
|
try {
|
46 |
|
|
memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211));
|
47 |
|
|
} catch (IOException e) {
|
48 |
|
|
throw new ServletException(e);
|
49 |
|
|
}
|
50 |
torben |
659 |
}
|
51 |
|
|
|
52 |
torben |
762 |
@Override
|
53 |
|
|
public void destroy() {
|
54 |
|
|
super.destroy();
|
55 |
|
|
|
56 |
|
|
memcache.shutdown(3, TimeUnit.SECONDS);
|
57 |
|
|
memcache = null;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
torben |
950 |
protected AppBean doLookup(String query) throws IOException {
|
61 |
torben |
667 |
|
62 |
torben |
659 |
|
63 |
torben |
759 |
String key = "marketstats:" + query.replace(' ', '_');
|
64 |
torben |
950 |
//String response = (String) memcache.get(key);
|
65 |
|
|
AppBean response = (AppBean) memcache.get(key);
|
66 |
torben |
659 |
|
67 |
|
|
if (response == null) {
|
68 |
torben |
705 |
response = doLookupWorker(query);
|
69 |
torben |
950 |
if (response != null) {
|
70 |
|
|
memcache.set(key, APP_TIMEOUT, response);
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
torben |
659 |
return response;
|
74 |
|
|
}
|
75 |
torben |
663 |
|
76 |
torben |
950 |
CommentsRequest buildCommentRequest(String appId, int start, int count) {
|
77 |
|
|
CommentsRequest commentsRequest = CommentsRequest.newBuilder()
|
78 |
|
|
.setAppId(appId)
|
79 |
|
|
.setStartIndex(start)
|
80 |
|
|
.setEntriesCount(count)
|
81 |
|
|
.build();
|
82 |
|
|
|
83 |
|
|
return commentsRequest;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
private ArrayList<CommentBean> loadComments(String appId) {
|
87 |
|
|
|
88 |
|
|
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
|
89 |
|
|
MarketSession session = new MarketSession();
|
90 |
|
|
session.login(login,password);
|
91 |
|
|
|
92 |
torben |
663 |
CommentCallback commentsCb = new CommentCallback();
|
93 |
|
|
commentsCb.setList( commentBeans );
|
94 |
torben |
659 |
|
95 |
torben |
756 |
session.setLocale( Locale.ROOT );
|
96 |
torben |
663 |
|
97 |
|
|
|
98 |
torben |
756 |
|
99 |
torben |
663 |
int start = 0;
|
100 |
|
|
do {
|
101 |
|
|
int count = 10;
|
102 |
|
|
if (start > 0)
|
103 |
|
|
count = Math.min(10, commentsCb.getEntryCount() );
|
104 |
|
|
|
105 |
torben |
757 |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
|
106 |
torben |
950 |
CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);
|
107 |
|
|
session.append(commentsRequest, commentsCb);
|
108 |
|
|
start +=10;
|
109 |
torben |
663 |
|
110 |
torben |
950 |
CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count);
|
111 |
|
|
session.append(commentsRequest2, commentsCb);
|
112 |
torben |
663 |
session.flush();
|
113 |
torben |
950 |
|
114 |
torben |
663 |
start +=10;
|
115 |
|
|
|
116 |
torben |
949 |
if (start >= MAXCOMMENTS)
|
117 |
torben |
663 |
break; //emergency brake
|
118 |
|
|
|
119 |
|
|
} while ( start < commentsCb.getEntryCount() );
|
120 |
torben |
950 |
|
121 |
|
|
return commentBeans;
|
122 |
torben |
663 |
}
|
123 |
torben |
659 |
|
124 |
torben |
950 |
|
125 |
torben |
663 |
|
126 |
torben |
950 |
protected AppBean doLookupWorker(String query) {
|
127 |
torben |
659 |
final StringBuilder sb = new StringBuilder();
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
MarketSession session = new MarketSession();
|
131 |
|
|
session.login(login,password);
|
132 |
|
|
|
133 |
torben |
662 |
|
134 |
torben |
659 |
AppsRequest appsRequest = AppsRequest.newBuilder()
|
135 |
torben |
705 |
.setQuery(query)
|
136 |
torben |
951 |
.setStartIndex(0)
|
137 |
|
|
.setEntriesCount(1)
|
138 |
torben |
659 |
.setWithExtendedInfo(true)
|
139 |
|
|
.build();
|
140 |
torben |
950 |
|
141 |
|
|
AppsCallback appsCb = new AppsCallback();
|
142 |
torben |
659 |
session.append(appsRequest, appsCb);
|
143 |
|
|
session.flush();
|
144 |
torben |
705 |
|
145 |
torben |
950 |
return appsCb.getResult();
|
146 |
|
|
}
|
147 |
torben |
705 |
|
148 |
torben |
950 |
public ArrayList<CommentBean> getComments(AppBean app) {
|
149 |
|
|
String key = "marketstats:comments:" + app.getId();
|
150 |
|
|
ArrayList<CommentBean> comments = (ArrayList<CommentBean>) memcache.get(key);
|
151 |
torben |
662 |
|
152 |
torben |
950 |
if (comments == null) {
|
153 |
|
|
comments = loadComments(app.getId());
|
154 |
|
|
memcache.set(key, COMMENT_TIMEOUT, comments);
|
155 |
|
|
}
|
156 |
|
|
return comments;
|
157 |
torben |
659 |
}
|
158 |
|
|
|
159 |
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
160 |
torben |
705 |
String query = request.getParameter("query");
|
161 |
torben |
950 |
|
162 |
|
|
AppBean app = doLookup(query);
|
163 |
|
|
if (app != null) {
|
164 |
|
|
request.setAttribute("app", app);
|
165 |
|
|
|
166 |
|
|
ArrayList<CommentBean> comments = getComments(app);
|
167 |
|
|
request.setAttribute("comments", comments);
|
168 |
|
|
|
169 |
|
|
getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response);
|
170 |
|
|
} else {
|
171 |
|
|
response.getWriter().print("No app found with query=" + query);
|
172 |
|
|
}
|
173 |
torben |
659 |
}
|
174 |
|
|
|
175 |
|
|
}
|