Introduce Argeo 2 security model-
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / osgi / useradmin / LdifUserAdmin.java
index 8cc7cb39581f5a8f4d50c47919e828969369fb31..e2cf903fca2c17a959c3e065c9927d097e684fdf 100644 (file)
@@ -1,6 +1,8 @@
 package org.argeo.osgi.useradmin;
 
 import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
@@ -15,11 +17,44 @@ import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
 import org.osgi.service.useradmin.UserAdmin;
 
+/** User admin implementation using LDIF file(s) as backend. */
 public class LdifUserAdmin implements UserAdmin {
        SortedMap<LdapName, LdifUser> users = new TreeMap<LdapName, LdifUser>();
        SortedMap<LdapName, LdifGroup> groups = new TreeMap<LdapName, LdifGroup>();
 
+       private final boolean isReadOnly;
+       private final URI uri;
+
+       public LdifUserAdmin(String uri) {
+               this(uri, true);
+       }
+
+       public LdifUserAdmin(String uri, boolean isReadOnly) {
+               this.isReadOnly = isReadOnly;
+               try {
+                       this.uri = new URI(uri);
+               } catch (URISyntaxException e) {
+                       throw new ArgeoUserAdminException("Invalid URI " + uri, e);
+               }
+
+               if (!isReadOnly && !this.uri.getScheme().equals("file:"))
+                       throw new UnsupportedOperationException(this.uri.getScheme()
+                                       + "not supported read-write.");
+
+               try {
+                       load(this.uri.toURL().openStream());
+               } catch (Exception e) {
+                       throw new ArgeoUserAdminException("Cannot open URL " + this.uri, e);
+               }
+       }
+
        public LdifUserAdmin(InputStream in) {
+               load(in);
+               isReadOnly = true;
+               this.uri = null;
+       }
+
+       protected void load(InputStream in) {
                try {
                        LdifParser ldifParser = new LdifParser();
                        SortedMap<LdapName, Attributes> allEntries = ldifParser.read(in);
@@ -45,10 +80,17 @@ public class LdifUserAdmin implements UserAdmin {
                        }
                } catch (Exception e) {
                        throw new ArgeoUserAdminException(
-                                       "Cannot initialise user admin service from LDIF", e);
+                                       "Cannot load user admin service from LDIF", e);
                }
        }
 
+       public void destroy() {
+               users.clear();
+               users = null;
+               groups.clear();
+               groups = null;
+       }
+
        @Override
        public Role getRole(String name) {
                LdapName key;
@@ -92,4 +134,8 @@ public class LdifUserAdmin implements UserAdmin {
                throw new UnsupportedOperationException();
        }
 
+       public boolean getIsReadOnly() {
+               return isReadOnly;
+       }
+
 }