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    public MultiDirectory(MultiDirectoryDescriptor descriptor) {
041        super(descriptor, MultiReference.class);
042    }
043
044    @Override
045    public MultiDirectoryDescriptor getDescriptor() {
046        return (MultiDirectoryDescriptor) descriptor;
047    }
048
049    @Override
050    public Session getSession() throws DirectoryException {
051        MultiDirectorySession session = new MultiDirectorySession(this);
052        addSession(session);
053        return session;
054    }
055
056    @Override
057    public List<Reference> getReferences(String referenceFieldName) {
058        Reference reference = new MultiReference(this, referenceFieldName);
059        return Collections.singletonList(reference);
060    }
061
062    @Override
063    public void invalidateDirectoryCache() throws DirectoryException {
064        DirectoryService dirService = Framework.getService(DirectoryService.class);
065        getCache().invalidateAll();
066        // and also invalidates the cache from the source directories
067        for (SourceDescriptor src : getDescriptor().sources) {
068            for (SubDirectoryDescriptor sub : src.subDirectories) {
069                Directory dir = dirService.getDirectory(sub.name);
070                if (dir != null) {
071                    dir.invalidateDirectoryCache();
072                }
073            }
074        }
075    }
076
077}