if (credential == null)
{
throw new IllegalArgumentException();
}
HibernateIdentityObject hibernateObject = safeGet(ctx, identityObject);
Session hibernateSession = getHibernateSession(ctx);
if (supportedFeatures.isCredentialSupported(hibernateObject.getIdentityType(),credential.getType()))
{
HibernateIdentityObjectCredentialType hibernateCredentialType = getHibernateIdentityObjectCredentialType(ctx, credential.getType());
if (hibernateCredentialType == null)
{
throw new IllegalStateException("Credential type not present in this store: " + credential.getType().getName());
}
HibernateIdentityObjectCredential hibernateCredential = hibernateObject.getCredential(credential.getType());
if (hibernateCredential == null)
{
hibernateCredential = new HibernateIdentityObjectCredential();
hibernateCredential.setType(hibernateCredentialType);
hibernateObject.addCredential(hibernateCredential);
}
Object value = null;
// Handle generic impl
if (credential.getEncodedValue() != null)
{
value = credential.getEncodedValue();
}
else
{
//TODO: support for empty password should be configurable
value = credential.getValue();
}
if (value instanceof String)
{
hibernateCredential.setTextValue(value.toString());
}
else if (value instanceof byte[])
{
hibernateCredential.setBinaryValue((byte[])value);
}
else
{
throw new IdentityException("Not supported credential value: " + value.getClass());
}
hibernateSession.persist(hibernateCredential);
hibernateObject.addCredential(hibernateCredential);
}
else
{
throw new IdentityException("CredentialType not supported for a given IdentityObjectType");