}
protected JetspeedPrincipal synchronizePrincipalAttributes(Entity entity)
{
JetspeedPrincipal updatedPrincipal = null;
JetspeedPrincipalManager principalManager = principalManagerProvider.getManager(principalManagerProvider.getPrincipalType(entity.getType()));
if (logger.isDebugEnabled()){
logger.debug("--- Synchronize principal attributes ---");
}
if (principalManager != null)
{
updatedPrincipal = principalManager.getPrincipal(entity.getId());
Map<String, Attribute> mappedEntityAttrs = entity.getMappedAttributes();
Collection<Attribute> attrsToBeUpdated = new ArrayList<Attribute>();
if (updatedPrincipal == null)
{
// principal does not exist yet, create it using the Jetspeed
// principal manager
updatedPrincipal = principalManager.newPrincipal(entity.getId(), true);
try
{
principalManager.addPrincipal(updatedPrincipal, null);
if (logger.isDebugEnabled()){
logger.debug("Adding principal "+updatedPrincipal.getName()+" of type "+updatedPrincipal.getType().getName()+" ...");
}
}
catch (SecurityException sexp)
{
if (logger.isErrorEnabled())
{
logger.error("Unexpected exception in adding new pricipal of type " + updatedPrincipal.getType().getName() + ".", sexp);
}
}
attrsToBeUpdated.addAll(mappedEntityAttrs.values());
}
else if (updatedPrincipal.isMapped())
{
if (logger.isDebugEnabled()){
logger.debug("Updating principal "+updatedPrincipal.getName()+" of type "+updatedPrincipal.getType().getName()+" ...");
}
SecurityAttributes principalAttrs = updatedPrincipal.getSecurityAttributes();
for (Map.Entry<String, Attribute> entityAttrEntry : mappedEntityAttrs.entrySet())
{
SecurityAttribute principalAttr = principalAttrs.getAttribute(entityAttrEntry.getKey());
Attribute entityAttr = entityAttrEntry.getValue();
if (principalAttr != null)
{
if (entityAttr.getDefinition().isMultiValue())
{
// TODO : multi-valued Principal attrs are not yet
// supported
}
else
{
if (!StringUtils.equals(principalAttr.getStringValue(), entityAttr.getValue()))
{
attrsToBeUpdated.add(entityAttr);
}
}
}
else
{
attrsToBeUpdated.add(entityAttr);
}
}
}
SecurityAttributes principalAttrs = updatedPrincipal.getSecurityAttributes();
Map<String, SecurityAttributeType> securityAttrTypes = principalAttrs.getSecurityAttributeTypes().getAttributeTypeMap();
// Step 1. update principal's attributes
for (Attribute addedEntityAttr : attrsToBeUpdated)
{
if (!addedEntityAttr.getDefinition().isMultiValue())
{
SecurityAttribute principalAttr = null;
try
{
SecurityAttributeType securityAttrType = securityAttrTypes.get(addedEntityAttr.getMappedName());
if (securityAttrType != null)
{
principalAttr = principalAttrs.getAttribute(addedEntityAttr.getMappedName(), true);
}
if (principalAttr != null)
principalAttr.setStringValue(addedEntityAttr.getValue());
if (logger.isDebugEnabled()){
logger.debug("Marked attribute "+principalAttr.getName()+" as updated for principal "+updatedPrincipal.getName()+". New value: "+principalAttr.getStringValue());
}
}
catch (SecurityException e)
{
if (logger.isErrorEnabled())
{
logger.error("Unexpected exception for attribute " + addedEntityAttr.getMappedName() + ".", e);
}
}
}
}
if (updatedPrincipal.isMapped())
{
boolean updated = (attrsToBeUpdated.size() > 0);
// Step 2, check whether attributes should be removed.
for (Map.Entry<String, SecurityAttribute> principalAttrEntry : principalAttrs.getAttributeMap().entrySet())
{
// TODO: check whether this attribute is mapped
if (!mappedEntityAttrs.containsKey(principalAttrEntry.getKey()))
{
try
{
principalAttrs.removeAttribute(principalAttrEntry.getKey());
updated = true;
if (logger.isDebugEnabled()){
logger.debug("Marked attribute "+principalAttrEntry.getKey()+" as removed for principal "+updatedPrincipal.getName());
}
}
catch (SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// step 3, update synchronized principal
if (updated)
{
try
{
principalManager.updatePrincipal(updatedPrincipal);
if (logger.isDebugEnabled()){
logger.debug("Committing attribute changes for principal "+updatedPrincipal.getName());
}
}
catch (SecurityException e)