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