class DeregisterServiceProviderServletListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        final ClassLoader classLoader = getClass().getClassLoader();
        final ServiceRegistry serviceRegistry = IIORegistry.getDefaultInstance();
        Iterator<Class<?>> categories = IIORegistry.getDefaultInstance().getCategories();
        if (categories != null) {
            while (categories.hasNext()) {
                Class<?> category = categories.next();
                Iterator<?> providers = serviceRegistry.getServiceProviders(category, false);
                if (providers != null) {
                    List providersToRemove = new ArrayList();
                    while (providers.hasNext()) {
                        Object provider = providers.next();
                        if (provider != null && classLoader.equals(provider.getClass().getClassLoader())) {
                            providersToRemove.add(provider);
                        }
                    }
                    for (Object provider : providersToRemove) {
                        serviceRegistry.deregisterServiceProvider(provider);
                        LOGGER.info("deregistered service provider {} from category {}", provider.getClass().getName(), category.getName());
                    }
                }
            }
        }
    }

    public  DeregisterServiceProviderServletListener();

}