Example usage for org.springframework.util IdGenerator getClass

List of usage examples for org.springframework.util IdGenerator getClass

Introduction

In this page you can find the example usage for org.springframework.util IdGenerator getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.springframework.integration.config.IdGeneratorConfigurer.java

private boolean setIdGenerator(ApplicationContext context) {
    try {//from www.j  ava 2s  . c  o m
        IdGenerator idGeneratorBean = context.getBean(IdGenerator.class);
        if (logger.isDebugEnabled()) {
            logger.debug("using custom MessageHeaders.IdGenerator [" + idGeneratorBean.getClass() + "]");
        }
        Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
        ReflectionUtils.makeAccessible(idGeneratorField);
        IdGenerator currentIdGenerator = (IdGenerator) ReflectionUtils.getField(idGeneratorField, null);
        if (currentIdGenerator != null) {
            if (currentIdGenerator.equals(idGeneratorBean)) {
                // same instance is already set, nothing needs to be done
                return false;
            } else {
                if (IdGeneratorConfigurer.theIdGenerator.getClass() == idGeneratorBean.getClass()) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Another instance of " + idGeneratorBean.getClass()
                                + " has already been established; ignoring");
                    }
                    return true;
                } else {
                    // different instance has been set, not legal
                    throw new BeanDefinitionStoreException(
                            "'MessageHeaders.idGenerator' has already been set and can not be set again");
                }
            }
        }
        if (logger.isInfoEnabled()) {
            logger.info("Message IDs will be generated using custom IdGenerator [" + idGeneratorBean.getClass()
                    + "]");
        }
        ReflectionUtils.setField(idGeneratorField, null, idGeneratorBean);
        IdGeneratorConfigurer.theIdGenerator = idGeneratorBean;
    } catch (NoSuchBeanDefinitionException e) {
        // No custom IdGenerator. We will use the default.
        int idBeans = context.getBeansOfType(IdGenerator.class).size();
        if (idBeans > 1 && logger.isWarnEnabled()) {
            logger.warn("Found too many 'IdGenerator' beans (" + idBeans + ") "
                    + "Will use the existing UUID strategy.");
        } else if (logger.isDebugEnabled()) {
            logger.debug("Unable to locate MessageHeaders.IdGenerator. Will use the existing UUID strategy.");
        }
        return false;
    } catch (IllegalStateException e) {
        // thrown from ReflectionUtils
        if (logger.isWarnEnabled()) {
            logger.warn("Unexpected exception occurred while accessing idGenerator of MessageHeaders."
                    + " Will use the existing UUID strategy.", e);
        }
        return false;
    }
    return true;
}