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.Collection; 025import java.util.HashSet; 026import java.util.Map; 027import java.util.Set; 028 029import org.nuxeo.ecm.core.schema.SchemaManager; 030import org.nuxeo.ecm.core.schema.types.Field; 031import org.nuxeo.ecm.core.schema.types.Schema; 032import org.nuxeo.ecm.directory.AbstractDirectory; 033import org.nuxeo.ecm.directory.DirectoryException; 034import org.nuxeo.ecm.directory.Session; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * @author Florent Guillaume 039 */ 040public class MemoryDirectory extends AbstractDirectory { 041 042 public final String schemaName; 043 044 public final Set<String> schemaSet; 045 046 public final String idField; 047 048 public final String passwordField; 049 050 public Map<String, Object> map; 051 052 public MemoryDirectorySession session; 053 054 protected boolean isReadOnly = false; 055 056 public MemoryDirectory(String name, String schema, String idField, String passwordField) throws DirectoryException { 057 this(name, schema, new HashSet<String>(), idField, passwordField); 058 059 SchemaManager sm = getSchemaManager(); 060 Schema sch = sm.getSchema(schema); 061 if (sch == null) { 062 throw new DirectoryException("Unknown schema :" + schema); 063 } 064 Collection<Field> fields = sch.getFields(); 065 for (Field f : fields) { 066 schemaSet.add(f.getName().getLocalName()); 067 } 068 } 069 070 public SchemaManager getSchemaManager() throws DirectoryException { 071 SchemaManager sm = Framework.getService(SchemaManager.class); 072 if (sm == null) { 073 throw new DirectoryException("Unable to look up type service"); 074 } 075 return sm; 076 } 077 078 public MemoryDirectory(String name, String schemaName, Set<String> schemaSet, String idField, String passwordField) { 079 super(name); 080 this.schemaName = schemaName; 081 this.schemaSet = schemaSet; 082 this.idField = idField; 083 this.passwordField = passwordField; 084 } 085 086 @Override 087 public String getName() { 088 return name; 089 } 090 091 @Override 092 public String getSchema() { 093 return schemaName; 094 } 095 096 @Override 097 public String getParentDirectory() { 098 return null; 099 } 100 101 @Override 102 public String getIdField() { 103 return idField; 104 } 105 106 @Override 107 public String getPasswordField() { 108 return passwordField; 109 } 110 111 @Override 112 public Session getSession() { 113 if (session == null) { 114 session = new MemoryDirectorySession(this); 115 } 116 addSession(session); 117 return session; 118 } 119 120 @Override 121 public void shutdown() { 122 super.shutdown(); 123 session = null; 124 } 125 126 public boolean isReadOnly() { 127 return isReadOnly; 128 } 129 130 public void setReadOnly(boolean isReadOnly) { 131 this.isReadOnly = isReadOnly; 132 } 133 134}