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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 953 - (hide annotations) (download)
Mon Jul 5 08:44:09 2010 UTC (13 years, 10 months ago) by torben
File size: 4683 byte(s)
remove unused var
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 952 protected AppBean lookupApp(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 952 response = lookupAppWorker(query);
69 torben 950 if (response != null) {
70     memcache.set(key, APP_TIMEOUT, response);
71     }
72     }
73 torben 659 return response;
74     }
75 torben 952
76     protected AppBean lookupAppWorker(String query) {
77    
78     MarketSession session = new MarketSession();
79     session.login(login,password);
80    
81    
82     AppsRequest appsRequest = AppsRequest.newBuilder()
83     .setQuery(query)
84     .setStartIndex(0)
85     .setEntriesCount(1)
86     .setWithExtendedInfo(true)
87     .build();
88    
89     AppsCallback appsCb = new AppsCallback();
90     session.append(appsRequest, appsCb);
91     session.flush();
92    
93     return appsCb.getResult();
94     }
95 torben 663
96 torben 950 CommentsRequest buildCommentRequest(String appId, int start, int count) {
97     CommentsRequest commentsRequest = CommentsRequest.newBuilder()
98     .setAppId(appId)
99     .setStartIndex(start)
100     .setEntriesCount(count)
101     .build();
102    
103     return commentsRequest;
104     }
105    
106     private ArrayList<CommentBean> loadComments(String appId) {
107    
108     ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
109     MarketSession session = new MarketSession();
110     session.login(login,password);
111    
112 torben 663 CommentCallback commentsCb = new CommentCallback();
113     commentsCb.setList( commentBeans );
114 torben 659
115 torben 756 session.setLocale( Locale.ROOT );
116 torben 663
117    
118 torben 756
119 torben 663 int start = 0;
120     do {
121     int count = 10;
122     if (start > 0)
123     count = Math.min(10, commentsCb.getEntryCount() );
124    
125 torben 757 //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
126 torben 950 CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);
127     session.append(commentsRequest, commentsCb);
128     start +=10;
129 torben 663
130 torben 950 CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count);
131     session.append(commentsRequest2, commentsCb);
132 torben 663 session.flush();
133 torben 950
134 torben 663 start +=10;
135    
136 torben 949 if (start >= MAXCOMMENTS)
137 torben 663 break; //emergency brake
138    
139     } while ( start < commentsCb.getEntryCount() );
140 torben 950
141     return commentBeans;
142 torben 663 }
143 torben 659
144 torben 950
145 torben 705
146 torben 952 @SuppressWarnings("unchecked")
147 torben 950 public ArrayList<CommentBean> getComments(AppBean app) {
148     String key = "marketstats:comments:" + app.getId();
149     ArrayList<CommentBean> comments = (ArrayList<CommentBean>) memcache.get(key);
150 torben 662
151 torben 950 if (comments == null) {
152     comments = loadComments(app.getId());
153     memcache.set(key, COMMENT_TIMEOUT, comments);
154     }
155     return comments;
156 torben 659 }
157    
158     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
159 torben 705 String query = request.getParameter("query");
160 torben 950
161 torben 952 AppBean app = lookupApp(query);
162 torben 950 if (app != null) {
163     request.setAttribute("app", app);
164    
165     ArrayList<CommentBean> comments = getComments(app);
166     request.setAttribute("comments", comments);
167    
168     getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response);
169     } else {
170     response.getWriter().print("No app found with query=" + query);
171     }
172 torben 659 }
173    
174     }

  ViewVC Help
Powered by ViewVC 1.1.20