Search in sources :

Example 16 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project camel by apache.

the class CamelSpringTestContextLoader method loadContext.

/**
     *  Modeled after the Spring implementation in {@link AbstractGenericContextLoader},
     *  this method creates and refreshes the application context while providing for
     *  processing of additional Camel specific post-refresh actions.  We do not provide the
     *  pre-post hooks for customization seen in {@link AbstractGenericContextLoader} because
     *  they probably are unnecessary for 90+% of users.
     *  <p/>
     *  For some functionality, we cannot use {@link org.springframework.test.context.TestExecutionListener} because we need
     *  to both produce the desired outcome during application context loading, and also cleanup
     *  after ourselves even if the test class never executes.  Thus the listeners, which
     *  only run if the application context is successfully initialized are insufficient to
     *  provide the behavior described above.
     */
@Override
public ApplicationContext loadContext(String... locations) throws Exception {
    Class<?> testClass = getTestClass();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");
    }
    try {
        GenericApplicationContext context = createContext(testClass, null);
        loadBeanDefinitions(context, locations);
        return loadContext(context, testClass);
    } finally {
        cleanup(testClass);
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext)

Example 17 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project camel by apache.

the class Configuration method shouldOnlyCollectRoutesOnce.

@Test
public void shouldOnlyCollectRoutesOnce() {
    GenericApplicationContext parent = new GenericApplicationContext();
    parent.refresh();
    ConfigurableApplicationContext context = new SpringApplicationBuilder(Configuration.class).web(false).parent(parent).run();
    ContextRefreshedEvent refreshEvent = new ContextRefreshedEvent(context);
    RoutesCollector collector = context.getBean(RoutesCollector.class);
    //no changes should happen here
    collector.onApplicationEvent(refreshEvent);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) RoutesCollector(org.apache.camel.spring.boot.RoutesCollector) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Test(org.junit.Test)

Example 18 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project camel by apache.

the class CamelContextFactoryBeanTest method testGenericApplicationContextUsingNamespaces.

public void testGenericApplicationContextUsingNamespaces() throws Exception {
    applicationContext = new GenericApplicationContext();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) applicationContext);
    xmlReader.loadBeanDefinitions(new ClassPathResource("org/apache/camel/spring/camelContextFactoryBean.xml"));
    // lets refresh to inject the applicationContext into beans
    applicationContext.refresh();
    CamelContext context = applicationContext.getBean("camel3", CamelContext.class);
    assertValidContext(context);
}
Also used : CamelContext(org.apache.camel.CamelContext) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 19 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project opennms by OpenNMS.

the class Installer method install.

/**
 * <p>install</p>
 *
 * @param argv an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 */
