]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java
Improve documentation
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / LdifUserAdmin.java
index 608a1f7518edd88eae8450c6e3f2e8fccac51167..a98422245d3a4b9d8c6034217a934325c68f8a03 100644 (file)
@@ -5,10 +5,9 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -19,71 +18,52 @@ import javax.naming.InvalidNameException;
 import javax.naming.NamingEnumeration;
 import javax.naming.directory.Attributes;
 import javax.naming.ldap.LdapName;
+import javax.transaction.TransactionManager;
 
 import org.apache.commons.io.IOUtils;
 import org.osgi.framework.Filter;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
 
-/** User admin implementation using LDIF file(s) as backend. */
+/**
+ * A user admin based on a LDIF files. Requires a {@link TransactionManager} and
+ * an open transaction for write access.
+ */
 public class LdifUserAdmin extends AbstractUserDirectory {
-       SortedMap<LdapName, DirectoryUser> users = new TreeMap<LdapName, DirectoryUser>();
-       SortedMap<LdapName, DirectoryGroup> groups = new TreeMap<LdapName, DirectoryGroup>();
+       private SortedMap<LdapName, DirectoryUser> users = new TreeMap<LdapName, DirectoryUser>();
+       private SortedMap<LdapName, DirectoryGroup> groups = new TreeMap<LdapName, DirectoryGroup>();
 
        private Map<String, Map<String, DirectoryUser>> userIndexes = new LinkedHashMap<String, Map<String, DirectoryUser>>();
 
-       // private Map<LdapName, List<LdifGroup>> directMemberOf = new
-       // TreeMap<LdapName, List<LdifGroup>>();
-
-       public LdifUserAdmin(String uri) {
-               this(uri, readOnlyDefault(uri));
-       }
-
-       public LdifUserAdmin(String uri, boolean isReadOnly) {
-               setReadOnly(isReadOnly);
-               try {
-                       setUri(new URI(uri));
-               } catch (URISyntaxException e) {
-                       throw new UserDirectoryException("Invalid URI " + uri, e);
-               }
-
-               if (!isReadOnly && !getUri().getScheme().equals("file"))
-                       throw new UnsupportedOperationException(getUri().getScheme()
-                                       + " not supported read-write.");
-
+       public LdifUserAdmin(String uri, String baseDn) {
+               this(fromUri(uri, baseDn));
        }
 
-       public LdifUserAdmin(URI uri, boolean isReadOnly) {
-               setReadOnly(isReadOnly);
-               setUri(uri);
-               if (!isReadOnly && !getUri().getScheme().equals("file"))
-                       throw new UnsupportedOperationException(getUri().getScheme()
-                                       + " not supported read-write.");
-
+       public LdifUserAdmin(Dictionary<String, ?> properties) {
+               super(properties);
        }
 
        public LdifUserAdmin(InputStream in) {
+               super(new Hashtable<String, Object>());
                load(in);
                setReadOnly(true);
                setUri(null);
        }
 
-       private static boolean readOnlyDefault(String uriStr) {
-               URI uri;
-               try {
-                       uri = new URI(uriStr);
-               } catch (Exception e) {
-                       throw new UserDirectoryException("Invalid URI " + uriStr, e);
-               }
-               if (uri.getScheme().equals("file")) {
-                       File file = new File(uri);
-                       return !file.canWrite();
-               }
-               return true;
+       private static Dictionary<String, Object> fromUri(String uri, String baseDn) {
+               Hashtable<String, Object> res = new Hashtable<String, Object>();
+               res.put(UserAdminConf.uri.property(), uri);
+               res.put(UserAdminConf.baseDn.property(), baseDn);
+               return res;
        }
 
        public void init() {
                try {
+                       if (getUri().getScheme().equals("file")) {
+                               File file = new File(getUri());
+                               if (!file.exists())
+                                       return;
+                       }
                        load(getUri().toURL().openStream());
                } catch (Exception e) {
                        throw new UserDirectoryException("Cannot open URL " + getUri(), e);
@@ -113,6 +93,7 @@ public class LdifUserAdmin extends AbstractUserDirectory {
                }
        }
 
+       @SuppressWarnings("unchecked")
        protected void load(InputStream in) {
                try {
                        users.clear();
@@ -136,16 +117,12 @@ public class LdifUserAdmin extends AbstractUserDirectory {
                                }
                        }
 
-                       // optimise
-                       // for (LdifGroup group : groups.values())
-                       // loadMembers(group);
-
                        // indexes
                        for (String attr : getIndexedUserProperties())
                                userIndexes.put(attr, new TreeMap<String, DirectoryUser>());
 
                        for (DirectoryUser user : users.values()) {
-                               Dictionary<String, Object> properties = user.getProperties();
+                               Dictionary<String, ?> properties = user.getProperties();
                                for (String attr : getIndexedUserProperties()) {
                                        Object value = properties.get(attr);
                                        if (value != null) {
@@ -184,25 +161,7 @@ public class LdifUserAdmin extends AbstractUserDirectory {
                return users.containsKey(dn) || groups.containsKey(dn);
        }
 
-       // @Override
-       // public boolean removeRole(String name) {
-       // LdapName dn = toDn(name);
-       // LdifUser role = null;
-       // if (users.containsKey(dn))
-       // role = users.remove(dn);
-       // else if (groups.containsKey(dn))
-       // role = groups.remove(dn);
-       // else
-       // throw new UserDirectoryException("There is no role " + name);
-       // if (role == null)
-       // return false;
-       // for (LdifGroup group : getDirectGroups(role)) {
-       // group.getAttributes().get(getMemberAttributeId())
-       // .remove(dn.toString());
-       // }
-       // return true;
-       // }
-
+       @SuppressWarnings("unchecked")
        protected List<DirectoryUser> doGetRoles(Filter f) {
                ArrayList<DirectoryUser> res = new ArrayList<DirectoryUser>();
                if (f == null) {