KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > RequestAndMetaInfoCentricCachePopulator


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util;
25
26 import java.security.Principal JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35
36 import org.apache.log4j.Logger;
37 import org.infoglue.cms.applications.common.Session;
38 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController;
39 import org.infoglue.cms.controllers.kernel.impl.simple.RepositoryController;
40 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeController;
41 import org.infoglue.cms.entities.content.ContentVO;
42 import org.infoglue.cms.entities.management.LanguageVO;
43 import org.infoglue.cms.entities.management.Repository;
44 import org.infoglue.cms.entities.management.RepositoryLanguage;
45 import org.infoglue.cms.entities.management.RepositoryVO;
46 import org.infoglue.cms.entities.structure.SiteNode;
47 import org.infoglue.cms.entities.structure.SiteNodeVO;
48 import org.infoglue.cms.exception.SystemException;
49 import org.infoglue.cms.security.InfoGluePrincipal;
50 import org.infoglue.cms.util.CmsPropertyHandler;
51 import org.infoglue.cms.util.FakeHttpServletRequest;
52 import org.infoglue.cms.util.FakeHttpServletResponse;
53 import org.infoglue.cms.util.FakeHttpSession;
54 import org.infoglue.deliver.applications.databeans.DatabaseWrapper;
55 import org.infoglue.deliver.applications.databeans.DeliveryContext;
56 import org.infoglue.deliver.controllers.kernel.impl.simple.BasicTemplateController;
57 import org.infoglue.deliver.controllers.kernel.impl.simple.ExtranetController;
58 import org.infoglue.deliver.controllers.kernel.impl.simple.IntegrationDeliveryController;
59 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController;
60 import org.infoglue.deliver.controllers.kernel.impl.simple.NodeDeliveryController;
61 import org.infoglue.deliver.controllers.kernel.impl.simple.RepositoryDeliveryController;
62 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController;
63
64
65 public class RequestAndMetaInfoCentricCachePopulator
66 {
67     public final static Logger logger = Logger.getLogger(RequestAndMetaInfoCentricCachePopulator.class.getName());
68
69     /**
70      * This method simulates a call to a page so all castor caches fills up before we throw the old page cache.
71      * @param db
72      * @param siteNodeId
73      * @param languageId
74      * @param contentId
75      */

76     
77     public void recache(DatabaseWrapper dbWrapper, Integer JavaDoc siteNodeId) throws SystemException, Exception JavaDoc
78     {
79         logger.info("recache starting..");
80
81         HttpHelper helper = new HttpHelper();
82         String JavaDoc recacheUrl = CmsPropertyHandler.getRecacheUrl() + "?siteNodeId=" + siteNodeId + "&refresh=true&isRecacheCall=true";
83         String JavaDoc response = helper.getUrlContent(recacheUrl);
84         
85         String JavaDoc recacheBaseUrl = CmsPropertyHandler.getRecacheUrl().replaceAll("/ViewPage.action", "");
86         String JavaDoc pathsToRecacheOnPublishing = CmsPropertyHandler.getPathsToRecacheOnPublishing();
87         if(pathsToRecacheOnPublishing.indexOf("pathsToRecacheOnPublishing") == -1)
88         {
89             String JavaDoc[] pathsToRecacheOnPublishingArray = pathsToRecacheOnPublishing.split(",");
90             for(int i = 0; i < pathsToRecacheOnPublishingArray.length; i++)
91             {
92                 recacheUrl = recacheBaseUrl + pathsToRecacheOnPublishingArray[i] + "?refresh=true&isRecacheCall=true";
93                 //System.out.println("calling recacheUrl:" + recacheUrl);
94
response = helper.getUrlContent(recacheUrl);
95             }
96         }
97         
98         LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(dbWrapper.getDatabase(), siteNodeId);
99         if(masterLanguageVO == null)
100             throw new SystemException("There was no master language for the siteNode " + siteNodeId);
101     
102         Integer JavaDoc languageId = masterLanguageVO.getLanguageId();
103         if(languageId == null)
104             languageId = masterLanguageVO.getLanguageId();
105                 
106         Integer JavaDoc contentId = new Integer JavaDoc(-1);
107         
108         Principal JavaDoc principal = (Principal JavaDoc)CacheController.getCachedObject("userCache", "anonymous");
109         if(principal == null)
110         {
111             Map JavaDoc arguments = new HashMap JavaDoc();
112             arguments.put("j_username", CmsPropertyHandler.getAnonymousUser());
113             arguments.put("j_password", CmsPropertyHandler.getAnonymousPassword());
114             
115             principal = ExtranetController.getController().getAuthenticatedPrincipal(dbWrapper.getDatabase(), arguments);
116             
117             if(principal != null)
118                 CacheController.cacheObject("userCache", "anonymous", principal);
119         }
120
121         FakeHttpSession fakeHttpServletSession = new FakeHttpSession();
122         FakeHttpServletResponse fakeHttpServletResponse = new FakeHttpServletResponse();
123         FakeHttpServletRequest fakeHttpServletRequest = new FakeHttpServletRequest();
124         fakeHttpServletRequest.setParameter("siteNodeId", "" + siteNodeId);
125         fakeHttpServletRequest.setParameter("languageId", "" + languageId);
126         fakeHttpServletRequest.setParameter("contentId", "" + contentId);
127         fakeHttpServletRequest.setRequestURI("ViewPage.action");
128
129         fakeHttpServletRequest.setAttribute("siteNodeId", "" + siteNodeId);
130         fakeHttpServletRequest.setAttribute("languageId", "" + languageId);
131         fakeHttpServletRequest.setAttribute("contentId", "" + contentId);
132
133         fakeHttpServletRequest.setServletContext(DeliverContextListener.getServletContext());
134         
135         BrowserBean browserBean = new BrowserBean();
136         //this.browserBean.setRequest(getRequest());
137

138         NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId);
139         IntegrationDeliveryController integrationDeliveryController = IntegrationDeliveryController.getIntegrationDeliveryController(siteNodeId, languageId, contentId);
140         TemplateController templateController = getTemplateController(dbWrapper, siteNodeId, languageId, contentId, new FakeHttpServletRequest(), (InfoGluePrincipal)principal, false, browserBean, nodeDeliveryController, integrationDeliveryController);
141         
142         DeliveryContext deliveryContext = DeliveryContext.getDeliveryContext(/*(InfoGluePrincipal)this.principal*/);
143         //deliveryContext.setRepositoryName(repositoryName);
144
deliveryContext.setSiteNodeId(siteNodeId);
145         deliveryContext.setContentId(contentId);
146         deliveryContext.setLanguageId(languageId);
147         deliveryContext.setPageKey("" + System.currentTimeMillis());
148         deliveryContext.setSession(new Session(fakeHttpServletSession));
149         deliveryContext.setInfoGlueAbstractAction(null);
150         deliveryContext.setHttpServletRequest(fakeHttpServletRequest);
151         deliveryContext.setHttpServletResponse(fakeHttpServletResponse);
152
153         templateController.setDeliveryContext(deliveryContext);
154         
155         //We don't want a page cache entry to be created
156
deliveryContext.setDisablePageCache(true);
157
158         SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId);
159         SiteNodeVO rootSiteNodeVO = templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId());
160         
161         recurseSiteNodeTree(rootSiteNodeVO.getId(), languageId, templateController, principal/*, dbWrapper*/, 1, 0);
162
163         List JavaDoc templates = ContentController.getContentController().getContentVOWithContentTypeDefinition("HTMLTemplate", dbWrapper.getDatabase());
164         Iterator JavaDoc templatesIterator = templates.iterator();
165         {
166             ContentVO template = (ContentVO)templatesIterator.next();
167
168             String JavaDoc templateString = templateController.getContentAttribute(template.getId(), languageId, "Template", true);
169         }
170         
171         RepositoryVO repository = RepositoryDeliveryController.getRepositoryDeliveryController().getMasterRepository(dbWrapper.getDatabase());
172         
173         RepositoryDeliveryController.getRepositoryDeliveryController().getRepositoryVOListFromServerName(dbWrapper.getDatabase(), "localhost", "8080", repository.getName());
174         
175         logger.info("recache stopped..");
176     }
177     
178     
179     private void recurseSiteNodeTree(Integer JavaDoc siteNodeId, Integer JavaDoc languageId, TemplateController templateController, Principal JavaDoc principal/*, DatabaseWrapper dbWrapper*/, int maxLevel, int currentLevel) throws Exception JavaDoc
180     {
181         SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(siteNodeId, templateController.getDatabase(), true);
182         SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId);
183
184         templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "Title", true);
185         templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "NavigationTitle", true);
186         templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "NiceURIName", true);
187         templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "Description", true);
188         templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "ComponentStructure", true);
189
190         List JavaDoc childPages = templateController.getChildPages(siteNodeId);
191         
192         templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId());
193         templateController.getParentSiteNode(siteNodeVO.getId());
194         
195         Collection JavaDoc childSiteNodes = siteNode.getChildSiteNodes();
196         
197         /*
198         List childSiteNodeIds = new ArrayList();
199         Iterator childSiteNodesIterator = childSiteNodes.iterator();
200         while(childSiteNodesIterator.hasNext())
201         {
202             SiteNode childSiteNode = (SiteNode)childSiteNodesIterator.next();
203             childSiteNodeIds.add(childSiteNode.getSiteNodeId());
204         }
205
206         templateController.commitDatabase();
207
208         Iterator childSiteNodeIdsIterator = childSiteNodeIds.iterator();
209         while(childSiteNodeIdsIterator.hasNext())
210         {
211             Integer childSiteNodeId = (Integer)childSiteNodeIdsIterator.next();
212             recurseSiteNodeTree(childSiteNodeId, languageId, templateController, principal);
213         }
214
215         */

