@Override
public void addDomain(String domain) throws DomainListException {
String lowerCasedDomain = domain.toLowerCase();
if (containsDomain(lowerCasedDomain)) {
throw new DomainListException(lowerCasedDomain + " already exists.");
}
EntityManager entityManager = entityManagerFactory.createEntityManager();
final EntityTransaction transaction = entityManager.getTransaction();
try {
transaction.begin();
JPADomain jpaDomain = new JPADomain(lowerCasedDomain);
entityManager.persist(jpaDomain);
transaction.commit();
} catch (PersistenceException e) {
getLogger().error("Failed to save domain", e);
if (transaction.isActive()) {
transaction.rollback();
}
throw new DomainListException("Unable to add domain " + domain, e);
} finally {
entityManager.close();
}
}