Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hibernate-core/hibernate-core.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jar {
manifest {
instruction 'Bundle-Description', 'Hibernate ORM Core'

instruction 'Import-Package',
instructionFirst 'Import-Package',
'javax.security.auth;resolution:=optional',
'javax.security.jacc;resolution:=optional',
'javax.validation;resolution:=optional',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ClassLoaderServiceImpl(ClassLoader classLoader) {
public ClassLoaderServiceImpl(Collection<ClassLoader> providedClassLoaders) {
final LinkedHashSet<ClassLoader> orderedClassLoaderSet = new LinkedHashSet<ClassLoader>();

// first add all provided class loaders, if any
// first, add all provided class loaders, if any
if ( providedClassLoaders != null ) {
for ( ClassLoader classLoader : providedClassLoaders ) {
if ( classLoader != null ) {
Expand All @@ -87,8 +87,9 @@ public ClassLoaderServiceImpl(Collection<ClassLoader> providedClassLoaders) {
}

// normalize adding known class-loaders...
// first, the Hibernate class loader
// then the Hibernate class loader
orderedClassLoaderSet.add( ClassLoaderServiceImpl.class.getClassLoader() );

// then the TCCL, if one...
final ClassLoader tccl = locateTCCL();
if ( tccl != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
package org.hibernate.internal;

import static org.jboss.logging.Logger.Level.DEBUG;
import static org.jboss.logging.Logger.Level.ERROR;
import static org.jboss.logging.Logger.Level.INFO;
import static org.jboss.logging.Logger.Level.WARN;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -33,37 +38,32 @@
import java.util.Hashtable;
import java.util.Properties;
import java.util.Set;

import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;

import org.jboss.logging.BasicLogger;
import org.jboss.logging.Cause;
import org.jboss.logging.LogMessage;
import org.jboss.logging.Message;
import org.jboss.logging.MessageLogger;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.cache.CacheException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
import org.hibernate.engine.jndi.JndiException;
import org.hibernate.engine.jndi.JndiNameException;
import org.hibernate.engine.loading.internal.CollectionLoadContext;
import org.hibernate.engine.loading.internal.EntityLoadContext;
import org.hibernate.engine.spi.CollectionKey;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.id.IntegralDataTypeHolder;
import org.hibernate.engine.jndi.JndiException;
import org.hibernate.engine.jndi.JndiNameException;
import org.hibernate.type.BasicType;
import org.hibernate.type.SerializationException;
import org.hibernate.type.Type;

import static org.jboss.logging.Logger.Level.DEBUG;
import static org.jboss.logging.Logger.Level.ERROR;
import static org.jboss.logging.Logger.Level.INFO;
import static org.jboss.logging.Logger.Level.WARN;
import org.jboss.logging.BasicLogger;
import org.jboss.logging.Cause;
import org.jboss.logging.LogMessage;
import org.jboss.logging.Message;
import org.jboss.logging.MessageLogger;

/**
* The jboss-logging {@link MessageLogger} for the hibernate-core module. It reserves message ids ranging from
Expand Down Expand Up @@ -1619,12 +1619,4 @@ void cannotResolveNonNullableTransientDependencies(String transientEntityString,
@LogMessage(level = INFO)
@Message( value = "'javax.persistence.validation.mode' named multiple values : %s", id = 448 )
void multipleValidationModes(String modes);

@LogMessage(level = WARN)
@Message(value = "Exception while loading a class or resource found during scanning", id = 449)
void unableToLoadScannedClassOrResource(@Cause Exception e);

@LogMessage(level = WARN)
@Message(value = "Exception while discovering OSGi service implementations : %s", id = 450)
void unableToDiscoverOsgiService(String service, @Cause Exception e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public EntityManagerFactory createEntityManagerFactory(String persistenceUnitNam
}

protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties) {
return getEntityManagerFactoryBuilderOrNull( persistenceUnitName, properties, null );
}

protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties, ClassLoader providedClassLoader) {
log.tracef( "Attempting to obtain correct EntityManagerFactoryBuilder for persistenceUnitName : %s", persistenceUnitName );

final Map integration = wrap( properties );
Expand Down Expand Up @@ -118,7 +122,7 @@ protected EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(Strin
continue;
}

return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnit, integration );
return Bootstrap.getEntityManagerFactoryBuilder( persistenceUnit, integration, providedClassLoader );
}

log.debug( "Found no matching persistence units" );
Expand Down
127 changes: 111 additions & 16 deletions hibernate-osgi/hibernate-osgi.gradle
Original file line number Diff line number Diff line change
@@ -1,28 +1,123 @@
configurations {
osgiRuntime
}

sourceSets {
testResult
testClientBundle
}

sourceSets.test {
compileClasspath += sourceSets.testResult.output
runtimeClasspath += sourceSets.testResult.output
}

sourceSets.testClientBundle {
compileClasspath += sourceSets.testResult.output
runtimeClasspath += sourceSets.testResult.output
}

dependencies {
compile( project( ':hibernate-core' ) )
compile( project( ':hibernate-entitymanager' ) )
// MUST use 4.3.1! 4.3.0 was compiled with "-target jsr14".
// http://blog.osgi.org/2012/10/43-companion-code-for-java-7.html
compile( "org.osgi:org.osgi.core:4.3.1" )
compile( project( ':hibernate-core' ) )
compile( project( ':hibernate-entitymanager' ) )
// MUST use 4.3.1! 4.3.0 was compiled with "-target jsr14".
// http://blog.osgi.org/2012/10/43-companion-code-for-java-7.html
compile( "org.osgi:org.osgi.core:4.3.1" )

testCompile( libraries.shrinkwrap_api )
testCompile( libraries.shrinkwrap )
testCompile( "org.jboss.arquillian.junit:arquillian-junit-container:1.0.3.Final" )
testCompile( "org.jboss.osgi.metadata:jbosgi-metadata:3.0.0.CR1" )
testRuntime( "org.jboss.arquillian.container:arquillian-osgi-felix:2.0.0.CR4" )
testRuntime( "org.apache.felix:org.apache.felix.framework:4.0.3" )
testRuntime( "org.apache.felix:org.apache.felix.main:4.0.3" )
testRuntime( "org.jboss.logmanager:jboss-logmanager:1.4.1.Final" )

// Local copies of all jars needed fur the OSGi runtime.
osgiRuntime( "org.jboss.arquillian.osgi:arquillian-osgi-bundle:1.0.3.Final" )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation of the OSGi dep's seems odd.

osgiRuntime( "org.ops4j.pax.url:pax-url-wrap:1.5.2" )
osgiRuntime( "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0-SNAPSHOT" )
osgiRuntime( "javax.enterprise:cdi-api:1.1-PFD" )
osgiRuntime( "org.jboss.spec.javax.interceptor:jboss-interceptors-api_1.2_spec:1.0.0.Alpha1" )
osgiRuntime( "org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.0.0.Alpha1" )
osgiRuntime( "commons-collections:commons-collections:3.2.1" )
osgiRuntime( "commons-pool:commons-pool:1.6" )
osgiRuntime( "commons-dbcp:commons-dbcp:1.4" )
osgiRuntime( "commons-lang:commons-lang:2.6" )
osgiRuntime( "net.sourceforge.serp:serp:1.14.1" )
osgiRuntime( "com.h2database:h2:1.3.170" )
osgiRuntime( "org.apache.servicemix.bundles:org.apache.servicemix.bundles.antlr:2.7.7_5" )
osgiRuntime( libraries.javassist )
osgiRuntime( "org.apache.servicemix.specs:org.apache.servicemix.specs.jsr303-api-1.0.0:2.2.0" )
osgiRuntime( "org.apache.servicemix.bundles:org.apache.servicemix.bundles.ant:1.8.2_2" )
osgiRuntime( "org.apache.servicemix.specs:org.apache.servicemix.specs.stax-api-1.2:2.2.0" )
osgiRuntime( "org.apache.servicemix.bundles:org.apache.servicemix.bundles.dom4j:1.6.1_5" )
osgiRuntime( libraries.commons_annotations )
osgiRuntime( libraries.jandex )
osgiRuntime( libraries.classmate )
osgiRuntime( libraries.logging )

testClientBundleCompile( project( ':hibernate-core' ) )
testClientBundleCompile( project( ':hibernate-entitymanager' ) )
// MUST use 4.3.1! 4.3.0 was compiled with "-target jsr14".
// http://blog.osgi.org/2012/10/43-companion-code-for-java-7.html
testClientBundleCompile( "org.osgi:org.osgi.core:4.3.1" )
}

def pomName() {
return 'Hibernate OSGi Support'
return 'Hibernate OSGi Support'
}

def pomDescription() {
return 'Support for running Hibernate O/RM in OSGi environments'
return 'Support for running Hibernate O/RM in OSGi environments'
}

jar {
manifest {
instruction 'Bundle-Activator', 'org.hibernate.osgi.HibernateBundleActivator'
instruction 'Bundle-Description', 'Hibernate ORM OSGi'
manifest {
instruction 'Bundle-Activator', 'org.hibernate.osgi.HibernateBundleActivator'
instruction 'Bundle-Description', 'Hibernate ORM OSGi'

instruction 'Import-Package',
// TODO: Shouldn't have to explicitly list this, but the plugin
// generates it with a [1.0,2) version.
'javax.persistence;version="2.1.0"',
'javax.persistence.spi;version="2.1.0"'
}
instruction 'Import-Package',
// TODO: Shouldn't have to explicitly list this, but the plugin
// generates it with a [1.0,2) version.
'javax.persistence;version="2.1.0"',
'javax.persistence.spi;version="2.1.0"'
}
}

task copyBnd(type: Copy) {
into "$buildDir/osgi-lib/bnd"
from "src/test/resources/bnd"
}

task runBnd(type: JavaExec){
main = "-jar"
args "$buildDir/osgi-lib/bnd/bnd.jar", "$buildDir/osgi-lib/bnd/cdi-api.bnd", "$buildDir/osgi-lib/bnd/el-api.bnd", "$buildDir/osgi-lib/bnd/jandex.bnd", "$buildDir/osgi-lib/bnd/javassist.bnd", "$buildDir/osgi-lib/bnd/serp.bnd"
}

task copyToLib(type: Copy) {
into "$buildDir/osgi-lib"
from configurations.osgiRuntime
}

task testClientBundleJar(type: Jar) {
from sourceSets.testClientBundle.output, sourceSets.testResult.output
destinationDir new File("$buildDir/osgi-lib")
archiveName "testClientBundle.jar"

// The OSGi plugin acts up when we need to export multiple source sets. Just do it manually.
manifest {
attributes("Export-Package" : "org.hibernate.osgi.test.client,org.hibernate.osgi.test.result",
"Bundle-Name" : "testClientBundle",
"Bundle-Activator" : "org.hibernate.osgi.test.client.OsgiTestActivator",
"Bundle-ManifestVersion" : "2",
"Bundle-SymbolicName" : "testClientBundle",
"Import-Package" : "javassist.util.proxy,javax.persistence,javax.persistence.spi,org.h2,org.hibernate,org.hibernate.proxy,org.osgi.framework")
}
}

runBnd.dependsOn copyToLib
runBnd.dependsOn copyBnd
test.dependsOn runBnd
test.dependsOn testClientBundleJar
test.dependsOn jar
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import javax.persistence.PersistenceException;

import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.jpa.boot.archive.spi.ArchiveContext;
import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptor;
import org.hibernate.jpa.boot.archive.spi.ArchiveEntry;
Expand All @@ -43,10 +42,7 @@
* @author Tim Ward
*/
public class OsgiArchiveDescriptor implements ArchiveDescriptor {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
OsgiArchiveDescriptor.class.getName()
);
private static final Logger LOG = Logger.getLogger( OsgiArchiveDescriptor.class );

private final Bundle persistenceBundle;
private final BundleWiring bundleWiring;
Expand Down Expand Up @@ -119,7 +115,7 @@ public InputStreamAccess getStreamAccess() {
context.obtainArchiveEntryHandler( entry ).handleEntry( entry, context );
}
catch ( Exception e ) {
LOG.unableToLoadScannedClassOrResource( e );
LOG.warn( "Exception while loading a class or resource found during scanning", e );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,45 @@ public class OsgiClassLoader extends ClassLoader {
private Map<String, URL> resourceCache = new HashMap<String, URL>();

/**
* Load the class and break on first found match.
* Load the class and break on first found match. DO NOT use ClassLoader#parent, which is typically the
* SystemClassLoader for most containers. Instead, allow the ClassNotFoundException to be thrown.
* ClassLoaderServiceImpl will check the SystemClassLoader later on.
*
* TODO: Should this throw a different exception or warn if multiple
* classes were found? Naming collisions can and do happen in OSGi...
*/
@Override
@SuppressWarnings("rawtypes")
protected Class<?> findClass(String name) throws ClassNotFoundException {
if ( classCache.containsKey( name ) ) {
return classCache.get( name );
}

for ( Bundle bundle : bundles ) {
try {
final Class clazz = bundle.loadClass( name );
if ( clazz != null ) {
classCache.put( name, clazz );
return clazz;
}
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
if ( classCache.containsKey( name ) ) {
return classCache.get( name );
}
catch ( Exception ignore ) {
}
}

for ( ClassLoader classLoader : classLoaders ) {
try {
final Class clazz = classLoader.loadClass( name );
if ( clazz != null ) {
classCache.put( name, clazz );
return clazz;

for ( Bundle bundle : bundles ) {
try {
final Class clazz = bundle.loadClass( name );
if ( clazz != null ) {
classCache.put( name, clazz );
return clazz;
}
}
catch ( Exception ignore ) {
}
}
catch ( Exception ignore ) {

for ( ClassLoader classLoader : classLoaders ) {
try {
final Class clazz = classLoader.loadClass( name );
if ( clazz != null ) {
classCache.put( name, clazz );
return clazz;
}
}
catch ( Exception ignore ) {
}
}
}
}

throw new ClassNotFoundException( "Could not load requested class : " + name );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.jpa.boot.spi.IntegratorProvider;
import org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList;
import org.hibernate.jpa.boot.spi.TypeContributorList;
Expand Down Expand Up @@ -90,7 +92,8 @@ public EntityManagerFactory createEntityManagerFactory(String persistenceUnitNam

osgiClassLoader.addBundle( requestingBundle );

return super.createEntityManagerFactory( persistenceUnitName, settings );
final EntityManagerFactoryBuilder builder = getEntityManagerFactoryBuilderOrNull( persistenceUnitName, settings, osgiClassLoader );
return builder == null ? null : builder.build();
}

@Override
Expand All @@ -106,7 +109,7 @@ public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitI

osgiClassLoader.addClassLoader( info.getClassLoader() );

return super.createContainerEntityManagerFactory( info, settings );
return Bootstrap.getEntityManagerFactoryBuilder( info, settings, osgiClassLoader ).build();
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -138,7 +141,7 @@ public List<StrategyRegistrationProvider> getStrategyRegistrationProviders() {
settings.put( EntityManagerFactoryBuilderImpl.STRATEGY_REGISTRATION_PROVIDERS, strategyRegistrationProviderList );

final List<TypeContributor> typeContributors = OsgiServiceUtil.getServiceImpls( TypeContributor.class, context );
TypeContributorList typeContributorList = new TypeContributorList() {
final TypeContributorList typeContributorList = new TypeContributorList() {
@Override
public List<TypeContributor> getTypeContributors() {
return typeContributors;
Expand Down
Loading