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.comcast.cdn.traffic_control.traffic_router.core.router.TrafficRouterManager.java

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
    applicationContext = event.getApplicationContext();
    if (trafficRouter != null) {
        trafficRouter.setApplicationContext(applicationContext);
        trafficRouter.configurationChanged();
    }// www  . ja  va2 s  .c om
}

From source file:com.devnexus.ting.core.applicationlistener.ContextRefreshedEventListener.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    // From: http://forum.springsource.org/showthread.php?t=84312&page=2
    //configurer.toString();
    if (event.getApplicationContext().getParent() == null) {
        if (environment.acceptsProfiles(SpringProfile.DEMO)) {
            if (!systemSetupService.isDatabaseSetup()) {
                LOGGER.info("Setting up database...");
                systemSetupService.setupDatabase();
            }//from  www .java 2 s.c  o m
        } else if (environment.acceptsProfiles(SpringProfile.CLOUD)) {
            try {
                cloud.getSingletonServiceConnector(DataSource.class, null);
            } catch (CloudException e) {
                LOGGER.error("Setting up Cloud Demo database...");
                systemSetupService.setupDatabase();
            }
        }
    }
}

From source file:es.galvarez.rest.config.DBInitializerApplicationStartupListener.java

public void onApplicationEvent(ContextRefreshedEvent event) {
    Role adminRole = null;//from   w w  w .ja v a2s  . c  om
    Role userRole = null;

    ApplicationContext applicationContext = event.getApplicationContext();
    ShaPasswordEncoder passwordEncoder = new ShaPasswordEncoder(256);
    passwordEncoder.setEncodeHashAsBase64(true);

    if (applicationContext != null) {
        RoleRepository roleRepository = applicationContext.getBean(RoleRepository.class);
        if (roleRepository != null) {
            if (roleRepository.findOne("ADMIN") == null) {
                adminRole = new Role();
                adminRole.setAuthority("ADMIN");
                roleRepository.save(adminRole);
            }
            if (roleRepository.findOne("USER") == null) {
                userRole = new Role();
                userRole.setAuthority("USER");
                roleRepository.save(userRole);
            }
        }
        UserRepository userRepository = applicationContext.getBean(UserRepository.class);
        if (userRepository != null) {
            if (userRepository.loadUserByUsername("admin") == null) {
                User adminUser = new User();
                adminUser.setUsername("admin");
                adminUser.setPassword(passwordEncoder.encodePassword("admin123", ""));
                adminUser.setAuthorities(Arrays.asList(
                        new Role[] { roleRepository.findOne("ADMIN"), roleRepository.findOne("USER") }));
                userRepository.save(adminUser);
            }
        }
    }
}

