KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > core > Core_Engine


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 // AK 14.12.2000
14
// AK 19.12.2000 use the EngineRenderer class
15
// AK 04.01.2001 change EngineRenderer call
16

17 package org.jahia.engines.core;
18
19 import java.io.File JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.jahia.content.ContentContainerListsXRefManager;
27 import org.jahia.content.ContentFieldXRefManager;
28 import org.jahia.content.ObjectKey;
29 import org.jahia.data.JahiaData;
30 import org.jahia.data.events.JahiaEvent;
31 import org.jahia.engines.EngineRenderer;
32 import org.jahia.engines.JahiaEngine;
33 import org.jahia.exceptions.JahiaException;
34 import org.jahia.exceptions.JahiaForbiddenAccessException;
35 import org.jahia.exceptions.JahiaPageNotFoundException;
36 import org.jahia.params.ParamBean;
37 import org.jahia.registries.ServicesRegistry;
38 import org.jahia.services.pages.JahiaPageDefinition;
39
40
41 public class Core_Engine implements JahiaEngine {
42
43     private static final org.apache.log4j.Logger logger =
44             org.apache.log4j.Logger.getLogger (Core_Engine.class);
45
46     private static Core_Engine instance = null;
47
48     /** The engine's name */
49     public static final String JavaDoc ENGINE_NAME = "core";
50
51
52     /**
53      * constructor AK 14.12.2000
54      */

55     private Core_Engine () {
56         logger.debug ("***** Starting " + Core_Engine.class.getName () + " engine *****");
57     }
58
59
60     /**
61      * getInstance AK 14.12.2000
62      */

63     public static synchronized Core_Engine getInstance () {
64         if (instance == null) {
65             instance = new Core_Engine ();
66         }
67         return instance;
68     }
69
70
71     /**
72      * authoriseRender AK 14.12.2000
73      */

74     public boolean authoriseRender (ParamBean jParams) {
75         return (jParams.getOperationMode () == ParamBean.EDIT);
76     }
77
78
79     /**
80      * needsJahiaData AK 14.12.2000
81      */

82     public boolean needsJahiaData (ParamBean jParams) {
83         return true;
84     }
85
86
87     /**
88      * renderLink AK 14.12.2000
89      */

90     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj)
91             throws JahiaException {
92         String JavaDoc params = EMPTY_STRING;
93 // params += "&op=" + jParams.getOperationMode();
94
// params += "&pid=" + jParams.getPageID();
95
params += "/op/" + jParams.getOperationMode ();
96         params += "/pid/" + jParams.getPageID ();
97         return jParams.composeEngineUrl (ENGINE_NAME, params);
98
99     }
100
101
102     /**
103      * handleActions AK 14.12.2000 AK 04.01.2001 use processCore()...
104      */

105     public void handleActions (ParamBean jParams, JahiaData jData)
106             throws JahiaException {
107         String JavaDoc ipAddr = jParams.getRequest ().getRemoteAddr ();
108         logger.debug ("Generating content for " + ipAddr + "...");
109         processCore (jData);
110         postProcessCore (jData);
111     }
112
113     /**
114      * Retrieve the engine name.
115      *
116      * @return the engine name.
117      */

118     public final String JavaDoc getName () {
119         return ENGINE_NAME;
120     }
121
122
123     /**
124      * Does the actual dispatching to the template, displaying all the objects that are
125      * accessible through the JahiaData facade object.
126      *
127      * @param jData the JahiaData object containing all the context data for the current
128      * request.
129      *
130      * @throws JahiaException can mean a lot of things, from errors in communicating with the
131      * database, to errors in the templates, etc...
132      */

