Skip to content

Commit

Permalink
HHH-8159 - Apply fixups indicated by analysis tools
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Apr 30, 2013
1 parent 97e2820 commit 2086658
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 118 deletions.
Expand Up @@ -51,37 +51,40 @@
* SessionFactory is registered as an OSGi ServiceFactory -- each requesting
* bundle gets its own instance of a SessionFactory. The use of services,
* rather than direct use of Configuration, is necessary to shield users
* from ClassLoader issues. See {@link #OsgiSessionFactoryService} for more
* from ClassLoader issues. See {@link OsgiSessionFactoryService} for more
* information.
*
* @author Brett Meyer
* @author Tim Ward
*/
@SuppressWarnings("UnusedDeclaration")
public class HibernateBundleActivator implements BundleActivator {

private OsgiClassLoader osgiClassLoader;

private OsgiJtaPlatform osgiJtaPlatform;

@Override
@SuppressWarnings("unchecked")
public void start(BundleContext context) throws Exception {

osgiClassLoader = new OsgiClassLoader();
// build a ClassLoader that uses all the necessary OSGi bundles, and place it into
// a well-known location so internals can access it
final OsgiClassLoader osgiClassLoader = new OsgiClassLoader();
osgiClassLoader.addBundle( FrameworkUtil.getBundle( Session.class ) );
osgiClassLoader.addBundle( FrameworkUtil.getBundle( HibernatePersistenceProvider.class ) );
ClassLoaderHelper.overridenClassLoader = osgiClassLoader;

osgiJtaPlatform = new OsgiJtaPlatform( context );
// Build a JtaPlatform specific for this OSGi context
final OsgiJtaPlatform osgiJtaPlatform = new OsgiJtaPlatform( context );

Dictionary properties = new Hashtable();
// In order to support existing persistence.xml files, register
// using the legacy provider name.
final Dictionary properties = new Hashtable();
// In order to support existing persistence.xml files, register using the legacy provider name.
properties.put( "javax.persistence.provider", HibernatePersistenceProvider.class.getName() );
context.registerService( PersistenceProvider.class.getName(),
new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ), properties );

context.registerService( SessionFactory.class.getName(),
new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ), new Hashtable());
context.registerService(
PersistenceProvider.class.getName(),
new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ),
properties
);
context.registerService(
SessionFactory.class.getName(),
new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ),
new Hashtable()
);
}

@Override
Expand Down
Expand Up @@ -37,33 +37,39 @@
import org.osgi.framework.wiring.BundleWiring;

/**
* ArchiveDescriptor implementation for describing archives in the OSGi sense
*
* @author Brett Meyer
* @author Tim Ward
*/
public class OsgiArchiveDescriptor implements ArchiveDescriptor {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
OsgiArchiveDescriptor.class.getName()
);

private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class,
OsgiArchiveDescriptor.class.getName() );

private BundleWiring bundleWiring;

private Bundle persistenceBundle;
private final Bundle persistenceBundle;
private final BundleWiring bundleWiring;

/**
* Creates a OsgiArchiveDescriptor
*
* @param persistenceBundle The bundle being described as an archive
*/
@SuppressWarnings("RedundantCast")
public OsgiArchiveDescriptor(Bundle persistenceBundle) {
this.persistenceBundle = persistenceBundle;
bundleWiring = (BundleWiring) persistenceBundle.adapt( BundleWiring.class );
}

