This commit is contained in:
Bill Burke 2014-07-14 11:36:57 -04:00
commit 8f7efd5b67
7 changed files with 623 additions and 658 deletions

View file

@ -13,10 +13,13 @@ import javax.persistence.Persistence;
*/ */
public class DefaultJpaConnectionProviderFactory implements JpaConnectionProviderFactory { public class DefaultJpaConnectionProviderFactory implements JpaConnectionProviderFactory {
private EntityManagerFactory emf; private volatile EntityManagerFactory emf;
private String unitName;
@Override @Override
public JpaConnectionProvider create(KeycloakSession session) { public JpaConnectionProvider create(KeycloakSession session) {
lazyInit();
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em = PersistenceExceptionConverter.create(em); em = PersistenceExceptionConverter.create(em);
session.getTransaction().enlist(new JpaKeycloakTransaction(em)); session.getTransaction().enlist(new JpaKeycloakTransaction(em));
@ -25,7 +28,9 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
@Override @Override
public void close() { public void close() {
emf.close(); if (emf != null) {
emf.close();
}
} }
@Override @Override
@ -35,8 +40,17 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
@Override @Override
public void init(Config.Scope config) { public void init(Config.Scope config) {
String unitName = config.get("unitName", "jpa-keycloak-identity-store"); unitName = config.get("unitName", "jpa-keycloak-identity-store");
emf = Persistence.createEntityManagerFactory(unitName, JpaUtils.getHibernateProperties()); }
private void lazyInit() {
if (emf == null) {
synchronized (this) {
if (emf == null) {
emf = Persistence.createEntityManagerFactory(unitName, JpaUtils.getHibernateProperties());
}
}
}
} }
} }

View file

@ -10,6 +10,7 @@ import org.keycloak.connections.mongo.api.MongoStore;
import org.keycloak.connections.mongo.impl.MongoStoreImpl; import org.keycloak.connections.mongo.impl.MongoStoreImpl;
import org.keycloak.connections.mongo.impl.context.TransactionMongoStoreInvocationContext; import org.keycloak.connections.mongo.impl.context.TransactionMongoStoreInvocationContext;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.util.JpaUtils;
import java.util.Collections; import java.util.Collections;
@ -36,13 +37,16 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro
private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class); private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class);
private MongoClient client; private volatile MongoClient client;
private MongoStore mongoStore; private MongoStore mongoStore;
private DB db; private DB db;
private Config.Scope config;
@Override @Override
public MongoConnectionProvider create(KeycloakSession session) { public MongoConnectionProvider create(KeycloakSession session) {
lazyInit();
TransactionMongoStoreInvocationContext invocationContext = new TransactionMongoStoreInvocationContext(mongoStore); TransactionMongoStoreInvocationContext invocationContext = new TransactionMongoStoreInvocationContext(mongoStore);
session.getTransaction().enlist(new MongoKeycloakTransaction(invocationContext)); session.getTransaction().enlist(new MongoKeycloakTransaction(invocationContext));
return new DefaultMongoConnectionProvider(db, mongoStore, invocationContext); return new DefaultMongoConnectionProvider(db, mongoStore, invocationContext);
@ -50,28 +54,38 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro
@Override @Override
public void init(Config.Scope config) { public void init(Config.Scope config) {
try { this.config = config;
String host = config.get("host", ServerAddress.defaultHost()); }
int port = config.getInt("port", ServerAddress.defaultPort());
String dbName = config.get("db", "keycloak");
boolean clearOnStartup = config.getBoolean("clearOnStartup", false);
String user = config.get("user"); private void lazyInit() {
String password = config.get("password"); if (client == null) {
if (user != null && password != null) { synchronized (this) {
MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); if (client == null) {
client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential)); try {
} else { String host = config.get("host", ServerAddress.defaultHost());
client = new MongoClient(host, port); int port = config.getInt("port", ServerAddress.defaultPort());
String dbName = config.get("db", "keycloak");
boolean clearOnStartup = config.getBoolean("clearOnStartup", false);
String user = config.get("user");
String password = config.get("password");
if (user != null && password != null) {
MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray());
client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential));
} else {
client = new MongoClient(host, port);
}
this.db = client.getDB(dbName);
this.mongoStore = new MongoStoreImpl(db, clearOnStartup, getManagedEntities());
logger.infof("Initialized mongo model. host: %s, port: %d, db: %s, clearOnStartup: %b", host, port, dbName, clearOnStartup);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} }
this.db = client.getDB(dbName);
this.mongoStore = new MongoStoreImpl(db, clearOnStartup, getManagedEntities());
logger.infof("Initialized mongo model. host: %s, port: %d, db: %s, clearOnStartup: %b", host, port, dbName, clearOnStartup);
} catch (Exception e) {
throw new RuntimeException(e);
} }
} }
@ -85,7 +99,9 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro
@Override @Override
public void close() { public void close() {
client.close(); if (client != null) {
client.close();
}
} }
@Override @Override

