001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Florent Guillaume
018 *
019 * $Id: MultiDirectory.java 25713 2007-10-05 16:06:58Z fguillaume $
020 */
021
022package org.nuxeo.ecm.directory.multi;
023
024import java.util.Collections;
025import java.util.List;
026
027import org.nuxeo.ecm.directory.AbstractDirectory;
028import org.nuxeo.ecm.directory.Directory;
029import org.nuxeo.ecm.directory.DirectoryException;
030import org.nuxeo.ecm.directory.Reference;
031import org.nuxeo.ecm.directory.Session;
032import org.nuxeo.ecm.directory.api.DirectoryService;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author Florent Guillaume
037 */
038public class MultiDirectory extends AbstractDirectory {
039
040    // used in double-checked locking for lazy init
041    protected volatile boolean initialized;
042
043    public MultiDirectory(MultiDirectoryDescriptor descriptor) {
044        super(descriptor, MultiReference.class);
045    }
046
047    @Override
048    public MultiDirectoryDescriptor getDescriptor() {
049        return (MultiDirectoryDescriptor) descriptor;
050    }
051
052    @Override
053    public Session getSession() throws DirectoryException {
054        MultiDirectorySession session = new MultiDirectorySession(this);
055        initializeIfNeeded();
056        addSession(session);
057        return session;
058    }
059
060    protected void initializeIfNeeded() {
061        if (!initialized) {
062            synchronized (this) {
063                if (!initialized) {
064                    initSchemaFieldMap();
065                    initialized = true;
066                }
067            }
068        }
069    }
070
071    @Override
072    public List<Reference> getReferences(String referenceFieldName) {
073        Reference reference = new MultiReference(this, referenceFieldName);
074        return Collections.singletonList(reference);
075    }
076
077    @Override
078    public void invalidateDirectoryCache() throws DirectoryException {
079        DirectoryService dirService = Framework.getService(DirectoryService.class);
080        getCache().invalidateAll();
081        // and also invalidates the cache from the source directories
082        for (SourceDescriptor src : getDescriptor().sources) {
083            for (SubDirectoryDescriptor sub : src.subDirectories) {
084                Directory dir = dirService.getDirectory(sub.name);
085                if (dir != null) {
086                    dir.invalidateDirectoryCache();
087                }
088            }
089        }
090    }
091
092}