Skip to content

Commit 8e3fd81

Browse files
committed
HHH-7995 Added support for TypeContributors in OSGi. Integrated with
envers
1 parent aecd5a4 commit 8e3fd81

File tree

8 files changed

+120
-13
lines changed

8 files changed

+120
-13
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import org.hibernate.dialect.MySQLDialect;
8282
import org.hibernate.dialect.function.SQLFunction;
8383
import org.hibernate.engine.ResultSetMappingDefinition;
84+
import org.hibernate.engine.jdbc.spi.JdbcServices;
8485
import org.hibernate.engine.spi.FilterDefinition;
8586
import org.hibernate.engine.spi.Mapping;
8687
import org.hibernate.engine.spi.NamedQueryDefinition;
@@ -128,10 +129,13 @@
128129
import org.hibernate.mapping.Table;
129130
import org.hibernate.mapping.TypeDef;
130131
import org.hibernate.mapping.UniqueKey;
132+
import org.hibernate.metamodel.spi.TypeContributions;
133+
import org.hibernate.metamodel.spi.TypeContributor;
131134
import org.hibernate.proxy.EntityNotFoundDelegate;
132135
import org.hibernate.secure.internal.JACCConfiguration;
133136
import org.hibernate.service.ServiceRegistry;
134137
import org.hibernate.service.ServiceRegistryBuilder;
138+
import org.hibernate.service.classloading.spi.ClassLoaderService;
135139
import org.hibernate.service.internal.StandardServiceRegistryImpl;
136140
import org.hibernate.tool.hbm2ddl.DatabaseMetadata;
137141
import org.hibernate.tool.hbm2ddl.IndexMetadata;
@@ -219,7 +223,9 @@ public class Configuration implements Serializable {
219223
protected Map<ExtendsQueueEntry, ?> extendsQueue;
220224

221225
protected Map<String, SQLFunction> sqlFunctions;
226+
222227
private TypeResolver typeResolver = new TypeResolver();
228+
private List<TypeContributor> typeContributorRegistrations = new ArrayList<TypeContributor>();
223229

224230
private EntityTuplizerFactory entityTuplizerFactory;
225231
// private ComponentTuplizerFactory componentTuplizerFactory; todo : HHH-3517 and HHH-1907
@@ -1743,7 +1749,8 @@ public Map<String, NamedQueryDefinition> getNamedQueries() {
17431749
*/
17441750
public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throws HibernateException {
17451751
LOG.debugf( "Preparing to build session factory with filters : %s", filterDefinitions );
1746-
1752+
1753+
buildTypeRegistrations( serviceRegistry );
17471754
secondPassCompile();
17481755
if ( !metadataSourceQueue.isEmpty() ) {
17491756
LOG.incompleteMappingMetadataCacheProcessing();
@@ -1765,6 +1772,39 @@ public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throw
17651772
sessionFactoryObserver
17661773
);
17671774
}
1775+
1776+
private void buildTypeRegistrations(ServiceRegistry serviceRegistry) {
1777+
final TypeContributions typeContributions = new TypeContributions() {
1778+
@Override
1779+
public void contributeType(BasicType type) {
1780+
typeResolver.registerTypeOverride( type );
1781+
}
1782+
1783+
@Override
1784+
public void contributeType(UserType type, String[] keys) {
1785+
typeResolver.registerTypeOverride( type, keys );
1786+
}
1787+
1788+
@Override
1789+
public void contributeType(CompositeUserType type, String[] keys) {
1790+
typeResolver.registerTypeOverride( type, keys );
1791+
}
1792+
};
1793+
1794+
// add Dialect contributed types
1795+
final Dialect dialect = serviceRegistry.getService( JdbcServices.class ).getDialect();
1796+
dialect.contributeTypes( typeContributions, serviceRegistry );
1797+
1798+
// add TypeContributor contributed types.
1799+
ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
1800+
for ( TypeContributor contributor : classLoaderService.loadJavaServices( TypeContributor.class ) ) {
1801+
contributor.contribute( typeContributions, serviceRegistry );
1802+
}
1803+
// from app registrations
1804+
for ( TypeContributor contributor : typeContributorRegistrations ) {
1805+
contributor.contribute( typeContributions, serviceRegistry );
1806+
}
1807+
}
17681808

17691809
/**
17701810
* Create a {@link SessionFactory} using the properties and mappings in this configuration. The
@@ -2452,6 +2492,10 @@ public void registerTypeOverride(CompositeUserType type, String[] keys) {
24522492
getTypeResolver().registerTypeOverride( type, keys );
24532493
}
24542494

2495+
public void registerTypeContributor(TypeContributor typeContributor) {
2496+
typeContributorRegistrations.add( typeContributor );
2497+
}
2498+
24552499
public SessionFactoryObserver getSessionFactoryObserver() {
24562500
return sessionFactoryObserver;
24572501
}

hibernate-core/src/main/java/org/hibernate/metamodel/spi/TypeContributor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
* Contract for contributing types.
3030
*
3131
* @author Steve Ebersole
32+
*
33+
* NOTE: Cherry-pick of HHH-7998 from metamodel. For merging simplicity, just
34+
* keep it in the o.h.metamodel.spi package.
3235
*/
3336
public interface TypeContributor {
3437
/**

hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.Properties;
4646
import java.util.Set;
4747
import java.util.StringTokenizer;
48+
4849
import javax.naming.BinaryRefAddr;
4950
import javax.naming.NamingException;
5051
import javax.naming.Reference;
@@ -60,10 +61,6 @@
6061
import javax.sql.DataSource;
6162

6263
import org.dom4j.Element;
63-
import org.jboss.logging.Logger;
64-
import org.xml.sax.EntityResolver;
65-
import org.xml.sax.InputSource;
66-
6764
import org.hibernate.HibernateException;
6865
import org.hibernate.Interceptor;
6966
import org.hibernate.MappingException;
@@ -103,13 +100,17 @@
103100
import org.hibernate.internal.util.xml.XmlDocument;
104101
import org.hibernate.mapping.AuxiliaryDatabaseObject;
105102
import org.hibernate.mapping.PersistentClass;
103+
import org.hibernate.metamodel.spi.TypeContributor;
106104
import org.hibernate.proxy.EntityNotFoundDelegate;
107105
import org.hibernate.secure.internal.JACCConfiguration;
108106
import org.hibernate.service.BootstrapServiceRegistryBuilder;
109107
import org.hibernate.service.ServiceRegistry;
110108
import org.hibernate.service.ServiceRegistryBuilder;
111109
import org.hibernate.service.internal.StandardServiceRegistryImpl;
112110
import org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl;
111+
import org.jboss.logging.Logger;
112+
import org.xml.sax.EntityResolver;
113+
import org.xml.sax.InputSource;
113114

114115
/**
115116
* Allow a fine tuned configuration of an EJB 3.0 EntityManagerFactory
@@ -1655,6 +1656,11 @@ public Ejb3Configuration addResource(String path, ClassLoader classLoader) throw
16551656
cfg.addResource( path, classLoader );
16561657
return this;
16571658
}
1659+
1660+
public Ejb3Configuration addTypeContributor(TypeContributor typeContributor) {
1661+
cfg.registerTypeContributor( typeContributor );
1662+
return this;
1663+
}
16581664

16591665
private enum XML_SEARCH {
16601666
HBM,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* JBoss, Home of Professional Open Source
5+
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
6+
* as indicated by the @authors tag. All rights reserved.
7+
* See the copyright.txt in the distribution for a
8+
* full listing of individual contributors.
9+
*
10+
* This copyrighted material is made available to anyone wishing to use,
11+
* modify, copy, or redistribute it subject to the terms and conditions
12+
* of the GNU Lesser General Public License, v. 2.1.
13+
* This program is distributed in the hope that it will be useful, but WITHOUT A
14+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15+
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16+
* You should have received a copy of the GNU Lesser General Public License,
17+
* v.2.1 along with this distribution; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19+
* MA 02110-1301, USA.
20+
*/
21+
package org.hibernate.envers.entities;
22+
23+
import org.hibernate.metamodel.spi.TypeContributions;
24+
import org.hibernate.metamodel.spi.TypeContributor;
25+
import org.hibernate.service.ServiceRegistry;
26+
27+
/**
28+
* @author Brett Meyer
29+
*/
30+
public class TypeContributorImpl implements TypeContributor {
31+
32+
@Override
33+
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
34+
typeContributions.contributeType( new RevisionTypeType(),
35+
new String[] { RevisionTypeType.class.getName() } );
36+
}
37+
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.hibernate.envers.entities.TypeContributorImpl

hibernate-envers/src/main/resources/OSGI-INF/blueprint/blueprint.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77
<bean id="integrator" class="org.hibernate.envers.event.EnversIntegrator"/>
88
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator"/>
9+
10+
<bean id="typeContributor" class="org.hibernate.envers.entities.TypeContributorImpl"/>
11+
<service ref="typeContributor" interface="org.hibernate.metamodel.spi.TypeContributor"/>
912

1013
</blueprint>

hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiPersistenceProvider.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.hibernate.ejb.Ejb3Configuration;
3636
import org.hibernate.ejb.HibernatePersistence;
3737
import org.hibernate.integrator.spi.Integrator;
38+
import org.hibernate.metamodel.spi.TypeContributor;
3839
import org.hibernate.osgi.util.OsgiServiceUtil;
3940
import org.hibernate.service.BootstrapServiceRegistryBuilder;
4041
import org.osgi.framework.Bundle;
@@ -68,7 +69,7 @@ public OsgiPersistenceProvider(OsgiClassLoader osgiClassLoader, OsgiJtaPlatform
6869

6970
@Override
7071
public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
71-
generateProperties( properties );
72+
properties = generateProperties( properties );
7273

7374
// TODO: This needs tested.
7475
properties.put( org.hibernate.ejb.AvailableSettings.SCANNER, new OsgiScanner( requestingBundle ) );
@@ -80,12 +81,12 @@ public EntityManagerFactory createEntityManagerFactory(String persistenceUnitNam
8081

8182
Ejb3Configuration cfg = new Ejb3Configuration();
8283
Ejb3Configuration configured = cfg.configure( persistenceUnitName, properties );
83-
return configured != null ? configured.buildEntityManagerFactory( getBuilder( properties ) ) : null;
84+
return configured != null ? configured.buildEntityManagerFactory( getBuilder( cfg, properties ) ) : null;
8485
}
8586

8687
@Override
8788
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
88-
generateProperties( properties );
89+
properties = generateProperties( properties );
8990

9091
// OSGi ClassLoaders must implement BundleReference
9192
properties.put( org.hibernate.ejb.AvailableSettings.SCANNER,
@@ -95,10 +96,10 @@ public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitI
9596

9697
Ejb3Configuration cfg = new Ejb3Configuration();
9798
Ejb3Configuration configured = cfg.configure( info, properties );
98-
return configured != null ? configured.buildEntityManagerFactory( getBuilder( properties ) ) : null;
99+
return configured != null ? configured.buildEntityManagerFactory( getBuilder( cfg, properties ) ) : null;
99100
}
100101

101-
private BootstrapServiceRegistryBuilder getBuilder(Map properties) {
102+
private BootstrapServiceRegistryBuilder getBuilder(Ejb3Configuration cfg, Map properties) {
102103
BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
103104

104105
final Collection<ClassLoader> classLoaders = (Collection<ClassLoader>) properties
@@ -114,17 +115,22 @@ private BootstrapServiceRegistryBuilder getBuilder(Map properties) {
114115
for ( Integrator integrator : integrators ) {
115116
builder.with( integrator );
116117
}
117-
118-
// TODO: other types of services?
118+
119+
List<TypeContributor> typeContributors = OsgiServiceUtil.getServiceImpls( TypeContributor.class, context );
120+
for (TypeContributor typeContributor : typeContributors) {
121+
cfg.addTypeContributor( typeContributor );
122+
}
119123

120124
return builder;
121125
}
122126

123-
private void generateProperties(Map properties) {
127+
private Map generateProperties(Map properties) {
124128
if ( properties == null ) {
125129
properties = new HashMap();
126130
}
127131

128132
properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
133+
134+
return properties;
129135
}
130136
}

hibernate-osgi/src/main/java/org/hibernate/osgi/OsgiSessionFactoryService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.hibernate.cfg.AvailableSettings;
2727
import org.hibernate.cfg.Configuration;
2828
import org.hibernate.integrator.spi.Integrator;
29+
import org.hibernate.metamodel.spi.TypeContributor;
2930
import org.hibernate.osgi.util.OsgiServiceUtil;
3031
import org.hibernate.service.BootstrapServiceRegistryBuilder;
3132
import org.hibernate.service.ServiceRegistry;
@@ -84,6 +85,11 @@ public Object getService(Bundle requestingBundle, ServiceRegistration registrati
8485
builder.with( integrator );
8586
}
8687

88+
List<TypeContributor> typeContributors = OsgiServiceUtil.getServiceImpls( TypeContributor.class, context );
89+
for (TypeContributor typeContributor : typeContributors) {
90+
configuration.registerTypeContributor( typeContributor );
91+
}
92+
8793
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder( builder.build() )
8894
.applySettings(configuration.getProperties()).buildServiceRegistry();
8995
return configuration.buildSessionFactory(serviceRegistry);

0 commit comments

Comments
 (0)