Example usage for org.springframework.context.event ContextRefreshedEvent getApplicationContext

List of usage examples for org.springframework.context.event ContextRefreshedEvent getApplicationContext

Introduction

In this page you can find the example usage for org.springframework.context.event ContextRefreshedEvent getApplicationContext.

Prototype

public final ApplicationContext getApplicationContext() 

Source Link

Document

Get the ApplicationContext that the event was raised for.

Usage

From source file:com.scottieknows.test.cassandra.CassandraClusterManager.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    ApplicationContext ctx = event.getApplicationContext();
    CassandraClusterManager ccm = ctx.getBean(CassandraClusterManager.class);
    String out = ccm.exec("list");
    if (!out.contains(cassandraConfigurationProperties.getClusterName() + "\n")) {
        createCluster(cassandraConfigurationProperties.getClusterVersion(),
                cassandraConfigurationProperties.getClusterNodes(),
                cassandraConfigurationProperties.getClusterName());
    } else if (!out.contains("*" + cassandraConfigurationProperties.getClusterName() + "\n")) {
        exec("switch " + cassandraConfigurationProperties.getClusterName());
    }/*from   w  w w. j  a v a2s .  co m*/
    String status = ccm.exec("status");
    if (!status.contains("UP")) {
        exec("start " + cassandraConfigurationProperties.getClusterName());
    }
    execHqlInitFiles(ctx);
    // XXX
    //        ccm.createOrVerifyKeyspace();
}

From source file:tv.arte.resteventapi.core.querying.convertion.RestEventApiCustomConvertersLoader.java

/**
 * Initializes the RestEventApi custom converters at the end of the Spring context loading
 *///from  w ww .j  ava  2s  .  com
@SuppressWarnings("rawtypes")
public void onApplicationEvent(ContextRefreshedEvent event) {

    if (conversionService instanceof GenericConversionService) {
        GenericConversionService genericConversionService = (GenericConversionService) conversionService;
        Map<String, Object> converters = event.getApplicationContext()
                .getBeansWithAnnotation(RestEventApiConverter.class);

        for (Entry<String, Object> beanEntry : converters.entrySet()) {
            Object beanEntryValue = beanEntry.getValue();

            if (beanEntryValue instanceof Converter) {
                genericConversionService.addConverter((Converter) beanEntryValue);
                if (logger.isDebugEnabled())
                    logger.debug("Converter bean with name " + beanEntry.getKey() + " has been added");
            } else if (beanEntryValue instanceof GenericConverter) {
                genericConversionService.addConverter((GenericConverter) beanEntryValue);
                if (logger.isDebugEnabled())
                    logger.debug("Converter bean with name " + beanEntry.getKey() + " has been added");
            } else {
                logger.error(
                        "Cannot consider the bean with name " + beanEntry.getKey() + " as a valid converter.");
            }
        }
    } else {
        String errorMessage = "RestEventApi custom convertes failed to be initialized. The convertion service is not of the expected type.";
        logger.error(errorMessage);
        throw new RestEventApiRuntimeException(errorMessage);
    }

}

From source file:tv.arte.resteventapi.core.presentation.decoration.RestEventApiDecorationProvider.java

/**
 * Load controller mappings at the end of Spring context init
 *///  w w  w  .  j ava2s . c o m
public void onApplicationEvent(ContextRefreshedEvent event) {

    Map<Class<?>, Method> hrefedBeansControllerMethodMapping = new HashMap<Class<?>, Method>();

    Map<String, ?> abstractMethodHandlers = event.getApplicationContext()
            .getBeansOfType(AbstractHandlerMethodMapping.class);
    for (Entry<String, ?> abstractMethodHandler : abstractMethodHandlers.entrySet()) {
        Map<?, HandlerMethod> methods = ((AbstractHandlerMethodMapping<?>) abstractMethodHandler.getValue())
                .getHandlerMethods();
        for (Entry<?, HandlerMethod> handMet : methods.entrySet()) {
            Method method = handMet.getValue().getMethod();
            Hrefed hrefedAnnotation = method.getAnnotation(Hrefed.class);
            if (hrefedAnnotation != null) {
                for (Class<?> hrefedClass : hrefedAnnotation.value()) {
                    if (hrefedBeansControllerMethodMapping.containsKey(hrefedClass)) {
                        throw new RestEventApiRuntimeException("Cannot have " + Hrefed.class.getSimpleName()
                                + " annotation on more that 1 controller handler methods."
                                + " The related Hrefed bean class is: " + hrefedClass
                                + ". The incriminated controller class: "
                                + method.getDeclaringClass().getSimpleName() + ", with method: "
                                + method.getName());
                    }

                    hrefedBeansControllerMethodMapping.put(hrefedClass, method);
                }
            }
        }
    }

    if (hrefedBeansControllerMethodMapping != null && !hrefedBeansControllerMethodMapping.isEmpty()) {
        RestEventApiDecorationProvider.hrefedBeansControllerMethodMapping.clear();
    }
    RestEventApiDecorationProvider.hrefedBeansControllerMethodMapping
            .putAll(hrefedBeansControllerMethodMapping);
}

