/**
   * According to the JSF 2.0 JavaDocs for {@link ExternalContext#getApplicationMap}, before a
   * managed-bean is removed from the map, any public no-argument void return methods annotated with
   * javax.annotation.PreDestroy must be called first.
   */
  @Override
  public Object remove(Object key) {
    String potentialManagedBeanName = (String) key;
    Object potentialManagedBeanValue = super.remove(key);

    if (beanManager.isManagedBean(potentialManagedBeanName, potentialManagedBeanValue)) {
      preDestroyInvoker.invokeAnnotatedMethods(potentialManagedBeanValue, preferPreDestroy);
    }

    return potentialManagedBeanValue;
  }
  /**
   * According to the JSF 2.0 JavaDocs for {@link ExternalContext#getApplicationMap}, before a
   * managed-bean is removed from the map, any public no-argument void return methods annotated with
   * javax.annotation.PreDestroy must be called first.
   */
  @Override
  public void clear() {
    Set<Map.Entry<String, Object>> mapEntries = entrySet();

    if (mapEntries != null) {

      for (Map.Entry<String, Object> mapEntry : mapEntries) {
        String potentialManagedBeanName = mapEntry.getKey();
        Object potentialManagedBeanValue = mapEntry.getValue();

        if (beanManager.isManagedBean(potentialManagedBeanName, potentialManagedBeanValue)) {
          preDestroyInvoker.invokeAnnotatedMethods(potentialManagedBeanValue, preferPreDestroy);
        }
      }
    }

    super.clear();
  }