Close current BeanBoxContext, clear singlton cache, call predestory methods
for each singleton if they have
183209
public void close() {
		for (Entry<Object, Object> singletons : singletonCache.entrySet()) {
			Object key = singletons.getKey();
			Object obj = singletons.getValue();
			if (key instanceof BeanBox) {
				BeanBox box = (BeanBox) key;
				if (box.getPreDestroy() != null)
					try {
						box.getPreDestroy().invoke(obj);
					} catch (Exception e) {
						// Eat it here, but usually need log it
					}
			}
		}
		bindCache.clear();
		beanBoxMetaCache.clear();
		singletonCache.clear();
	}
1304614
public static void cleanup ()
  {
    // Reinitialize singletons to the default values
    if (LocaleCache.isInstantiated ())
      LocaleCache.getInstance ().reinitialize ();
    if (CountryCache.isInstantiated ())
      CountryCache.getInstance ().reinitialize ();
    if (LanguageCache.isInstantiated ())
      LanguageCache.getInstance ().reinitialize ();
    if (SerializationConverterRegistry.isInstantiated ())
      SerializationConverterRegistry.getInstance ().reinitialize ();
    if (MimeTypeDeterminator.isInstantiated ())
      MimeTypeDeterminator.getInstance ().reinitialize ();
    if (ThirdPartyModuleRegistry.isInstantiated ())
      ThirdPartyModuleRegistry.getInstance ().reinitialize ();
    if (TypeConverterRegistry.isInstantiated ())
      TypeConverterRegistry.getInstance ().reinitialize ();
    if (URLProtocolRegistry.isInstantiated ())
      URLProtocolRegistry.getInstance ().reinitialize ();
    if (EqualsImplementationRegistry.isInstantiated ())
      EqualsImplementationRegistry.getInstance ().reinitialize ();
    if (HashCodeImplementationRegistry.isInstantiated ())
      HashCodeImplementationRegistry.getInstance ().reinitialize ();

    // Clear caches
    if (DefaultTextResolver.isInstantiated ())
      DefaultTextResolver.getInstance ().clearCache ();
    EnumHelper.clearCache ();
    ResourceBundleHelper.clearCache ();
    if (RegExCache.isInstantiated ())
      RegExCache.getInstance ().clearCache ();
    CollatorHelper.clearCache ();
    LocaleHelper.clearCache ();
    StatisticsManager.clearCache ();
    SystemProperties.clearWarnedPropertyNames ();
    if (ImageDataManager.isInstantiated ())
      ImageDataManager.getInstance ().clearCache ();

    // Clean this one last as it is used in equals and hashCode implementations!
    ClassHierarchyCache.clearCache ();
  }
14556
@FFDCIgnore(Exception.class)
    public void deactivate(ComponentContext componentContext) {
        String methodName = "deactivate";

        if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
            Tr.event(tc, "Deactivating the WebContainer bundle");
        }
        
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, methodName, "Posting STOPPED_EVENT");
        }
        Event event = this.eventService.createEvent(WebContainerConstants.STOPPED_EVENT);
        this.eventService.postEvent(event);
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, methodName, "Posted STOPPED_EVENT");
        }

        this.classLoadingSRRef.deactivate(componentContext);
        this.sessionHelperSRRef.deactivate(componentContext);
        this.cacheManagerSRRef.deactivate(componentContext);
        this.injectionEngineSRRef.deactivate(componentContext);
        this.managedObjectServiceSRRef.deactivate(context);
        this.servletContainerInitializers.deactivate(componentContext);
        this.transferContextServiceRef.deactivate(componentContext);
        this.webMBeanRuntimeServiceRef.deactivate(componentContext);

        // will now purge each host as it becomes redundant rather than all here.
        //this.vhostManager.purge(); // Clear/purge all maps.

        WebContainer.instance.compareAndSet(this, null);
        extensionFactories.clear();

        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, methodName, "Deactivating the WebContainer bundle: Complete");
        }                
    }
242755
public void deactivate(ComponentContext context) {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
            Tr.event(this, tc, "Deactivating");
        }
        if (null != this.future) {
            synchronized (this.timerLock) {
                if (null != this.future) {
                    this.future.cancel(true);
                    this.future = null;
                }
            } // end-sync
        }
        // TODO purge the groupings/sessions maps, or dump to disk once
        // persistence is added
    }
111376
public void dispose(){
        classAnalysisMap.clear();
        classAnalysisEngineMap.clear();
        analysisLocals.clear();
        databaseFactoryMap.clear();
        databaseMap.clear();
        methodAnalysisEngineMap.clear();
    }
227621
public static void reset() {
		globalBeanBoxContext.close();
		globalNextAllowAnnotation = true;
		globalNextAllowSpringJsrAnnotation = true;
		globalNextValueTranslator = new DefaultValueTranslator();
		CREATE_METHOD = "create";
		CONFIG_METHOD = "config";
		globalBeanBoxContext = new BeanBoxContext();

	}
574093
public void release(Contextual<T> contextual, T instance) {
        synchronized (dependentInstances) {
            for (ContextualInstance<?> dependentInstance : dependentInstances) {
                // do not destroy contextual again, since it's just being destroyed
                if (contextual == null || !(dependentInstance.getContextual().equals(contextual))) {
                    destroy(dependentInstance);
                }
            }
        }
        if (resourceReferences != null) {
            for (ResourceReference<?> reference : resourceReferences) {
                reference.release();
            }
        }
    }
