Example usage for org.springframework.web.context WebApplicationContext getBeanNamesForType

List of usage examples for org.springframework.web.context WebApplicationContext getBeanNamesForType

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getBeanNamesForType.

Prototype

String[] getBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit);

Source Link

Document

Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

Usage

From source file:org.orderofthebee.addons.support.tools.repo.caches.CacheLookupUtils.java

/**
 * Resolves the {@link CacheStatistics cache statistics} of the {@link TransactionalCache transactional cache} instance that facades a
 * specific {@link SimpleCache shared/*from  w ww . ja v a  2s. c  om*/
 * cache} and provides it in a more script-friendly representation.
 *
 * @param sharedCacheName
 *            the name of the shared cache
 * @return a facade to a snapshot of the cache statistics
 */
public static AlfrescoCacheStatsFacade resolveStatisticsViaTransactional(final String sharedCacheName) {
    ParameterCheck.mandatoryString("sharedCacheName", sharedCacheName);

    LOGGER.debug("Trying to resolve transactional cache for shared cache {}", sharedCacheName);

    String txnCacheName = null;

    final WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext();

    if (applicationContext instanceof ConfigurableApplicationContext) {
        final ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext)
                .getBeanFactory();
        final String[] txnCacheBeanNames = applicationContext.getBeanNamesForType(TransactionalCache.class,
                false, false);

        // this is a rather ugly reference lookup, but so far I see no other way
        for (final String txnCacheBeanName : txnCacheBeanNames) {
            final BeanDefinition txnCacheDefinition = beanFactory.getBeanDefinition(txnCacheBeanName);

            final PropertyValue sharedCacheValue = txnCacheDefinition.getPropertyValues()
                    .getPropertyValue("sharedCache");
            final PropertyValue nameValue = txnCacheDefinition.getPropertyValues().getPropertyValue("name");
            if (nameValue != null && sharedCacheValue != null) {
                final Object sharedCacheRef = sharedCacheValue.getValue();
                if (sharedCacheRef instanceof RuntimeBeanReference) {
                    final String sharedCacheBeanName = ((RuntimeBeanReference) sharedCacheRef).getBeanName();

                    Object nameValueObj = nameValue.getValue();
                    if (nameValueObj instanceof TypedStringValue) {
                        nameValueObj = ((TypedStringValue) nameValueObj).getValue();
                    }

                    if (EqualsHelper.nullSafeEquals(sharedCacheBeanName, sharedCacheName)) {
                        if (txnCacheName != null) {
                            LOGGER.info("Shared cache {} is referenced by multiple transactional caches",
                                    sharedCacheName);
                            txnCacheName = null;
                            break;
                        }
                        txnCacheName = String.valueOf(nameValueObj);
                        LOGGER.debug("Resolved transactional cache {} for shared cache {}", txnCacheName,
                                sharedCacheName);
                    } else {
                        try {
                            final DefaultSimpleCache<?, ?> defaultSimpleCache = applicationContext
                                    .getBean(sharedCacheBeanName, DefaultSimpleCache.class);
                            if (EqualsHelper.nullSafeEquals(defaultSimpleCache.getCacheName(),
                                    sharedCacheName)) {
                                if (txnCacheName != null) {
                                    LOGGER.info(
                                            "Shared cache {} is referenced by multiple transactional caches",
                                            sharedCacheName);
                                    txnCacheName = null;
                                    break;
                                }
                                txnCacheName = String.valueOf(nameValueObj);
                                LOGGER.debug("Resolved transactional cache {} for shared cache {}",
                                        txnCacheName, sharedCacheName);
                            }
                            continue;
                        } catch (final BeansException be) {
                            // ignore - can be expected e.g. in EE or with alternative cache implementations
                        }
                    }
                }
            }
        }

        if (txnCacheName == null) {
            LOGGER.debug("Unable to resolve unique transactional cache for shared cache {}", sharedCacheName);
        }
    } else {
        LOGGER.debug(
                "Application context is not a configurable application context - unable to resolve transactional cache");
    }

    AlfrescoCacheStatsFacade facade = null;
    if (txnCacheName != null) {
        final CacheStatistics cacheStatistics = applicationContext.getBean("cacheStatistics",
                CacheStatistics.class);
        try {
            final Map<OpType, OperationStats> allStats = cacheStatistics.allStats(txnCacheName);
            facade = new AlfrescoCacheStatsFacade(allStats);
        } catch (final NoStatsForCache e) {
            facade = new AlfrescoCacheStatsFacade(Collections.emptyMap());
        }
    }

    return facade;
}