/[projects]/android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java
ViewVC logotype

Contents of /android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 949 - (show annotations) (download)
Mon Jul 5 06:17:05 2010 UTC (13 years, 10 months ago) by torben
File size: 4326 byte(s)
Limit the number of comment requests
1 package dk.thoerup.marketstats;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.net.InetSocketAddress;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.Locale;
9 import java.util.concurrent.TimeUnit;
10 import java.util.logging.Logger;
11
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import net.spy.memcached.MemcachedClient;
18
19 import com.gc.android.market.api.MarketSession;
20 import com.gc.android.market.api.model.Market.AppsRequest;
21 import com.gc.android.market.api.model.Market.CommentsRequest;
22
23
24
25 public class ShowStats extends HttpServlet {
26 private static final long serialVersionUID = 1L;
27
28 static final int MAXCOMMENTS = 50;
29 static final int TIMEOUT = 30*60;
30
31 static final Logger log = Logger.getLogger(ShowStats.class.getName());
32
33 String login;
34 String password;
35
36 MemcachedClient memcache = null;
37
38
39 @Override
40 public void init() throws ServletException {
41 super.init();
42
43 login = getServletContext().getInitParameter("login");
44 password = getServletContext().getInitParameter("password");
45
46 try {
47 memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211));
48 } catch (IOException e) {
49 throw new ServletException(e);
50 }
51 }
52
53 @Override
54 public void destroy() {
55 super.destroy();
56
57 memcache.shutdown(3, TimeUnit.SECONDS);
58 memcache = null;
59 }
60
61 protected String doLookup(String query) throws IOException {
62
63
64 String key = "marketstats:" + query.replace(' ', '_');
65 String response = (String) memcache.get(key);
66
67 if (response == null) {
68 response = doLookupWorker(query);
69 memcache.set(key, TIMEOUT, response);
70 response += "<!-- new lookup -->";
71 } else {
72 response += "<!-- from memcached -->";
73 }
74 return response;
75 }
76
77 private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) {
78 CommentCallback commentsCb = new CommentCallback();
79 commentsCb.setList( commentBeans );
80
81 session.setLocale( Locale.ROOT );
82
83
84
85 int start = 0;
86 do {
87 int count = 10;
88 if (start > 0)
89 count = Math.min(10, commentsCb.getEntryCount() );
90
91 //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
92 CommentsRequest commentsRequest = CommentsRequest.newBuilder()
93 .setAppId(appId)
94 .setStartIndex(start)
95 .setEntriesCount(count)
96 .build();
97
98 session.append(commentsRequest, commentsCb);
99 session.flush();
100 start +=10;
101
102 if (start >= MAXCOMMENTS)
103 break; //emergency brake
104
105 } while ( start < commentsCb.getEntryCount() );
106 }
107
108
109 protected String doLookupWorker(String query) {
110 final StringBuilder sb = new StringBuilder();
111
112
113 MarketSession session = new MarketSession();
114 session.login(login,password);
115
116
117 AppsRequest appsRequest = AppsRequest.newBuilder()
118 .setQuery(query)
119 .setStartIndex(0).setEntriesCount(10)
120 .setWithExtendedInfo(true)
121 .build();
122
123
124 AppsCallback appsCb = new AppsCallback() ;
125 appsCb.setStringBuffer(sb);
126
127 ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
128
129
130
131 session.append(appsRequest, appsCb);
132 session.flush();
133
134 String appId = appsCb.getAppId();
135
136 if (appId != null) {
137
138 loadComments(appId, session, commentBeans);
139
140 sb.append("-----------------------------------------------------------------\n");
141 for (CommentBean c : commentBeans) {
142 sb.append("User: " + c.author + "\n");
143 sb.append("Rating: " + c.rating + "\n");
144 sb.append("Time: " + new Date(c.time).toString() + "\n");
145 sb.append( c.text + "\n");
146 sb.append("\n");
147
148 }
149 sb.append("Comments: " + commentBeans.size() + "\n");
150 }
151 sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n");
152
153
154 return sb.toString();
155 }
156
157 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
158 String query = request.getParameter("query");
159
160 response.setContentType("text/html");
161 PrintWriter out = response.getWriter();
162 out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" );
163 }
164
165 }

  ViewVC Help
Powered by ViewVC 1.1.20