}
public void internalAdd(Entity entity, DistinguishedName dn) throws SecurityException
{
if (entityExists(entity)) { throw new SecurityException(SecurityException.PRINCIPAL_ALREADY_EXISTS.createScoped(entity.getType(), entity.getId())); }
DirContextAdapter context = new DirContextAdapter();
if (dn != null)
{
dn.add(configuration.getLdapIdAttribute(), entity.getId());
for (AttributeDef attrDef : configuration.getAttributeDefinitions())
{
Attribute entityAttr = entity.getAttribute(attrDef.getName());
BasicAttribute basicAttr = null;
if (entityAttr != null)
{
if (attrDef.isMultiValue())
{
Collection<String> entityAttrValues = entityAttr.getValues();
if (entityAttrValues != null && entityAttrValues.size() > 0)
{
basicAttr = new BasicAttribute(attrDef.getName());
for (String val : entityAttrValues)
{
basicAttr.add(val);
}
}
} else
{
basicAttr = new BasicAttribute(attrDef.getName());
basicAttr.add(entityAttr.getValue());
}
} else
{
if (attrDef.isIdAttribute())
{
basicAttr = new BasicAttribute(attrDef.getName());
basicAttr.add(entity.getId());
} else if (attrDef.isRequired())
{
String requiredValue = attrDef.getRequiredDefaultValue();
if (requiredValue != null && requiredValue.length() > 0)
{
basicAttr = new BasicAttribute(attrDef.getName());
basicAttr.add(attrDef.getRequiredDefaultValue());
}
} else
{
// TODO missing required attribute value, throw
// exception
// return;
}
}
if (basicAttr != null)
{
context.setAttribute(basicAttr);
}
}
BasicAttribute attr = new BasicAttribute("objectClass");
for (String objClass : configuration.getObjectClassesArray())
{
attr.add(objClass);
}
context.setAttribute(attr);
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());