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.Collections;
025import java.util.HashSet;
026import java.util.LinkedHashMap;
027import java.util.Map;
028import java.util.Set;
029
030import org.nuxeo.ecm.core.schema.SchemaManager;
031import org.nuxeo.ecm.core.schema.types.Field;
032import org.nuxeo.ecm.core.schema.types.Schema;
033import org.nuxeo.ecm.directory.AbstractDirectory;
034import org.nuxeo.ecm.directory.DirectoryException;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @author Florent Guillaume
039 */
040public class MemoryDirectory extends AbstractDirectory {
041
042    public final Set<String> schemaSet;
043
044    protected final Map<String, Map<String, Object>> data;
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        data = Collections.synchronizedMap(new LinkedHashMap<String, Map<String, Object>>());
061    }
062
063    @Override
064    public MemoryDirectoryDescriptor getDescriptor() {
065        return (MemoryDirectoryDescriptor) descriptor;
066    }
067
068    @Override
069    public MemoryDirectorySession getSession() {
070        MemoryDirectorySession session = new MemoryDirectorySession(this);
071        addSession(session);
072        return session;
073    }
074
075}