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.Reference;
030import org.nuxeo.ecm.directory.api.DirectoryService;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @author Florent Guillaume
035 */
036public class MultiDirectory extends AbstractDirectory {
037
038    public MultiDirectory(MultiDirectoryDescriptor descriptor) {
039        super(descriptor, MultiReference.class);
040    }
041
042    @Override
043    public MultiDirectoryDescriptor getDescriptor() {
044        return (MultiDirectoryDescriptor) descriptor;
045    }
046
047    @Override
048    public MultiDirectorySession getSession() {
049        MultiDirectorySession session = new MultiDirectorySession(this);
050        addSession(session);
051        return session;
052    }
053
054    @Override
055    public List<Reference> getReferences(String referenceFieldName) {
056        Reference reference = new MultiReference(this, referenceFieldName);
057        return Collections.singletonList(reference);
058    }
059
060    @Override
061    public void invalidateDirectoryCache() {
062        DirectoryService dirService = Framework.getService(DirectoryService.class);
063        getCache().invalidateAll();
064        // and also invalidates the cache from the source directories
065        for (SourceDescriptor src : getDescriptor().sources) {
066            for (SubDirectoryDescriptor sub : src.subDirectories) {
067                Directory dir = dirService.getDirectory(sub.name);
068                if (dir != null) {
069                    dir.invalidateDirectoryCache();
070                }
071            }
072        }
073    }
074
075}