Skip to content

Commit

Permalink
Port 2nd level cache integration to use infinispan-hibernate-cache-sp…
Browse files Browse the repository at this point in the history
…i module.

Move deprecated RegionFactory implementations to a separate legacy module.
  • Loading branch information
pferraro committed May 2, 2018
1 parent 3f26191 commit 772fe48
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 167 deletions.
11 changes: 11 additions & 0 deletions component-matrix/pom.xml
Expand Up @@ -321,6 +321,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jipijapa-hibernate5-legacy</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
Expand Down Expand Up @@ -3808,6 +3813,12 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-hibernate-cache-spi</artifactId>
<version>${version.org.infinispan}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-hibernate-cache-commons</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions feature-pack/pom.xml
Expand Up @@ -900,6 +900,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jipijapa-hibernate5-legacy</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
Expand Down Expand Up @@ -2255,6 +2259,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-hibernate-cache-spi</artifactId>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
Expand Down
Expand Up @@ -29,6 +29,7 @@

<resources>
<artifact name="${org.wildfly:jipijapa-hibernate5}"/>
<artifact name="${org.wildfly:jipijapa-hibernate5-legacy}"/>
</resources>

<dependencies>
Expand Down
Expand Up @@ -30,6 +30,7 @@


<resources>
<artifact name="${org.infinispan:infinispan-hibernate-cache-spi}"/>
<artifact name="${org.infinispan:infinispan-hibernate-cache-commons}"/>
<artifact name="${org.infinispan:infinispan-hibernate-cache-spi}"/>
<artifact name="${org.infinispan:infinispan-hibernate-cache-v51}"/>
Expand All @@ -39,8 +40,10 @@
<module name="org.hibernate"/>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="org.hibernate.jipijapa-hibernate5" services="import"/>
<module name="org.infinispan" services="import"/>
<module name="org.infinispan.commons"/>
<module name="org.jboss.as.jpa.spi"/>
<module name="org.jboss.logging"/>
</dependencies>
</module>
53 changes: 53 additions & 0 deletions jpa/hibernate5-legacy/pom.xml
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ JBoss, Home of Professional Open Source
~ Copyright 2018, Red Hat Middleware LLC, and individual contributors
~ by the @authors tag. See the copyright.txt in the distribution for a
~ full listing of individual contributors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~ http://www.apache.org/licenses/LICENSE-2.0
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-jpa-parent</artifactId>
<!--
Maintain separation between the artifact id and the version to help prevent
merge conflicts between commits changing the GA and those changing the V.
-->
<version>13.0.0.Alpha1-SNAPSHOT</version>
</parent>

<artifactId>jipijapa-hibernate5-legacy</artifactId>

<name>jipijapa Hibernate 5.x (JPA 2.1) legacy integration</name>

<dependencies>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-hibernate-cache-v51</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>
</project>
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.as.jpa.hibernate5.infinispan;

import java.util.Properties;

import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.CacheException;

/**
* Infinispan-backed region factory for use with standalone (i.e. non-JPA) Hibernate applications.
* @author Paul Ferraro
* @author Scott Marlow
* @deprecated Use {@link org.infinispan.hibernate.cache.v51.InfinispanRegionFactory} instead in conjunction with "hibernate.cache.infinispan.shared" set to false.
*/
@Deprecated
public class InfinispanRegionFactory extends org.infinispan.hibernate.cache.v51.InfinispanRegionFactory {
private static final long serialVersionUID = 6526170943015350422L;

@Override
public void start(SessionFactoryOptions settings, Properties properties) throws CacheException {
properties.setProperty("hibernate.cache.infinispan.shared", "false");
super.start(settings, properties);
}
}
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.as.jpa.hibernate5.infinispan;

import java.util.Properties;

import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.CacheException;

/**
* Infinispan-backed region factory that retrieves its cache manager from the Infinispan subsystem.
* This is used for (JPA) container managed persistence contexts.
* Each deployment application will use a unique Hibernate cache region name in the shared cache.
*
* @author Paul Ferraro
* @author Scott Marlow
* @deprecated Use {@link org.infinispan.hibernate.cache.v51.InfinispanRegionFactory} instead.
*/
@Deprecated
public class SharedInfinispanRegionFactory extends org.infinispan.hibernate.cache.v51.InfinispanRegionFactory {
private static final long serialVersionUID = -3277051412715973863L;

@Override
public void start(SessionFactoryOptions settings, Properties properties) throws CacheException {
properties.setProperty("hibernate.cache.infinispan.shared", "true");
super.start(settings, properties);
}
}
8 changes: 7 additions & 1 deletion jpa/hibernate5/pom.xml
Expand Up @@ -120,7 +120,7 @@

<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-hibernate-cache-v51</artifactId>
<artifactId>infinispan-hibernate-cache-spi</artifactId>
<scope>provided</scope>
</dependency>

Expand All @@ -130,6 +130,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
Expand Down
Expand Up @@ -27,15 +27,12 @@
import static org.infinispan.hibernate.cache.spi.InfinispanProperties.PENDING_PUTS_CACHE_RESOURCE_PROP;
import static org.infinispan.hibernate.cache.spi.InfinispanProperties.QUERY_CACHE_RESOURCE_PROP;
import static org.infinispan.hibernate.cache.spi.InfinispanProperties.TIMESTAMPS_CACHE_RESOURCE_PROP;
import static org.jboss.as.jpa.hibernate5.infinispan.InfinispanRegionFactory.CACHE_CONTAINER;
import static org.jboss.as.jpa.hibernate5.infinispan.InfinispanRegionFactory.DEFAULT_CACHE_CONTAINER;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.hibernate.cfg.AvailableSettings;
import org.jboss.as.jpa.hibernate5.infinispan.SharedInfinispanRegionFactory;
import org.jipijapa.cache.spi.Classification;
import org.jipijapa.event.impl.internal.Notification;

