1 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 54 final private org.apache.log4j.Logger logger = 55 org.apache.log4j.Logger.getLogger (ContentContainerTools.class); 56 57 58 59 private static ContentContainerTools instance; 60 61 public static final String CONTENT_CONTAINER_CACHE = "ContentContainerCache"; 63 public static final String FIELD_IDS_BY_CONTAINER_CACHE = "FieldIDsByContainerCache"; 65 66 public static final String FIELD_IDS_AND_DEFS_BY_CONTAINER_CACHE = "FieldIDsAndDefsByContainerCache"; 67 68 public static final String CONTAINER_PROPERTIES_CACHE = "ContainerPropertiesCache"; 69 70 71 private static Cache cacheContainers; 72 73 74 private static Cache cacheFieldIDsByContainer; 75 76 77 private static Cache cacheFieldIDsAndDefsByContainer; 78 79 80 private static Cache cacheContainerProperties; 81 82 private static String WITH_MARKED_FOR_DELETE = "WITH_MARKED_FOR_DELETE"; 83 84 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 109 public static synchronized ContentContainerTools getInstance () { 110 if (instance == null) { 111 instance = new ContentContainerTools (); 112 } 113 114 return instance; 115 } 116 117 124 public ContentContainer getContainer (int containerID) 125 throws JahiaException { 126 return getContainer (containerID, false); 127 } 128 129 137 public ContentContainer getContainer(int containerID, 138 boolean forceLoadFromDB) throws JahiaException { 139 ContentContainer cachedContainer = null; 140 if (!forceLoadFromDB) { 142 cachedContainer = (ContentContainer) cacheContainers 143 .get(new Integer (containerID)); 144 } 145 if (cachedContainer == null) { 146 cachedContainer = ContentContainerDB.getInstance() 147 .getContainer(containerID); 148 if (cachedContainer != null) { 149 cacheContainers.put(new Integer (containerID), 150 cachedContainer); 151 } 152 } 153 if (cachedContainer != null) { 155 return (ContentContainer) cachedContainer.clone(); 156 } else { 157 return null; 158 } 159 } 160 161 169 public Properties getContainerProperties(int containerID) 170 throws JahiaException 171 { 172 Integer key = new Integer (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 198 public Vector getFieldIDsByContainer (int containerID, 199 EntryLoadRequest loadRequest) 200 throws JahiaException{ 201 return getFieldIDsByContainer (containerID, loadRequest, false); 202 } 203 204 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 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 result = JahiaContainerUtilsDB.getInstance() 254 .db_get_field_ids_in_container(containerID, 255 loadRequest); 256 } 257 } else{ 258 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 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 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 result = JahiaContainerUtilsDB.getInstance() 330 .db_get_field_ids_and_defs_in_container(containerID, 331 loadRequest); 332 } 333 } else{ 334 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 363 public void invalidateContainerFromCache (int containerID) { 364 Integer ctnId = new Integer (containerID); 365 synchronized(cacheContainers){ 366 cacheContainers.remove(ctnId); 367 } 368 synchronized (cacheContainerProperties){ 369 cacheContainerProperties.remove(ctnId); 370 } 371 } 372 373 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 getFieldIDsCacheKey(int containerID, int wfs, 417 boolean withMarkedForDelete) { 418 Object 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 keyStr = new StringBuffer (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 |