PageRenderTime 41ms CodeModel.GetById 24ms app.highlight 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/jpa/hibernate3/src/main/java/org/jboss/as/jpa/hibernate3/HibernatePersistenceProviderAdaptor.java

#
Java | 156 lines | 102 code | 17 blank | 37 comment | 11 complexity | fb503de48a668723bf9a7b6f46753bc4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright 2011, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.jboss.as.jpa.hibernate3;
 24
 25import java.lang.reflect.Method;
 26import java.util.Map;
 27import java.util.Properties;
 28
 29import org.hibernate.cfg.Configuration;
 30import org.hibernate.cfg.Environment;
 31import org.jboss.as.clustering.infinispan.subsystem.CacheConfigurationService;
 32import org.jboss.as.jpa.hibernate3.infinispan.InfinispanRegionFactory;
 33import org.jboss.as.jpa.hibernate3.infinispan.SharedInfinispanRegionFactory;
 34import org.jboss.as.jpa.spi.JtaManager;
 35import org.jboss.as.jpa.spi.ManagementAdaptor;
 36import org.jboss.as.jpa.spi.PersistenceProviderAdaptor;
 37import org.jboss.as.jpa.spi.PersistenceUnitMetadata;
 38import org.jboss.msc.service.ServiceBuilder;
 39import org.jboss.msc.service.ServiceRegistry;
 40import org.jboss.msc.service.ServiceTarget;
 41
 42/**
 43 * Implements the PersistenceProviderAdaptor for Hibernate 3.3.x or higher 3.x
 44 *
 45 * @author Scott Marlow
 46 */
 47public class HibernatePersistenceProviderAdaptor implements PersistenceProviderAdaptor {
 48
 49    private static final String DEFAULT_REGION_FACTORY = SharedInfinispanRegionFactory.class.getName();
 50    public static final String SCANNER = "hibernate.ejb.resource_scanner";
 51    private static final String HIBERNATE_ANNOTATION_SCANNER_CLASS = "org.jboss.as.jpa.hibernate3.HibernateAnnotationScanner";
 52
 53    @Override
 54    public void injectJtaManager(JtaManager jtaManager) {
 55        JBossAppServerJtaPlatform.initJBossAppServerJtaPlatform(jtaManager);
 56    }
 57
 58    @Override
 59    public void addProviderProperties(Map properties, PersistenceUnitMetadata pu) {
 60        putPropertyIfAbsent(pu, properties, Environment.TRANSACTION_MANAGER_STRATEGY, JBossAppServerJtaPlatform.class.getName());
 61        putPropertyIfAbsent(pu, properties, Configuration.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
 62        addAnnotationScanner(pu);
 63    }
 64
 65    /**
 66     * Use reflection to see if we are using Hibernate 3.3.x or older (which doesn't have the
 67     * org.hibernate.ejb.packaging.Scanner class)
 68     *
 69     * @param pu
 70     */
 71    private void addAnnotationScanner(PersistenceUnitMetadata pu) {
 72        try {
 73            Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
 74            pu.getProperties().put(SCANNER, HIBERNATE_ANNOTATION_SCANNER_CLASS);
 75        } catch (Throwable ignore) {
 76
 77        }
 78    }
 79
 80    @Override
 81    public void addProviderDependencies(ServiceRegistry registry, ServiceTarget target, ServiceBuilder<?> builder, PersistenceUnitMetadata pu) {
 82        Properties properties = pu.getProperties();
 83        if (Boolean.parseBoolean(properties.getProperty(Environment.USE_SECOND_LEVEL_CACHE))) {
 84            if (properties.getProperty(Environment.CACHE_REGION_PREFIX) == null) {
 85                // cache entries for this PU will be identified by scoped pu name + Entity class name
 86                properties.put(Environment.CACHE_REGION_PREFIX, pu.getScopedPersistenceUnitName());
 87            }
 88            String regionFactory = properties.getProperty(Environment.CACHE_REGION_FACTORY);
 89            if (regionFactory == null) {
 90                regionFactory = DEFAULT_REGION_FACTORY;
 91                properties.setProperty(Environment.CACHE_REGION_FACTORY, regionFactory);
 92            }
 93            if (regionFactory.equals(DEFAULT_REGION_FACTORY)) {
 94                // Set infinispan defaults
 95                String container = properties.getProperty(InfinispanRegionFactory.CACHE_CONTAINER);
 96                if (container == null) {
 97                    container = InfinispanRegionFactory.DEFAULT_CACHE_CONTAINER;
 98                    properties.setProperty(InfinispanRegionFactory.CACHE_CONTAINER, container);
 99                }
100                String entity = properties.getProperty(InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_ENTITY_RESOURCE);
101                String collection = properties.getProperty(InfinispanRegionFactory.COLLECTION_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_ENTITY_RESOURCE);
102                String query = properties.getProperty(InfinispanRegionFactory.QUERY_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_QUERY_RESOURCE);
103                String timestamps = properties.getProperty(InfinispanRegionFactory.TIMESTAMPS_CACHE_RESOURCE_PROP, InfinispanRegionFactory.DEF_QUERY_RESOURCE);
104                builder.addDependency(CacheConfigurationService.getServiceName(container, entity));
105                builder.addDependency(CacheConfigurationService.getServiceName(container, collection));
106                builder.addDependency(CacheConfigurationService.getServiceName(container, timestamps));
107                builder.addDependency(CacheConfigurationService.getServiceName(container, query));
108            }
109        }
110    }
111
112    private void putPropertyIfAbsent(PersistenceUnitMetadata pu, Map properties, String property, Object value) {
113        if (!pu.getProperties().containsKey(property)) {
114            properties.put(property, value);
115        }
116    }
117
118    @Override
119    public void beforeCreateContainerEntityManagerFactory(PersistenceUnitMetadata pu) {
120        if (pu.getProperties().containsKey(SCANNER)) {
121            try {
122                Class<?> scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
123                // get method for public static void setThreadLocalPersistenceUnitMetadata(final PersistenceUnitMetadata pu) {
124                Method setThreadLocalPersistenceUnitMetadata = scanner.getMethod("setThreadLocalPersistenceUnitMetadata", PersistenceUnitMetadata.class);
125                setThreadLocalPersistenceUnitMetadata.invoke(null, pu);
126            } catch (Throwable ignore) {
127
128            }
129        }
130    }
131
132    @Override
133    public void afterCreateContainerEntityManagerFactory(PersistenceUnitMetadata pu) {
134        if (pu.getProperties().containsKey(SCANNER)) {
135            // clear backdoor annotation scanner access to pu
136            try {
137                Class<?> scanner = Configuration.class.getClassLoader().loadClass(HIBERNATE_ANNOTATION_SCANNER_CLASS);
138                // get method for public static void clearThreadLocalPersistenceUnitMetadata() {
139                Method clearThreadLocalPersistenceUnitMetadata = scanner.getMethod("clearThreadLocalPersistenceUnitMetadata");
140                clearThreadLocalPersistenceUnitMetadata.invoke(null);
141            } catch (Throwable ignore) {
142            }
143        }
144    }
145
146    @Override
147    public ManagementAdaptor getManagementAdaptor() {
148        return null;
149    }
150
151    @Override
152    public void cleanup(PersistenceUnitMetadata pu) {
153        HibernateAnnotationScanner.cleanup(pu);
154    }
155}
156