KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > containers > ContentContainerTools


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  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 09-AUG-2003, Jahia Solutions Sarl, Khue Nguyen
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.services.containers;
42
43 import java.util.*;
44
45 import org.jahia.exceptions.JahiaException;
46 import org.jahia.exceptions.JahiaInitializationException;
47 import org.jahia.services.cache.Cache;
48 import org.jahia.services.cache.CacheFactory;
49 import org.jahia.services.version.EntryLoadRequest;
50
51 class ContentContainerTools {
52
53     /** logging */
54     final private org.apache.log4j.Logger logger =
55             org.apache.log4j.Logger.getLogger (ContentContainerTools.class);
56
57
58     /** this class unique instance */
59     private static ContentContainerTools instance;
60
61     // the Container cache name.
62
public static final String JavaDoc CONTENT_CONTAINER_CACHE = "ContentContainerCache";
63     // field IDs by container cache.
64
public static final String JavaDoc FIELD_IDS_BY_CONTAINER_CACHE = "FieldIDsByContainerCache";
65
66     public static final String JavaDoc FIELD_IDS_AND_DEFS_BY_CONTAINER_CACHE = "FieldIDsAndDefsByContainerCache";
67
68     public static final String JavaDoc CONTAINER_PROPERTIES_CACHE = "ContainerPropertiesCache";
69     
70     /** Containers cache */
71     private static Cache cacheContainers;
72
73     /** Field IDs by Container Cache */
74     private static Cache cacheFieldIDsByContainer;
75     
76     /** Field IDs and Defs by Container Cache */
77     private static Cache cacheFieldIDsAndDefsByContainer;
78
79     /** Container properties cache */
80     private static Cache cacheContainerProperties;
81
82     private static String JavaDoc WITH_MARKED_FOR_DELETE = "WITH_MARKED_FOR_DELETE";
83
84     /**
85      * Default constructor, creates a new <code>ContentContainerTools</code> instance.
86      */

87     private ContentContainerTools () {
88         logger.info ("***** Starting up the ContentContainerTools singleton *****");
89
90         try {
91             cacheContainers = CacheFactory.createCache (CONTENT_CONTAINER_CACHE);
92
93             cacheFieldIDsByContainer = CacheFactory.createCache (FIELD_IDS_BY_CONTAINER_CACHE);
94             
95             cacheFieldIDsAndDefsByContainer = CacheFactory.createCache (FIELD_IDS_AND_DEFS_BY_CONTAINER_CACHE);
96
97             cacheContainerProperties = CacheFactory.createCache (CONTAINER_PROPERTIES_CACHE);
98
99         } catch (JahiaInitializationException e) {
100             logger.warn (e);
101         }
102     }
103
104     /**
105      * Return the unique instance of this class
106      *
107      * @return the unique instance of this class
108      */

109     public static synchronized ContentContainerTools getInstance () {
110         if (instance == null) {
111             instance = new ContentContainerTools ();
112         }
113
114         return instance;
115     }
116
117     /**
118      * Get a ContentContainer from its ID
119      *
120      * @param containerID
121      *
122      * @throws JahiaException if the container doesn't exist, or there's a DB error
123      */

124     public ContentContainer getContainer (int containerID)
125             throws JahiaException {
126         return getContainer (containerID, false);
127     }
128
129     /**
130      * Get a ContentContainer from its ID
131      *
132      * @param containerID
133      * @param forceLoadFromDB if true, force loading from db
134      *
135      * @throws JahiaException If the container doesn't exist, or there's a DB error
136      */

137     public ContentContainer getContainer(int containerID,
138             boolean forceLoadFromDB) throws JahiaException {
139         ContentContainer cachedContainer = null;
140         //synchronized (cacheContainers) {
141
if (!forceLoadFromDB) {
142                 cachedContainer = (ContentContainer) cacheContainers
143                         .get(new Integer JavaDoc(containerID));
144             }
145             if (cachedContainer == null) {
146                 cachedContainer = ContentContainerDB.getInstance()
147                         .getContainer(containerID);
148                 if (cachedContainer != null) {
149                     cacheContainers.put(new Integer JavaDoc(containerID),
150                             cachedContainer);
151                 }
152             }
153         //}
154
if (cachedContainer != null) {
155             return (ContentContainer) cachedContainer.clone();
156         } else {
157             return null;
158         }
159     }
160
161     /**
162      * Returns properties of the specified container.
163      *
164      * @param containerID the Id of the container
165      * @return properties of the specified container
166      * @throws JahiaException If the container doesn't exist, or there's a DB
167      * error
168      */

169       public Properties getContainerProperties(int containerID)
170         throws JahiaException
171       {
172         Integer JavaDoc key = new Integer JavaDoc(containerID);
173         Properties cachedProperties = (Properties)cacheContainerProperties.get(key);
174         if (cachedProperties == null)
175         {
176           synchronized (cacheContainerProperties)
177           {
178             cachedProperties = (Properties)cacheContainerProperties.get(key);
179             if (cachedProperties == null)
180             {
181               cachedProperties = new JahiaContainerPropDB()
182                 .getProperties(containerID);
183               cacheContainerProperties.put(key, cachedProperties);
184             }
185           }
186         }
187         return cachedProperties;
188       }
189
190     /**
191      * Get the list of field IDs for a given container ID
192      *
193      * @param containerID
194      * @param loadRequest
195      * @return Vector
196      * @throws JahiaException
197      */

198     public Vector getFieldIDsByContainer (int containerID,
199                                           EntryLoadRequest loadRequest)
200     throws JahiaException{
201         return getFieldIDsByContainer (containerID, loadRequest, false);
202     }
203
204     /**
205      * Get a ContentContainer from its ID
206      *
207      * @param containerID
208      * @param forceLoadFromDB if true, force loading from db
209      * @param loadRequest
210      * @return Vector
211      * @throws JahiaException If the container doesn't exist, or there's a DB error
212      */

213     public Vector getFieldIDsByContainer (int containerID,
214                                           EntryLoadRequest loadRequest,
215                                           boolean forceLoadFromDB)
216     throws JahiaException {
217         boolean fromCache = false;
218         if ( containerID <=0 ){
219             return new Vector();
220         }
221         synchronized (cacheFieldIDsByContainer)
222         {
223             Vector result = new Vector();
224             if (forceLoadFromDB) {
225                 result = JahiaContainerUtilsDB.getInstance()
226                     .db_get_field_ids_in_container(containerID, loadRequest);
227             }
228             else {
229                 if ( loadRequest == null ){
230                     result = (Vector)cacheFieldIDsByContainer.get(String.valueOf(containerID));
231                     if (result != null) {
232                         fromCache = true;
233                     } else {
234                         // load the content container from the database
235
result = JahiaContainerUtilsDB.getInstance()
236                             .db_get_field_ids_in_container(containerID,
237                             loadRequest);
238                         if ( result != null ){
239                             cacheFieldIDsByContainer.put(String.valueOf(containerID),
240                                                         result);
241                         }
242                     }
243                 } else if ( loadRequest.getWorkflowState() >=
244                      EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
245                     result = (Vector)
246                         cacheFieldIDsByContainer.get(getFieldIDsCacheKey(containerID,
247                                             loadRequest.getWorkflowState(),
248                                             loadRequest.isWithMarkedForDeletion()));
249                     if (result != null) {
250                         fromCache = true;
251                     } else {
252                         // load the content container from the database
253
result = JahiaContainerUtilsDB.getInstance()
254                             .db_get_field_ids_in_container(containerID,
255                             loadRequest);
256                     }
257                 } else{
258                     // versioned
259
// load the content container from the database
260
result = JahiaContainerUtilsDB.getInstance()
261                         .db_get_field_ids_in_container(containerID,
262                         loadRequest);
263                 }
264             }
265             if (result != null && !fromCache && loadRequest != null &&
266                 loadRequest.getWorkflowState() >=
267                 EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
268                 cacheFieldIDsByContainer.put(getFieldIDsCacheKey(containerID,
269                                             loadRequest.getWorkflowState(),
270                                             loadRequest.isWithMarkedForDeletion()),
271                                             result);
272             }
273             if (result == null) {
274                 result = new Vector();
275             }
276             return (Vector)result.clone();
277         }
278     }
279
280     /**
281      * Get a ContentContainer from its ID
282      *
283      * @param containerID
284      * @param forceLoadFromDB if true, force loading from db
285      * @param loadRequest
286      * @return Map
287      * @throws JahiaException If the container doesn't exist, or there's a DB error
288      */

289     public Map getFieldIDsAndDefsByContainer (int containerID,
290                                           EntryLoadRequest loadRequest,
291                                           boolean forceLoadFromDB)
292     throws JahiaException {
293         boolean fromCache = false;
294         if ( containerID <=0 ){
295             return new HashMap();
296         }
297         synchronized (cacheFieldIDsAndDefsByContainer)
298         {
299             Map result;
300             if (forceLoadFromDB) {
301                 result = JahiaContainerUtilsDB.getInstance()
302                     .db_get_field_ids_and_defs_in_container(containerID, loadRequest);
303             }
304             else {
305                 if ( loadRequest == null ){
306                     result = (Map)cacheFieldIDsAndDefsByContainer.get(String.valueOf(containerID));
307                     if (result != null) {
308                         fromCache = true;
309                     } else {
310                         // load the content container from the database
311
result = JahiaContainerUtilsDB.getInstance()
312                             .db_get_field_ids_and_defs_in_container(containerID,
313                             loadRequest);
314                         if ( result != null ){
315                             cacheFieldIDsAndDefsByContainer.put(String.valueOf(containerID),
316                                                         result);
317                         }
318                     }
319                 } else if ( loadRequest.getWorkflowState() >=
320                      EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
321                     result = (Map)
322                         cacheFieldIDsAndDefsByContainer.get(getFieldIDsCacheKey(containerID,
323                                             loadRequest.getWorkflowState(),
324                                             loadRequest.isWithMarkedForDeletion()));
325                     if (result != null) {
326                         fromCache = true;
327                     } else {
328                         // load the content container from the database
329
result = JahiaContainerUtilsDB.getInstance()
330                             .db_get_field_ids_and_defs_in_container(containerID,
331                             loadRequest);
332                     }
333                 } else{
334                     // versioned
335
// load the content container from the database
336
result = JahiaContainerUtilsDB.getInstance()
337                         .db_get_field_ids_and_defs_in_container(containerID,
338                         loadRequest);
339                 }
340             }
341             if (result != null && !fromCache && loadRequest != null &&
342                 loadRequest.getWorkflowState() >=
343                 EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
344                 cacheFieldIDsAndDefsByContainer.put(getFieldIDsCacheKey(containerID,
345                                             loadRequest.getWorkflowState(),
346                                             loadRequest.isWithMarkedForDeletion()),
347                                             result);
348             }
349             if (result == null) {
350                 result = new HashMap();
351             }
352             return result;
353         }
354     }
355     
356     /**
357      * Removes a content container from the cache if it was present in the cache. If not,
358      * this does nothing.
359      *
360      * @param containerID the identifier for the container to try to remove from the
361      * cache.
362      */

363     public void invalidateContainerFromCache (int containerID) {
364         Integer JavaDoc ctnId = new Integer JavaDoc(containerID);
365         synchronized(cacheContainers){
366             cacheContainers.remove(ctnId);
367         }
368         synchronized (cacheContainerProperties){
369             cacheContainerProperties.remove(ctnId);
370         }
371     }
372
373     /**
374      * Invalidate field IDs By container cache for the given containerID
375      *
376      * @param containerID
377      */

378     public void invalidateFieldIDsByContainerFromCache(int containerID) {
379         synchronized (cacheFieldIDsByContainer) {
380
381             cacheFieldIDsByContainer.remove(String.valueOf(containerID));
382             if (!CacheFactory.getInstance().isKeyHierarchyEnabled()) {
383                 cacheFieldIDsByContainer.remove(getFieldIDsCacheKey(
384                         containerID, EntryLoadRequest.ACTIVE_WORKFLOW_STATE,
385                         false));
386
387                 cacheFieldIDsByContainer.remove(getFieldIDsCacheKey(
388                         containerID, EntryLoadRequest.STAGING_WORKFLOW_STATE,
389                         false));
390
391                 cacheFieldIDsByContainer.remove(getFieldIDsCacheKey(
392                         containerID, EntryLoadRequest.STAGING_WORKFLOW_STATE,
393                         true));
394             }
395         }
396         synchronized (cacheFieldIDsAndDefsByContainer) {
397
398             cacheFieldIDsAndDefsByContainer.remove(String.valueOf(containerID));
399
400             if (!CacheFactory.getInstance().isKeyHierarchyEnabled()) {
401                 cacheFieldIDsAndDefsByContainer.remove(getFieldIDsCacheKey(
402                         containerID, EntryLoadRequest.ACTIVE_WORKFLOW_STATE,
403                         false));
404
405                 cacheFieldIDsAndDefsByContainer.remove(getFieldIDsCacheKey(
406                         containerID, EntryLoadRequest.STAGING_WORKFLOW_STATE,
407                         false));
408
409                 cacheFieldIDsAndDefsByContainer.remove(getFieldIDsCacheKey(
410                         containerID, EntryLoadRequest.STAGING_WORKFLOW_STATE,
411                         true));
412             }
413         }
414     }
415
416     private Object JavaDoc getFieldIDsCacheKey(int containerID, int wfs,
417             boolean withMarkedForDelete) {
418         Object JavaDoc key = null;
419         if (CacheFactory.getInstance().isKeyHierarchyEnabled()) {
420             List keyList = new ArrayList(3);
421             keyList.add(String.valueOf(containerID));
422             if (wfs > EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
423                 keyList.add(Integer
424                         .toString(EntryLoadRequest.STAGING_WORKFLOW_STATE));
425                 if (withMarkedForDelete) {
426                     keyList.add(WITH_MARKED_FOR_DELETE);
427                 }
428             } else {
429                 keyList.add(String.valueOf(wfs));
430             }
431             key = keyList;
432         } else {
433             StringBuffer JavaDoc keyStr = new StringBuffer JavaDoc(String.valueOf(containerID));
434             keyStr.append("_");
435             if (wfs > EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
436                 keyStr.append(EntryLoadRequest.STAGING_WORKFLOW_STATE);
437                 if (withMarkedForDelete) {
438                     keyStr.append(WITH_MARKED_FOR_DELETE);
439                 }
440             } else {
441                 keyStr.append(String.valueOf(wfs));
442             }
443             key = keyStr.toString();
444         }
445         return key;
446     }
447 }
448
Popular Tags