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,50 +58,63 @@ public class OsgiArchiveDescriptor implements ArchiveDescriptor {
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 ) {
try { // TODO: Is there a better way to check this? Karaf is including
final InputStream inputStream = persistenceBundle.getResource( resource ).openStream(); // directories.
if ( !resource.endsWith( "/" ) ) {
// TODO: Is using resource as the names correct? try {
// 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 );
}
}; final InputStreamAccess inputStreamAccess = new InputStreamAccess() {
@Override
final ArchiveEntry entry = new ArchiveEntry() { public String getStreamName() {
@Override return resource;
public String getName() { }
return resource;
} @Override
public InputStream accessInputStream() {
@Override return openInputStream();
public String getNameWithinArchive() { }
return resource;
} @Override
public NamedInputStream asNamedInputStream() {
@Override return new NamedInputStream( resource, openInputStream() );
public InputStreamAccess getStreamAccess() { }
return inputStreamAccess;
} private InputStream openInputStream() {
}; try {
return persistenceBundle.getResource( resource ).openStream();
context.obtainArchiveEntryHandler( entry ).handleEntry( entry, context ); }
} catch ( IOException e ) {
catch ( Exception e ) { throw new PersistenceException(
LOG.unableToLoadScannedClassOrResource( e ); "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 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 class OsgiArchiveDescriptorFactory extends StandardArchiveDescriptorFacto
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(); return new OsgiArchiveDescriptor( persistenceBundle );
if ( "bundle".equals( protocol ) ) { }
return new OsgiArchiveDescriptor( persistenceBundle );
} @Override
return super.buildArchiveDescriptor( url, entry ); 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 ); 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 class OsgiPersistenceProvider extends HibernatePersistenceProvider {
} }
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() );