133     private void processCore (JahiaData jData)
134             throws JahiaException {
135
136         if (jData.params ().getPage () == null) {
137             if (!jData.params().getContentPage().checkReadAccess(jData.params().
138                 getUser())) {
139                 throw new JahiaForbiddenAccessException();
140             }
141         }
142
143         if (jData.params ().getPage () == null
144             || jData.params ().getPage ().getPageTemplate () == null) {
145             // this can happen in the page doesn't exist in this operation mode
146
// in a certain language.
147
throw new JahiaPageNotFoundException (jData.params ().getPageID (),
148                     jData.params ().getLocale ().toString (),
149                     jData.params ().getOperationMode ());
150
151         }
152
153         if (jData.params().settings().isPageCounterIncrementActivated()) {
154             // increment for each corerequest
155
if (!(jData.params ().getEntryLoadRequest ().isVersioned () ||
156                     jData.params ().getEntryLoadRequest ().isDeleted ())
157                     && (jData.params ().newPageRequest ()
158                     || (jData.params ().getLastEngineName () == null)
159                     || (jData.params ().getLastEngineName ().equals (ENGINE_NAME)))) {
160                 jData.params ().getPage ().incrementCounter ();
161                 jData.params ().getPage ().commitChanges (false);
162             }
163         }
164
165         JahiaEvent theEvent = new JahiaEvent(this, jData.params(), jData.params ().getPage ());
166         ServicesRegistry.getInstance().getJahiaEventService().
167             fireLoadPage(theEvent);
168
169         // compose the fileName...
170
String JavaDoc fileName = resolveJSPFullFileName(jData);
171
172         // compose a new hashmap with engine properties...
173
HashMap JavaDoc engineHashMap = new HashMap JavaDoc ();
174         engineHashMap.put (ENGINE_NAME_PARAM, ENGINE_NAME);
175         engineHashMap.put (ENGINE_OUTPUT_FILE_PARAM, fileName);
176         engineHashMap.put (RENDER_TYPE_PARAM, new Integer JavaDoc (JahiaEngine.RENDERTYPE_FORWARD));
177
178         EngineRenderer.getInstance ().render (jData, engineHashMap);
179     }
180
181     private String JavaDoc resolveJSPFullFileName (JahiaData jData) {
182         String JavaDoc jspFullFileName = null;
183
184         JahiaPageDefinition template = jData.params ().getPage ().getPageTemplate ();
185         if (template != null) {
186             jspFullFileName = template.getSourcePath ();
187         }
188
189         // now let's check for template override parameter
190
if (jData.params ().getParameter (ParamBean.TEMPLATE_PARAMETER) != null) {
191             // we must test the path to make sure it doesn't have any ../
192
// sequences which are VERY dangerous.
193

194             String JavaDoc pathToAlternateTemplate = jData.params ().getParameter (ParamBean.TEMPLATE_PARAMETER);
195             if (pathToAlternateTemplate.indexOf("..") == -1) {
196                 logger.debug ("template source path :" + jspFullFileName);
197
198                 // the code below is quite powerful, it allows us to set an
199
// extension on the name of the template that will specify
200
// the mime type we want to use. For example simple.xml will
201
// use the mime type (text/xml) (if mapped properly in the
202
// web.xml configuration file or in the server config), and
203
// then the actual dispatching will be done to simple.jsp
204
String JavaDoc justFileName = pathToAlternateTemplate;
205                 String JavaDoc justPath = "";
206                 int altLastSlashPos = pathToAlternateTemplate.lastIndexOf("/");
207                 if (altLastSlashPos != -1) {
208                     justFileName = pathToAlternateTemplate.substring(altLastSlashPos + 1);
209                     justPath = pathToAlternateTemplate.substring(0, altLastSlashPos + 1);
210                 }
211                 String JavaDoc mimeType = jData.params().getContext().getMimeType(justFileName);
212                 if (mimeType != null) {
213                     // we found a mime type, let's use it.
214
logger.debug("Using mime type " + mimeType);
215                     jData.params().setResponseMimeType(mimeType);
216                 }
217                 int extensionPos = justFileName.lastIndexOf(".");
218                 if (extensionPos != -1) {
219                     String JavaDoc extension = justFileName.substring(extensionPos);
220                     if (!extension.equalsIgnoreCase(".jsp")) {
221                         justFileName = justFileName.substring(0, extensionPos) + ".jsp";
222                     }
223                 }
224                 pathToAlternateTemplate = justPath + justFileName;
225
226                 int lastSlashPos = jspFullFileName.lastIndexOf ("/");
227                 if (lastSlashPos != -1) {
228                     jspFullFileName = jspFullFileName.substring(0, lastSlashPos + 1) + pathToAlternateTemplate;
229                 }
230                 logger.debug ("resolvedJSPFullFileName :" + jspFullFileName);
231
232                 File JavaDoc jspFile = new File JavaDoc (jData.params().getContext ().getRealPath (jspFullFileName));
233                 if (!jspFile.exists ()) {
234                     jspFullFileName = "/jsp/test/" + jData.params ().getParameter (ParamBean.TEMPLATE_PARAMETER);
235                 }
236                 logger.debug ("Overriding page template with URL template : " + jspFullFileName);
237
238                 // since we overrided the template, we deactivate the cache for
239
// the page.
240
// expires the cache immediately, not even storing it.
241
jData.params().setCacheExpirationDate(new Date JavaDoc());
242             }
243         }
244
245         return jspFullFileName;
246     }
247
248
249     /**
250      * General post-process method. Add here any code that should be executed after dispatching
251      * to the template
252      *
253      * @param jData the current request JahiaData object
254      *
255      * @throws JahiaException thrown in case there was a problem communicating with the
256      * database.
257      */

258     private void postProcessCore (JahiaData jData)
259             throws JahiaException {
260         handleAbsoluteReferencesChanges (jData);
261     }
262
263     /**
264      * This method collects all the references stored during the request in the JahiaFieldSet
265      * and JahiaContainerSet objects and handles the differences with the references that are
266      * stored in the database. If new references are added they are also added to the database
267      * and if references have been removed, they are removed (duh !:))
268      *
269      * @param jData the JahiaData object containing the JahiaFieldSet and JahiaContainerSet
270      * objects to use for the difference, as well as the reference to the ParamBean
271      * object used to know the current page.
272      *
273      * @throws JahiaException thrown if there were problems while communicating with the
274      * database.
275      */

