KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > variant > VariantCache


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.variant;
17
18 import org.outerj.daisy.repository.variant.*;
19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
20 import org.outerj.daisy.repository.RepositoryException;
21 import org.outerj.daisy.repository.RepositoryListener;
22 import org.outerj.daisy.repository.RepositoryEventType;
23 import org.outerj.daisy.repository.DocumentVariantEventType;
24 import EDU.oswego.cs.dl.util.concurrent.Mutex;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 public class VariantCache implements RepositoryListener {
30     private VariantStrategy variantStrategy;
31     private AuthenticatedUser cacheUser;
32
33     private Mutex branchesMutex = new Mutex();
34     private boolean branchesLoaded = false;
35     private Map JavaDoc branchesById;
36     private Map JavaDoc branchesByName;
37     private Branches branches;
38
39     private Mutex languagesMutex = new Mutex();
40     private boolean languagesLoaded = false;
41     private Map JavaDoc languagesById;
42     private Map JavaDoc languagesByName;
43     private Languages languages;
44
45     public VariantCache(VariantStrategy variantStrategy, AuthenticatedUser cacheUser) {
46         this.variantStrategy = variantStrategy;
47         this.cacheUser = cacheUser;
48     }
49
50     private void assureBranchesLoaded() throws RepositoryException {
51         if (branchesLoaded)
52             return;
53
54         try {
55             branchesMutex.acquire();
56         } catch (InterruptedException JavaDoc e) {
57             throw new RuntimeException JavaDoc(e);
58         }
59         try {
60             if (branchesLoaded)
61                 return;
62
63             Map JavaDoc branchesById = new HashMap JavaDoc();
64             Map JavaDoc branchesByName = new HashMap JavaDoc();
65             BranchImpl[] branches = variantStrategy.getAllBranches(cacheUser);
66             for (int i = 0; i < branches.length; i++) {
67                 BranchImpl branch = branches[i];
68                 branch.makeReadOnly();
69                 branchesById.put(new Long JavaDoc(branch.getId()), branch);
70                 branchesByName.put(branch.getName(), branch);
71             }
72             this.branchesById = branchesById;
73             this.branchesByName = branchesByName;
74             this.branches = new BranchesImpl(branches);
75             this.branchesLoaded = true;
76         } finally {
77             branchesMutex.release();
78         }
79     }
80
81     private void assureLanguagesLoaded() throws RepositoryException {
82         if (languagesLoaded)
83             return;
84
85         try {
86             languagesMutex.acquire();
87         } catch (InterruptedException JavaDoc e) {
88             throw new RuntimeException JavaDoc(e);
89         }
90         try {
91             if (languagesLoaded)
92                 return;
93
94             Map JavaDoc languagesById = new HashMap JavaDoc();
95             Map JavaDoc languagesByName = new HashMap JavaDoc();
96             LanguageImpl[] languages = variantStrategy.getAllLanguages(cacheUser);
97             for (int i = 0; i < languages.length; i++) {
98                 LanguageImpl language = languages[i];
99                 language.makeReadOnly();
100                 languagesById.put(new Long JavaDoc(language.getId()), language);
101                 languagesByName.put(language.getName(), language);
102             }
103             this.languagesById = languagesById;
104             this.languagesByName = languagesByName;
105             this.languages = new LanguagesImpl(languages);
106             this.languagesLoaded = true;
107         } finally {
108             languagesMutex.release();
109         }
110     }
111
112     public Branch getBranch(long id) throws RepositoryException {
113         assureBranchesLoaded();
114         Branch branch = (Branch)branchesById.get(new Long JavaDoc(id));
115         if (branch == null)
116             throw new BranchNotFoundException(id);
117         return branch;
118     }
119
120     public Branch getBranchByName(String JavaDoc name) throws RepositoryException {
121         assureBranchesLoaded();
122         Branch branch = (Branch)branchesByName.get(name);
123         if (branch == null)
124             throw new BranchNotFoundException(name);
125         return branch;
126     }
127
128     public Branches getBranches() throws RepositoryException {
129         assureBranchesLoaded();
130         return branches;
131     }
132
133     public Language getLanguage(long id) throws RepositoryException {
134         assureLanguagesLoaded();
135         Language language = (Language)languagesById.get(new Long JavaDoc(id));
136         if (language == null)
137             throw new LanguageNotFoundException(id);
138         return language;
139     }
140
141     public Language getLanguageByName(String JavaDoc name) throws RepositoryException {
142         assureLanguagesLoaded();
143         Language language = (Language)languagesByName.get(name);
144         if (language == null)
145             throw new LanguageNotFoundException(name);
146         return language;
147     }
148
149     public Languages getLanguages() throws RepositoryException {
150         assureLanguagesLoaded();
151         return languages;
152     }
153
154     public void repositoryEvent(RepositoryEventType eventType, long id, long updateCount) {
155         if (eventType == RepositoryEventType.BRANCH_CREATED || eventType == RepositoryEventType.BRANCH_DELETED
156                 || eventType == RepositoryEventType.BRANCH_UPDATED) {
157             branchesLoaded = false;
158         } else if (eventType == RepositoryEventType.LANGUAGE_CREATED || eventType == RepositoryEventType.LANGUAGE_DELETED
159                 || eventType == RepositoryEventType.LANGUAGE_UPDATED) {
160             languagesLoaded = false;
161         }
162     }
163
164     public void variantEvent(DocumentVariantEventType eventType, long documentId, long branchId, long languageId, long updateCount) {
165         // do nothing
166
}
167 }
168
Popular Tags