public void install(final String[] argv) throws Exception {
    printHeader();
    loadProperties();
    parseArguments(argv);
    final boolean doDatabase = (m_update_database || m_do_inserts || m_update_iplike || m_update_unicode || m_fix_constraint);
    if (!doDatabase && m_tomcat_conf == null && !m_install_webapp && m_library_search_path == null) {
        usage(options, m_commandLine, "Nothing to do.  Use -h for help.", null);
        System.exit(1);
    }
    if (doDatabase) {
        final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.OPENNMS_DATASOURCE_CONFIG_FILE_NAME);
        InputStream is = new FileInputStream(cfgFile);
        final JdbcDataSource adminDsConfig = new DataSourceConfigurationFactory(is).getJdbcDataSource(ADMIN_DATA_SOURCE_NAME);
        final DataSource adminDs = new SimpleDataSource(adminDsConfig);
        is.close();
        is = new FileInputStream(cfgFile);
        final JdbcDataSource dsConfig = new DataSourceConfigurationFactory(is).getJdbcDataSource(OPENNMS_DATA_SOURCE_NAME);
        final DataSource ds = new SimpleDataSource(dsConfig);
        is.close();
        m_installerDb.setForce(m_force);
        m_installerDb.setIgnoreNotNull(m_ignore_not_null);
        m_installerDb.setNoRevert(m_do_not_revert);
        m_installerDb.setAdminDataSource(adminDs);
        m_installerDb.setPostgresOpennmsUser(dsConfig.getUserName());
        m_installerDb.setDataSource(ds);
        m_installerDb.setDatabaseName(dsConfig.getDatabaseName());
        m_migrator.setDataSource(ds);
        m_migrator.setAdminDataSource(adminDs);
        m_migrator.setValidateDatabaseVersion(!m_ignore_database_version);
        m_migration.setDatabaseName(dsConfig.getDatabaseName());
        m_migration.setSchemaName(dsConfig.getSchemaName());
        m_migration.setAdminUser(adminDsConfig.getUserName());
        m_migration.setAdminPassword(adminDsConfig.getPassword());
        m_migration.setDatabaseUser(dsConfig.getUserName());
        m_migration.setDatabasePassword(dsConfig.getPassword());
        m_migration.setChangeLog("changelog.xml");
    }
    checkIPv6();
    /*
         * Make sure we can execute the rrdtool binary when the
         * JniRrdStrategy is enabled.
         */
    boolean using_jni_rrd_strategy = System.getProperty("org.opennms.rrd.strategyClass", "").contains("JniRrdStrategy");
    if (using_jni_rrd_strategy) {
        File rrd_binary = new File(System.getProperty("rrd.binary"));
        if (!rrd_binary.canExecute()) {
            throw new Exception("Cannot execute the rrdtool binary '" + rrd_binary.getAbsolutePath() + "' required by the current RRD strategy. Update the rrd.binary field in opennms.properties appropriately.");
        }
    }
    if (!Boolean.getBoolean("skip-native")) {
        String icmp_path = findLibrary("jicmp", m_library_search_path, false);
        String icmp6_path = findLibrary("jicmp6", m_library_search_path, false);
        String jrrd_path = findLibrary("jrrd", m_library_search_path, false);
        String jrrd2_path = findLibrary("jrrd2", m_library_search_path, false);
        writeLibraryConfig(icmp_path, icmp6_path, jrrd_path, jrrd2_path);
    }
    /*
         * Everything needs to use the administrative data source until we
         * verify that the opennms database is created below (and where we
         * create it if it doesn't already exist).
         */
    verifyFilesAndDirectories();
    if (m_install_webapp) {
        checkWebappOldOpennmsDir();
        checkServerXmlOldOpennmsContext();
    }
    if (m_update_database || m_fix_constraint) {
    // OLDINSTALL m_installerDb.readTables();
    }
    m_installerDb.disconnect();
    if (doDatabase) {
        m_migrator.validateDatabaseVersion();
        System.out.println(String.format("* using '%s' as the PostgreSQL user for OpenNMS", m_migration.getAdminUser()));
        System.out.println(String.format("* using '%s' as the PostgreSQL database name for OpenNMS", m_migration.getDatabaseName()));
        if (m_migration.getSchemaName() != null) {
            System.out.println(String.format("* using '%s' as the PostgreSQL schema name for OpenNMS", m_migration.getSchemaName()));
        }
    }
    if (m_update_database) {
        m_migrator.prepareDatabase(m_migration);
    }
    if (doDatabase) {
        m_installerDb.checkUnicode();
        m_installerDb.checkTime();
    }
    handleConfigurationChanges();
    final GenericApplicationContext context = new GenericApplicationContext();
    context.setClassLoader(Bootstrap.loadClasses(new File(m_opennms_home), true));
    if (m_update_database) {
        m_installerDb.databaseSetUser();
        m_installerDb.disconnect();
        for (final Resource resource : context.getResources("classpath*:/changelog.xml")) {
            System.out.println("- Running migration for changelog: " + resource.getDescription());
            m_migration.setAccessor(new ExistingResourceAccessor(resource));
            m_migrator.migrate(m_migration);
        }
    }
    if (m_update_unicode) {
        System.out.println("WARNING: the -U option is deprecated, it does nothing now");
    }
    if (m_do_vacuum) {
        m_installerDb.vacuumDatabase(m_do_full_vacuum);
    }
    if (m_install_webapp) {
        installWebApp();
    }
    if (m_tomcat_conf != null) {
        updateTomcatConf();
    }
    if (m_update_iplike) {
        m_installerDb.updateIplike();
    }
    if (m_update_database && m_remove_database) {
        m_installerDb.disconnect();
        m_installerDb.databaseRemoveDB();
    }
    if (doDatabase) {
        m_installerDb.disconnect();
    }
    if (m_update_database) {
        createConfiguredFile();
    }
    System.out.println();
    System.out.println("Installer completed successfully!");
    if (!m_skip_upgrade_tools) {
        System.setProperty("opennms.manager.class", "org.opennms.upgrade.support.Upgrade");
        Bootstrap.main(new String[] {});
    }
    context.close();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) Resource(org.springframework.core.io.Resource) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) SimpleDataSource(org.opennms.core.db.install.SimpleDataSource) DataSource(javax.sql.DataSource) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleDataSource(org.opennms.core.db.install.SimpleDataSource) DataSourceConfigurationFactory(org.opennms.core.db.DataSourceConfigurationFactory) File(java.io.File)

Example 20 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project cxf by apache.

the class EngineLifecycleTest method setUpBus.

public void setUpBus(boolean includeService) throws Exception {
    applicationContext = new GenericApplicationContext();
    readBeans(new ClassPathResource("/org/apache/cxf/systest/http_jetty/cxf.xml"));
    readBeans(new ClassPathResource("META-INF/cxf/cxf.xml"));
    readBeans(new ClassPathResource("jetty-engine.xml", getClass()));
    if (includeService) {
        readBeans(new ClassPathResource("server-lifecycle-beans.xml", getClass()));
    }
    // bring in some property values from a Properties file
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    Properties properties = new Properties();
    properties.setProperty("staticResourceURL", getStaticResourceURL());
    cfg.setProperties(properties);
    // now actually do the replacement
    cfg.postProcessBeanFactory(applicationContext.getBeanFactory());
    applicationContext.refresh();
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)378 Test (org.junit.jupiter.api.Test)193 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)106 Test (org.junit.Test)74 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)72 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 ClassPathResource (org.springframework.core.io.ClassPathResource)36 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 QueueChannel (org.springframework.integration.channel.QueueChannel)16 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)15 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)15 GenericMessage (org.springframework.messaging.support.GenericMessage)14 Properties (java.util.Properties)13 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)13 File (java.io.File)12 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)12 Message (org.springframework.messaging.Message)12 InputStreamResource (org.springframework.core.io.InputStreamResource)11 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10