1 package org.roller.presentation; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.apache.struts.action.Action; 6 import org.apache.struts.action.ActionForm; 7 import org.apache.struts.action.ActionForward; 8 import org.apache.struts.action.ActionMapping; 9 import org.roller.RollerException; 10 import org.roller.model.Roller; 11 import org.roller.model.WeblogManager; 12 13 import java.util.Date ; 14 import java.util.List ; 15 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 import org.roller.config.RollerRuntimeConfig; 20 21 22 27 public class MainPageAction extends Action 28 { 29 private static TimedHolder mPopularWebsites = new TimedHolder(30 * 60 * 1000); 31 private static TimedHolder mRecentEntries = new TimedHolder(120 * 60 * 1000); 32 private static TimedHolder mPinnedEntries = new TimedHolder(120 * 60 * 1000); 33 34 private static Log mLogger = 35 LogFactory.getFactory().getInstance(MainPageAction.class); 36 37 40 public ActionForward execute( 41 ActionMapping mapping, ActionForm form, 42 HttpServletRequest req, HttpServletResponse res) 43 throws Exception 44 { 45 RollerContext rctx = RollerContext.getRollerContext(req); 46 47 req.setAttribute("version",rctx.getRollerVersion()); 48 req.setAttribute("buildTime",rctx.getRollerBuildTime()); 49 req.setAttribute("baseURL", rctx.getContextUrl(req)); 50 req.setAttribute("data", new MainPageData(req)); 51 52 54 boolean allowNewUsers = 55 RollerRuntimeConfig.getBooleanProperty("users.registration.enabled"); 56 57 java.security.Principal prince = req.getUserPrincipal(); 58 if (prince != null) 59 { 60 req.setAttribute("loggedIn",Boolean.TRUE); 61 req.setAttribute("userName",prince.getName()); 62 } 63 else if (allowNewUsers) 64 { 65 req.setAttribute("allowNewUsers",Boolean.TRUE); 66 } 67 req.setAttribute("leftPage","/theme/status.jsp"); 68 69 return mapping.findForward("main.page"); 70 } 71 72 public static void flushMainPageCache() 73 { 74 mLogger.debug("Flushing recent and pinned entries"); 75 mRecentEntries.expire(); 76 mPinnedEntries.expire(); 77 } 78 79 82 public static class MainPageData 83 { 84 private HttpServletRequest mRequest = null; 85 86 public MainPageData(HttpServletRequest req) 87 { 88 mRequest = req; 89 } 90 91 95 public List getPopularWebsites(int num) throws RollerException 96 { 97 List list = (List )mPopularWebsites.getObject(); 98 if (list == null) 99 { 100 mLogger.debug("Refreshing popular websites list"); 101 Roller roller = RollerRequest.getRollerRequest(mRequest).getRoller(); 102 list = roller.getRefererManager().getDaysPopularWebsites(num); 103 mPopularWebsites.setObject(list); 104 } 105 return list; 106 } 107 108 112 public List getRecentWeblogEntries(int num) throws RollerException 113 { 114 List list = (List )mRecentEntries.getObject(); 115 try { 116 if (list == null) 117 { 118 mLogger.debug("Refreshing recent entries list"); 119 Roller roller = RollerRequest.getRollerRequest(mRequest).getRoller(); 120 list = roller.getWeblogManager().getWeblogEntries( 121 null, null, new Date (), null, WeblogManager.PUB_ONLY, new Integer (num)); mRecentEntries.setObject(list); 128 } 129 } 130 catch (Exception e) 131 { 132 mLogger.error(e); 133 } 134 return list; 135 } 136 137 141 public List getWeblogEntriesPinnedToMain(int num) throws RollerException 142 { 143 List list = (List )mPinnedEntries.getObject(); 144 if (list == null) 145 { 146 mLogger.debug("Refreshing pinned entries list"); 147 Roller roller = RollerRequest.getRollerRequest(mRequest).getRoller(); 148 list = roller.getWeblogManager() 149 .getWeblogEntriesPinnedToMain(new Integer (num)); 150 mPinnedEntries.setObject(list); 151 } 152 return list; 153 } 154 } 155 156 157 public static class TimedHolder 158 { 159 private Object obj = null; 160 private long updated = 0L; 161 private long timeout = 3000L; 163 164 public TimedHolder(long timeout) 165 { 166 this.timeout = timeout; 167 } 168 169 public synchronized void setObject(Object obj) 170 { 171 this.obj = obj; 172 this.updated = new Date ().getTime(); 173 } 174 175 public synchronized void expire() 176 { 177 this.obj = null; 178 } 179 180 public Object getObject() 181 { 182 long currentTime = new Date ().getTime(); 183 if ((currentTime - this.updated) > this.timeout) 184 { 185 return null; 186 } 187 else 188 { 189 return this.obj; 190 } 191 } 192 } 193 } 194 195 | Popular Tags |