HHH-8160 Corrected OSGi scanner

This commit is contained in:
Brett Meyer 2013-04-08 18:59:21 -04:00
parent cc4ef44670
commit 73891e06d5
3 changed files with 81 additions and 52 deletions

View File

@ -20,9 +20,12 @@
*/ */
package org.hibernate.osgi; package org.hibernate.osgi;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
import javax.persistence.PersistenceException;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.jpa.boot.archive.spi.ArchiveContext; import org.hibernate.jpa.boot.archive.spi.ArchiveContext;
import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptor; import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptor;
@ -55,9 +58,10 @@ public OsgiArchiveDescriptor(Bundle persistenceBundle) {
public void visitArchive(ArchiveContext context) { public void visitArchive(ArchiveContext context) {
Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE ); Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE );
for ( final String resource : resources ) { for ( final String resource : resources ) {
// TODO: Is there a better way to check this? Karaf is including
// directories.
if ( !resource.endsWith( "/" ) ) {
try { try {
final InputStream inputStream = persistenceBundle.getResource( resource ).openStream();
// TODO: Is using resource as the names correct? // TODO: Is using resource as the names correct?
final InputStreamAccess inputStreamAccess = new InputStreamAccess() { final InputStreamAccess inputStreamAccess = new InputStreamAccess() {
@ -68,12 +72,23 @@ public String getStreamName() {
@Override @Override
public InputStream accessInputStream() { public InputStream accessInputStream() {
return inputStream; return openInputStream();
} }
@Override @Override
public NamedInputStream asNamedInputStream() { public NamedInputStream asNamedInputStream() {
return new NamedInputStream( resource, inputStream ); return new NamedInputStream( resource, openInputStream() );
}
private InputStream openInputStream() {
try {
return persistenceBundle.getResource( resource ).openStream();
}
catch ( IOException e ) {
throw new PersistenceException(
"Unable to open an InputStream on the OSGi Bundle resource!",
e );
}
} }
}; };
@ -102,5 +117,6 @@ public InputStreamAccess getStreamAccess() {
} }
} }
} }
}
} }

View File

@ -22,15 +22,15 @@
import java.net.URL; import java.net.URL;
import org.hibernate.jpa.boot.archive.internal.StandardArchiveDescriptorFactory;
import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptor; import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptor;
import org.hibernate.jpa.boot.archive.spi.ArchiveDescriptorFactory;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
/** /**
* @author Brett Meyer * @author Brett Meyer
* @author Tim Ward * @author Tim Ward
*/ */
public class OsgiArchiveDescriptorFactory extends StandardArchiveDescriptorFactory { public class OsgiArchiveDescriptorFactory implements ArchiveDescriptorFactory {
private Bundle persistenceBundle; private Bundle persistenceBundle;
@ -38,12 +38,25 @@ public OsgiArchiveDescriptorFactory(Bundle persistenceBundle) {
this.persistenceBundle = persistenceBundle; this.persistenceBundle = persistenceBundle;
} }
@Override
public ArchiveDescriptor buildArchiveDescriptor(URL url) {
return buildArchiveDescriptor( url, "" );
}
@Override @Override
public ArchiveDescriptor buildArchiveDescriptor(URL url, String entry) { public ArchiveDescriptor buildArchiveDescriptor(URL url, String entry) {
final String protocol = url.getProtocol();
if ( "bundle".equals( protocol ) ) {
return new OsgiArchiveDescriptor( persistenceBundle ); return new OsgiArchiveDescriptor( persistenceBundle );
} }
return super.buildArchiveDescriptor( url, entry );
@Override
public URL getJarURLFromURLEntry(URL url, String entry) throws IllegalArgumentException {
// not used
return null;
}
@Override
public URL getURLFromPath(String jarPath) {
// not used
return null;
} }
} }

View File

@ -64,7 +64,7 @@ public EntityManagerFactory createEntityManagerFactory(String persistenceUnitNam
} }
properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
// TODO: This needs tested. // TODO: This needs tested.
properties.put( org.hibernate.ejb.AvailableSettings.SCANNER, properties.put( org.hibernate.jpa.AvailableSettings.SCANNER,
new OsgiScanner( requestingBundle ) ); new OsgiScanner( requestingBundle ) );
osgiClassLoader.addBundle( requestingBundle ); osgiClassLoader.addBundle( requestingBundle );
@ -79,7 +79,7 @@ public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitI
} }
properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform ); properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
// OSGi ClassLoaders must implement BundleReference // OSGi ClassLoaders must implement BundleReference
properties.put( org.hibernate.ejb.AvailableSettings.SCANNER, properties.put( org.hibernate.jpa.AvailableSettings.SCANNER,
new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) ); new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) );
osgiClassLoader.addClassLoader( info.getClassLoader() ); osgiClassLoader.addClassLoader( info.getClassLoader() );