Index: src/main/java/org/glassfish/osgijdbc/JDBCExtender.java =================================================================== --- src/main/java/org/glassfish/osgijdbc/JDBCExtender.java (revision 36641) +++ src/main/java/org/glassfish/osgijdbc/JDBCExtender.java (working copy) @@ -105,7 +105,16 @@ private void removeURLHandler() { if (urlHandlerService != null) { - urlHandlerService.unregister(); + try { + urlHandlerService.unregister(); + } catch(IllegalStateException e) { + // If an IllegalStateException gets thrown, the URL handler + // has already been removed. + + return; + } + + urlHandlerService = null; } } Index: src/main/java/org/glassfish/osgijdbc/GlassFishResourceProviderService.java =================================================================== --- src/main/java/org/glassfish/osgijdbc/GlassFishResourceProviderService.java (revision 36641) +++ src/main/java/org/glassfish/osgijdbc/GlassFishResourceProviderService.java (working copy) @@ -50,6 +50,8 @@ import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceReference; /** * A service to export jdbc resources in GlassFish to OSGi's service-registy.
@@ -139,7 +141,12 @@ */ public void unRegisterJdbcResources(){ for(ServiceRegistration serviceRegistration : services){ - unRegisterJdbcResource(serviceRegistration); + try { + unRegisterJdbcResource(serviceRegistration); + } catch(Throwable t) { + logger.log(Level.SEVERE, "An error occurred while unregistering a " + + "JDBC resource.", t); + } } preDestroy(); } @@ -149,11 +156,62 @@ * @param serviceRegistration ServiceRegistration */ private void unRegisterJdbcResource(ServiceRegistration serviceRegistration) { - debug("unregistering resource ["+serviceRegistration.getReference().getProperty(Constants.JNDI_NAME)+"]"); - DataSourceProxy dsProxy = (DataSourceProxy)serviceRegistration.getReference().getBundle(). - getBundleContext().getService(serviceRegistration.getReference()); - dsProxy.invalidate(); - serviceRegistration.unregister(); + ServiceReference serviceReference; + try { + serviceReference = serviceRegistration.getReference(); + } catch(IllegalStateException e) { + // If a IllegalStateException gets thrown here, the JDBC Resource + // has already been unregistered. Return from this method in this + // case. + return; + } + + debug("unregistering resource ["+serviceReference.getProperty(Constants.JNDI_NAME)+"]"); + + Bundle bundle = serviceReference.getBundle(); + if(bundle == null) { + // If serviceReference.getBundle() returns a null reference, + // the JDBC Resource has already been unregistered. + return; + } + + BundleContext bundleContext = bundle.getBundleContext(); + + if(bundleContext != null) { + Object dataSourceProxyObject; + + try { + dataSourceProxyObject = bundleContext.getService(serviceReference); + } catch(IllegalStateException e) { + // If an IllegalStateException gets thrown, the bundle is not active + // and would have already been stopped. + dataSourceProxyObject = null; + } + + if(dataSourceProxyObject instanceof DataSourceProxy) { + DataSourceProxy dsProxy = (DataSourceProxy)dataSourceProxyObject; + try { + dsProxy.invalidate(); + } catch(Throwable t) { + logger.log(Level.SEVERE, "An error occurred while invalidating " + + "the data source proxy.", t); + } + } + + try { + bundleContext.ungetService(serviceReference); + } catch(IllegalStateException e) { + // If an IllegalStateException gets thrown, the bundle is not active + // and would have already been stopped. + } + } + + try { + serviceRegistration.unregister(); + } catch(IllegalStateException e) { + // If an IllegalStateException gets thrown here, the service has already + // been unregistered. + } } /**