@Override
public void visitArchive(ArchiveContext context) {
Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE );
final Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE );
for ( final String resource : resources ) {
// TODO: Is there a better way to check this? Karaf is including
// directories.
// 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 InputStreamAccess inputStreamAccess = new InputStreamAccess() {
@Override
public String getStreamName() {
Expand Down
Expand Up @@ -27,13 +27,19 @@
import org.osgi.framework.Bundle;

/**
* ArchiveDescriptorFactory implementation for OSGi environments
*
* @author Brett Meyer
* @author Tim Ward
*/
public class OsgiArchiveDescriptorFactory implements ArchiveDescriptorFactory {

private Bundle persistenceBundle;

/**
* Creates a OsgiArchiveDescriptorFactory
*
* @param persistenceBundle The OSGi bundle being scanned
*/
public OsgiArchiveDescriptorFactory(Bundle persistenceBundle) {
this.persistenceBundle = persistenceBundle;
}
Expand Down
Expand Up @@ -41,32 +41,28 @@
* @author Tim Ward
*/
public class OsgiClassLoader extends ClassLoader {

private List<ClassLoader> classLoaders = new ArrayList<ClassLoader>();

private List<Bundle> bundles = new ArrayList<Bundle>();

private Map<String, Class<?>> classCache = new HashMap<String, Class<?>>();

private Map<String, URL> resourceCache = new HashMap<String, URL>();

private Map<String, Enumeration<URL>> resourceListCache = new HashMap<String, Enumeration<URL>>();

/**
* Load the class and break on first found match.
* TODO: Should this throw a different exception or warn if multiple
* classes were found? Naming collisions can and do happen in OSGi...
*/
@SuppressWarnings("rawtypes")
@Override
@SuppressWarnings("rawtypes")
protected Class<?> findClass(String name) throws ClassNotFoundException {
if ( classCache.containsKey( name ) ) {
return classCache.get( name );
}

for ( Bundle bundle : bundles ) {
try {
Class clazz = bundle.loadClass( name );
final Class clazz = bundle.loadClass( name );
if ( clazz != null ) {
classCache.put( name, clazz );
return clazz;
Expand All @@ -78,7 +74,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {

for ( ClassLoader classLoader : classLoaders ) {
try {
Class clazz = classLoader.loadClass( name );
final Class clazz = classLoader.loadClass( name );
if ( clazz != null ) {
classCache.put( name, clazz );
return clazz;
Expand All @@ -104,7 +100,7 @@ protected URL findResource(String name) {

for ( Bundle bundle : bundles ) {
try {
URL resource = bundle.getResource( name );
final URL resource = bundle.getResource( name );
if ( resource != null ) {
resourceCache.put( name, resource );
return resource;
Expand All @@ -116,7 +112,7 @@ protected URL findResource(String name) {

for ( ClassLoader classLoader : classLoaders ) {
try {
URL resource = classLoader.getResource( name );
final URL resource = classLoader.getResource( name );
if ( resource != null ) {
resourceCache.put( name, resource );
return resource;
Expand All @@ -135,8 +131,8 @@ protected URL findResource(String name) {
* TODO: Should this throw a different exception or warn if multiple
* classes were found? Naming collisions can and do happen in OSGi...
*/
@SuppressWarnings("unchecked")
@Override
@SuppressWarnings("unchecked")
protected Enumeration<URL> findResources(String name) {
if ( resourceListCache.containsKey( name ) ) {
return resourceListCache.get( name );
Expand All @@ -146,7 +142,7 @@ protected Enumeration<URL> findResources(String name) {

for ( Bundle bundle : bundles ) {
try {
Enumeration<URL> resources = bundle.getResources( name );
final Enumeration<URL> resources = bundle.getResources( name );
if ( resources != null ) {
enumerations.add( resources );
}
Expand All @@ -157,7 +153,7 @@ protected Enumeration<URL> findResources(String name) {

for ( ClassLoader classLoader : classLoaders ) {
try {
Enumeration<URL> resources = classLoader.getResources( name );
final Enumeration<URL> resources = classLoader.getResources( name );
if ( resources != null ) {
enumerations.add( resources );
}
Expand All @@ -166,7 +162,7 @@ protected Enumeration<URL> findResources(String name) {
}
}

Enumeration<URL> aggEnumeration = new Enumeration<URL>() {
final Enumeration<URL> aggEnumeration = new Enumeration<URL>() {
@Override
public boolean hasMoreElements() {
for ( Enumeration<URL> enumeration : enumerations ) {
Expand All @@ -193,14 +189,27 @@ public URL nextElement() {
return aggEnumeration;
}

/**
* Adds a ClassLoader to the wrapped set of ClassLoaders
*
* @param classLoader The ClassLoader to add
*/
public void addClassLoader( ClassLoader classLoader ) {
classLoaders.add( classLoader );
}

/**
* Adds a Bundle to the wrapped set of Bundles
*
* @param bundle The Bundle to add
*/
public void addBundle( Bundle bundle ) {
bundles.add( bundle );
}


/**
* Clear all resources.
*/
public void clear() {
classCache.clear();
resourceCache.clear();
Expand Down
Expand Up @@ -38,24 +38,28 @@
* @author Brett Meyer
*/
public class OsgiJtaPlatform implements JtaPlatform {

private static final long serialVersionUID = 1L;

private BundleContext bundleContext;

/**
* Constructs a OsgiJtaPlatform
*
* @param bundleContext The OSGi bundle context
*/
public OsgiJtaPlatform(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}

@Override
public TransactionManager retrieveTransactionManager() {
ServiceReference sr = bundleContext.getServiceReference( TransactionManager.class.getName() );
final ServiceReference sr = bundleContext.getServiceReference( TransactionManager.class.getName() );
return (TransactionManager) bundleContext.getService( sr );
}

@Override
public UserTransaction retrieveUserTransaction() {
ServiceReference sr = bundleContext.getServiceReference( UserTransaction.class.getName() );
final ServiceReference sr = bundleContext.getServiceReference( UserTransaction.class.getName() );
return (UserTransaction) bundleContext.getService( sr );
}

Expand Down

0 comments on commit 2086658

Please sign in to comment.