]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/directory/ldap/AbstractLdapEntry.java
Decorrelate directory implementation from user admin
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / directory / ldap / AbstractLdapEntry.java
1 package org.argeo.util.directory.ldap;
2
3 import javax.naming.directory.Attributes;
4 import javax.naming.ldap.LdapName;
5
6 public abstract class AbstractLdapEntry implements LdapEntry {
7 private final AbstractLdapDirectory directory;
8
9 private final LdapName dn;
10
11 private Attributes publishedAttributes;
12
13 protected AbstractLdapEntry(AbstractLdapDirectory userAdmin, LdapName dn, Attributes attributes) {
14 this.directory = userAdmin;
15 this.dn = dn;
16 this.publishedAttributes = attributes;
17 }
18
19 @Override
20 public LdapName getDn() {
21 return dn;
22 }
23
24 public synchronized Attributes getAttributes() {
25 return isEditing() ? getModifiedAttributes() : publishedAttributes;
26 }
27
28 /** Should only be called from working copy thread. */
29 protected synchronized Attributes getModifiedAttributes() {
30 assert getWc() != null;
31 return getWc().getModifiedData().get(getDn());
32 }
33
34 protected synchronized boolean isEditing() {
35 return getWc() != null && getModifiedAttributes() != null;
36 }
37
38 private synchronized LdapEntryWorkingCopy getWc() {
39 return directory.getWorkingCopy();
40 }
41
42 protected synchronized void startEditing() {
43 // if (frozen)
44 // throw new IllegalStateException("Cannot edit frozen view");
45 if (directory.isReadOnly())
46 throw new IllegalStateException("User directory is read-only");
47 assert getModifiedAttributes() == null;
48 getWc().startEditing(this);
49 // modifiedAttributes = (Attributes) publishedAttributes.clone();
50 }
51
52 public synchronized void publishAttributes(Attributes modifiedAttributes) {
53 publishedAttributes = modifiedAttributes;
54 }
55
56 protected AbstractLdapDirectory getDirectory() {
57 return directory;
58 }
59
60 @Override
61 public int hashCode() {
62 return dn.hashCode();
63 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (this == obj)
68 return true;
69 if (obj instanceof LdapEntry) {
70 LdapEntry that = (LdapEntry) obj;
71 return this.dn.equals(that.getDn());
72 }
73 return false;
74 }
75
76 @Override
77 public String toString() {
78 return dn.toString();
79 }
80
81 }