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: MemoryDirectory.java 30381 2008-02-20 20:12:09Z gracinet $
020 */
021
022package org.nuxeo.ecm.directory.memory;
023
024import java.util.HashSet;
025import java.util.Map;
026import java.util.Set;
027
028import org.nuxeo.ecm.core.schema.SchemaManager;
029import org.nuxeo.ecm.core.schema.types.Field;
030import org.nuxeo.ecm.core.schema.types.Schema;
031import org.nuxeo.ecm.directory.AbstractDirectory;
032import org.nuxeo.ecm.directory.DirectoryException;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author Florent Guillaume
037 */
038public class MemoryDirectory extends AbstractDirectory {
039
040    public final Set<String> schemaSet;
041
042    public Map<String, Object> map;
043
044    public MemoryDirectorySession session;
045
046    public MemoryDirectory(MemoryDirectoryDescriptor descriptor) {
047        super(descriptor, null);
048        Set<String> schemaSet = descriptor.schemaSet;
049        if (schemaSet == null) {
050            Schema schema = Framework.getService(SchemaManager.class).getSchema(getSchema());
051            if (schema == null) {
052                throw new DirectoryException("Unknown schema :" + getSchema());
053            }
054            schemaSet = new HashSet<>();
055            for (Field field : schema.getFields()) {
056                schemaSet.add(field.getName().getLocalName());
057            }
058        }
059        this.schemaSet = schemaSet;
060    }
061
062    @Override
063    public MemoryDirectoryDescriptor getDescriptor() {
064        return (MemoryDirectoryDescriptor) descriptor;
065    }
066
067    @Override
068    public MemoryDirectorySession getSession() {
069        if (session == null) {
070            session = new MemoryDirectorySession(this);
071        }
072        addSession(session);
073        return session;
074    }
075
076    @Override
077    public void shutdown() {
078        super.shutdown();
079        session = null;
080    }
081
082}