import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
-import javax.transaction.xa.Xid;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.useradmin.UserAdmin;
/** Base class for a {@link UserDirectory}. */
-abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
- private final static Log log = LogFactory
- .getLog(AbstractUserDirectory.class);
+public abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
+ private final static Log log = LogFactory.getLog(AbstractUserDirectory.class);
private final Hashtable<String, Object> properties;
- private final String baseDn;
- private final String userObjectClass;
- private final String groupObjectClass;
+ private final LdapName baseDn;
+ private final String userObjectClass, userBase, groupObjectClass, groupBase;
private final boolean readOnly;
private final URI uri;
private UserAdmin externalRoles;
- private List<String> indexedUserProperties = Arrays.asList(new String[] {
- LdifName.uid.name(), LdifName.mail.name(), LdifName.cn.name() });
+ private List<String> indexedUserProperties = Arrays
+ .asList(new String[] { LdifName.uid.name(), LdifName.mail.name(), LdifName.cn.name() });
private String memberAttributeId = "member";
- private List<String> credentialAttributeIds = Arrays
- .asList(new String[] { LdifName.userpassword.name() });
+ private List<String> credentialAttributeIds = Arrays.asList(new String[] { LdifName.userPassword.name() });
+ // JTA
private TransactionManager transactionManager;
- private ThreadLocal<UserDirectoryWorkingCopy> workingCopy = new ThreadLocal<UserDirectoryWorkingCopy>();
- private Xid editingTransactionXid = null;
+ private WcXaResource xaResource = new WcXaResource(this);
- AbstractUserDirectory(Dictionary<String, ?> props) {
+ public AbstractUserDirectory(Dictionary<String, ?> props) {
properties = new Hashtable<String, Object>();
for (Enumeration<String> keys = props.keys(); keys.hasMoreElements();) {
String key = keys.nextElement();
try {
uri = new URI(uriStr);
} catch (URISyntaxException e) {
- throw new UserDirectoryException("Badly formatted URI "
- + uriStr, e);
+ throw new UserDirectoryException("Badly formatted URI " + uriStr, e);
}
- baseDn = UserAdminConf.baseDn.getValue(properties).toString();
+ try {
+ baseDn = new LdapName(UserAdminConf.baseDn.getValue(properties));
+ } catch (InvalidNameException e) {
+ throw new UserDirectoryException("Badly formated base DN " + UserAdminConf.baseDn.getValue(properties), e);
+ }
String readOnlyStr = UserAdminConf.readOnly.getValue(properties);
if (readOnlyStr == null) {
readOnly = readOnlyDefault(uri);
- properties.put(UserAdminConf.readOnly.property(),
- Boolean.toString(readOnly));
+ properties.put(UserAdminConf.readOnly.name(), Boolean.toString(readOnly));
} else
readOnly = new Boolean(readOnlyStr);
userObjectClass = UserAdminConf.userObjectClass.getValue(properties);
+ userBase = UserAdminConf.userBase.getValue(properties);
groupObjectClass = UserAdminConf.groupObjectClass.getValue(properties);
+ groupBase = UserAdminConf.groupBase.getValue(properties);
}
/** Returns the groups this user is a direct member of. */
}
- boolean isEditing() {
- if (editingTransactionXid == null)
- return false;
- return workingCopy.get() != null;
+ protected boolean isEditing() {
+ return xaResource.wc() != null;
}
protected UserDirectoryWorkingCopy getWorkingCopy() {
- UserDirectoryWorkingCopy wc = workingCopy.get();
+ UserDirectoryWorkingCopy wc = xaResource.wc();
if (wc == null)
return null;
- if (wc.getXid() == null) {
- workingCopy.set(null);
- return null;
- }
return wc;
}
- void checkEdit() {
+ protected void checkEdit() {
Transaction transaction;
try {
transaction = transactionManager.getTransaction();
throw new UserDirectoryException("Cannot get transaction", e);
}
if (transaction == null)
- throw new UserDirectoryException(
- "A transaction needs to be active in order to edit");
- if (editingTransactionXid == null) {
- UserDirectoryWorkingCopy wc = new UserDirectoryWorkingCopy(this);
+ throw new UserDirectoryException("A transaction needs to be active in order to edit");
+ if (xaResource.wc() == null) {
try {
- transaction.enlistResource(wc);
- editingTransactionXid = wc.getXid();
- workingCopy.set(wc);
+ transaction.enlistResource(xaResource);
} catch (Exception e) {
- throw new UserDirectoryException("Cannot enlist " + wc, e);
+ throw new UserDirectoryException("Cannot enlist " + xaResource, e);
}
} else {
- if (workingCopy.get() == null)
- throw new UserDirectoryException("Transaction "
- + editingTransactionXid + " already editing");
- else if (!editingTransactionXid.equals(workingCopy.get().getXid()))
- throw new UserDirectoryException("Working copy Xid "
- + workingCopy.get().getXid() + " inconsistent with"
- + editingTransactionXid);
}
}
- List<Role> getAllRoles(DirectoryUser user) {
+ protected List<Role> getAllRoles(DirectoryUser user) {
List<Role> allRoles = new ArrayList<Role>();
if (user != null) {
collectRoles(user, allRoles);
@Override
public User getUser(String key, String value) {
// TODO check value null or empty
- List<DirectoryUser> collectedUsers = new ArrayList<DirectoryUser>(
- getIndexedUserProperties().size());
+ List<DirectoryUser> collectedUsers = new ArrayList<DirectoryUser>(getIndexedUserProperties().size());
if (key != null) {
doGetUser(key, value, collectedUsers);
} else {
if (collectedUsers.size() == 1)
return collectedUsers.get(0);
else if (collectedUsers.size() > 1)
- log.warn(collectedUsers.size() + " users for "
- + (key != null ? key + "=" : "") + value);
+ log.warn(collectedUsers.size() + " users for " + (key != null ? key + "=" : "") + value);
return null;
}
- protected void doGetUser(String key, String value,
- List<DirectoryUser> collectedUsers) {
+ protected void doGetUser(String key, String value, List<DirectoryUser> collectedUsers) {
try {
- Filter f = FrameworkUtil.createFilter("(&(" + objectClass + "="
- + getUserObjectClass() + ")(" + key + "=" + value + "))");
+ Filter f = FrameworkUtil.createFilter("(" + key + "=" + value + ")");
List<DirectoryUser> users = doGetRoles(f);
collectedUsers.addAll(users);
} catch (InvalidSyntaxException e) {
- throw new UserDirectoryException("Cannot get user with " + key
- + "=" + value, e);
+ throw new UserDirectoryException("Cannot get user with " + key + "=" + value, e);
}
}
@Override
public Authorization getAuthorization(User user) {
- return new LdifAuthorization((DirectoryUser) user,
- getAllRoles((DirectoryUser) user));
+ return new LdifAuthorization((DirectoryUser) user, getAllRoles((DirectoryUser) user));
}
@Override
checkEdit();
UserDirectoryWorkingCopy wc = getWorkingCopy();
LdapName dn = toDn(name);
- if ((daoHasRole(dn) && !wc.getDeletedUsers().containsKey(dn))
- || wc.getNewUsers().containsKey(dn))
+ if ((daoHasRole(dn) && !wc.getDeletedUsers().containsKey(dn)) || wc.getNewUsers().containsKey(dn))
throw new UserDirectoryException("Already a role " + name);
- BasicAttributes attrs = new BasicAttributes();
- attrs.put("dn", dn.toString());
+ BasicAttributes attrs = new BasicAttributes(true);
+ // attrs.put(LdifName.dn.name(), dn.toString());
Rdn nameRdn = dn.getRdn(dn.size() - 1);
// TODO deal with multiple attr RDN
attrs.put(nameRdn.getType(), nameRdn.getValue());
LdifUser newRole;
BasicAttribute objClass = new BasicAttribute(objectClass.name());
if (type == Role.USER) {
- String userObjClass = getUserObjectClass();
+ String userObjClass = newUserObjectClass(dn);
objClass.add(userObjClass);
if (inetOrgPerson.name().equals(userObjClass)) {
objClass.add(organizationalPerson.name());
} else if (organizationalPerson.name().equals(userObjClass)) {
objClass.add(person.name());
}
- objClass.add(top);
+ objClass.add(top.name());
attrs.put(objClass);
newRole = new LdifUser(this, dn, attrs);
} else if (type == Role.GROUP) {
- objClass.add(getGroupObjectClass());
- objClass.add(top);
+ String groupObjClass = getGroupObjectClass();
+ objClass.add(groupObjClass);
+ // objClass.add(LdifName.extensibleObject.name());
+ objClass.add(top.name());
attrs.put(objClass);
newRole = new LdifGroup(this, dn, attrs);
} else
}
for (LdapName groupDn : getDirectGroups(dn)) {
DirectoryUser group = doGetRole(groupDn);
- group.getAttributes().get(getMemberAttributeId())
- .remove(dn.toString());
+ group.getAttributes().get(getMemberAttributeId()).remove(dn.toString());
}
return actuallyDeleted;
}
}
- void clearEditingTransactionXid() {
- editingTransactionXid = null;
- }
-
// UTILITIES
protected LdapName toDn(String name) {
try {
}
// GETTERS
-
- String getMemberAttributeId() {
+ protected String getMemberAttributeId() {
return memberAttributeId;
}
- List<String> getCredentialAttributeIds() {
+ protected List<String> getCredentialAttributeIds() {
return credentialAttributeIds;
}
return readOnly;
}
- UserAdmin getExternalRoles() {
+ protected UserAdmin getExternalRoles() {
return externalRoles;
}
- public String getBaseDn() {
- return baseDn;
+ public LdapName getBaseDn() {
+ // always clone so that the property is not modified by reference
+ return (LdapName) baseDn.clone();
}
- protected String getUserObjectClass() {
+ /** dn can be null, in that case a default should be returned. */
+ public String getUserObjectClass() {
return userObjectClass;
}
- protected String getGroupObjectClass() {
+ public String getUserBase() {
+ return userBase;
+ }
+
+ protected String newUserObjectClass(LdapName dn) {
+ return getUserObjectClass();
+ }
+
+ public String getGroupObjectClass() {
return groupObjectClass;
}
- public Dictionary<String, ?> getProperties() {
+ public String getGroupBase() {
+ return groupBase;
+ }
+
+ public Dictionary<String, Object> getProperties() {
return properties;
}
this.transactionManager = transactionManager;
}
+ public WcXaResource getXaResource() {
+ return xaResource;
+ }
+
}