1232823
@Override
    public void discard() {
        final boolean isTraceOn = TraceComponent.isAnyTracingEnabled();
        if (isTraceOn && tc.isEntryEnabled()) {
            Tr.entry(tc, "discard");
        }

        discarded = true;

        if (state == DESTROYED) {
            return;
        }

        setState(DESTROYED);

        destroyHandleList();

        // Release any JCDI creational contexts that may exist.         F743-29174
        this.releaseManagedObjectContext();

        if (pmiBean != null) {
            pmiBean.discardCount(); // F743-27070
            pmiBean.beanDestroyed();
        }

        if (isTraceOn && tc.isEntryEnabled()) {
            Tr.exit(tc, "discard");
        }
    }
61728
public void clearCache()
  {
    // XXX: see if can remove this, and rely on the invocation cache existing
    LruCache<Object,I> invocationCache = _invocationCache;

    if (invocationCache != null) {
      invocationCache.clear();
    }
  }
125334
@Override
    public void destroy() throws Exception {
        for (final InstanceHolder instanceHolder : this.instances.values()) {
            if (instanceHolder.destructionCallback != null) {
                try {
                    instanceHolder.destructionCallback.run();
                } catch (Exception e) {
                    this.logger.warn(
                            "Destruction callback for bean named '"
                                    + instanceHolder.name
                                    + "' failed.",
                            e);
                }
            }
        }

        this.instances.clear();
    }
1301933
public static void destroyAllActive(ContextualStorage storage)
    {
        Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
        for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet())
        {
            Contextual bean = storage.getBean(entry.getKey());

            ContextualInstanceInfo<?> contextualInstanceInfo = entry.getValue();
            bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
        }
    }
23795
@Override
    public void contextDestroyed(ServletContextEvent arg0) {
        if (endpointManager != null) {
            if (!FrameworkState.isStopping()) {
                endpointManager.closeAllOpenSessions();
                endpointManager.clear();
            }
            else {
                if (tc.isDebugEnabled()) {
                    Tr.debug(tc, "Server is being shutdown, no need to stop stop clients");
                }
            }
        }

    }
1370741
@PreDestroy
    public void destroyBeansOnPreDestroy()
    {
        Map<String, ContextualStorage> oldWindowContextStorages = forceNewStorage();
        if (!oldWindowContextStorages.isEmpty())
        {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ServletContext servletContext = null;
            if (facesContext == null)
            {
                try
                {
                    servletContext = applicationContextBean.getServletContext();
                }
                catch (Throwable e)
                {
                    Logger.getLogger(FlowScopeBeanHolder.class.getName()).log(Level.WARNING,
                        "Cannot locate servletContext to create FacesContext on @PreDestroy flow scope beans. "
                                + "The beans will be destroyed without active FacesContext instance.");
                    servletContext = null;
                }
            }
            if (facesContext == null &&
                servletContext != null)
            {
                try
                {
                    ExternalContext externalContext = new StartupServletExternalContextImpl(servletContext, false);
                    ExceptionHandler exceptionHandler = new ExceptionHandlerImpl();
                    facesContext = new StartupFacesContextImpl(externalContext, 
                            (ReleaseableExternalContext) externalContext, exceptionHandler, false);
                    for (ContextualStorage contextualStorage : oldWindowContextStorages.values())
                    {
                        FlowScopedContextImpl.destroyAllActive(contextualStorage);
                    }
                }
                finally
                {
                    facesContext.release();
                }
            }
            else
            {
                for (ContextualStorage contextualStorage : oldWindowContextStorages.values())
                {
                    FlowScopedContextImpl.destroyAllActive(contextualStorage);
                }
            }
        }
    }
797653
@Override
    public void release()
    {
        _applicationFactory = null;
        _currentFacesContext = null;
        if (_defaultExternalContext != null)
        {
            _defaultExternalContext.release();
            _defaultExternalContext = null;
        }
        
        _application = null;
        _externalContext = null;
        _viewRoot = null;
        _renderKitFactory = null;
        _elContext = null;
        _exceptionHandler = null;
        _cachedRenderKit = null;
        _cachedRenderKitId = null;
        _separatorChar = null;
        
        // Spec JSF 2 section getAttributes when release is called the attributes map
        // must!!! be cleared! (probably to trigger some clearance methods on possible
        // added entries before nullifying everything)
        if (_attributes != null)
        {
            _attributes.clear();
            _attributes = null;
        }
        
        _released = true;
        FacesContext.setCurrentInstance(null);
    }
806826
protected void deactivate(ComponentContext context) {
        final boolean trace = TraceComponent.isAnyTracingEnabled();
        if (trace && tc.isEntryEnabled())
            Tr.entry(this, tc, "deactivate");

        lock.writeLock().lock();
        try {
            if (isInitialized) {
                if (classloader != null) {
                    if (isDerbyEmbedded.get())
                        shutdownDerbyEmbedded();
                    classloader = null;
                }
                for (Iterator<Class<? extends CommonDataSource>> it = introspectedClasses.iterator(); it.hasNext(); it.remove())
                    Introspector.flushFromCaches(it.next());
                isInitialized = false;
            }
        } finally {
            lock.writeLock().unlock();
        }

        if (trace && tc.isEntryEnabled())
            Tr.exit(this, tc, "deactivate");
    }
56511
public static void dispose() {

    if (xml2containerMap != null) {
      for (SpringContainer container : xml2containerMap.values()) {
        container.dispose();
      }
      xml2containerMap.clear();
    }
  }
1068027
private static void destroyControl( Object controlInstance )
    {
        assert controlInstance instanceof ControlBean : controlInstance.getClass().getName();
        BeanContext beanContext = ( ( ControlBean ) controlInstance ).getBeanContext();
        if ( beanContext != null ) {
            if(LOG.isTraceEnabled())
                LOG.trace("Removing control " + controlInstance + " from ControlBeanContext " + beanContext);
            beanContext.remove( controlInstance );
        }
    }