216
217         Iterator JavaDoc childSiteNodesIterator = childSiteNodes.iterator();
218         while(childSiteNodesIterator.hasNext())
219         {
220             SiteNode childSiteNode = (SiteNode)childSiteNodesIterator.next();
221             if(maxLevel > currentLevel)
222                 recurseSiteNodeTree(childSiteNode.getId(), languageId, templateController, principal, maxLevel, currentLevel + 1);
223         }
224
225      
226         Repository repository = RepositoryController.getController().getRepositoryWithId(siteNodeVO.getRepositoryId(), templateController.getDatabase());
227         Collection JavaDoc languages = repository.getRepositoryLanguages();
228         Iterator JavaDoc languagesIterator = languages.iterator();
229         while(languagesIterator.hasNext())
230         {
231             RepositoryLanguage repositoryLanguage = (RepositoryLanguage)languagesIterator.next();
232             LanguageDeliveryController.getLanguageDeliveryController().getLanguageIfSiteNodeSupportsIt(templateController.getDatabase(), repositoryLanguage.getLanguage().getId(), siteNodeId);
233         }
234    
235         
236         Integer JavaDoc contentId = new Integer JavaDoc(-1);
237         
238         FakeHttpSession fakeHttpServletSession = new FakeHttpSession();
239         FakeHttpServletResponse fakeHttpServletResponse = new FakeHttpServletResponse();
240         FakeHttpServletRequest fakeHttpServletRequest = new FakeHttpServletRequest();
241         fakeHttpServletRequest.setParameter("siteNodeId", "" + siteNodeId);
242         fakeHttpServletRequest.setParameter("languageId", "" + languageId);
243         fakeHttpServletRequest.setParameter("contentId", "" + contentId);
244         fakeHttpServletRequest.setRequestURI("ViewPage.action");
245
246         fakeHttpServletRequest.setAttribute("siteNodeId", "" + siteNodeId);
247         fakeHttpServletRequest.setAttribute("languageId", "" + languageId);
248         fakeHttpServletRequest.setAttribute("contentId", "" + contentId);
249
250         fakeHttpServletRequest.setServletContext(DeliverContextListener.getServletContext());
251         
252         BrowserBean browserBean = new BrowserBean();
253         //this.browserBean.setRequest(getRequest());
254

