2 Replies Latest reply on Mar 19, 2009 8:23 AM by Phil Forum

    IdentityStore and EntityManager with JTA transactions

    Kuba Stepniak Newbie

      Hi,
      I've been trying to do a following thing: I have two persistance units one for regular data and the other one for authentication. My application is deployed on JBoss AS with default transaction managment so implicitly it's JTA. My question is: how can I specify in components.xml file which entityManager should be used by identityStore? Or maybe identityStore recognises it automagically? Maybe I missed sth but I couldn't find any information on this issue. And I think it's more general because I didnt come across any examples of injecting JTA entityManagers into components in components.xml. Let's assume of course that it's not possible to do using annotations.


      Thanks,
      Kuba

        • 1. Re: IdentityStore and EntityManager with JTA transactions
          Shane Bryzak Master

          You can set it in components.xml:



          <security:jpa-identity-store entity-manager="#{myEntityManager}"/>


          • 2. Re: IdentityStore and EntityManager with JTA transactions
            Phil Forum Newbie

            I still cannot get IdentityManagement to persist anything in the database. Read access works fine.


            My components.xml:


            <persistence:managed-persistence-context name="entityManager"
                             auto-create="true"
                             persistence-unit-jndi-name="java:/MartensRDFEntityManagerFactory"/>
            <security:rule-based-permission-resolver security-rules="#{securityRules}"/> 
            
            <security:jpa-identity-store user-class="xx.martens.rdf.model.User"
                                 role-class="xx.martens.rdf.model.Role"
                                 entity-manager="#{entityManager}"/>
            


            The following simple test shows, that users present in the database can be accessed, but a new user is not persisted:


            public class IdentityManagementTest extends SeamTest {
            
                 private static LogProvider log = Logging.getLogProvider(IdentityManagementTest.class);
            
                 public EntityManager getEntityManager()
                 {
                    return (EntityManager) getInstance( "entityManager" );
                 }
                 
                 @Test
                 public void createUser() throws Exception {
                      new ComponentTest() {
            
                           String newUsername = "user_created_by_createUser";
                           
                           @Override
                           protected void testComponents() throws Exception {
                                IdentityManager identityManager = IdentityManager.instance();
                                assertNotNull(identityManager);
                                Credentials credentials = (Credentials) Component.getInstance(Credentials.class); 
                                credentials.setUsername("admin");
                                credentials.setPassword("admin");
                                Identity identity = Identity.instance();
                                assertNotNull(identity.login());
                                // this is for being able to perform the operations below.
                                assertTrue(identity.hasRole("admin"));
                                // the test itself:
                                log.debug("createUser returned: " + createUser(newUsername, newUsername));
                                assertTrue(identityManager.userExists(newUsername));
                           }
                           
                           @Transactional
                           private boolean createUser(String name, String password) {
                                IdentityManager identityManager = IdentityManager.instance();
                                assertNotNull(identityManager);
                                return identityManager.createUser(name, password);
                           }
            
                      }.run();
                 }
            



            Running the test above, I get the following:



            • No Exceptions are thrown.

            • The log output shows, that createUser() returns true.

            • The assertion assertTrue(identityManager.userExists(newUsername)) fails.



            Thanks, Phil