[keycloak-user] Issue creating EntityManagerFactory from custom UserFederationProviderFactory

Anthony Fryer Anthony.Fryer at virginaustralia.com
Wed Apr 6 18:52:16 EDT 2016


There’s definitely some challenges getting the classloaders to play nicely with each other.  I ended up getting this to work by just adding the following one line to the properties used when creating the EntityManagerFactory…

// Adding "hibernate.classLoaders" property is critical for this to work
p.put("hibernate.classLoaders", Arrays.asList(this.getClass().getClassLoader()));
…
emf = Persistence.createEntityManagerFactory("acmeEntities", p);

Thanks,

Anthony

From: Stian Thorgersen [mailto:sthorger at redhat.com]
Sent: Wednesday, 6 April 2016 8:56 PM
To: Anthony Fryer
Cc: keycloak-user (keycloak-user at lists.jboss.org)
Subject: Re: [keycloak-user] Issue creating EntityManagerFactory from custom UserFederationProviderFactory

It's not very easy to get Persistence to pick-up the persistence.xml file from a module. We've got a fix for this coming in 1.9.2, but you can just copy/paste the lines from https://github.com/keycloak/keycloak/blob/master/model/jpa/src/main/java/org/keycloak/connections/jpa/util/JpaUtils.java#L43. It uses Hibernate classes directly to create the EntityManagerFactory which allows specifying the classloader. Just use the classloader for your UserFederationProviderFactory implementation.

On 6 April 2016 at 05:12, Anthony Fryer <Anthony.Fryer at virginaustralia.com<mailto:Anthony.Fryer at virginaustralia.com>> wrote:
Hi All,

I’m implementing a UserFederationProviderFactory and want to create an EntityManagerFactory from one of its methods.  I have packaged up a persistence.xml in the META-INF folder of the SPI jar file and deployed this as a module to the keycloak standalone server.

My module.xml looks like this…

<module xmlns="urn:jboss:module:1.3" name="acme.keycloak-acme-user-federation">
    <resources>
        <resource-root path="keycloak-acme-user-federation-1.0.0.jar">
            <filter>
                <include path="META-INF/**"/>
            </filter>
        </resource-root>
    </resources>
    <dependencies>
        <module name="org.keycloak.keycloak-core"/>
        <module name="org.keycloak.keycloak-server-spi"/>
        <module name="javax.api"/>
        <module name="javaee.api"/>
        <module name="org.hibernate" />
        <module name="org.jboss.ws.cxf.jbossws-cxf-client"/>
    </dependencies>

</module>

In my UserFederationProviderFactory I have a method like this…

                private EntityManagerFactory getEntityManagerFactory(UserFederationProviderModel model) {
                                if (emf == null) {
                                                logger.trace("Creating entityManagerFactory...");
                                                Map<String, String> config = model.getConfig();
                                                Properties p = new Properties();
                                                // for now just use hibernate built in connection factory
                                                p.put("hibernate.connection.driver_class", config.get(DATABASE_DRIVER_CLASS_NAME));
                                                p.put("hibernate.connection.url", config.get(DATABASE_URL));
                                                p.put("hibernate.connection.username", config.get(DATABASE_USER));
                                                p.put("hibernate.connection.password", config.get(DATABASE_PASSWORD));
                                                p.put("hibernate.show_sql", "true");
                                                p.put("hibernate.format_sql", "true");
                                                emf = Persistence.createEntityManagerFactory("acmeEntities", p);
                                }

                                return emf;
                }

When this method is called, it always returns the error “No Persistence provider for EntityManager named acmeEntities”.

I’m 90% sure this is to do with the ClassLoader being used by Persistence not being able to see the META-INF/persistence.xml packaged up in the keycloak-acme-user-federation-1.0.0.jar.  Does anyone have an idea what I need to do to my module configuration to get this working?

Thanks,

Anthony Fryer


The content of this e-mail, including any attachments, is a confidential communication between Virgin Australia Airlines Pty Ltd (Virgin Australia) or its related entities (or the sender if this email is a private communication) and the intended addressee and is for the sole use of that intended addressee. If you are not the intended addressee, any use, interference with, disclosure or copying of this material is unauthorized and prohibited. If you have received this e-mail in error please contact the sender immediately and then delete the message and any attachment(s). There is no warranty that this email is error, virus or defect free. This email is also subject to copyright. No part of it should be reproduced, adapted or communicated without the written consent of the copyright owner. If this is a private communication it does not represent the views of Virgin Australia or its related entities. Please be aware that the contents of any emails sent to or from Virgin Australia or its related entities may be periodically monitored and reviewed. Virgin Australia and its related entities respect your privacy. Our privacy policy can be accessed from our website: www.virginaustralia.com<http://www.virginaustralia.com>

_______________________________________________
keycloak-user mailing list
keycloak-user at lists.jboss.org<mailto:keycloak-user at lists.jboss.org>
https://lists.jboss.org/mailman/listinfo/keycloak-user

The content of this e-mail, including any attachments, is a confidential communication between Virgin Australia Airlines Pty Ltd (Virgin Australia) or its related entities (or the sender if this email is a private communication) and the intended addressee and is for the sole use of that intended addressee. If you are not the intended addressee, any use, interference with, disclosure or copying of this material is unauthorized and prohibited. If you have received this e-mail in error please contact the sender immediately and then delete the message and any attachment(s). There is no warranty that this email is error, virus or defect free. This email is also subject to copyright. No part of it should be reproduced, adapted or communicated without the written consent of the copyright owner. If this is a private communication it does not represent the views of Virgin Australia or its related entities. Please be aware that the contents of any emails sent to or from Virgin Australia or its related entities may be periodically monitored and reviewed. Virgin Australia and its related entities respect your privacy. Our privacy policy can be accessed from our website: www.virginaustralia.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/keycloak-user/attachments/20160406/0403e38f/attachment-0001.html 


More information about the keycloak-user mailing list