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: MemoryDirectoryFactory.java 30374 2008-02-20 16:31:28Z gracinet $
020 */
021
022package org.nuxeo.ecm.directory.memory;
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.nuxeo.ecm.directory.Directory;
030import org.nuxeo.ecm.directory.DirectoryException;
031import org.nuxeo.ecm.directory.DirectoryFactory;
032import org.nuxeo.ecm.directory.api.DirectoryService;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author Florent Guillaume
037 */
038public class MemoryDirectoryFactory implements DirectoryFactory {
039
040    private final Map<String, MemoryDirectory> directories;
041
042    private final DirectoryService directoryService;
043
044    public MemoryDirectoryFactory() throws DirectoryException {
045        directories = new HashMap<String, MemoryDirectory>();
046        directoryService = Framework.getService(DirectoryService.class);
047    }
048
049    public String getName() {
050        return "memdirs";
051    }
052
053    public void registerDirectory(MemoryDirectory directory) {
054        String directoryName = directory.getName();
055        directories.put(directoryName, directory);
056        directoryService.registerDirectory(directoryName, this);
057    }
058
059    public void unregisterDirectory(MemoryDirectory directory) {
060        String directoryName = directory.getName();
061        directoryService.unregisterDirectory(directoryName, this);
062        directories.remove(directoryName);
063    }
064
065    public Directory getDirectory(String name) {
066        return directories.get(name);
067    }
068
069    public void shutdown() {
070    }
071
072    public List<Directory> getDirectories() {
073        return new ArrayList<Directory>(directories.values());
074    }
075
076}