Skip to content
Permalink
Browse files

Create custom Infinispan region factory that pulls its cache containe…

…r from the appropriate service instead of relying on jndi.

Modify HibernatePersistenceProviderAdaptor to depend on new CacheConfigurationService instead of the EmbeddedCacheManagerService.
Bundle hiberate-infinispan + infinispan with the jboss-as-jpa-hibernate? module instead of the hibernate module.
  • Loading branch information...
pferraro authored and bstansberry committed Dec 7, 2011
1 parent 6aa25ba commit d005b39c50fb390ffe590a70b60fc4827cac3cca
@@ -578,7 +578,6 @@
<maven-resource group="org.hibernate" artifact="hibernate-core" />
<maven-resource group="org.hibernate.common" artifact="hibernate-commons-annotations" />
<maven-resource group="org.hibernate" artifact="hibernate-entitymanager" />
<maven-resource group="org.hibernate" artifact="hibernate-infinispan" />
</module-def>

<module-def name="org.hibernate.envers">
@@ -732,10 +731,14 @@

<module-def name="org.jboss.as.jpa.hibernate" slot="3">
<maven-resource group="org.jboss.as" artifact="jboss-as-jpa-hibernate3"/>
<maven-resource group="org.hibernate" artifact="hibernate-infinispan"/>
<maven-resource group="org.jboss.as" artifact="jboss-as-jpa-hibernate-infinispan"/>
</module-def>

<module-def name="org.jboss.as.jpa.hibernate" slot="4">
<maven-resource group="org.jboss.as" artifact="jboss-as-jpa-hibernate4"/>
<maven-resource group="org.hibernate" artifact="hibernate-infinispan"/>
<maven-resource group="org.jboss.as" artifact="jboss-as-jpa-hibernate-infinispan"/>
</module-def>

<module-def name="org.jboss.as.jpa.openjpa">
@@ -944,6 +944,11 @@
<artifactId>jboss-as-jpa-hibernate4</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jpa-hibernate-infinispan</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jpa-openjpa</artifactId>
@@ -38,7 +38,6 @@
<module name="org.apache.ant"/>
<module name="org.apache.commons.collections"/>
<module name="org.dom4j"/>
<module name="org.infinispan"/>
<module name="org.javassist"/>
<module name="org.jboss.as.jpa.hibernate" slot="4"/>
<module name="org.jboss.logging"/>
@@ -35,8 +35,9 @@
<module name="javax.transaction.api"/>
<module name="org.hibernate" slot="3" optional="true"/> <!-- org.hibernate:3 must be created manually with Hibernate 3 jars -->
<module name="org.hibernate.validator"/>
<module name="org.infinispan"/>
<module name="org.jboss.as.jpa.spi"/>
<module name="org.jboss.as.naming"/>
<module name="org.jboss.as.server"/>
<module name="org.jboss.jandex"/>
<module name="org.jboss.logging"/>
<module name="org.jboss.msc"/>
@@ -35,10 +35,10 @@
<module name="javax.transaction.api"/>
<module name="org.hibernate"/>
<module name="org.hibernate.validator"/>
<module name="org.infinispan"/>
<module name="org.jboss.as.controller"/>
<module name="org.jboss.as.jpa.spi"/>
<module name="org.jboss.as.jpa.util"/>
<module name="org.jboss.as.naming"/>
<module name="org.jboss.as.server"/>
<module name="org.jboss.jandex"/>
<module name="org.jboss.logging"/>
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2010, 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.
-->

<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.jboss.as</groupId>
<artifactId>jboss-as-jpa-parent</artifactId>
<version>7.1.0.CR1-SNAPSHOT</version>
</parent>

<artifactId>jboss-as-jpa-hibernate-infinispan</artifactId>
<packaging>jar</packaging>

<name>JBoss Application Server: Infinispan Hibernate 2nd level cache provider</name>

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-clustering-infinispan</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-server</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>
-AgeneratedTranslationFilesPath=${project.basedir}/target/generated-translation-files
</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, 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.hibernate.cache.infinispan;

import java.util.Properties;

import org.hibernate.cache.CacheException;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.infinispan.manager.EmbeddedCacheManager;
import org.jboss.as.server.CurrentServiceContainer;
import org.jboss.msc.service.ServiceName;

