KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > schema > RepositorySchemaCache


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.schema;
17
18 import org.outerj.daisy.repository.schema.*;
19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
20 import org.outerj.daisy.repository.RepositoryException;
21 import java.util.*;
22
23 import EDU.oswego.cs.dl.util.concurrent.Mutex;
24
25 // NOTE: the implementation of this class should not make any assumptions on the
26
// order in which the events arrive, since in case of remote cache invalidation
27
// we can't make any assumptions about this.
28
public class RepositorySchemaCache implements RepositorySchemaListener {
29     private SchemaStrategy schemaStrategy;
30     private AuthenticatedUser systemUser;
31
32     private Mutex partTypesMutex = new Mutex();
33     private boolean partTypesLoaded = false;
34     private Map partTypesById;
35     private Map partTypesByName;
36     private Collection allPartTypes;
37     private PartTypes partTypes;
38
39     private Mutex fieldTypesMutex = new Mutex();
40     private boolean fieldTypesLoaded = false;
41     private Map fieldTypesById;
42     private Map fieldTypesByName;
43     private Collection allFieldTypes;
44     private FieldTypes fieldTypes;
45
46     private Mutex documentTypesMutex = new Mutex();
47     private boolean documentTypesLoaded = false;
48     private Map documentTypesById;
49     private Map documentTypesByName;
50     private Collection allDocumentTypes;
51     private DocumentTypes documentTypes;
52
53     public RepositorySchemaCache(SchemaStrategy schemaStrategy, AuthenticatedUser systemUser) {
54         this.schemaStrategy = schemaStrategy;
55         this.systemUser = systemUser;
56     }
57
58     private void assurePartTypesLoaded() throws RepositoryException {
59         if (partTypesLoaded)
60             return;
61
62         try {
63             partTypesMutex.acquire();
64         } catch (InterruptedException JavaDoc e) {
65             throw new RuntimeException JavaDoc(e);
66         }
67         try {
68             if (partTypesLoaded)
69                 return;
70
71             // load, cache, index the PartTypes
72
Map partTypesById = new HashMap();
73             Map partTypesByName = new HashMap();
74             Collection partTypes = schemaStrategy.getAllPartTypes(systemUser);
75             Iterator partTypesIt = partTypes.iterator();
76             while (partTypesIt.hasNext()) {
77                 PartTypeImpl partType = (PartTypeImpl)partTypesIt.next();
78                 partType.makeReadOnly();
79                 partTypesById.put(new Long JavaDoc(partType.getId()), partType);
80                 partTypesByName.put(partType.getName(), partType);
81             }
82             this.partTypesById = partTypesById;
83             this.partTypesByName = partTypesByName;
84             this.allPartTypes = partTypes;
85             this.partTypes = new PartTypesImpl((PartType[])allPartTypes.toArray(new PartType[0]));
86             this.partTypesLoaded = true;
87         } finally {
88             partTypesMutex.release();
89         }
90     }
91
92     private void assureFieldTypesLoaded() throws RepositoryException {
93         if (fieldTypesLoaded)
94             return;
95
96         try {
97             fieldTypesMutex.acquire();
98         } catch (InterruptedException JavaDoc e) {
99             throw new RuntimeException JavaDoc(e);
100         }
101         try {
102             if (fieldTypesLoaded)
103                 return;
104
105             // load, cache, index the FieldTypes
106
Map fieldTypesById = new HashMap();
107             Map fieldTypesByName = new HashMap();
108             Collection fieldTypes = schemaStrategy.getAllFieldTypes(systemUser);
109             Iterator fieldTypesIt = fieldTypes.iterator();
110             while (fieldTypesIt.hasNext()) {
111                 FieldTypeImpl fieldType = (FieldTypeImpl)fieldTypesIt.next();
112                 fieldType.makeReadOnly();
113                 fieldTypesById.put(new Long JavaDoc(fieldType.getId()), fieldType);
114                 fieldTypesByName.put(fieldType.getName(), fieldType);
115             }
116             this.fieldTypesById = fieldTypesById;
117             this.fieldTypesByName = fieldTypesByName;
118             this.allFieldTypes = fieldTypes;
119             this.fieldTypes = new FieldTypesImpl((FieldType[])allFieldTypes.toArray(new FieldType[0]));
120             this.fieldTypesLoaded = true;
121         } finally {
122             fieldTypesMutex.release();
123         }
124     }
125
126     private void assureDocumentTypesLoaded() throws RepositoryException {
127         if (documentTypesLoaded)
128             return;
129
130         assurePartTypesLoaded();
131         assureFieldTypesLoaded();
132
133         try {
134             documentTypesMutex.acquire();
135         } catch (InterruptedException JavaDoc e) {
136             throw new RuntimeException JavaDoc(e);
137         }
138         try {
139             if (documentTypesLoaded)
140                 return;
141
142             // load, cache, index the DocumentTypes
143
Map documentTypesById = new HashMap();
144             Map documentTypesByName = new HashMap();
145             Collection documentTypes = schemaStrategy.getAllDocumentTypes(systemUser);
146             Iterator documentTypesIt = documentTypes.iterator();
147             while (documentTypesIt.hasNext()) {
148                 DocumentTypeImpl documentType = (DocumentTypeImpl)documentTypesIt.next();
149                 documentType.makeReadOnly();
150                 documentTypesById.put(new Long JavaDoc(documentType.getId()), documentType);
151                 documentTypesByName.put(documentType.getName(), documentType);
152             }
153             this.documentTypesById = documentTypesById;
154             this.documentTypesByName = documentTypesByName;
155             this.allDocumentTypes = documentTypes;
156             this.documentTypes = new DocumentTypesImpl((DocumentType[])allDocumentTypes.toArray(new DocumentType[0]));
157             this.documentTypesLoaded = true;
158         } finally {
159             documentTypesMutex.release();
160         }
161     }
162
163     public FieldType getFieldTypeById(long id) throws RepositoryException {
164         assureFieldTypesLoaded();
165         FieldType fieldType = (FieldType)fieldTypesById.get(new Long JavaDoc(id));
166         if (fieldType == null)
167             throw new FieldTypeNotFoundException(id);
168         return fieldType;
169     }
170
171     public DocumentType getDocumentTypeById(long id) throws RepositoryException {
172         assureDocumentTypesLoaded();
173         DocumentType documentType = (DocumentType)documentTypesById.get(new Long JavaDoc(id));
174         if (documentType == null)
175             throw new DocumentTypeNotFoundException(id);
176         return documentType;
177     }
178
179     public FieldType getFieldTypeByName(String JavaDoc name) throws RepositoryException {
180         assureFieldTypesLoaded();
181         FieldType fieldType = (FieldType)fieldTypesByName.get(name);
182         if (fieldType == null)
183             throw new FieldTypeNotFoundException(name);
184         return fieldType;
185     }
186
187     public PartType getPartTypeById(long id) throws RepositoryException {
188         assurePartTypesLoaded();
189         PartType partType = (PartType)partTypesById.get(new Long JavaDoc(id));
190         if (partType == null)
191             throw new PartTypeNotFoundException(id);
192         return partType;
193     }
194
195     public PartTypes getAllPartTypes() throws RepositoryException {
196         assurePartTypesLoaded();
197         return partTypes;
198     }
199
200     public PartType getPartTypeByName(String JavaDoc name) throws RepositoryException {
201         assurePartTypesLoaded();
202         PartType partType = (PartType)partTypesByName.get(name);
203         if (partType == null)
204             throw new PartTypeNotFoundException(name);
205         return partType;
206     }
207
208     public FieldTypes getAllFieldTypes() throws RepositoryException {
209         assureFieldTypesLoaded();
210         return fieldTypes;
211     }
212
213     public DocumentTypes getAllDocumentTypes() throws RepositoryException {
214         assureDocumentTypesLoaded();
215         return documentTypes;
216     }
217
218     public DocumentType getDocumentTypeByName(String JavaDoc name) throws RepositoryException {
219         assureDocumentTypesLoaded();
220         DocumentType documentType = (DocumentType)documentTypesByName.get(name);
221         if (documentType == null)
222             throw new DocumentTypeNotFoundException(name);
223         return documentType;
224     }
225
226     public void modelChange(RepositorySchemaEventType type, long id, long updateCount) {
227         if (type == RepositorySchemaEventType.DOCUMENT_TYPE_CREATED)
228             documentTypeCreated(id);
229         else if (type == RepositorySchemaEventType.DOCUMENT_TYPE_UPDATED)
230             documentTypeUpdated(id, updateCount);
231         else if (type == RepositorySchemaEventType.DOCUMENT_TYPE_DELETED)
232             documentTypeDeleted(id);
233         else if (type == RepositorySchemaEventType.FIELD_TYPE_CREATED)
234             fieldTypeCreated(id);
235         else if (type == RepositorySchemaEventType.FIELD_TYPE_UPDATED)
236             fieldTypeUpdated(id, updateCount);
237         else if (type == RepositorySchemaEventType.FIELD_TYPE_DELETED)
238             fieldTypeDeleted(id);
239         else if (type == RepositorySchemaEventType.PART_TYPE_CREATED)
240             partTypeCreated(id);
241         else if (type == RepositorySchemaEventType.PART_TYPE_UPDATED)
242             partTypeUpdated(id, updateCount);
243         else if (type == RepositorySchemaEventType.PART_TYPE_DELETED)
244             partTypeDeleted(id);
245         else
246             throw new RuntimeException JavaDoc("Unsupported ChangEventType: " + type);
247     }
248
249     public void partTypeUpdated(long id, long updateCount) {
250         if (!partTypesLoaded)
251             return;
252
253         try {
254             partTypesMutex.acquire();
255         } catch (InterruptedException JavaDoc e) {
256             throw new RuntimeException JavaDoc(e);
257         }
258         try {
259             PartType currentPartType = (PartType)partTypesById.get(new Long JavaDoc(id));
260             if (currentPartType != null && currentPartType.getUpdateCount() == updateCount)
261                 return;
262
263             this.partTypesLoaded = false;
264             this.documentTypesLoaded = false;
265         } finally {
266             partTypesMutex.release();
267         }
268     }
269
270     private void partTypeDeleted(long id) {
271         if (!partTypesLoaded)
272             return;
273
274         try {
275             partTypesMutex.acquire();
276         } catch (InterruptedException JavaDoc e) {
277             throw new RuntimeException JavaDoc(e);
278         }
279         try {
280             PartType currentPartType = (PartType)partTypesById.get(new Long JavaDoc(id));
281             if (currentPartType == null)
282                 return;
283
284             this.partTypesLoaded = false;
285         } finally {
286             partTypesMutex.release();
287         }
288     }
289
290     public void partTypeCreated(long id) {
291         if (!partTypesLoaded)
292             return;
293
294         try {
295             partTypesMutex.acquire();
296         } catch (InterruptedException JavaDoc e) {
297             throw new RuntimeException JavaDoc(e);
298         }
299         try {
300             if (partTypesById.containsKey(new Long JavaDoc(id)))
301                 return;
302
303             this.partTypesLoaded = false;
304
305             // the newly created PartType can't possibly be in use by a DocumentType yet, so
306
// no need to refresh those.
307
} finally {
308             partTypesMutex.release();
309         }
310     }
311
312     public void documentTypeUpdated(long id, long updateCount) {
313         if (!documentTypesLoaded)
314             return;
315
316         try {
317             documentTypesMutex.acquire();
318         } catch (InterruptedException JavaDoc e) {
319             throw new RuntimeException JavaDoc(e);
320         }
321         try {
322             // check for duplicate event
323
DocumentType currentDocumentType = (DocumentType)documentTypesById.get(new Long JavaDoc(id));
324             if (currentDocumentType != null && currentDocumentType.getUpdateCount() == updateCount)
325                 return;
326
327             this.documentTypesLoaded = false;
328         } finally {
329             documentTypesMutex.release();
330         }
331     }
332
333     private void documentTypeDeleted(long id) {
334         if (!documentTypesLoaded)
335             return;
336
337         try {
338             documentTypesMutex.acquire();
339         } catch (InterruptedException JavaDoc e) {
340             throw new RuntimeException JavaDoc(e);
341         }
342         try {
343             DocumentType currentDocumentType = (DocumentType)documentTypesById.get(new Long JavaDoc(id));
344             if (currentDocumentType == null)
345                 return;
346
347             this.documentTypesLoaded = false;
348         } finally {
349             documentTypesMutex.release();
350         }
351     }
352
353     public void documentTypeCreated(long id) {
354         if (!documentTypesLoaded)
355             return;
356
357         try {
358             documentTypesMutex.acquire();
359         } catch (InterruptedException JavaDoc e) {
360             throw new RuntimeException JavaDoc(e);
361         }
362         try {
363             if (documentTypesById.containsKey(new Long JavaDoc(id)))
364                 return;
365
366             this.documentTypesLoaded = false;
367         } finally {
368             documentTypesMutex.release();
369         }
370     }
371
372     public void fieldTypeUpdated(long id, long updateCount) {
373         if (!fieldTypesLoaded)
374             return;
375
376         try {
377             fieldTypesMutex.acquire();
378         } catch (InterruptedException JavaDoc e) {
379             throw new RuntimeException JavaDoc(e);
380         }
381         try {
382             FieldType currentFieldType = (FieldType)fieldTypesById.get(new Long JavaDoc(id));
383             if (currentFieldType != null && currentFieldType.getUpdateCount() == updateCount)
384                 return;
385
386             this.fieldTypesLoaded = false;
387             this.documentTypesLoaded = false;
388         } finally {
389             fieldTypesMutex.release();
390         }
391     }
392
393     private void fieldTypeDeleted(long id) {
394         if (!fieldTypesLoaded)
395             return;
396
397         try {
398             fieldTypesMutex.acquire();
399         } catch (InterruptedException JavaDoc e) {
400             throw new RuntimeException JavaDoc(e);
401         }
402         try {
403             FieldType currentFieldType = (FieldType)fieldTypesById.get(new Long JavaDoc(id));
404             if (currentFieldType == null)
405                 return;
406
407             this.fieldTypesLoaded = false;
408         } finally {
409             fieldTypesMutex.release();
410         }
411     }
412
413     public void fieldTypeCreated(long id) {
414         if (!fieldTypesLoaded)
415             return;
416
417         try {
418             fieldTypesMutex.acquire();
419         } catch (InterruptedException JavaDoc e) {
420             throw new RuntimeException JavaDoc(e);
421         }
422         try {
423             if (fieldTypesById.containsKey(new Long JavaDoc(id)))
424                 return;
425
426             this.fieldTypesLoaded = false;
427
428             // the newly created FieldType can't possibly be in use by a DocumentType yet, so
429
// no need to refresh those.
430
} finally {
431             fieldTypesMutex.release();
432         }
433     }
434 }
435
Popular Tags