|
35 | 35 | import org.osgi.framework.BundleActivator;
|
36 | 36 | import org.osgi.framework.BundleContext;
|
37 | 37 | import org.osgi.framework.FrameworkUtil;
|
| 38 | +import org.osgi.framework.ServiceRegistration; |
38 | 39 |
|
39 | 40 | /**
|
40 | 41 | * This BundleActivator provides three different uses of Hibernate in OSGi
|
|
58 | 59 | * @author Tim Ward
|
59 | 60 | */
|
60 | 61 | public class HibernateBundleActivator implements BundleActivator {
|
61 |
| - |
62 |
| - private OsgiClassLoader osgiClassLoader; |
63 |
| - |
64 |
| - private OsgiJtaPlatform osgiJtaPlatform; |
65 |
| - |
| 62 | + |
| 63 | + private ServiceRegistration<?> persistenceProviderService; |
| 64 | + private ServiceRegistration<?> sessionFactoryService; |
| 65 | + |
66 | 66 | @Override
|
67 | 67 | public void start(BundleContext context) throws Exception {
|
68 | 68 |
|
69 |
| - osgiClassLoader = new OsgiClassLoader(); |
| 69 | + OsgiClassLoader osgiClassLoader = new OsgiClassLoader(); |
70 | 70 | osgiClassLoader.addBundle( FrameworkUtil.getBundle( Session.class ) );
|
71 | 71 | osgiClassLoader.addBundle( FrameworkUtil.getBundle( HibernatePersistence.class ) );
|
72 | 72 | ClassLoaderHelper.overridenClassLoader = osgiClassLoader;
|
73 | 73 |
|
74 |
| - osgiJtaPlatform = new OsgiJtaPlatform( context ); |
| 74 | + OsgiJtaPlatform osgiJtaPlatform = new OsgiJtaPlatform( context ); |
75 | 75 |
|
76 | 76 | Dictionary properties = new Hashtable();
|
77 | 77 | // In order to support existing persistence.xml files, register
|
78 | 78 | // using the legacy provider name.
|
79 | 79 | properties.put( "javax.persistence.provider", HibernatePersistence.class.getName() );
|
80 |
| - context.registerService( PersistenceProvider.class.getName(), |
| 80 | + persistenceProviderService = context.registerService( PersistenceProvider.class.getName(), |
81 | 81 | new OsgiPersistenceProviderService( osgiClassLoader, osgiJtaPlatform, context ), properties );
|
82 | 82 |
|
83 |
| - context.registerService( SessionFactory.class.getName(), |
| 83 | + sessionFactoryService = context.registerService( SessionFactory.class.getName(), |
84 | 84 | new OsgiSessionFactoryService( osgiClassLoader, osgiJtaPlatform, context ), new Hashtable());
|
85 | 85 | }
|
86 | 86 |
|
87 | 87 | @Override
|
88 | 88 | public void stop(BundleContext context) throws Exception {
|
89 |
| - // Nothing else to do? |
| 89 | + persistenceProviderService.unregister(); |
| 90 | + persistenceProviderService = null; |
| 91 | + sessionFactoryService.unregister(); |
| 92 | + sessionFactoryService = null; |
| 93 | + |
| 94 | + ClassLoaderHelper.overridenClassLoader = null; |
90 | 95 | }
|
91 | 96 | }
|
0 commit comments