View file

@ -57,6 +57,16 @@
<artifactId>keycloak-model-jpa</artifactId> <artifactId>keycloak-model-jpa</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-sessions-mem</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-sessions-jpa</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-audit-api</artifactId> <artifactId>keycloak-audit-api</artifactId>

View file

@ -14,21 +14,15 @@
<class>org.keycloak.models.jpa.entities.SocialLinkEntity</class> <class>org.keycloak.models.jpa.entities.SocialLinkEntity</class>
<class>org.keycloak.models.jpa.entities.AuthenticationLinkEntity</class> <class>org.keycloak.models.jpa.entities.AuthenticationLinkEntity</class>
<class>org.keycloak.models.jpa.entities.UserEntity</class> <class>org.keycloak.models.jpa.entities.UserEntity</class>
<class>org.keycloak.models.jpa.entities.UserSessionEntity</class>
<class>org.keycloak.models.jpa.entities.ClientUserSessionAssociationEntity</class>
<class>org.keycloak.models.jpa.entities.UsernameLoginFailureEntity</class>
<class>org.keycloak.models.jpa.entities.UserRoleMappingEntity</class> <class>org.keycloak.models.jpa.entities.UserRoleMappingEntity</class>
<class>org.keycloak.models.jpa.entities.ScopeMappingEntity</class> <class>org.keycloak.models.jpa.entities.ScopeMappingEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes> <!-- JpaUserSessionProvider -->
<class>org.keycloak.models.sessions.jpa.entities.ClientUserSessionAssociationEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.UserSessionEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.UsernameLoginFailureEntity</class>
<properties> <!-- JpaAuditProvider -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-keycloak-audit-store" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>org.keycloak.audit.jpa.EventEntity</class> <class>org.keycloak.audit.jpa.EventEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes> <exclude-unlisted-classes>true</exclude-unlisted-classes>
@ -37,5 +31,4 @@
<property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.hbm2ddl.auto" value="update" />
</properties> </properties>
</persistence-unit> </persistence-unit>
</persistence> </persistence>

View file

@ -22,15 +22,7 @@
<class>org.keycloak.models.sessions.jpa.entities.UserSessionEntity</class> <class>org.keycloak.models.sessions.jpa.entities.UserSessionEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.UsernameLoginFailureEntity</class> <class>org.keycloak.models.sessions.jpa.entities.UsernameLoginFailureEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes> <!-- JpaAuditProvider -->
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-keycloak-audit-store" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<class>org.keycloak.audit.jpa.EventEntity</class> <class>org.keycloak.audit.jpa.EventEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes> <exclude-unlisted-classes>true</exclude-unlisted-classes>
@ -39,5 +31,4 @@
<property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.hbm2ddl.auto" value="update" />
</properties> </properties>
</persistence-unit> </persistence-unit>
</persistence> </persistence>

File diff suppressed because it is too large Load diff

View file

@ -38,56 +38,4 @@
<property name="hibernate.format_sql" value="true" /> <property name="hibernate.format_sql" value="true" />
</properties> </properties>
</persistence-unit> </persistence-unit>
<persistence-unit name="jpa-keycloak-audit-store" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.keycloak.audit.jpa.EventEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.url" value="jdbc:h2:mem:test"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
<!--
<persistence-unit name="picketlink-keycloak-identity-store" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.picketlink.idm.jpa.model.sample.simple.AttributedTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.AccountTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.RoleTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.GroupTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.IdentityTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.RelationshipTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.RelationshipIdentityTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.PartitionTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.PasswordCredentialTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.DigestCredentialTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.X509CredentialTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.OTPCredentialTypeEntity</class>
<class>org.picketlink.idm.jpa.model.sample.simple.AttributeTypeEntity</class>
<class>org.keycloak.models.picketlink.mappings.RealmEntity</class>
<class>org.keycloak.models.picketlink.mappings.ApplicationEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.url" value="jdbc:h2:mem:test"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
-->
</persistence> </persistence>