1 16 package org.outerj.daisy.repository.commonimpl; 17 18 import org.outerj.daisy.repository.*; 19 20 import java.util.*; 21 22 public class CollectionCache implements RepositoryListener { 23 private CollectionStrategy collectionStrategy; 24 private AuthenticatedUser systemUser; 25 26 private boolean cacheLoaded = false; 27 private Map collectionsById; 28 private Map collectionsByName; 29 private Collection collections; 30 private DocumentCollections documentCollections; 31 32 public CollectionCache(CollectionStrategy collectionStrategy, AuthenticatedUser systemUser) { 33 this.collectionStrategy = collectionStrategy; 34 this.systemUser = systemUser; 35 } 36 37 public void repositoryEvent(RepositoryEventType eventType, long id, long updateCount) { 38 if (eventType == RepositoryEventType.COLLECTION_CREATED) 39 collectionCreated(id); 40 else if (eventType == RepositoryEventType.COLLECTION_DELETED) 41 collectionDeleted(id); 42 else if (eventType == RepositoryEventType.COLLECTION_UPDATED) 43 collectionUpdated(id, updateCount); 44 } 45 46 public void variantEvent(DocumentVariantEventType eventType, long documentId, long branchId, long languageId, long updateCount) { 47 } 49 50 public void assureCacheLoaded() throws RepositoryException { 51 if (cacheLoaded) 52 return; 53 54 synchronized(this) { 55 if (cacheLoaded) 56 return; 57 58 collections = collectionStrategy.loadCollections(systemUser); 59 60 HashMap newCollectionsById = new HashMap(); 61 HashMap newCollectionsByName = new HashMap(); 62 Iterator collectionsIt = collections.iterator(); 63 while (collectionsIt.hasNext()) { 64 DocumentCollectionImpl collection = (DocumentCollectionImpl)collectionsIt.next(); 65 collection.makeReadOnly(); 66 newCollectionsById.put(new Long (collection.getId()), collection); 67 newCollectionsByName.put(collection.getName(), collection); 68 } 69 collectionsById = newCollectionsById; 70 collectionsByName = newCollectionsByName; 71 documentCollections = new DocumentCollectionsImpl((DocumentCollection[])collections.toArray(new DocumentCollection[0])); 72 this.cacheLoaded = true; 73 } 74 } 75 76 public DocumentCollectionImpl getCollection(long id) throws RepositoryException { 77 assureCacheLoaded(); 78 DocumentCollectionImpl collection = (DocumentCollectionImpl)collectionsById.get(new Long (id)); 79 if (collection == null) 80 throw new CollectionNotFoundException(id); 81 else 82 return collection; 83 } 84 85 public DocumentCollectionImpl getCollectionByName(String name) throws RepositoryException { 86 assureCacheLoaded(); 87 DocumentCollectionImpl collection = (DocumentCollectionImpl)collectionsByName.get(name); 88 if (collection == null) 89 throw new CollectionNotFoundException(name); 90 else 91 return collection; 92 } 93 94 public DocumentCollections getCollections() throws RepositoryException { 95 assureCacheLoaded(); 96 return documentCollections; 97 } 98 99 private void collectionCreated(long id) { 100 synchronized(this) { 101 if (!cacheLoaded) 102 return; 103 104 if (collectionsById.containsKey(new Long (id))) 106 return; 107 108 cacheLoaded = false; 109 } 110 } 111 112 private void collectionDeleted(long id) { 113 synchronized(this) { 114 if (!cacheLoaded) 115 return; 116 117 if (!collectionsById.containsKey(new Long (id))) 119 return; 120 121 cacheLoaded = false; 122 } 123 } 124 125 private void collectionUpdated(long id, long updateCount) { 126 synchronized(this) { 127 if (!cacheLoaded) 128 return; 129 130 DocumentCollection currentCollection = (DocumentCollection)collectionsById.get(new Long (id)); 132 if (currentCollection != null && currentCollection.getUpdateCount() == updateCount) 133 return; 134 135 cacheLoaded = false; 136 } 137 } 138 } 139 | Popular Tags |