From source file:org.craftercms.commons.ebus.config.EBusBeanAutoConfiguration.java

@Override
public void onApplicationEvent(final ContextRefreshedEvent contextRefreshedEvent) {
    ApplicationContext ctx = contextRefreshedEvent.getApplicationContext();

    if (applicationContext != ctx) {
        return;/*  ww w. j  a  va 2  s.c o  m*/
    }

    if (null == beanResolver) {
        beanResolver = new BeanFactoryResolver(ctx);
    }

    if (null == conversionService) {
        try {
            conversionService = ctx.getBean(ConsumerBeanAutoConfiguration.REACTOR_CONVERSION_SERVICE_BEAN_NAME,
                    ConversionService.class);
        } catch (BeansException be) {
            // TODO: log that conversion service is not found.
        }
    }

    synchronized (this) {
        if (started) {
            return;
        }

        Set<Method> methods;
        Class<?> type;
        for (String beanName : ctx.getBeanDefinitionNames()) {
            type = ctx.getType(beanName);
            methods = findHandlerMethods(type, LISTENER_METHOD_FILTER);
            if (methods != null && methods.size() > 0) {
                wireBean(ctx.getBean(beanName), methods);
            }
        }

        started = true;
    }
}

From source file:com.brienwheeler.apps.schematool.SchemaToolBean.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    if (closeContextOnDone)
        ((AbstractApplicationContext) event.getApplicationContext()).close();
}

From source file:com.turingoal.cms.core.filter.SystemInitListener.java

/**
 * SpringBean???/*from  w w  w . ja va  2s . c o m*/
 */
@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
    // web spring mvcroot application context ,? contextroot application context? ??onApplicationEvent???????root application context?????????
    if (event.getApplicationContext().getParent() == null) {
        log.info("************[{}]??!************", application.getServletContextName());
        // license
        TgLicenseChcker.checkInit(application, SystemInitListener.class);
        // ????
        application.setAttribute("openValidateCaptchaCode", openValidateCaptchaCode);
        // ??????
        application.setAttribute("openValidateCaptchaCode4admin", openValidateCaptchaCode4admin);
    }
}

From source file:org.alfresco.filesys.AbstractServerConfigurationBean.java

public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextRefreshedEvent) {
        ContextRefreshedEvent refreshEvent = (ContextRefreshedEvent) event;
        ApplicationContext refreshContext = refreshEvent.getApplicationContext();
        if (refreshContext != null && refreshContext.equals(applicationContext)) {
            // Initialize the bean

            init();/* ww w  . j a  va 2 s.c  o  m*/
        }
    }
}

From source file:ch.ralscha.extdirectspring.controller.MethodRegistrar.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

    ApplicationContext context = (ApplicationContext) event.getSource();

    String[] beanNames = context.getBeanNamesForType(Object.class);

    for (String beanName : beanNames) {

        Class<?> handlerType = context.getType(beanName);
        final Class<?> userType = ClassUtils.getUserClass(handlerType);

        Set<Method> methods = MethodIntrospector.selectMethods(userType, new MethodFilter() {
            @Override//  w  ww . ja v a  2s.c  o m
            public boolean matches(Method method) {
                return AnnotationUtils.findAnnotation(method, ExtDirectMethod.class) != null;
            }
        });

        for (Method method : methods) {
            ExtDirectMethod directMethodAnnotation = AnnotationUtils.findAnnotation(method,
                    ExtDirectMethod.class);
            final String beanAndMethodName = beanName + "." + method.getName();
            if (directMethodAnnotation.value().isValid(beanAndMethodName, userType, method)) {
                this.methodInfoCache.put(beanName, handlerType, method, event.getApplicationContext());

                // /CLOVER:OFF
                if (log.isDebugEnabled()) {
                    String info = "Register " + beanAndMethodName + "(" + directMethodAnnotation.value();
                    if (StringUtils.hasText(directMethodAnnotation.group())) {
                        info += ", " + directMethodAnnotation.group();
                    }
                    info += ")";
                    log.debug(info);
                }
                // /CLOVER:ON
            }
        }

    }
}