Expand All @@ -46,9 +43,10 @@
*/
public class HibernateSecondLevelCache {

private static final String DEFAULT_REGION_FACTORY = SharedInfinispanRegionFactory.class.getName();
private static final String DEFAULT_REGION_FACTORY = "org.infinispan.hibernate.cache.v51.InfinispanRegionFactory";

public static final String CACHE_TYPE = "cachetype"; // shared (jpa) or private (for native applications)
public static final String CACHE_PRIVATE = "private";
public static final String CONTAINER = "container";
public static final String NAME = "name";
public static final String CACHES = "caches";
Expand All @@ -67,12 +65,12 @@ public static void addSecondLevelCacheDependencies(Properties mutableProperties,
regionFactory = DEFAULT_REGION_FACTORY;
mutableProperties.setProperty(AvailableSettings.CACHE_REGION_FACTORY, regionFactory);
}
if (regionFactory.equals(DEFAULT_REGION_FACTORY)) {
if (Boolean.parseBoolean(mutableProperties.getProperty(ManagedEmbeddedCacheManagerProvider.SHARED, ManagedEmbeddedCacheManagerProvider.DEFAULT_SHARED))) {
// Set infinispan defaults
String container = mutableProperties.getProperty(CACHE_CONTAINER);
String container = mutableProperties.getProperty(ManagedEmbeddedCacheManagerProvider.CACHE_CONTAINER);
if (container == null) {
container = DEFAULT_CACHE_CONTAINER;
mutableProperties.setProperty(CACHE_CONTAINER, container);
container = ManagedEmbeddedCacheManagerProvider.DEFAULT_CACHE_CONTAINER;
mutableProperties.setProperty(ManagedEmbeddedCacheManagerProvider.CACHE_CONTAINER, container);
}

/**
Expand All @@ -93,6 +91,7 @@ public static Set<String> findCaches(Properties properties) {
caches.add(properties.getProperty(IMMUTABLE_ENTITY_CACHE_RESOURCE_PROP, DEF_ENTITY_RESOURCE));
caches.add(properties.getProperty(COLLECTION_CACHE_RESOURCE_PROP, DEF_ENTITY_RESOURCE));
caches.add(properties.getProperty(NATURAL_ID_CACHE_RESOURCE_PROP, DEF_ENTITY_RESOURCE));

if (properties.containsKey(PENDING_PUTS_CACHE_RESOURCE_PROP)) {
caches.add(properties.getProperty(PENDING_PUTS_CACHE_RESOURCE_PROP));
}
Expand Down
@@ -0,0 +1,96 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2018, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.jpa.hibernate5;

import java.util.Properties;

import org.hibernate.cache.CacheException;
import org.hibernate.cfg.AvailableSettings;
import org.infinispan.hibernate.cache.spi.EmbeddedCacheManagerProvider;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.manager.impl.AbstractDelegatingEmbeddedCacheManager;
import org.jipijapa.cache.spi.Classification;
import org.jipijapa.cache.spi.Wrapper;
import org.jipijapa.event.impl.internal.Notification;
import org.kohsuke.MetaInfServices;

/**
* Provides a managed {@link EmbeddedCacheManager} instance to Infinispan's region factory implementation.
* @author Paul Ferraro
*/
@MetaInfServices(EmbeddedCacheManagerProvider.class)
public class ManagedEmbeddedCacheManagerProvider implements EmbeddedCacheManagerProvider {
public static final String CACHE_CONTAINER = "hibernate.cache.infinispan.container";
public static final String DEFAULT_CACHE_CONTAINER = "hibernate";
public static final String SHARED = "hibernate.cache.infinispan.shared";
public static final String DEFAULT_SHARED = "true";

@Override
public EmbeddedCacheManager getEmbeddedCacheManager(Properties properties) {

Properties settings = new Properties();
String container = properties.getProperty(CACHE_CONTAINER, DEFAULT_CACHE_CONTAINER);
settings.setProperty(HibernateSecondLevelCache.CONTAINER, container);

if (!Boolean.parseBoolean(properties.getProperty(SHARED, DEFAULT_SHARED))) {
HibernateSecondLevelCache.addSecondLevelCacheDependencies(properties, null);

settings.setProperty(HibernateSecondLevelCache.CACHE_TYPE, HibernateSecondLevelCache.CACHE_PRIVATE);

// Find a suitable service name to represent this session factory instance
String name = properties.getProperty(AvailableSettings.SESSION_FACTORY_NAME);
if (name != null) {
settings.setProperty(HibernateSecondLevelCache.NAME, name);
}

settings.setProperty(HibernateSecondLevelCache.CACHES, String.join(" ", HibernateSecondLevelCache.findCaches(properties)));
}

try {
return new JipiJapaCacheManager(Notification.startCache(Classification.INFINISPAN, settings));
} catch (CacheException e) {
throw e;
} catch (Exception e) {
throw new CacheException(e);
}
}

private static class JipiJapaCacheManager extends AbstractDelegatingEmbeddedCacheManager {
private final Wrapper wrapper;

JipiJapaCacheManager(Wrapper wrapper) {
super((EmbeddedCacheManager) wrapper.getValue());
this.wrapper = wrapper;
}

@Override
public void stop() {
Notification.stopCache(Classification.INFINISPAN, this.wrapper);
}

@Override
public void close() {
this.stop();
}
}
}

0 comments on commit 772fe48

Please sign in to comment.