255         NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId);
256         IntegrationDeliveryController integrationDeliveryController = IntegrationDeliveryController.getIntegrationDeliveryController(siteNodeId, languageId, contentId);
257         TemplateController subTemplateController = getTemplateController(templateController.getDatabaseWrapper(), siteNodeId, languageId, contentId, new FakeHttpServletRequest(), (InfoGluePrincipal)principal, false, browserBean, nodeDeliveryController, integrationDeliveryController);
258
259         DeliveryContext deliveryContext = DeliveryContext.getDeliveryContext(/*(InfoGluePrincipal)this.principal*/);
260         //deliveryContext.setRepositoryName(repositoryName);
261
deliveryContext.setSiteNodeId(siteNodeId);
262         deliveryContext.setContentId(contentId);
263         deliveryContext.setLanguageId(languageId);
264         deliveryContext.setPageKey("" + System.currentTimeMillis());
265         deliveryContext.setSession(new Session(fakeHttpServletSession));
266         deliveryContext.setInfoGlueAbstractAction(null);
267         deliveryContext.setHttpServletRequest(fakeHttpServletRequest);
268         deliveryContext.setHttpServletResponse(fakeHttpServletResponse);
269
270         subTemplateController.setDeliveryContext(deliveryContext);
271         
272         //We don't want a page cache entry to be created
273
deliveryContext.setDisablePageCache(true);
274
275         SiteNodeVO rootSiteNodeVO = templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId());
276
277         String JavaDoc pagePath = subTemplateController.getCurrentPagePath();
278                 
279         CacheController.cacheObject("newPagePathCache", deliveryContext.getPageKey(), pagePath);
280     }
281     
282     /**
283      * This method should be much more sophisticated later and include a check to see if there is a
284      * digital asset uploaded which is more specialized and can be used to act as serverside logic to the template.
285      * The method also consideres wheter or not to invoke the preview-version with administrative functioality or the
286      * normal site-delivery version.
287      */

288     
289     public TemplateController getTemplateController(DatabaseWrapper dbWrapper, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, HttpServletRequest JavaDoc request, InfoGluePrincipal infoGluePrincipal, boolean allowEditOnSightAtAll, BrowserBean browserBean, NodeDeliveryController nodeDeliveryController, IntegrationDeliveryController integrationDeliveryController) throws SystemException, Exception JavaDoc
290     {
291         TemplateController templateController = new BasicTemplateController(dbWrapper, infoGluePrincipal);
292         templateController.setStandardRequestParameters(siteNodeId, languageId, contentId);
293         templateController.setHttpRequest(request);
294         templateController.setBrowserBean(browserBean);
295         templateController.setDeliveryControllers(nodeDeliveryController, null, integrationDeliveryController);
296         
297         return templateController;
298     }
299
300
301 }
Popular Tags