KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > lock > LockPrerequisites


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 package org.jahia.services.lock;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.jahia.content.ContentObject;
21 import org.jahia.content.JahiaObject;
22 import org.jahia.content.ObjectKey;
23 import org.jahia.data.fields.FieldTypes;
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.registries.ServicesRegistry;
26 import org.jahia.services.cache.Cache;
27 import org.jahia.services.containers.ContainerFactoryProxy;
28 import org.jahia.services.containers.ContentContainer;
29 import org.jahia.services.containers.ContentContainerList;
30 import org.jahia.services.fields.ContentField;
31 import org.jahia.services.pages.ContentPage;
32 import org.jahia.services.usermanager.JahiaUser;
33 import org.jahia.services.version.ContentObjectEntryState;
34 import org.jahia.services.version.EntryLoadRequest;
35
36 /**
37  * <p>Title: Jahia locking system implementation.</p>
38  * <p>Description:
39  * Define some methods verifying if locks can be acquirable in given contexts.
40  * </p>
41  * <p>Copyright: MAP (Jahia Solutions S�rl 2003)</p>
42  * <p>Company: Jahia Solutions S�rl</p>
43  * @author MAP
44  * @version 1.0
45  */

46 public class LockPrerequisites {
47
48     /**
49      * Define if a lock can be acquirable in a con
50      *
51      * @param lockKey The lock key identifying the lock.
52      * @param owner The lock owner
53      * @param lockID The lock ID
54      * @param justTesting true if we just want to test if the lock is acquireable
55      * in order to report current possible actions, or false if we really want
56      * to test this in an acquirement context. The main difference is that when
57      * in real acquirement mode, locks will be released if we are in the same
58      * context (ie the same user), and when justTesting=true this will not be
59      * done.
60      * @return True if the lock is acquirable, false otherwise.
61      *
62      * @todo Yes, I know that the succession of "if else if else" is very
63      * disgracious ! So there would be better to put these lock verification
64      * sequences in separate classes; one for each test.
65      */

66     public boolean isLockAcquirable(LockKey lockKey, JahiaUser owner,
67             String JavaDoc lockID, boolean justTesting) {
68         LockService lockRegistry = ServicesRegistry.getInstance().getLockService();
69         // Is the lock already acquired in the same context ? ...
70
if (lockRegistry.isAlreadyAcquiredInContext(lockKey, owner, lockID)) {
71             resetPrerequisite(lockKey); // Clear results from a previous check
72
return true;
73         // ... or is it already acquired from another context ?
74
} else if (lockRegistry.isAlreadyAcquired(lockKey)) {
75             // In this case the lock is not acquirable. But it can be stolen, if
76
// user has admin access !
77
resetPrerequisite(lockKey);
78             LockPrerequisitesResult lpr = new LockPrerequisitesResult(lockKey);
79             lpr.put(lockKey);
80             synchronized (this) {
81                 lockPrerequisitesResultMap.put(lockKey, lpr);
82                 lockAlreadyAcquiredMap.put(lockKey, new Boolean JavaDoc(true));
83             }
84             return false;
85         }
86         // Let's look about the prerequisites.
87
resetPrerequisite(lockKey);
88         LockPrerequisitesResult results = new LockPrerequisitesResult(lockKey);
89
90         // Lock for workflow engine.
91
if (LockKey.WORKFLOW_TYPE.equals(lockKey.getType())) {
92             // first let's check if the page properties are being edited.
93
verifyLockForParentPage(lockKey, owner, lockKey.getPageID(), justTesting, results);
94             // now let's check to see if the page objects are being edited.
95
verifyLockForPageChilds(lockKey, owner, justTesting, results);
96             lockPrerequisitesResultMap.put(lockKey, results);
97         }
98         // Lock for add container engine.
99
else if (LockKey.ADD_CONTAINER_TYPE.equals(lockKey.getType())) {
100             ContentPage parentPage = getParentPage(lockKey);
101             if (parentPage != null) {
102                 verifyLockForWorkflow(lockKey, owner, parentPage, justTesting, results);
103                 verifyLockForParentPage(lockKey, owner, parentPage.getID(), justTesting, results);
104             }
105             int containerListID = lockKey.getId();
106             if (containerListID != 0) {
107                 verifyLockForParentContainerList(containerListID, lockKey.getPageID(), owner, justTesting, results);
108             }
109             lockPrerequisitesResultMap.put(lockKey, results);
110         }
111         // Lock for update container engine.
112
else if (LockKey.UPDATE_CONTAINER_TYPE.equals(lockKey.getType())) {
113             ContentPage parentPage = getParentPage(lockKey);
114             if (parentPage != null) {
115                 verifyLockForWorkflow(lockKey, owner, parentPage, justTesting, results);
116                 verifyLockForParentPage(lockKey, owner, parentPage.getID(), justTesting, results);
117             }
118             try {
119                 ContentContainer contentContainer = ContentContainer.getContainer(lockKey.getId());
120                 verifyLockForParentContainerList(contentContainer.getParentContainerListID(), contentContainer.getPageID(), owner, justTesting, results);
121                 verifyLockForDeleteContainer(contentContainer.getID(), contentContainer.getPageID(), owner, justTesting, results);
122             } catch (JahiaException je) {
123                 logger.warn("Cannot get the content container ID " + lockKey.getId(), je);
124             }
125             verifyLockForContainerChildPages(lockKey, owner, justTesting, results);
126             // the following check is currently deactivated because we don't do
127
// the test when editing a page field whether there are any containers
128
// that are being updated.
129
// verifyLockForThisParentPageField(lockKey, owner, justTesting, results);
130
lockPrerequisitesResultMap.put(lockKey, results);
131         }
132         // Lock for delete container engine
133
else if (LockKey.DELETE_CONTAINER_TYPE.equals(lockKey.getType())) {
134             ContentPage parentPage = getParentPage(lockKey);
135             if (parentPage != null) {
136                 verifyLockForWorkflow(lockKey, owner, parentPage, justTesting, results);
137                 verifyLockForParentPage(lockKey, owner, parentPage.getID(), justTesting, results);
138             }
139             int parentContainerListID = -1;
140             ContentContainerList parentContainerList = null;
141             try {
142                 ContentContainer contentContainer = ContentContainer.getContainer(lockKey.getId()) ;
143                 parentContainerListID = contentContainer.getParentContainerListID();
144                 parentContainerList = ContentContainerList.getContainerList(parentContainerListID);
145                 verifyLockForContainerChilds(contentContainer, owner, justTesting, results, true);
146             } catch (JahiaException je) {
147                 logger.warn("Cannot get the content container ID " + lockKey.getId(), je);
148             }
149             if (parentContainerListID > 0) {
150                 verifyLockForParentContainerList(parentContainerListID, parentContainerList.getPageID(), owner,
151                                                  justTesting, results);
152             }
153             verifyLockForUpdateContainer(lockKey.getId(), lockKey.getPageID(), owner, justTesting, results);
154             lockPrerequisitesResultMap.put(lockKey, results);
155         }
156         // Lock for update field engine.
157
else if (LockKey.UPDATE_FIELD_TYPE.equals(lockKey.getType())) {
158             ContentPage parentPage = getParentPage(lockKey);
159             if (parentPage != null) {
160                 verifyLockForWorkflow(lockKey, owner, parentPage, justTesting, results);
161                 verifyLockForParentPage(lockKey, owner, parentPage.getID(), justTesting, results);
162             }
163             verifyLockForFieldChildPages(lockKey, owner, justTesting, results);
164             lockPrerequisitesResultMap.put(lockKey, results);
165         }
166         // Lock for update container list engine.
167
else if (LockKey.UPDATE_CONTAINERLIST_TYPE.equals(lockKey.getType())) {
168             ContentPage parentPage = getParentPage(lockKey);
169             if (parentPage != null) {
170                 verifyLockForWorkflow(lockKey, owner, parentPage, justTesting, results);
171                 verifyLockForParentPage(lockKey, owner, parentPage.getID(), justTesting, results);
172             }
173             verifyLockForAddContainer(lockKey.getId(), lockKey.getPageID(), owner, justTesting, results);
174             try {
175                 if (lockKey.getId() != 0) {
176                     ContentContainerList containerList = ContentContainerList.
177                         getContainerList(lockKey.getId());
178                     verifyLockForContainerListChilds(containerList, owner, justTesting, results);
179                 }
180             } catch (JahiaException je) {
181                 logger.warn("Cannot get the container list ID " + lockKey.getId(), je);
182             }
183             lockPrerequisitesResultMap.put(lockKey, results);
184         }
185         // Lock for update page engine.
186
else if (LockKey.UPDATE_PAGE_TYPE.equals(lockKey.getType())) {
187             ContentPage parentPage = getParentPage(lockKey);
188             if (parentPage != null) {
189                 verifyLockForWorkflow(lockKey, owner, parentPage, justTesting, results);
190                 verifyLockForParentPage(lockKey, owner, parentPage.getID(), justTesting, results);
191             }
192             verifyLockForPageChilds(lockKey, owner, justTesting, results);
193             lockPrerequisitesResultMap.put(lockKey, results);
194         }
195         if (results.size() > 0) {
196             return false;
197         }
198         return true;
199     }
200
201     public LockPrerequisitesResult getLockPrerequisitesResult(LockKey lockKey) {
202         LockPrerequisitesResult results = (LockPrerequisitesResult)lockPrerequisitesResultMap.get(lockKey);
203         return results;
204     }
205
206     public boolean isLockAlreadyAcquired(LockKey lockKey) {
207         Boolean JavaDoc isAlreadyAcquired = (Boolean JavaDoc)lockAlreadyAcquiredMap.get(lockKey);
208         return isAlreadyAcquired == null ? false : isAlreadyAcquired.booleanValue();
209     }
210
211     public void flush() {
212         lockAlreadyAcquiredMap.flush();
213         lockPrerequisitesResultMap.flush();
214     }
215
216     public static LockPrerequisites getInstance() {
217         if (lockPrerequisites == null) {
218             lockPrerequisites = new LockPrerequisites();
219         }
220         return lockPrerequisites;
221     }
222
223     protected synchronized void resetPrerequisite(LockKey lockKey) {
224         lockPrerequisitesResultMap.remove(lockKey);
225         lockAlreadyAcquiredMap.remove(lockKey);
226     }
227
228     private void verifyLockForContainerChildPages (LockKey
229         contentContainerLockKey, JahiaUser owner, boolean justTesting,
230         LockPrerequisitesResult results) {
231         // Verify if the container page fields properties are not edited.
232
// The child page type can change.
233
try {
234             ContentContainer contentContainer = ContentContainer.getContainer(contentContainerLockKey.getId());
235             ArrayList JavaDoc containerChilds = contentContainer.getChilds(null, null, ContainerFactoryProxy.LOAD_FIELDS);
236             for (int containerChildIndex = 0; containerChildIndex < containerChilds.size(); containerChildIndex++) {
237                 Object JavaDoc containerChild = containerChilds.get(containerChildIndex);
238                 if (containerChild instanceof ContentField) {
239                     ContentField contentField = (ContentField)containerChild;
240                     if (contentField.getType() == FieldTypes.PAGE) {
241                         ContentObjectEntryState entryState = contentField.getEntryState(EntryLoadRequest.STAGED);
242                         int pageID = -1;
243                         if (entryState != null) {
244                             try {
245                                 pageID = Integer.parseInt(contentField.getValue(
246                                     entryState));
247                             } catch ( Throwable JavaDoc t ){
248                                 // could be <empty>
249
}
250                         }
251                         if (pageID > 0) {
252                             LockKey lockKey = LockKey.composeLockKey(LockKey.UPDATE_PAGE_TYPE, pageID, pageID);
253                             putLockIfNotSameContext(lockKey, owner, justTesting, results);
254
255                             ContentPage curChildPage = ContentPage.getPage(pageID);
256                             verifyLockForWorkflow(lockKey, owner, curChildPage, justTesting, results);
257                         }
258                     }
259                 }
260             }
261         } catch(JahiaException je) {
262             logger.warn("Problem getting content container ID " +
263                         contentContainerLockKey.getId() + " child pages", je);
264         }
265     }
266
267     private void verifyLockForPageChilds (LockKey contentPageLockKey,
268                                           JahiaUser owner, boolean justTesting,
269                                           LockPrerequisitesResult results) {
270         try {
271             ContentPage contentPage = (ContentPage)ContentPage.getInstance(contentPageLockKey.getObjectKey());
272             ArrayList JavaDoc pageChilds = contentPage.getChilds(null, (EntryLoadRequest)null);
273             for (int pageChildIndex = 0; pageChildIndex < pageChilds.size(); pageChildIndex++) {
274                 Object JavaDoc pageChild = pageChilds.get(pageChildIndex);
275                 if (pageChild instanceof ContentField) {
276                     ContentField contentField = (ContentField)pageChild;
277                     if (contentField.getType() != FieldTypes.PAGE) {
278                         LockKey lockKey = LockKey.composeLockKey(LockKey.UPDATE_FIELD_TYPE, contentField.getID(), contentField.getPageID());
279                         putLockIfNotSameContext(lockKey, owner, justTesting, results);
280                     }
281                 } else if (pageChild instanceof ContentContainerList) {
282                     ContentContainerList containerList = (ContentContainerList)pageChild;
283                     LockKey lockKey = LockKey.composeLockKey(LockKey.UPDATE_CONTAINERLIST_TYPE, containerList.getID(), containerList.getPageID());
284                     putLockIfNotSameContext(lockKey, owner, justTesting, results);
285                     lockKey = LockKey.composeLockKey(LockKey.ADD_CONTAINER_TYPE, containerList.getID(), containerList.getPageID());
286                     putLockIfNotSameContext(lockKey, owner, justTesting, results);
287                     verifyLockForContainerListChilds(containerList, owner, justTesting, results);
288                 }
289             }
290         } catch (JahiaException je) {
291             logger.warn("Problem when iterate through page childs to get a lock !", je);
292         } catch (ClassNotFoundException JavaDoc cnfe) {
293             logger.error("Lock key object (" + contentPageLockKey + ") not correct !", cnfe);
294         }
295     }
296
297     private void verifyLockForContainerChilds (ContentContainer container,
298                                                JahiaUser owner,
299                                                boolean justTesting,
300                                                LockPrerequisitesResult results,
301                                                boolean verifySubPages)
302             throws JahiaException {
303         ArrayList JavaDoc containerChilds = container.getChilds(null, null, verifySubPages ? ContainerFactoryProxy.LOAD_FIELD_AND_SUBCONTAINER_LISTS : ContainerFactoryProxy.LOAD_SUBCONTAINER_LISTS);
304         for (int containerChildIndex = 0; containerChildIndex < containerChilds.size(); containerChildIndex++) {
305             Object JavaDoc containerChild = containerChilds.get(containerChildIndex);
306             if (containerChild instanceof ContentField) {
307                 ContentField contentField = (ContentField)containerChild;
308                 // Get page field childs, lock verification on other field
309
// (included in container) is not required
310
if (contentField.getType() == FieldTypes.PAGE) {
311                     ContentObjectEntryState entryState = contentField.getEntryState(EntryLoadRequest.STAGED);
312                     int childPageID = -1;
313                     if (entryState != null) {
314                         try {
315                             childPageID = Integer.parseInt(contentField.getValue(entryState));
316                         } catch ( Throwable JavaDoc t ){
317                             // could be <empty>
318
}
319                     }
320                     if (childPageID > 0) {
321                         LockKey lockKey = LockKey.composeLockKey(LockKey.UPDATE_PAGE_TYPE, childPageID, childPageID);
322                         putLockIfNotSameContext(lockKey, owner, justTesting, results);
323
324                         ContentPage curChildPage = ContentPage.getPage(childPageID);
325                         verifyLockForWorkflow(lockKey, owner, curChildPage, justTesting, results);
326
327                         verifyLockForPageChilds(lockKey, owner, justTesting, results);
328                     }
329                 }
330             } else if (containerChild instanceof ContentContainerList) {
331                 ContentContainerList containerList = (ContentContainerList)containerChild;
332                 LockKey lockKey = LockKey.composeLockKey(LockKey.UPDATE_CONTAINERLIST_TYPE, containerList.getID(), containerList.getPageID());
333                 putLockIfNotSameContext(lockKey, owner, justTesting, results);
334                 verifyLockForContainerListChilds(containerList, owner, justTesting, results);
335             }
336         }
337     }
338
339     private void verifyLockForWorkflow (LockKey lockKey, JahiaUser owner,
340                                         ContentPage parentPage,
341                                         boolean justTesting,
342                                         LockPrerequisitesResult results) {
343         LockKey workflowLockKey = LockKey.composeLockKey(LockKey.WORKFLOW_TYPE, parentPage.getID(), parentPage.getID());
344         putLockIfNotSameContext(workflowLockKey, owner, justTesting, results);
345         // Verify if the parent page in the specified language is not in waiting
346
// for approval mode.
347
Map JavaDoc languagesStates = parentPage.getLanguagesStates(true);
348         Iterator JavaDoc languageIt = languagesStates.values().iterator();
349         boolean hasPageAWaitingState = false;
350         while (languageIt.hasNext()) {
351             Integer JavaDoc languageState = (Integer JavaDoc)languageIt.next();
352             if (languageState.intValue() == EntryLoadRequest.WAITING_WORKFLOW_STATE) {
353                 hasPageAWaitingState = true;
354                 break;
355             }
356         }
357         if (hasPageAWaitingState) {
358             workflowLockKey = LockKey.composeLockKey(LockKey.WAITING_FOR_APPROVAL_TYPE, parentPage.getID(), parentPage.getID());
359             results.put(workflowLockKey);
360         }
361     }
362
363     private void verifyLockForParentPage(LockKey lockKey, JahiaUser owner, int pageID, boolean justTesting, LockPrerequisitesResult results) {
364         // Verify if the parent page properties are not edited. Template can change.
365
// This test is debatable, perhaps should we refine it in the page properties
366
// engine and let the user modify the page title only.
367
lockKey = LockKey.composeLockKey(LockKey.UPDATE_PAGE_TYPE, pageID, pageID);
368         putLockIfNotSameContext(lockKey, owner, justTesting, results);
369     }
370
371     private void verifyLockForParentContainerList(int containerListID, int pageID, JahiaUser owner, boolean justTesting, LockPrerequisitesResult results) {
372         // Verify if the parent container list is not edited.
373
LockKey updateContainerListLockKey = LockKey.composeLockKey(LockKey.UPDATE_CONTAINERLIST_TYPE, containerListID, pageID);
374         putLockIfNotSameContext(updateContainerListLockKey, owner, justTesting, results);
375     }
376
377     private void verifyLockForDeleteContainer(int containerID, int pageID, JahiaUser owner, boolean justTesting, LockPrerequisitesResult results) {
378          // Verify if the content container is not in deletion mode.
379
LockKey deleteContainerLockKey = LockKey.composeLockKey(LockKey.DELETE_CONTAINER_TYPE, containerID, pageID);
380         putLockIfNotSameContext(deleteContainerLockKey, owner, justTesting, results);
381     }
382
383     private void verifyLockForThisParentPageField(LockKey lockKey, JahiaUser owner, boolean justTesting, LockPrerequisitesResult results) {
384         // Verify if this parent page field is not edited (parent page type can change).
385
// Get only the direct parent field, not all the parent list.
386
// Je vous rassure tout de suite meme moi je ne comprend pas ce que j'ecris.
387
try {
388             LockKey updateChildLockKey;
389             ContentField parentField = (ContentField)getParentPage(lockKey).getParent(null, null, null);
390             if (parentField != null) { // If null then it is the site home page.
391
ContentObject parentObject = parentField.getParent(null, null, null);
392                 if (parentObject instanceof ContentContainer) {
393                     ContentContainer parentContainer = (ContentContainer) parentObject;
394                     updateChildLockKey = LockKey.composeLockKey(LockKey.UPDATE_CONTAINER_TYPE, parentObject.getID(), parentContainer.getPageID());
395                     putLockIfNotSameContext(updateChildLockKey, owner, justTesting, results);
396                 } else if (parentObject instanceof ContentPage) {
397                     updateChildLockKey = LockKey.composeLockKey(LockKey.UPDATE_FIELD_TYPE, parentField.getID(), parentField.getPageID());
398                     putLockIfNotSameContext(updateChildLockKey, owner, justTesting, results);
399                 }
400             }
401         } catch (JahiaException je) {
402             logger.warn("Cannot get the parent page field or the parent container of parent page field.", je);
403         }
404     }
405
406     private void verifyLockForFieldChildPages(LockKey lockKey, JahiaUser owner, boolean justTesting, LockPrerequisitesResult results) {
407         // Verify if the child page properties is not edited.
408
// This page field type can change.
409
try {
410             ContentField contentField = ContentField.getField(lockKey.getId());
411             if (contentField.getType() == FieldTypes.PAGE) {
412                 ContentObjectEntryState entryState = contentField.getEntryState(EntryLoadRequest.STAGED);
413                 int pageID = -1;
414                 if (entryState != null) {
415                     try {
416                         pageID = Integer.parseInt(contentField.getValue(
417                             entryState));
418                     } catch ( Throwable JavaDoc t ){
419                         // could be <empty>
420
}
421                 }
422                 if (pageID > 0) {
423                     LockKey updatePageLockKey = LockKey.composeLockKey(LockKey.UPDATE_PAGE_TYPE, pageID, pageID);
424                     putLockIfNotSameContext(updatePageLockKey, owner, justTesting, results);
425                 }
426             }
427         } catch (JahiaException je) {
428             logger.warn("Cannot get the content page field (ID " + lockKey.getId() + ")", je);
429         }
430     }
431
432     private void verifyLockForUpdateContainer(int containerID, int pageID, JahiaUser owner, boolean justTesting, LockPrerequisitesResult results) {
433         LockKey updateContainerLockKey = LockKey.composeLockKey(LockKey.UPDATE_CONTAINER_TYPE, containerID, pageID);
434         putLockIfNotSameContext(updateContainerLockKey, owner, justTesting, results);
435     }
436
437     private void verifyLockForAddContainer(int containerListID, int pageID, JahiaUser owner, boolean justTesting, LockPrerequisitesResult results) {
438         // Verify if a container is not added to the container list.
439
LockKey addContainerLockKey = LockKey.composeLockKey(LockKey.ADD_CONTAINER_TYPE, containerListID, pageID);
440         putLockIfNotSameContext(addContainerLockKey, owner, justTesting, results);
441     }
442
443     private void verifyLockForContainerListChilds(ContentContainerList containerList, JahiaUser owner, boolean justTesting, LockPrerequisitesResult results) {
444         // Verify if any content container is not edited or in deletion mode.
445
try {
446             ArrayList JavaDoc contentContainers = ContentContainerList.getContainerList(containerList.getID()).getChilds(null, null);
447             for (int contentContainerIndex = 0; contentContainerIndex < contentContainers.size(); contentContainerIndex++) {
448                 ContentContainer contentContainer = (ContentContainer)contentContainers.get(contentContainerIndex);
449                 LockKey deleteContainerLockKey = LockKey.composeLockKey(LockKey.DELETE_CONTAINER_TYPE, contentContainer.getID(), contentContainer.getPageID());
450                 putLockIfNotSameContext(deleteContainerLockKey, owner, justTesting, results);
451                 LockKey updateContainerLockKey = LockKey.composeLockKey(LockKey.UPDATE_CONTAINER_TYPE, contentContainer.getID(), contentContainer.getPageID());
452                 putLockIfNotSameContext(updateContainerLockKey, owner, justTesting, results);
453                 // Whithout child page. The container already has the lock.
454
verifyLockForContainerChilds(contentContainer, owner, justTesting, results, false);
455             }
456         } catch (JahiaException je) {
457             logger.warn("Cannot get any containers childs (ID " + containerList.getID() + ")", je);
458         }
459     }
460
461     private ContentPage getParentPage(LockKey lockKey) {
462         int parentPageID = -1;
463         ObjectKey objectKey = lockKey.getObjectKey();
464         if (lockKey.getPageID() != -1) {
465             parentPageID = lockKey.getPageID();
466         } else {
467             int idInType = objectKey.getIdInType();
468
469             if (idInType > 0) {
470                 try {
471                     JahiaObject jahiaObject = JahiaObject.getInstance(objectKey);
472                     if (jahiaObject != null) {
473                         if (jahiaObject instanceof ContentContainer) {
474                             ContentContainer contentContainer = (
475                                 ContentContainer)
476                                 jahiaObject;
477                             parentPageID = contentContainer.getPageID();
478                         } else if (jahiaObject instanceof ContentContainerList) {
479                             ContentContainerList contentContainerList = (
480                                 ContentContainerList) jahiaObject;
481                             parentPageID = contentContainerList.getPageID();
482                         } else if (jahiaObject instanceof ContentField) {
483                             ContentField contentField = (ContentField)
484                                 jahiaObject;
485                             parentPageID = contentField.getPageID();
486                         } else if (jahiaObject instanceof ContentPage) {
487                             ContentPage contentPage = (ContentPage) jahiaObject;
488                             // WARNING : this is an exception !!
489
parentPageID = contentPage.getID();
490                         }
491                     } else {
492                         logger.warn("Unable to load content object " +
493                                     objectKey);
494                     }
495                 } catch (ClassNotFoundException JavaDoc cnfe) {
496                     logger.error(
497                         "Error while trying to find parent page for lock key " +
498                         lockKey, cnfe);
499                 }
500             }
501         }
502         ContentPage parentPage = null;
503         if (parentPageID != -1) {
504             try {
505                 parentPage = ContentPage.getPage(parentPageID);
506             } catch (Throwable JavaDoc t) {
507                 logger.warn("Can't get parent page (ID " + parentPageID + ")", t);
508                 return null;
509             }
510         } else {
511             logger.warn("Warning : couldn't find parent page ID for lock key :" + lockKey + ", some locks may not be resolved properly");
512         }
513         return parentPage;
514     }
515
516     private void putLockIfNotSameContext (LockKey lockKey, JahiaUser owner,
517                                           boolean justTesting,
518                                           LockPrerequisitesResult results) {
519         LockService lockRegistry = ServicesRegistry.getInstance().getLockService();
520         // this is used to simulate release without actually doing it.
521
boolean wouldHaveReleased = false;
522         // #ifdef RELEASE_SAME_CONTEXT
523
// Comment these lines if should not release lock.
524
HashMap JavaDoc lockInfo = lockRegistry.getInfo(lockKey);
525         // If lock is already acquired in context then release it.
526
if (lockInfo != null) {
527             // JahiaUser owner = (JahiaUser)lockInfo.get(LockRegistry.OWNER);
528
String JavaDoc lockID = (String JavaDoc) lockInfo.get(LockRegistry.ID);
529             if (lockRegistry.isAlreadyAcquiredInContext(lockKey, owner,
530                 lockID)) {
531                 if (justTesting) {
532                     wouldHaveReleased = true;
533                 } else {
534                     lockRegistry.release(lockKey, owner, lockID);
535                 }
536             }
537         }
538         // #endif
539
// From another context then no way to obtain it.
540
if (lockRegistry.isAlreadyAcquired(lockKey)) {
541             if (justTesting) {
542                 if (!wouldHaveReleased) {
543                     results.put(lockKey);
544                 }
545             } else {
546                 results.put(lockKey);
547             }
548         }
549     }
550
551     private LockPrerequisites() {
552         try {
553             lockPrerequisitesResultMap = ServicesRegistry.getInstance().getJahiaCacheServiceBis().
554                         createCache("LockPrerequisitesResultMap");
555             lockAlreadyAcquiredMap = ServicesRegistry.getInstance().getJahiaCacheServiceBis().
556                         createCache("LockAlreadyAcquiredMap");
557         } catch (JahiaException je) {
558             logger.error ("Error while creating lock cache", je);
559         }
560     }
561
562     private Cache lockPrerequisitesResultMap;
563     private Cache lockAlreadyAcquiredMap;
564     private static LockPrerequisites lockPrerequisites = null;
565
566     private static org.apache.log4j.Logger logger =
567             org.apache.log4j.Logger.getLogger(LockKey.class);
568
569 }
570
Popular Tags