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.namespace;
017:
018: import org.outerj.daisy.repository.*;
019: import org.outerj.daisy.repository.commonimpl.RepositoryStrategy;
020: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
021: import org.outerj.daisy.repository.namespace.Namespace;
022: import org.outerj.daisy.repository.namespace.NamespaceNotFoundException;
023:
024: import java.util.Map;
025: import java.util.HashMap;
026:
027: public class NamespaceCache implements RepositoryListener {
028: private final RepositoryStrategy repositoryStrategy;
029: private final AuthenticatedUser cacheUser;
030: private boolean cacheLoaded = false;
031: private Map namespacesByName = new HashMap();
032: private Map namespacesById = new HashMap();
033:
034: private String repositoryNamespace;
035:
036: public NamespaceCache(RepositoryStrategy repositoryStrategy,
037: AuthenticatedUser cacheUser) {
038: this .repositoryStrategy = repositoryStrategy;
039: this .cacheUser = cacheUser;
040: }
041:
042: public String getRepositoryNamespace() {
043: assureCacheLoaded();
044: return repositoryNamespace;
045: }
046:
047: public Namespace getNamespace(String namespaceName)
048: throws NamespaceNotFoundException {
049: assureCacheLoaded();
050:
051: Namespace namespace = (Namespace) namespacesByName
052: .get(namespaceName);
053: if (namespace == null)
054: throw new NamespaceNotFoundException(namespaceName);
055: else
056: return namespace;
057: }
058:
059: public Namespace getNamespace(long id)
060: throws NamespaceNotFoundException {
061: assureCacheLoaded();
062:
063: Namespace namespace = (Namespace) namespacesById.get(new Long(
064: id));
065: if (namespace == null)
066: throw new NamespaceNotFoundException(id);
067: else
068: return namespace;
069: }
070:
071: private void assureCacheLoaded() {
072: if (cacheLoaded)
073: return;
074:
075: synchronized (this ) {
076: if (cacheLoaded)
077: return;
078:
079: try {
080: Map newNamespacesByName = new HashMap();
081: Map newNamespacesById = new HashMap();
082: Namespace[] namespaces = repositoryStrategy
083: .getAllNamespaces(cacheUser);
084: for (int i = 0; i < namespaces.length; i++) {
085: newNamespacesByName.put(namespaces[i].getName(),
086: namespaces[i]);
087: newNamespacesById.put(new Long(namespaces[i]
088: .getId()), namespaces[i]);
089: }
090: this .namespacesByName = newNamespacesByName;
091: this .namespacesById = newNamespacesById;
092:
093: repositoryNamespace = repositoryStrategy
094: .getRepositoryNamespace(cacheUser);
095: } catch (RepositoryException e) {
096: throw new RepositoryRuntimeException(
097: "Error loading namespace cache.", e);
098: }
099:
100: cacheLoaded = true;
101: }
102: }
103:
104: public void repositoryEvent(RepositoryEventType eventType,
105: Object id, long updateCount) {
106: if (eventType.isNamespaceEvent()) {
107: synchronized (this ) {
108: cacheLoaded = false;
109: }
110: }
111: }
112:
113: }
|