/**
* @author Paul Ferraro
*/
public class InfinispanRegionFactory extends org.hibernate.cache.infinispan.InfinispanRegionFactory {
private static final long serialVersionUID = -3277051412715973863L;

public static final String CACHE_CONTAINER = "hibernate.cache.infinispan.container";
public static final String DEFAULT_CACHE_CONTAINER = "hibernate";

public InfinispanRegionFactory() {
super();
}

public InfinispanRegionFactory(Properties props) {
super(props);
}

@Override
protected EmbeddedCacheManager createCacheManager(Properties properties) throws CacheException {
String container = ConfigurationHelper.getString(CACHE_CONTAINER, properties, DEFAULT_CACHE_CONTAINER);
ServiceName serviceName = ServiceName.JBOSS.append("infinispan", container);
return (EmbeddedCacheManager) CurrentServiceContainer.getServiceContainer().getRequiredService(serviceName).getValue();
}

@Override
public void stop() {
// Do not attempt to stop our cache manager because it wasn't created by this region factory.
}
}
@@ -33,9 +33,7 @@
<version>7.1.0.CR1-SNAPSHOT</version>
</parent>

<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jpa-hibernate3</artifactId>
<version>7.1.0.CR1-SNAPSHOT</version>

<name>JBoss Application Server: Hibernate 3.6.x JPA integration </name>

@@ -82,11 +80,6 @@
<artifactId>jboss-as-jpa-spi</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-naming</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-server</artifactId>
@@ -23,16 +23,16 @@
package org.jboss.as.jpa.hibernate3;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.hibernate.cfg.Configuration;
import org.jboss.as.jpa.spi.JtaManager;
import org.jboss.as.jpa.spi.ManagementAdaptor;
import org.jboss.as.jpa.spi.PersistenceProviderAdaptor;
import org.jboss.as.jpa.spi.PersistenceUnitMetadata;
import org.jboss.as.naming.deployment.ContextNames;
import org.jboss.as.naming.deployment.JndiName;
import org.jboss.msc.service.ServiceName;

