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.variant;
017:
018: import org.outerj.daisy.repository.variant.*;
019: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
020: import org.outerj.daisy.repository.*;
021:
022: import java.util.Map;
023: import java.util.HashMap;
024: import java.util.concurrent.locks.Lock;
025: import java.util.concurrent.locks.ReentrantLock;
026:
027: public class VariantCache implements RepositoryListener {
028: private VariantStrategy variantStrategy;
029: private AuthenticatedUser cacheUser;
030:
031: private Lock branchesLock = new ReentrantLock();
032: private boolean branchesLoaded = false;
033: private Map branchesById;
034: private Map branchesByName;
035: private Branches branches;
036:
037: private Lock languagesLock = new ReentrantLock();
038: private boolean languagesLoaded = false;
039: private Map languagesById;
040: private Map languagesByName;
041: private Languages languages;
042:
043: public VariantCache(VariantStrategy variantStrategy,
044: AuthenticatedUser cacheUser) {
045: this .variantStrategy = variantStrategy;
046: this .cacheUser = cacheUser;
047: }
048:
049: private void assureBranchesLoaded() throws RepositoryException {
050: if (branchesLoaded)
051: return;
052:
053: try {
054: branchesLock.lockInterruptibly();
055: } catch (InterruptedException e) {
056: throw new RuntimeException(e);
057: }
058: try {
059: if (branchesLoaded)
060: return;
061:
062: Map branchesById = new HashMap();
063: Map branchesByName = new HashMap();
064: BranchImpl[] branches = variantStrategy
065: .getAllBranches(cacheUser);
066: for (int i = 0; i < branches.length; i++) {
067: BranchImpl branch = branches[i];
068: branch.makeReadOnly();
069: branchesById.put(new Long(branch.getId()), branch);
070: branchesByName.put(branch.getName(), branch);
071: }
072: this .branchesById = branchesById;
073: this .branchesByName = branchesByName;
074: this .branches = new BranchesImpl(branches);
075: this .branchesLoaded = true;
076: } finally {
077: branchesLock.unlock();
078: }
079: }
080:
081: private void assureLanguagesLoaded() throws RepositoryException {
082: if (languagesLoaded)
083: return;
084:
085: try {
086: languagesLock.lockInterruptibly();
087: } catch (InterruptedException e) {
088: throw new RuntimeException(e);
089: }
090: try {
091: if (languagesLoaded)
092: return;
093:
094: Map languagesById = new HashMap();
095: Map languagesByName = new HashMap();
096: LanguageImpl[] languages = variantStrategy
097: .getAllLanguages(cacheUser);
098: for (int i = 0; i < languages.length; i++) {
099: LanguageImpl language = languages[i];
100: language.makeReadOnly();
101: languagesById.put(new Long(language.getId()), language);
102: languagesByName.put(language.getName(), language);
103: }
104: this .languagesById = languagesById;
105: this .languagesByName = languagesByName;
106: this .languages = new LanguagesImpl(languages);
107: this .languagesLoaded = true;
108: } finally {
109: languagesLock.unlock();
110: }
111: }
112:
113: public Branch getBranch(long id) throws RepositoryException {
114: assureBranchesLoaded();
115: Branch branch = (Branch) branchesById.get(new Long(id));
116: if (branch == null)
117: throw new BranchNotFoundException(id);
118: return branch;
119: }
120:
121: public Branch getBranchByName(String name)
122: throws RepositoryException {
123: assureBranchesLoaded();
124: Branch branch = (Branch) branchesByName.get(name);
125: if (branch == null)
126: throw new BranchNotFoundException(name);
127: return branch;
128: }
129:
130: public Branches getBranches() throws RepositoryException {
131: assureBranchesLoaded();
132: return branches;
133: }
134:
135: public Language getLanguage(long id) throws RepositoryException {
136: assureLanguagesLoaded();
137: Language language = (Language) languagesById.get(new Long(id));
138: if (language == null)
139: throw new LanguageNotFoundException(id);
140: return language;
141: }
142:
143: public Language getLanguageByName(String name)
144: throws RepositoryException {
145: assureLanguagesLoaded();
146: Language language = (Language) languagesByName.get(name);
147: if (language == null)
148: throw new LanguageNotFoundException(name);
149: return language;
150: }
151:
152: public Languages getLanguages() throws RepositoryException {
153: assureLanguagesLoaded();
154: return languages;
155: }
156:
157: public void repositoryEvent(RepositoryEventType eventType,
158: Object id, long updateCount) {
159: if (eventType.isBranchEvent()) {
160: branchesLoaded = false;
161: } else if (eventType.isLanguageEvent()) {
162: languagesLoaded = false;
163: }
164: }
165:
166: }
|