package dk.thoerup.marketstats; import java.io.IOException; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Locale; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.spy.memcached.MemcachedClient; import com.gc.android.market.api.MarketSession; import com.gc.android.market.api.model.Market.AppsRequest; import com.gc.android.market.api.model.Market.CommentsRequest; @WebServlet(urlPatterns={"/ShowStats"}) public class ShowStats extends HttpServlet { private static final long serialVersionUID = 1L; static final int MAXCOMMENTS = 30; static final int APP_TIMEOUT = 30*60; static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4; static final Logger log = Logger.getLogger(ShowStats.class.getName()); String login; String password; MemcachedClient memcache = null; @Override public void init() throws ServletException { super.init(); login = getServletContext().getInitParameter("login"); password = getServletContext().getInitParameter("password"); try { memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); } catch (IOException e) { throw new ServletException(e); } } @Override public void destroy() { super.destroy(); memcache.shutdown(3, TimeUnit.SECONDS); memcache = null; } protected AppBean lookupApp(String query) throws IOException { String key = "marketstats:" + query.replace(' ', '_'); //String response = (String) memcache.get(key); AppBean response = (AppBean) memcache.get(key); if (response == null) { response = lookupAppWorker(query); if (response != null) { memcache.set(key, APP_TIMEOUT, response); } } return response; } protected AppBean lookupAppWorker(String query) { MarketSession session = new MarketSession(); session.login(login,password); AppsRequest appsRequest = AppsRequest.newBuilder() .setQuery(query) .setStartIndex(0) .setEntriesCount(1) .setWithExtendedInfo(true) .build(); AppsCallback appsCb = new AppsCallback(); session.append(appsRequest, appsCb); session.flush(); return appsCb.getResult().get(0); } CommentsRequest buildCommentRequest(String appId, int start, int count) { CommentsRequest commentsRequest = CommentsRequest.newBuilder() .setAppId(appId) .setStartIndex(start) .setEntriesCount(count) .build(); return commentsRequest; } private ArrayList loadComments(String appId) { ArrayList commentBeans = new ArrayList(); MarketSession session = new MarketSession(); session.login(login,password); CommentCallback commentsCb = new CommentCallback(); commentsCb.setList( commentBeans ); session.setLocale( Locale.ROOT ); int start = 0; do { int count = 10; if (start > 0) count = Math.min(10, commentsCb.getEntryCount() ); //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); for (int i=0; i<5;i++) { CommentsRequest commentsRequest = buildCommentRequest(appId,start,count); session.append(commentsRequest, commentsCb); start +=10; } session.flush(); if (start >= MAXCOMMENTS) break; //emergency brake } while ( start < commentsCb.getEntryCount() ); return commentBeans; } @SuppressWarnings("unchecked") public ArrayList getComments(AppBean app) { String key = "marketstats:comments:" + app.getId(); ArrayList comments = (ArrayList) memcache.get(key); if (comments == null) { comments = loadComments(app.getId()); memcache.set(key, COMMENT_TIMEOUT, comments); } return comments; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String query = "pname:" + request.getParameter("app"); AppBean app = lookupApp(query); if (app != null) { request.setAttribute("app", app); ArrayList comments = getComments(app); request.setAttribute("comments", comments); getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response); } else { response.getWriter().print("No app found with query=" + query); } } }