001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.commonimpl;
017:
018: import org.outerj.daisy.repository.*;
019:
020: import java.util.*;
021:
022: public class CollectionCache implements RepositoryListener {
023: private CollectionStrategy collectionStrategy;
024: private AuthenticatedUser cacheUser;
025:
026: private boolean cacheLoaded = false;
027: private Map<Long, DocumentCollectionImpl> collectionsById;
028: private Map<String, DocumentCollectionImpl> collectionsByName;
029: private Collection<DocumentCollectionImpl> collections;
030: private DocumentCollections documentCollections;
031:
032: public CollectionCache(CollectionStrategy collectionStrategy,
033: AuthenticatedUser cacheUser) {
034: this .collectionStrategy = collectionStrategy;
035: this .cacheUser = cacheUser;
036: }
037:
038: public void repositoryEvent(RepositoryEventType eventType,
039: Object id, long updateCount) {
040: if (eventType == RepositoryEventType.COLLECTION_CREATED)
041: collectionCreated((Long) id);
042: else if (eventType == RepositoryEventType.COLLECTION_DELETED)
043: collectionDeleted((Long) id);
044: else if (eventType == RepositoryEventType.COLLECTION_UPDATED)
045: collectionUpdated((Long) id, updateCount);
046: }
047:
048: public void assureCacheLoaded() throws RepositoryException {
049: if (cacheLoaded)
050: return;
051:
052: synchronized (this ) {
053: if (cacheLoaded)
054: return;
055:
056: collections = collectionStrategy.loadCollections(cacheUser);
057:
058: Map<Long, DocumentCollectionImpl> newCollectionsById = new HashMap<Long, DocumentCollectionImpl>();
059: Map<String, DocumentCollectionImpl> newCollectionsByName = new HashMap<String, DocumentCollectionImpl>();
060: for (DocumentCollectionImpl collection : collections) {
061: collection.makeReadOnly();
062: newCollectionsById.put(new Long(collection.getId()),
063: collection);
064: newCollectionsByName.put(collection.getName(),
065: collection);
066: }
067: collectionsById = newCollectionsById;
068: collectionsByName = newCollectionsByName;
069: documentCollections = new DocumentCollectionsImpl(
070: collections.toArray(new DocumentCollection[0]));
071: this .cacheLoaded = true;
072: }
073: }
074:
075: public DocumentCollectionImpl getCollection(long id)
076: throws RepositoryException {
077: assureCacheLoaded();
078: DocumentCollectionImpl collection = collectionsById
079: .get(new Long(id));
080: if (collection == null)
081: throw new CollectionNotFoundException(id);
082: else
083: return collection;
084: }
085:
086: public DocumentCollectionImpl getCollectionByName(String name)
087: throws RepositoryException {
088: assureCacheLoaded();
089: DocumentCollectionImpl collection = collectionsByName.get(name);
090: if (collection == null)
091: throw new CollectionNotFoundException(name);
092: else
093: return collection;
094: }
095:
096: public DocumentCollections getCollections()
097: throws RepositoryException {
098: assureCacheLoaded();
099: return documentCollections;
100: }
101:
102: private void collectionCreated(Long id) {
103: synchronized (this ) {
104: if (!cacheLoaded)
105: return;
106:
107: // check for duplicate event
108: if (collectionsById.containsKey(id))
109: return;
110:
111: cacheLoaded = false;
112: }
113: }
114:
115: private void collectionDeleted(Long id) {
116: synchronized (this ) {
117: if (!cacheLoaded)
118: return;
119:
120: // check for duplicate event
121: if (!collectionsById.containsKey(id))
122: return;
123:
124: cacheLoaded = false;
125: }
126: }
127:
128: private void collectionUpdated(Long id, long updateCount) {
129: synchronized (this ) {
130: if (!cacheLoaded)
131: return;
132:
133: // check for duplicate event
134: DocumentCollection currentCollection = collectionsById
135: .get(id);
136: if (currentCollection != null
137: && currentCollection.getUpdateCount() == updateCount)
138: return;
139:
140: cacheLoaded = false;
141: }
142: }
143: }
|