From source file:com.helpinput.spring.support.SourceFileMonitorListener.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    ContextHolder.beanRegistIntercpterHolder.register(new ProxybeanRegistInterceptor());

    ApplicationContext applicationContext = event.getApplicationContext();
    DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) ((AbstractApplicationContext) applicationContext)
            .getBeanFactory();/*  w w w.j av a 2s .  c  om*/

    PropertyEditorRegister.registerProtertyEditor(dlbf, GLClassEditor.class);
    //get root applicationContext
    while (applicationContext.getParent() != null) {
        applicationContext = applicationContext.getParent();
    }

    if (monitor == null)
        monitor = new SourceFileMonitor(dirs, applicationContext);

    if (!monitor.isRunning()) {
        try {
            monitor.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:com.scottieknows.test.autoconfigure.PostgresAutoConfiguration.java

@Override
public void onApplicationEvent(ApplicationEvent event) {
    log.info("receieved application event " + event.getClass().getName());
    if (event instanceof ContextRefreshedEvent) {
        ContextRefreshedEvent e = (ContextRefreshedEvent) event;
        ApplicationContext ctx = e.getApplicationContext();
        restartPostgres(ctx);//from w  ww  . j a  v  a 2  s  .  co m
    } else if (event instanceof ContextClosedEvent) {
        process.ifPresent(p -> p.stop());
    }
}

From source file:com.example.app.config.ProjectConfig.java

/**
 * Example to test if weaving is working.
 *
 * @param event the event.//from  w w w.  jav  a  2 s .c  o m
 */
@SuppressFBWarnings("DM_EXIT")
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

    if (!new AspectWeavingTest().isConfigured()) {
        ApplicationContext applicationContext = event.getApplicationContext();
        try {
            if (applicationContext instanceof AbstractApplicationContext)
                ((AbstractApplicationContext) applicationContext).close();
        } finally {
            ApplicationContextException ex = new ApplicationContextException(
                    "AspectJ weaving is not working. Configure compile-time or load-time weaving.");
            _logger.fatal("AspectJ weaving misconfiguration.", ex);
        }
        System.exit(1);
    }

}

From source file:com.quancheng.saluki.springsupport.internal.ServiceBean.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    synchronized (lock) {
        if (initialled) {
            return;
        } else {/*from ww w.jav a 2  s  . co m*/
            ApplicationContext applicationContext = event.getApplicationContext();
            Map<String, ServiceBean> instances = applicationContext.getBeansOfType(ServiceBean.class);
            if (!instances.isEmpty()) {
                ConfigBean configBean = applicationContext.getBean(ConfigBean.class);
                this.thrallProperties = configBean;
                RpcServiceConfig rpcSerivceConfig = new RpcServiceConfig();
                this.addRegistyAddress(rpcSerivceConfig);
                rpcSerivceConfig.setApplication(thrallProperties.getApplication());
                this.addHostAndPort(rpcSerivceConfig);
                rpcSerivceConfig.setMonitorinterval(thrallProperties.getMonitorinterval());
                for (Map.Entry<String, ServiceBean> entry : instances.entrySet()) {
                    ServiceBean serviceBean = entry.getValue();
                    rpcSerivceConfig.addServiceDefinition(serviceBean.getServiceName(), serviceBean.getGroup(),
                            serviceBean.getVersion(), serviceBean.getRef());
                }
                this.rpcService = rpcSerivceConfig;
                initialled = true;
                rpcSerivceConfig.export();
            }
        }

    }

}

From source file:net.przemkovv.sphinx.config.PopulateDatabaseOnStartup.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    if (!was_executed) {
        was_executed = true;/*w w w .jav  a  2s  .  c om*/
    } else {
        return;
    }
    logger.debug("Context name: {}, {}", event.getApplicationContext().getId(),
            event.getApplicationContext().getDisplayName());

    Permission permission_new_task = new Permission("NEW_TASK");
    Permission permission_edit_task = new Permission("UPDATE_TASK");
    Permission permission_remove_task = new Permission("REMOVE_TASK");

    Role role = new Role("TASK_MANAGER");
    role.getPermissions().add(permission_new_task);
    role.getPermissions().add(permission_edit_task);
    role.getPermissions().add(permission_remove_task);

    roleService.save(role);
    Role role2 = new Role("TASK2_MANAGER");
    role2.getPermissions().add(permission_remove_task);
    roleService.save(role2);

    User userJohn = new User();
    userJohn.setEmail("john@gmail.com");
    userJohn.setFirstName("John");
    userJohn.setLastName("Malkovic");
    userService.setPassword(userJohn, "john");
    userService.save(userJohn);

    User userBob = new User();
    userBob.setEmail("bob@gmail.com");
    userBob.setFirstName("Bob");
    userBob.setLastName("Marley");
    userService.setPassword(userBob, "bob");
    userService.save(userBob);

    taskService.createTask("Hello World!", "Display \"Hello World!\"");
    taskService.createTask("2+2", "Return value of 2+2");
}

From source file:dstrelec.nats.config.NatsListenerEndpointRegistry.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    if (event.getApplicationContext().equals(this.applicationContext)) {
        this.contextRefreshed = true;
    }/*from  w w  w.  j a v  a  2  s  .  co m*/
}