KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > CollectionCache


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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         // do nothing
48
}
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 JavaDoc(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 JavaDoc(id));
79         if (collection == null)
80             throw new CollectionNotFoundException(id);
81         else
82             return collection;
83     }
84
85     public DocumentCollectionImpl getCollectionByName(String JavaDoc 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             // check for duplicate event
105
if (collectionsById.containsKey(new Long JavaDoc(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             // check for duplicate event
118
if (!collectionsById.containsKey(new Long JavaDoc(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             // check for duplicate event
131
DocumentCollection currentCollection = (DocumentCollection)collectionsById.get(new Long JavaDoc(id));
132             if (currentCollection != null && currentCollection.getUpdateCount() == updateCount)
133                 return;
134
135             cacheLoaded = false;
136         }
137     }
138 }
139
Popular Tags