KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > MainPageAction


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 JavaDoc;
14 import java.util.List JavaDoc;
15
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19 import org.roller.config.RollerRuntimeConfig;
20
21
22 /**
23  * Main page action for Roller.
24  * @struts.action name="main" path="/main" scope="request"
25  * @struts.action-forward name="main.page" path="/main.jsp"
26  */

27 public class MainPageAction extends Action
28 {
29     // TODO: make timeouts configurable
30
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     /**
38      * Loads model and forwards to main.page.
39      */

40     public ActionForward execute(
41         ActionMapping mapping, ActionForm form,
42         HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
43         throws Exception JavaDoc
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         /* I am pretty sure this value isn't used on the display anymore,
53          * but I am scared to remove it ... Allen G */

54         boolean allowNewUsers =
55                 RollerRuntimeConfig.getBooleanProperty("users.registration.enabled");
56
57         java.security.Principal JavaDoc 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     /**
80      * Page model.
81      */

82     public static class MainPageData
83     {
84         private HttpServletRequest JavaDoc mRequest = null;
85         
86         public MainPageData(HttpServletRequest JavaDoc req)
87         {
88             mRequest = req;
89         }
90         
91         /**
92          * Get list of most popular websites in terms of day hits.
93          * @param num Number of entries to return (takes effect on next cache refresh)
94          */

95         public List JavaDoc getPopularWebsites(int num) throws RollerException
96         {
97             List JavaDoc list = (List JavaDoc)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         /**
109          * Get list of recent weblog entries.
110          * @param num Number of entries to return (takes effect on next cache refresh)
111          */

112         public List JavaDoc getRecentWeblogEntries(int num) throws RollerException
113         {
114             List JavaDoc list = (List JavaDoc)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, // userName
122
null, // startDate
123
new Date JavaDoc(), // endDate
124
null, // catName
125
WeblogManager.PUB_ONLY, // status
126
new Integer JavaDoc(num)); // maxEntries
127
mRecentEntries.setObject(list);
128             }
129             }
130             catch (Exception JavaDoc e)
131             {
132                 mLogger.error(e);
133             }
134             return list;
135         }
136         
137         /**
138          * Get list of recent weblog pinned entries
139          * @param num Number of entries to return (takes effect on next cache refresh)
140          */

141         public List JavaDoc getWeblogEntriesPinnedToMain(int num) throws RollerException
142         {
143             List JavaDoc list = (List JavaDoc)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 JavaDoc(num));
150                 mPinnedEntries.setObject(list);
151             }
152             return list;
153         }
154     }
155     
156     /** Hold object and expire after timeout passes. */
157     public static class TimedHolder
158     {
159         private Object JavaDoc obj = null;
160         private long updated = 0L;
161         private long timeout = 3000L; // 3 seconds ?? -Lance
162

163         /** Create holder with timeout */
164         public TimedHolder(long timeout)
165         {
166             this.timeout = timeout;
167         }
168         /** Set object and reset the timeout clock */
169         public synchronized void setObject(Object JavaDoc obj)
170         {
171             this.obj = obj;
172             this.updated = new Date JavaDoc().getTime();
173         }
174         /** Force object to expire */
175         public synchronized void expire()
176         {
177             this.obj = null;
178         }
179         /** Get object or null if object has expired */
180         public Object JavaDoc getObject()
181         {
182             long currentTime = new Date JavaDoc().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