import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.ldap.LdapName;
-import javax.transaction.TransactionManager;
import org.argeo.naming.LdifParser;
import org.argeo.naming.LdifWriter;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;
-/**
- * A user admin based on a LDIF files. Requires a {@link TransactionManager} and
- * an open transaction for write access.
- */
+/** A user admin based on a LDIF files. */
public class LdifUserAdmin extends AbstractUserDirectory {
private SortedMap<LdapName, DirectoryUser> users = new TreeMap<LdapName, DirectoryUser>();
private SortedMap<LdapName, DirectoryGroup> groups = new TreeMap<LdapName, DirectoryGroup>();
public LdifUserAdmin(String uri, String baseDn) {
- this(fromUri(uri, baseDn));
+ this(fromUri(uri, baseDn), false);
}
public LdifUserAdmin(Dictionary<String, ?> properties) {
- super(null, properties);
+ this(properties, false);
+ }
+
+ protected LdifUserAdmin(Dictionary<String, ?> properties, boolean scoped) {
+ super(null, properties, scoped);
}
public LdifUserAdmin(URI uri, Dictionary<String, ?> properties) {
- super(uri, properties);
+ super(uri, properties, false);
}
@Override
}
Dictionary<String, Object> properties = cloneProperties();
properties.put(UserAdminConf.readOnly.name(), "true");
- LdifUserAdmin scopedUserAdmin = new LdifUserAdmin(properties);
+ LdifUserAdmin scopedUserAdmin = new LdifUserAdmin(properties, true);
scopedUserAdmin.groups = Collections.unmodifiableSortedMap(groups);
scopedUserAdmin.users = Collections.unmodifiableSortedMap(users);
return scopedUserAdmin;
}
public void init() {
+
try {
- if (getUri().getScheme().equals("file")) {
- File file = new File(getUri());
+ URI u = new URI(getUri());
+ if (u.getScheme().equals("file")) {
+ File file = new File(u);
if (!file.exists())
return;
}
- load(getUri().toURL().openStream());
+ load(u.toURL().openStream());
} catch (Exception e) {
throw new UserDirectoryException("Cannot open URL " + getUri(), e);
}
throw new UserDirectoryException("Cannot save LDIF user admin: no URI is set");
if (isReadOnly())
throw new UserDirectoryException("Cannot save LDIF user admin: " + getUri() + " is read-only");
- try (FileOutputStream out = new FileOutputStream(new File(getUri()))) {
+ try (FileOutputStream out = new FileOutputStream(new File(new URI(getUri())))) {
save(out);
- } catch (IOException e) {
+ } catch (IOException | URISyntaxException e) {
throw new UserDirectoryException("Cannot save user admin to " + getUri(), e);
}
}