001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 *
017 * $Id: MemoryDirectory.java 30381 2008-02-20 20:12:09Z gracinet $
018 */
019
020package org.nuxeo.ecm.directory.memory;
021
022import java.util.Collection;
023import java.util.HashSet;
024import java.util.Map;
025import java.util.Set;
026
027import org.nuxeo.ecm.core.schema.SchemaManager;
028import org.nuxeo.ecm.core.schema.types.Field;
029import org.nuxeo.ecm.core.schema.types.Schema;
030import org.nuxeo.ecm.directory.AbstractDirectory;
031import org.nuxeo.ecm.directory.DirectoryException;
032import org.nuxeo.ecm.directory.Session;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author Florent Guillaume
037 */
038public class MemoryDirectory extends AbstractDirectory {
039
040    public final String schemaName;
041
042    public final Set<String> schemaSet;
043
044    public final String idField;
045
046    public final String passwordField;
047
048    public Map<String, Object> map;
049
050    public MemoryDirectorySession session;
051
052    protected boolean isReadOnly = false;
053
054    public MemoryDirectory(String name, String schema, String idField, String passwordField) throws DirectoryException {
055        this(name, schema, new HashSet<String>(), idField, passwordField);
056
057        SchemaManager sm = getSchemaManager();
058        Schema sch = sm.getSchema(schema);
059        if (sch == null) {
060            throw new DirectoryException("Unknown schema :" + schema);
061        }
062        Collection<Field> fields = sch.getFields();
063        for (Field f : fields) {
064            schemaSet.add(f.getName().getLocalName());
065        }
066    }
067
068    public SchemaManager getSchemaManager() throws DirectoryException {
069        SchemaManager sm = Framework.getService(SchemaManager.class);
070        if (sm == null) {
071            throw new DirectoryException("Unable to look up type service");
072        }
073        return sm;
074    }
075
076    public MemoryDirectory(String name, String schemaName, Set<String> schemaSet, String idField, String passwordField) {
077        super(name);
078        this.schemaName = schemaName;
079        this.schemaSet = schemaSet;
080        this.idField = idField;
081        this.passwordField = passwordField;
082    }
083
084    @Override
085    public String getName() {
086        return name;
087    }
088
089    @Override
090    public String getSchema() {
091        return schemaName;
092    }
093
094    @Override
095    public String getParentDirectory() {
096        return null;
097    }
098
099    @Override
100    public String getIdField() {
101        return idField;
102    }
103
104    @Override
105    public String getPasswordField() {
106        return passwordField;
107    }
108
109    @Override
110    public Session getSession() {
111        if (session == null) {
112            session = new MemoryDirectorySession(this);
113        }
114        addSession(session);
115        return session;
116    }
117
118    @Override
119    public void shutdown() {
120        super.shutdown();
121        session = null;
122    }
123
124    public boolean isReadOnly() {
125        return isReadOnly;
126    }
127
128    public void setReadOnly(boolean isReadOnly) {
129        this.isReadOnly = isReadOnly;
130    }
131
132}