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;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
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;
@ -55,50 +58,63 @@ public class OsgiArchiveDescriptor implements ArchiveDescriptor {
public void visitArchive(ArchiveContext context) {
Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE );
for ( final String resource : resources ) {
try {
final InputStream inputStream = persistenceBundle.getResource( resource ).openStream();
// TODO: Is using resource as the names correct?
final InputStreamAccess inputStreamAccess = new InputStreamAccess() {
@Override
public String getStreamName() {
return resource;
}
@Override
public InputStream accessInputStream() {
return inputStream;
}
@Override
public NamedInputStream asNamedInputStream() {
return new NamedInputStream( resource, inputStream );
}
// TODO: Is there a better way to check this? Karaf is including
// directories.
if ( !resource.endsWith( "/" ) ) {
try {
// TODO: Is using resource as the names correct?
};
final ArchiveEntry entry = new ArchiveEntry() {
@Override
public String getName() {
return resource;
}
@Override
public String getNameWithinArchive() {
return resource;
}
@Override
public InputStreamAccess getStreamAccess() {
return inputStreamAccess;
}
};
context.obtainArchiveEntryHandler( entry ).handleEntry( entry, context );
}
catch ( Exception e ) {
LOG.unableToLoadScannedClassOrResource( e );
final InputStreamAccess inputStreamAccess = new InputStreamAccess() {
@Override
public String getStreamName() {
return resource;
}
@Override
public InputStream accessInputStream() {
return openInputStream();
}
@Override
public NamedInputStream asNamedInputStream() {
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 );
}
}
};
final ArchiveEntry entry = new ArchiveEntry() {
@Override
public String getName() {
return resource;
}
@Override
public String getNameWithinArchive() {
return resource;
}
@Override
public InputStreamAccess getStreamAccess() {
return inputStreamAccess;
}
};
context.obtainArchiveEntryHandler( entry ).handleEntry( entry, context );
}
catch ( Exception e ) {
LOG.unableToLoadScannedClassOrResource( e );
}
}
}
}

View File

@ -22,15 +22,15 @@ package org.hibernate.osgi;
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.ArchiveDescriptorFactory;
import org.osgi.framework.Bundle;
/**
* @author Brett Meyer
* @author Tim Ward
*/
public class OsgiArchiveDescriptorFactory extends StandardArchiveDescriptorFactory {
public class OsgiArchiveDescriptorFactory implements ArchiveDescriptorFactory {
private Bundle persistenceBundle;
@ -38,12 +38,25 @@ public class OsgiArchiveDescriptorFactory extends StandardArchiveDescriptorFacto
this.persistenceBundle = persistenceBundle;
}
@Override
public ArchiveDescriptor buildArchiveDescriptor(URL url) {
return buildArchiveDescriptor( url, "" );
}
@Override
public ArchiveDescriptor buildArchiveDescriptor(URL url, String entry) {
final String protocol = url.getProtocol();
if ( "bundle".equals( protocol ) ) {
return new OsgiArchiveDescriptor( persistenceBundle );
}
return super.buildArchiveDescriptor( url, entry );
return new OsgiArchiveDescriptor( persistenceBundle );
}
@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 class OsgiPersistenceProvider extends HibernatePersistenceProvider {
}
properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
// TODO: This needs tested.
properties.put( org.hibernate.ejb.AvailableSettings.SCANNER,
properties.put( org.hibernate.jpa.AvailableSettings.SCANNER,
new OsgiScanner( requestingBundle ) );
osgiClassLoader.addBundle( requestingBundle );
@ -79,7 +79,7 @@ public class OsgiPersistenceProvider extends HibernatePersistenceProvider {
}
properties.put( AvailableSettings.JTA_PLATFORM, osgiJtaPlatform );
// 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() ) );
osgiClassLoader.addClassLoader( info.getClassLoader() );