/**
@@ -42,6 +42,12 @@
*/
public class HibernatePersistenceProviderAdaptor implements PersistenceProviderAdaptor {

private static final String DEFAULT_REGION_FACTORY = "org.jboss.as.jpa.hibernate.cache.infinispan.InfinispanRegionFactory";
private static final String DEFAULT_CACHE_CONTAINER = "hibernate";
private static final String DEFAULT_ENTITY_CACHE = "entity";
private static final String DEFAULT_COLLECTION_CACHE = "entity";
private static final String DEFAULT_QUERY_CACHE = "local-query";
private static final String DEFAULT_TIMESTAMPS_CACHE = "timestamps";
public static final String SCANNER = "hibernate.ejb.resource_scanner";
private static final String HIBERNATE_ANNOTATION_SCANNER_CLASS = "org.jboss.as.jpa.hibernate3.HibernateAnnotationScanner";

@@ -74,46 +80,54 @@ private void addAnnotationScanner(PersistenceUnitMetadata pu) {

@Override
public Iterable<ServiceName> getProviderDependencies(PersistenceUnitMetadata pu) {
//
String cacheManager = pu.getProperties().getProperty("hibernate.cache.infinispan.cachemanager");
String useCache = pu.getProperties().getProperty("hibernate.cache.use_second_level_cache");
String regionFactoryClass = pu.getProperties().getProperty("hibernate.cache.region.factory_class");
if ((useCache != null && useCache.equalsIgnoreCase("true")) ||
cacheManager != null) {
if (regionFactoryClass == null) {
regionFactoryClass = "org.hibernate.cache.infinispan.JndiInfinispanRegionFactory";
pu.getProperties().put("hibernate.cache.region.factory_class", regionFactoryClass);
Properties properties = pu.getProperties();
if (Boolean.parseBoolean(properties.getProperty("hibernate.cache.use_second_level_cache"))) {
if (properties.getProperty("hibernate.cache.region_prefix") == null) {
// cache entries for this PU will be identified by scoped pu name + Entity class name
properties.put("hibernate.cache.region_prefix", pu.getScopedPersistenceUnitName());
}
if (cacheManager == null) {
cacheManager = "java:jboss/infinispan/hibernate";
pu.getProperties().put("hibernate.cache.infinispan.cachemanager", cacheManager);
String regionFactory = properties.getProperty("hibernate.cache.region.factory_class");
if (regionFactory == null) {
regionFactory = DEFAULT_REGION_FACTORY;
properties.setProperty("hibernate.cache.region.factory_class", regionFactory);
}
if (pu.getProperties().getProperty("hibernate.cache.region_prefix") == null) {
// cache entries for this PU will be identified by scoped pu name + Entity class name
pu.getProperties().put("hibernate.cache.region_prefix", pu.getScopedPersistenceUnitName());
if (regionFactory.equals(DEFAULT_REGION_FACTORY)) {
// Set infinispan defaults
String container = properties.getProperty("hibernate.cache.infinispan.container");
if (container == null) {
container = DEFAULT_CACHE_CONTAINER;
properties.setProperty("hibernate.cache.infinispan.container", container);
}
String entity = properties.getProperty("hibernate.cache.infinispan.entity.cfg", DEFAULT_ENTITY_CACHE);
String collection = properties.getProperty("hibernate.cache.infinispan.collection.cfg", DEFAULT_COLLECTION_CACHE);
String query = properties.getProperty("hibernate.cache.infinispan.query.cfg", DEFAULT_QUERY_CACHE);
String timestamps = properties.getProperty("hibernate.cache.infinispan.timestamps.cfg", DEFAULT_TIMESTAMPS_CACHE);
Set<ServiceName> result = new HashSet<ServiceName>();
result.add(this.getCacheConfigServiceName(container, entity));
result.add(this.getCacheConfigServiceName(container, collection));
result.add(this.getCacheConfigServiceName(container, timestamps));
result.add(this.getCacheConfigServiceName(container, query));
return result;
}
ArrayList<ServiceName> result = new ArrayList<ServiceName>();
result.add(ContextNames.bindInfoFor(toJndiName(cacheManager).toString()).getBinderServiceName());
return result;
}
return null;
}

private ServiceName getCacheConfigServiceName(String container, String cache) {
return ServiceName.JBOSS.append("infinispan", container, "config", cache);
}

private void putPropertyIfAbsent(Map properties, String property, Object value) {
if (!properties.containsKey(property)) {
properties.put(property, value);
}
}

private static JndiName toJndiName(String value) {
return value.startsWith("java:") ? JndiName.of(value) : JndiName.of("java:jboss").append(value.startsWith("/") ? value.substring(1) : value);
}

@Override
public void beforeCreateContainerEntityManagerFactory(PersistenceUnitMetadata pu) {
if (pu.getProperties().containsKey(SCANNER)) {
try {
Class scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
Class<?> scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
// get method for public static void setThreadLocalPersistenceUnitMetadata(final PersistenceUnitMetadata pu) {
Method setThreadLocalPersistenceUnitMetadata = scanner.getMethod("setThreadLocalPersistenceUnitMetadata", PersistenceUnitMetadata.class);
setThreadLocalPersistenceUnitMetadata.invoke(null, pu);
@@ -128,9 +142,9 @@ public void afterCreateContainerEntityManagerFactory(PersistenceUnitMetadata pu)
if (pu.getProperties().containsKey(SCANNER)) {
// clear backdoor annotation scanner access to pu
try {
Class scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
Class<?> scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
// get method for public static void clearThreadLocalPersistenceUnitMetadata() {
Method clearThreadLocalPersistenceUnitMetadata = scanner.getMethod("clearThreadLocalPersistenceUnitMetadata", null);
Method clearThreadLocalPersistenceUnitMetadata = scanner.getMethod("clearThreadLocalPersistenceUnitMetadata");
clearThreadLocalPersistenceUnitMetadata.invoke(null);
} catch (Throwable ignore) {
}
@@ -141,6 +155,5 @@ public void afterCreateContainerEntityManagerFactory(PersistenceUnitMetadata pu)
public ManagementAdaptor getManagementAdaptor() {
return null;
}

}

@@ -33,9 +33,7 @@
<version>7.1.0.CR1-SNAPSHOT</version>
</parent>

<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jpa-hibernate4</artifactId>
<version>7.1.0.CR1-SNAPSHOT</version>

<name>JBoss Application Server: Hibernate 4.0.x JPA integration </name>

@@ -77,11 +75,6 @@
<artifactId>jboss-as-jpa-spi</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-naming</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-server</artifactId>
Oops, something went wrong.

0 comments on commit d005b39

Please sign in to comment.
You can’t perform that action at this time.