KickJava   Java API By Example, From Geeks To Geeks.

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


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

71     
72     public void recache(DatabaseWrapper dbWrapper, Integer JavaDoc siteNodeId) throws SystemException, Exception JavaDoc
73     {
74         logger.info("recache starting..");
75
76         LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(dbWrapper.getDatabase(), siteNodeId);
77         if(masterLanguageVO == null)
78             throw new SystemException("There was no master language for the siteNode " + siteNodeId);
79     
80         Integer JavaDoc languageId = masterLanguageVO.getLanguageId();
81         if(languageId == null)
82             languageId = masterLanguageVO.getLanguageId();
83                 
84         Integer JavaDoc contentId = new Integer JavaDoc(-1);
85         
86         Principal JavaDoc principal = (Principal JavaDoc)CacheController.getCachedObject("userCache", "anonymous");
87         if(principal == null)
88         {
89             Map JavaDoc arguments = new HashMap JavaDoc();
90             arguments.put("j_username", CmsPropertyHandler.getAnonymousUser());
91             arguments.put("j_password", CmsPropertyHandler.getAnonymousPassword());
92             
93             principal = ExtranetController.getController().getAuthenticatedPrincipal(dbWrapper.getDatabase(), arguments);
94             
95             if(principal != null)
96                 CacheController.cacheObject("userCache", "anonymous", principal);
97         }
98
99         FakeHttpSession fakeHttpServletSession = new FakeHttpSession();
100         FakeHttpServletResponse fakeHttpServletResponse = new FakeHttpServletResponse();
101         FakeHttpServletRequest fakeHttpServletRequest = new FakeHttpServletRequest();
102         fakeHttpServletRequest.setParameter("siteNodeId", "" + siteNodeId);
103         fakeHttpServletRequest.setParameter("languageId", "" + languageId);
104         fakeHttpServletRequest.setParameter("contentId", "" + contentId);
105         fakeHttpServletRequest.setRequestURI("ViewPage.action");
106
107         fakeHttpServletRequest.setAttribute("siteNodeId", "" + siteNodeId);
108         fakeHttpServletRequest.setAttribute("languageId", "" + languageId);
109         fakeHttpServletRequest.setAttribute("contentId", "" + contentId);
110
111         fakeHttpServletRequest.setServletContext(DeliverContextListener.getServletContext());
112         
113         BrowserBean browserBean = new BrowserBean();
114         //this.browserBean.setRequest(getRequest());
115

116         NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId);
117         IntegrationDeliveryController integrationDeliveryController = IntegrationDeliveryController.getIntegrationDeliveryController(siteNodeId, languageId, contentId);
118         TemplateController templateController = getTemplateController(dbWrapper, siteNodeId, languageId, contentId, new FakeHttpServletRequest(), (InfoGluePrincipal)principal, false, browserBean, nodeDeliveryController, integrationDeliveryController);
119         
120         DeliveryContext deliveryContext = DeliveryContext.getDeliveryContext(/*(InfoGluePrincipal)this.principal*/);
121         //deliveryContext.setRepositoryName(repositoryName);
122
deliveryContext.setSiteNodeId(siteNodeId);
123         deliveryContext.setContentId(contentId);
124         deliveryContext.setLanguageId(languageId);
125         deliveryContext.setPageKey("" + System.currentTimeMillis());
126         deliveryContext.setSession(new Session(fakeHttpServletSession));
127         deliveryContext.setInfoGlueAbstractAction(null);
128         deliveryContext.setHttpServletRequest(fakeHttpServletRequest);
129         deliveryContext.setHttpServletResponse(fakeHttpServletResponse);
130
131         templateController.setDeliveryContext(deliveryContext);
132         
133         //We don't want a page cache entry to be created
134
deliveryContext.setDisablePageCache(true);
135
136         Integer JavaDoc rootMetaInfoContentId = templateController.getMetaInformationContentId(siteNodeId);
137         logger.info("rootMetaInfoContentId:" + rootMetaInfoContentId);
138         
139         recurseSiteNodeTree(siteNodeId, languageId, templateController);
140
141         Integer JavaDoc topContentId = null;
142         ContentVO contentVO = templateController.getContent(rootMetaInfoContentId);
143         logger.info("contentVO:" + contentVO.getName());
144         ContentVO parentContentVO = templateController.getContent(contentVO.getParentContentId());
145         logger.info("parentContentVO:" + parentContentVO.getName());
146         while(parentContentVO != null)
147         {
148             topContentId = parentContentVO.getContentId();
149
150             if(parentContentVO.getParentContentId() != null)
151                 parentContentVO = templateController.getContent(parentContentVO.getParentContentId());
152             else
153                 parentContentVO = null;
154         }
155         
156         logger.info("topContentId:" + topContentId);
157         
158         if(topContentId != null)
159             recurseContentTree(topContentId, languageId, templateController);
160
161     }
162     
163     
164     private void recurseContentTree(Integer JavaDoc contentId, Integer JavaDoc languageId, TemplateController templateController) throws Exception JavaDoc
165     {
166         Content content = ContentController.getContentController().getReadOnlyContentWithId(contentId, templateController.getDatabase());
167         ContentVO contentVO = templateController.getContent(contentId);
168         
169         Collection JavaDoc childContents = content.getChildren();
170         
171         Iterator JavaDoc childContentsIterator = childContents.iterator();
172         while(childContentsIterator.hasNext())
173         {
174             Content childContent = (Content)childContentsIterator.next();
175             recurseContentTree(childContent.getId(), languageId, templateController);
176             
177             logger.info("Before read title of content...");
178             templateController.getContentAttribute(childContent.getId(), languageId, "Title", true);
179             templateController.getContentAttribute(childContent.getId(), languageId, "NavigationTitle", true);
180             //templateController.getContentAttribute(childContent.getId(), languageId, "LeadIn", true);
181
//templateController.getContentAttribute(childContent.getId(), languageId, "FullText", true);
182
logger.info("Read title of content...");
183         }
184         //Thread.sleep(100);
185
}
186     
187     private void recurseSiteNodeTree(Integer JavaDoc siteNodeId, Integer JavaDoc languageId, TemplateController templateController) throws Exception JavaDoc
188     {
189         SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(siteNodeId, templateController.getDatabase(), true);
190         SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId);
191         Collection JavaDoc childSiteNodes = siteNode.getChildSiteNodes();
192         
193         Iterator JavaDoc childSiteNodesIterator = childSiteNodes.iterator();
194         while(childSiteNodesIterator.hasNext())
195         {
196             SiteNode childSiteNode = (SiteNode)childSiteNodesIterator.next();
197             recurseSiteNodeTree(childSiteNode.getSiteNodeId(), languageId, templateController);
198             
199             Integer JavaDoc metaInfoContentId = templateController.getMetaInformationContentId(childSiteNode.getSiteNodeId());
200             //System.out.println("childSiteNode:" + childSiteNode.getName());
201
//System.out.println("metaInfoContentId:" + metaInfoContentId);
202
templateController.getContentAttribute(metaInfoContentId, languageId, "ComponentStructure", true);
203         }
204     }
205
206     
207     /**
208      * This method should be much more sophisticated later and include a check to see if there is a
209      * digital asset uploaded which is more specialized and can be used to act as serverside logic to the template.
210      * The method also consideres wheter or not to invoke the preview-version with administrative functioality or the
211      * normal site-delivery version.
212      */

213     
214     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
215     {
216         TemplateController templateController = new BasicTemplateController(dbWrapper, infoGluePrincipal);
217         templateController.setStandardRequestParameters(siteNodeId, languageId, contentId);
218         templateController.setHttpRequest(request);
219         templateController.setBrowserBean(browserBean);
220         templateController.setDeliveryControllers(nodeDeliveryController, null, integrationDeliveryController);
221         
222         return templateController;
223     }
224
225 }
Popular Tags