0 Replies Latest reply on Mar 31, 2009 3:46 AM by Jeff Yu

    do we need JBossIdentitySessionTemplate class?

    Jeff Yu Master

      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();
      }
      


      This is without considering catching the exceptions on rollback() and close() method.

      Therefore, I was coming up with a Template class, named it JBossIdentityTemplate, the class is like:
      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;
      
      }
      



      With this class, the implementation class would be like:

      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();
      
       }
      
      


      What do you think of this idea? Comments and feedback are welcome.