Java tutorial
/** * PureInfo TGirls * @(#)GetPicArrServlet.java 1.0 2009-3-10 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.tgirls.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections.iterators.LoopingIterator; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.json.JSONException; import org.json.JSONObject; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.force.exception.PureException; import com.pureinfo.tfr.cache.core.CacheManager; import com.pureinfo.tfr.cache.core.IReadCache; import com.pureinfo.tgirls.domain.IPhotoMgr; import com.pureinfo.tgirls.json.model.JsonBase; import com.pureinfo.tgirls.json.model.JsonBase.ErrorCode; import com.pureinfo.tgirls.model.Photo; public class GetPicArrServlet extends HttpServlet { /** * Comment for <code>serialVersionUID</code> */ private static final long serialVersionUID = 1L; private Logger logger = Logger.getLogger(this.getClass().getName()); IReadCache cache = CacheManager.getReadFromDBCache("view.pics"); @Override protected void doGet(HttpServletRequest _req, HttpServletResponse _resp) throws ServletException, IOException { String type = _req.getParameter("pictype"); String s = _req.getParameter("size"); String sstartIndex = _req.getParameter("startIndex"); if (StringUtils.isEmpty(s)) { s = "50"; } if (StringUtils.isEmpty(sstartIndex)) { sstartIndex = "0"; } int startIndex = Integer.parseInt(sstartIndex); int resultsize = Integer.parseInt(s); logger.debug("to get pics with type:" + type + " and size:" + resultsize); _resp.setContentType("text/json; charset=utf-8"); JsonBase json = new JsonBase(); //List<Photo> result = new ArrayList<Photo>(); List<Photo> cacheList = null; if ((cacheList = (List<Photo>) cache.get("pics." + type)) == null) { try { startIndex = 0; IPhotoMgr mgr = (IPhotoMgr) ArkContentHelper.getContentMgrOf(Photo.class); if (StringUtils.isEmpty(type)) { cacheList = mgr.getRandomPics(500); } else { cacheList = mgr.getRandomPics(Integer.parseInt(type), 500); } if (cacheList != null) { cache.put("pics." + type, cacheList); } } catch (PureException e) { logger.error("error when get pics.", e); } } if (cacheList == null || cacheList.isEmpty()) { json.setErrorCode(ErrorCode.ERROR.getCode()); json.setErrorMsg(""); _resp.getWriter().write(json.toString()); return; } //Collections.shuffle(cacheList); //result.addAll(cacheList); //if (resultsize < result.size()) { // result.subList(0, resultsize); //} try { Photo photo; List<JSONObject> jsonList = new ArrayList<JSONObject>(); int temp = 0; int i = 0; if (startIndex == 0) { startIndex = new Random().nextInt(cacheList.size()); } Iterator<Photo> itor = new LoopingIterator(cacheList); while (itor.hasNext() && i < resultsize) { photo = itor.next(); if (temp < startIndex) { logger.debug("currentIndex[" + temp + "] and startIndex[" + startIndex + "]"); temp++; continue; } logger.debug("to add photo[" + photo + "]"); jsonList.add(new JSONObject(photo)); i++; } Collections.shuffle(jsonList); json.put("pics", jsonList); json.put("nextIndex", startIndex + resultsize); } catch (JSONException e) { throw new RuntimeException(); } // logger.debug("result:" + json.toString()); _resp.getWriter().write(json.toString()); return; } }