Hi All,
when I used the IdentitySession API, the typical template would be like:
try { IdentitySession.beginTransaction(); do operations; identitySession.getTransaction().commit(); } catch (Exception e) { identitySession.getTransaction.rollback(); } finally { identitySession.close(); }
public abstract class JBossIdentityTemplate<T> { private static Logger logger = Logger.getLogger(JBossIdentityTemplate.class.toString()); private IdentitySession session; public JBossIdentityTemplate(IdentitySession session) { this.session = session; } public T execute() throws IdentityException { T o = null; try { session.beginTransaction(); o = doExecute(session); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); } finally { closeSession(); } return o; } private void closeSession() { try { session.close(); } catch (IdentityException e) { logger.log(Level.SEVERE, "Error in closing the session"); } } public abstract T doExecute(IdentitySession session) throws Exception; }
public Group createGroup(final Group group) throws Exception{ return new JBossIdentityTemplate<Group>(identitySession) { @Override public Group doExecute(IdentitySession session) throws Exception { org.jboss.identity.idm.api.Group theGroup = identitySession.getPersistenceManager().createGroup(group.getName(), "GROUP"); group.setId(theGroup.getId()); AttributesManager attrManager = identitySession.getAttributesManager(); attrManager.addAttribute(theGroup, GROUP_DESCRIPTION, group.getDescription()); return group; } }.execute(); }