String persistenceUnit = configurationMD.getOptionSingleValue(PERSISTENCE_UNIT);
String populateMembershipTypes = configurationMD.getOptionSingleValue(POPULATE_MEMBERSHIP_TYPES);
String populateIdentityObjectTypes = configurationMD.getOptionSingleValue(POPULATE_IDENTITY_OBJECT_TYPES);
HibernateEntityManager em = bootstrapHibernateEntityManager(persistenceUnit);
// Attribute mappings - helper structures
for (IdentityObjectTypeMetaData identityObjectTypeMetaData : configurationMD.getSupportedIdentityTypes())
{
Set<String> names = new HashSet<String>();
Map<String, IdentityObjectAttributeMetaData> metadataMap = new HashMap<String, IdentityObjectAttributeMetaData>();
Map<String, String> reverseMap = new HashMap<String, String>();
for (IdentityObjectAttributeMetaData attributeMetaData : identityObjectTypeMetaData.getAttributes())
{
names.add(attributeMetaData.getName());
metadataMap.put(attributeMetaData.getName(), attributeMetaData);
if (attributeMetaData.getStoreMapping() != null)
{
reverseMap.put(attributeMetaData.getStoreMapping(), attributeMetaData.getName());
}
}
// Use unmodifiableSet as it'll be exposed directly
attributeMappings.put(identityObjectTypeMetaData.getName(), Collections.unmodifiableSet(names));
attributesMetaData.put(identityObjectTypeMetaData.getName(), metadataMap);
reverseAttributeMappings.put(identityObjectTypeMetaData.getName(), reverseMap);
}
attributeMappings = Collections.unmodifiableMap(attributeMappings);
if (populateMembershipTypes != null && populateMembershipTypes.equalsIgnoreCase("true"))
{
List<String> memberships = new LinkedList<String>();
for (String membership : configurationMD.getSupportedRelationshipTypes())
{
memberships.add(membership);
}
try
{
populateRelationshipTypes(em, memberships.toArray(new String[memberships.size()]));
}
catch (Exception e)
{
throw new IdentityException("Failed to populate relationship types", e);
}
}
if (populateIdentityObjectTypes != null && populateIdentityObjectTypes.equalsIgnoreCase("true"))
{
List<String> types = new LinkedList<String>();
for (IdentityObjectTypeMetaData metaData : configurationMD.getSupportedIdentityTypes())
{
types.add(metaData.getName());
}
try
{
populateObjectTypes(em, types.toArray(new String[types.size()]));
}
catch (Exception e)
{
throw new IdentityException("Failed to populate identity object types", e);
}
}
if (supportedCredentialTypes != null && supportedCredentialTypes.size() > 0)
{
try
{
populateCredentialTypes(em, supportedCredentialTypes.toArray(new String[supportedCredentialTypes.size()]));
}
catch (Exception e)
{
throw new IdentityException("Failed to populated credential types");
}
}
String realmAware = configurationMD.getOptionSingleValue(IS_REALM_AWARE);
if (realmAware != null && realmAware.equalsIgnoreCase("true"))
{
this.isRealmAware = true;
}
String allowNotDefineAttributes = configurationMD.getOptionSingleValue(ALLOW_NOT_DEFINED_ATTRIBUTES);
if (allowNotDefineAttributes != null && allowNotDefineAttributes.equalsIgnoreCase("true"))
{
this.isAllowNotDefinedAttributes = true;
}
// Default realm
HibernateRealm realm = null;
try
{
em.getTransaction().begin();
realm = (HibernateRealm)em.getSession().
createCriteria(HibernateRealm.class).add(Restrictions.eq("name", DEFAULT_REALM_NAME)).uniqueResult();
em.getTransaction().commit();
}
catch (HibernateException e)
{
// Realm does not exist