diff --git a/documentation/documentation.gradle b/documentation/documentation.gradle index 49ff5fca04..21e3a5647d 100644 --- a/documentation/documentation.gradle +++ b/documentation/documentation.gradle @@ -71,6 +71,8 @@ dependencies { testRuntime( libraries.mariadb ) testRuntime( libraries.mssql ) + testCompile "org.osgi:org.osgi.core:4.3.1" + if (db.equalsIgnoreCase("oracle")) { dependencies { testRuntime( libraries.oracle ) { diff --git a/documentation/src/main/asciidoc/userguide/chapters/osgi/OSGi.adoc b/documentation/src/main/asciidoc/userguide/chapters/osgi/OSGi.adoc index d3f0bd7d42..51f23e5dee 100644 --- a/documentation/src/main/asciidoc/userguide/chapters/osgi/OSGi.adoc +++ b/documentation/src/main/asciidoc/userguide/chapters/osgi/OSGi.adoc @@ -1,6 +1,7 @@ [[osgi]] == OSGi -:sourcedir: extras +:sourcedir: ../../../../../test/java/org/hibernate/userguide/osgi +:extrasdir: extras === OSGi Specification and Environment @@ -73,7 +74,7 @@ You can deploy the `DataSource` manually (Karaf has a `deploy` dir), or through ==== [source,xml] ---- -include::{sourcedir}/datasource-h2.xml[] +include::{extrasdir}/datasource-h2.xml[] ---- ==== @@ -103,7 +104,7 @@ The container takes the name of your persistence unit, then automatically inject ==== [source,xml] ---- -include::{sourcedir}/blueprint.xml[] +include::{extrasdir}/blueprint.xml[] ---- ==== @@ -137,11 +138,12 @@ The service handles the OSGi `ClassLoader`, discovered extension points, scannin Manually creating an `EntityManagerFactory` is guaranteed to NOT work during runtime! ==== +[[osgi-discover-EntityManagerFactory]] .Discover/Use `EntityManagerFactory` ==== [source,java] ---- -include::{sourcedir}/UnmanagedJPAHibernateUtil.java[] +include::{sourcedir}/jpa/HibernateUtil.java[tag=osgi-discover-EntityManagerFactory, indent=0] ---- ==== @@ -170,11 +172,12 @@ It is VITAL that your `SessionFactory` be obtained through the service, rather t Manually creating a `SessionFactory` is guaranteed to NOT work during runtime! ==== +[[osgi-discover-SessionFactory]] .Discover/Use `SessionFactory` ==== [source,java] ---- -include::{sourcedir}/NativeHibernateUtil.java[] +include::{sourcedir}/_native/HibernateUtil.java[tag=osgi-discover-SessionFactory, indent=0] ---- ==== @@ -207,7 +210,7 @@ Add `OSGI-INF/blueprint/blueprint.xml` to your classpath. Envers' blueprint is a ==== [source,xml] ---- -include::{sourcedir}/extension_point_blueprint.xml[] +include::{extrasdir}/extension_point_blueprint.xml[] ---- ==== diff --git a/documentation/src/main/asciidoc/userguide/chapters/osgi/extras/NativeHibernateUtil.java b/documentation/src/main/asciidoc/userguide/chapters/osgi/extras/NativeHibernateUtil.java deleted file mode 100644 index dd53006a76..0000000000 --- a/documentation/src/main/asciidoc/userguide/chapters/osgi/extras/NativeHibernateUtil.java +++ /dev/null @@ -1,19 +0,0 @@ -public class HibernateUtil { - - private SessionFactory sf; - - public Session getSession() { - return getSessionFactory().openSession(); - } - - private SessionFactory getSessionFactory() { - if ( sf == null ) { - Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class ); - BundleContext context = thisBundle.getBundleContext(); - - ServiceReference sr = context.getServiceReference( SessionFactory.class.getName() ); - sf = ( SessionFactory ) context.getService( sr ); - } - return sf; - } -} \ No newline at end of file diff --git a/documentation/src/main/asciidoc/userguide/chapters/osgi/extras/UnmanagedJPAHibernateUtil.java b/documentation/src/main/asciidoc/userguide/chapters/osgi/extras/UnmanagedJPAHibernateUtil.java deleted file mode 100644 index 1a82ae45a7..0000000000 --- a/documentation/src/main/asciidoc/userguide/chapters/osgi/extras/UnmanagedJPAHibernateUtil.java +++ /dev/null @@ -1,21 +0,0 @@ -public class HibernateUtil { - - private EntityManagerFactory emf; - - public EntityManager getEntityManager() { - return getEntityManagerFactory().createEntityManager(); - } - - private EntityManagerFactory getEntityManagerFactory() { - if ( emf == null ) { - Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class ); - BundleContext context = thisBundle.getBundleContext(); - - ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() ); - PersistenceProvider persistenceProvider = ( PersistenceProvider ) context.getService( serviceReference ); - - emf = persistenceProvider.createEntityManagerFactory( "YourPersistenceUnitName", null ); - } - return emf; - } -} \ No newline at end of file diff --git a/documentation/src/test/java/org/hibernate/userguide/osgi/_native/HibernateUtil.java b/documentation/src/test/java/org/hibernate/userguide/osgi/_native/HibernateUtil.java new file mode 100644 index 0000000000..044be8747a --- /dev/null +++ b/documentation/src/test/java/org/hibernate/userguide/osgi/_native/HibernateUtil.java @@ -0,0 +1,35 @@ +package org.hibernate.userguide.osgi._native; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; + +//tag::osgi-discover-SessionFactory[] +public class HibernateUtil { + + private SessionFactory sf; + + public Session getSession() { + return getSessionFactory().openSession(); + } + + private SessionFactory getSessionFactory() { + if ( sf == null ) { + Bundle thisBundle = FrameworkUtil.getBundle( + HibernateUtil.class + ); + BundleContext context = thisBundle.getBundleContext(); + + ServiceReference sr = context.getServiceReference( + SessionFactory.class.getName() + ); + sf = ( SessionFactory ) context.getService( sr ); + } + return sf; + } +} +//end::osgi-discover-SessionFactory[] \ No newline at end of file diff --git a/documentation/src/test/java/org/hibernate/userguide/osgi/jpa/HibernateUtil.java b/documentation/src/test/java/org/hibernate/userguide/osgi/jpa/HibernateUtil.java new file mode 100644 index 0000000000..3a9402df71 --- /dev/null +++ b/documentation/src/test/java/org/hibernate/userguide/osgi/jpa/HibernateUtil.java @@ -0,0 +1,44 @@ +package org.hibernate.userguide.osgi.jpa; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.spi.PersistenceProvider; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; + +//tag::osgi-discover-EntityManagerFactory[] +public class HibernateUtil { + + private EntityManagerFactory emf; + + public EntityManager getEntityManager() { + return getEntityManagerFactory().createEntityManager(); + } + + private EntityManagerFactory getEntityManagerFactory() { + if ( emf == null ) { + Bundle thisBundle = FrameworkUtil.getBundle( + HibernateUtil.class + ); + BundleContext context = thisBundle.getBundleContext(); + + ServiceReference serviceReference = context.getServiceReference( + PersistenceProvider.class.getName() + ); + PersistenceProvider persistenceProvider = ( PersistenceProvider ) context + .getService( + serviceReference + ); + + emf = persistenceProvider.createEntityManagerFactory( + "YourPersistenceUnitName", + null + ); + } + return emf; + } +} +//end::osgi-discover-EntityManagerFactory[] \ No newline at end of file