276     private void handleAbsoluteReferencesChanges (JahiaData jData)
277             throws JahiaException {
278         // first let's retrieve the accesses maps from the JahiaData structure
279
Set JavaDoc absoluteFieldAccesses = jData.fields ().getAbsoluteFieldAccesses ();
280         Set JavaDoc absoluteContainerListAccesses = jData.containers ()
281                 .getAbsoluteContainerListAccesses ();
282
283         // now we must compare it to the database store to see if there are
284
// differences, and if so update the database...
285

286         Set JavaDoc fieldKeys = ContentFieldXRefManager.getInstance ().getAbsoluteFieldsFromPageID (
287                 jData.params ().getPageID ());
288         Set JavaDoc containerListKeys = ContentContainerListsXRefManager.getInstance ()
289                 .getAbsoluteContainerListsFromPageID (jData.params ().getPageID ());
290
291         // first let's convert the keys into IDs.
292
Set JavaDoc databaseAbsoluteFieldIDs = new HashSet JavaDoc ();
293         Iterator JavaDoc fieldKeyIter = fieldKeys.iterator ();
294         while (fieldKeyIter.hasNext ()) {
295             ObjectKey curKey = (ObjectKey) fieldKeyIter.next ();
296             logger.debug ("Converting databaseAbsoluteFieldKey " + curKey);
297             databaseAbsoluteFieldIDs.add (new Integer JavaDoc (curKey.getIdInType ()));
298         }
299         Set JavaDoc databaseAbsoluteContainerListIDs = new HashSet JavaDoc ();
300         Iterator JavaDoc containerListKeyIter = containerListKeys.iterator ();
301         while (containerListKeyIter.hasNext ()) {
302             ObjectKey curKey = (ObjectKey) containerListKeyIter.next ();
303             logger.debug ("Converting databaseAbsoluteContainerListKey " + curKey);
304             databaseAbsoluteContainerListIDs.add (new Integer JavaDoc (curKey.getIdInType ()));
305         }
306
307         // okay now we have two sets of IDs that we can compare.
308
Set JavaDoc addedFieldIDs = new HashSet JavaDoc (absoluteFieldAccesses);
309         addedFieldIDs.removeAll (databaseAbsoluteFieldIDs);
310         Set JavaDoc removedFieldIDs = new HashSet JavaDoc (databaseAbsoluteFieldIDs);
311         removedFieldIDs.removeAll (absoluteFieldAccesses);
312
313         Set JavaDoc addedContainerListIDs = new HashSet JavaDoc (absoluteContainerListAccesses);
314         addedContainerListIDs.removeAll (databaseAbsoluteContainerListIDs);
315         Set JavaDoc removedContainerListIDs = new HashSet JavaDoc (databaseAbsoluteContainerListIDs);
316         removedContainerListIDs.removeAll (absoluteContainerListAccesses);
317
318         // we know have the differences, we can process them...
319
Iterator JavaDoc addedFieldIDIter = addedFieldIDs.iterator ();
320         while (addedFieldIDIter.hasNext ()) {
321             Integer JavaDoc curFieldID = (Integer JavaDoc) addedFieldIDIter.next ();
322             logger.debug (
323                     "Adding reference from page ID " + jData.params ().getPageID () +
324                     " to field " +
325                     curFieldID);
326             ContentFieldXRefManager.getInstance ().setAbsoluteFieldPageID (
327                     curFieldID.intValue (),
328                     jData.params ().getPageID ());
329         }
330         Iterator JavaDoc removedFieldIDIter = removedFieldIDs.iterator ();
331         while (removedFieldIDIter.hasNext ()) {
332             Integer JavaDoc curFieldID = (Integer JavaDoc) removedFieldIDIter.next ();
333             logger.debug (
334                     "Removing reference from page ID " + jData.params ().getPageID () +
335                     " to field " +
336                     curFieldID);
337             ContentFieldXRefManager.getInstance ().removeAbsoluteFieldPageID (
338                     curFieldID.intValue (), jData.params ().getPageID ());
339         }
340
341         Iterator JavaDoc addedContainerListIDIter = addedContainerListIDs.iterator ();
342         while (addedContainerListIDIter.hasNext ()) {
343             Integer JavaDoc curContainerListID = (Integer JavaDoc) addedContainerListIDIter.next ();
344             logger.debug (
345                     "Adding reference from page ID " + jData.params ().getPageID () +
346                     " to container list " +
347                     curContainerListID);
348             ContentContainerListsXRefManager.getInstance ().setAbsoluteContainerListPageID (
349                     curContainerListID.intValue (), jData.params ().getPageID ());
350         }
351         Iterator JavaDoc removedContainerListIDIter = removedContainerListIDs.iterator ();
352         while (removedContainerListIDIter.hasNext ()) {
353             Integer JavaDoc curContainerListID = (Integer JavaDoc) removedContainerListIDIter.next ();
354             logger.debug (
355                     "Removing reference from page ID " + jData.params ().getPageID () +
356                     " to container list " +
357                     curContainerListID);
358             ContentContainerListsXRefManager.getInstance ().removeAbsoluteContainerListPageID (
359                     curContainerListID.intValue (), jData.params ().getPageID ());
360         }
361     }
362
363 }
364
Popular Tags