Example usage for org.springframework.jdbc.datasource.init ResourceDatabasePopulator ResourceDatabasePopulator

List of usage examples for org.springframework.jdbc.datasource.init ResourceDatabasePopulator ResourceDatabasePopulator

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource.init ResourceDatabasePopulator ResourceDatabasePopulator.

Prototype

public ResourceDatabasePopulator() 

Source Link

Document

Construct a new ResourceDatabasePopulator with default settings.

Usage

From source file:aka.pirana.springsecurity.config.PersistenceConfig.java

@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
    System.out.println("aka.pirana.springsecurity.config.PersistenceConfig.dataSourceInitializer()");
    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource(dataSource);
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    resourceDatabasePopulator.addScript(new ClassPathResource("db.sql"));
    dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
    dataSourceInitializer.setEnabled(Boolean.parseBoolean(initDatabase));
    return dataSourceInitializer;
}

From source file:cn.org.once.cstack.config.DatabaseConfiguration.java

private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setSqlScriptEncoding("utf-8");
    populator.addScript(dataScript);// w w  w .ja v  a  2s .c o m
    return populator;
}

From source file:org.jblogcms.core.config.PersistenceContext.java

@Bean
public DataSourceInitializer databasePopulator() {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(/*from w w w . j  ava 2  s . com*/
            new ClassPathResource("org/springframework/social/connect/jdbc/JdbcUsersConnectionRepository.sql"));
    populator.setContinueOnError(true);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDatabasePopulator(populator);
    initializer.setDataSource(dataSource());

    return initializer;
}

From source file:org.cloudfoundry.workers.stocks.batch.BatchConfiguration.java

/**
 * We have to have certain records and certain tables for our application to
 * work correctly. This object will be used to ensure that certain
 * <CODE>sql</CODE> files are executed on application startup.
 * // w ww. j a v a 2  s .co  m
 * TODO make the SQL a little smarter about not recreating the tables unless
 * they don't exist.
 * 
 */
@Bean
public DataSourceInitializer dataSourceInitializer() {
    DataSourceInitializer dsi = new DataSourceInitializer();
    dsi.setDataSource(dsConfig.dataSource());
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    String[] scripts = "/batch_%s.sql,/stocks_%s.sql".split(",");
    String scriptSuffix = cloudEnvironment().isCloudFoundry() ? "psql" : "h2";
    for (String s : scripts) {
        ClassPathResource classPathResource = new ClassPathResource(String.format(s, scriptSuffix));
        resourceDatabasePopulator.addScript(classPathResource);
    }
    dsi.setDatabasePopulator(resourceDatabasePopulator);
    dsi.setEnabled(true);
    return dsi;
}

From source file:com.vladmihalcea.util.DatabaseScriptLifecycleHandler.java

protected ResourceDatabasePopulator createResourceDatabasePopulator() {
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    resourceDatabasePopulator.setCommentPrefix(getCommentPrefix());
    resourceDatabasePopulator.setContinueOnError(isContinueOnError());
    resourceDatabasePopulator.setIgnoreFailedDrops(isIgnoreFailedDrops());
    resourceDatabasePopulator.setSqlScriptEncoding(getSqlScriptEncoding());
    return resourceDatabasePopulator;
}

From source file:ch.algotrader.cache.CacheTest.java

@BeforeClass
public static void beforeClass() {

    context = new AnnotationConfigApplicationContext();
    context.getEnvironment().setActiveProfiles("embeddedDataSource", "simulation");

    // register in-memory db
    EmbeddedDatabaseFactory dbFactory = new EmbeddedDatabaseFactory();
    dbFactory.setDatabaseType(EmbeddedDatabaseType.H2);
    dbFactory.setDatabaseName("testdb;MODE=MYSQL;DATABASE_TO_UPPER=FALSE");

    database = dbFactory.getDatabase();/*from w w w .  j a v  a  2  s.  c  o m*/
    context.getDefaultListableBeanFactory().registerSingleton("dataSource", database);

    EngineManager engineManager = Mockito.mock(EngineManager.class);
    Mockito.when(engineManager.getCurrentEPTime()).thenReturn(new Date());
    Mockito.when(engineManager.getCurrentEPTime()).thenReturn(new Date());
    context.getDefaultListableBeanFactory().registerSingleton("engineManager", engineManager);

    AtomicReference<TransactionService> transactionService = new AtomicReference<>();
    Engine engine = new NoopEngine(StrategyImpl.SERVER);

    context.getDefaultListableBeanFactory().registerSingleton("serverEngine", engine);

    ExternalMarketDataService externalMarketDataService = Mockito.mock(ExternalMarketDataService.class);
    context.getDefaultListableBeanFactory().registerSingleton("externalMarketDataService",
            externalMarketDataService);

    context.getDefaultListableBeanFactory().registerSingleton("historicalDataService",
            new NoopHistoricalDataServiceImpl());

    Mockito.when(externalMarketDataService.getFeedType()).thenReturn(FeedType.IB.name());

    // register Wirings
    context.register(CommonConfigWiring.class, CoreConfigWiring.class, EventDispatchWiring.class,
            EventDispatchPostInitWiring.class, HibernateWiring.class, CacheWiring.class, DaoWiring.class,
            ServiceWiring.class, SimulationWiring.class);

    context.refresh();

    transactionService.set(context.getBean(TransactionService.class));

    cache = context.getBean(CacheManager.class);
    txTemplate = context.getBean(TransactionTemplate.class);

    // create the database
    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ClassPathResource("/db/h2/h2.sql"));
    DatabasePopulatorUtils.execute(dbPopulator, database);

    // populate the database
    SessionFactory sessionFactory = context.getBean(SessionFactory.class);
    Session session = sessionFactory.openSession();

    Exchange exchange1 = new ExchangeImpl();
    exchange1.setName("IDEALPRO");
    exchange1.setCode("IDEALPRO");
    exchange1.setTimeZone("US/Eastern");
    session.save(exchange1);

    SecurityFamily family1 = new SecurityFamilyImpl();
    family1.setName("FX");
    family1.setTickSizePattern("0<0.1");
    family1.setCurrency(Currency.USD);
    family1.setExchange(exchange1);
    family1.setTradeable(true);
    family1.setContractSize(1.0);
    securityFamilyId1 = (Long) session.save(family1);

    SecurityFamily family2 = new SecurityFamilyImpl();
    family2.setName("NON_TRADEABLE");
    family2.setTickSizePattern("0<0.1");
    family2.setCurrency(Currency.USD);
    family2.setTradeable(false);
    family2.setContractSize(1.0);
    securityFamilyId2 = (Long) session.save(family2);

    Forex security1 = new ForexImpl();
    security1.setSymbol("EUR.USD");
    security1.setBaseCurrency(Currency.EUR);
    security1.setSecurityFamily(family1);
    securityId1 = (Long) session.save(security1);

    Forex security2 = new ForexImpl();
    security2.setSymbol("GBP.USD");
    security2.setBaseCurrency(Currency.GBP);
    security2.setSecurityFamily(family1);
    security2.setUnderlying(security1);
    securityId2 = (Long) session.save(security2);

    Strategy strategy1 = new StrategyImpl();
    strategy1.setName(STRATEGY_NAME);
    strategyId1 = (Long) session.save(strategy1);

    Position position1 = new PositionImpl();
    position1.setQuantity(222);
    position1.setStrategy(strategy1);
    position1.setSecurity(security2);
    position1.setCost(new BigDecimal(0.0));
    position1.setRealizedPL(new BigDecimal(0.0));

    session.save(position1);

    Property property1 = new PropertyImpl();
    property1.setName(PROPERTY_NAME);
    property1.setDoubleValue(10.0);
    property1.setPropertyHolder(strategy1);
    session.save(property1);
    strategy1.getProps().put(PROPERTY_NAME, property1);

    Account account1 = new AccountImpl();
    account1.setName("TEST");
    account1.setBroker("TEST");
    account1.setOrderServiceType(OrderServiceType.SIMULATION.toString());
    accountId1 = (Long) session.save(account1);

    session.flush();
    session.close();
}

From source file:org.springframework.cloud.stream.module.jdbc.JdbcSinkConfiguration.java

@ConditionalOnProperty("initialize")
@Bean//from   w ww.  j  a v  a 2 s  . c  o  m
public DataSourceInitializer nonBootDataSourceInitializer(DataSource dataSource,
        ResourceLoader resourceLoader) {
    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource(dataSource);
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.setIgnoreFailedDrops(true);
    dataSourceInitializer.setDatabasePopulator(databasePopulator);
    if ("true".equals(properties.getInitialize())) {
        databasePopulator.addScript(new DefaultInitializationScriptResource(properties));
    } else {
        databasePopulator.addScript(resourceLoader.getResource(properties.getInitialize()));
    }
    return dataSourceInitializer;
}

From source file:org.springframework.cloud.stream.app.jdbc.sink.JdbcSinkConfiguration.java

@ConditionalOnProperty("jdbc.initialize")
@Bean/*from   w  ww  . ja va2 s . co m*/
public DataSourceInitializer nonBootDataSourceInitializer(DataSource dataSource,
        ResourceLoader resourceLoader) {
    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource(dataSource);
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.setIgnoreFailedDrops(true);
    dataSourceInitializer.setDatabasePopulator(databasePopulator);
    if ("true".equals(properties.getInitialize())) {
        databasePopulator.addScript(new DefaultInitializationScriptResource(properties));
    } else {
        databasePopulator.addScript(resourceLoader.getResource(properties.getInitialize()));
    }
    return dataSourceInitializer;
}

From source file:ch.algotrader.cache.CacheTest.java

@AfterClass
public static void afterClass() {

    // cleanup the database
    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ByteArrayResource("DROP ALL OBJECTS".getBytes(Charsets.US_ASCII)));

    DatabasePopulatorUtils.execute(dbPopulator, database);

    context.close();/*from   ww w  .  jav  a 2  s  . com*/

    net.sf.ehcache.CacheManager ehCacheManager = net.sf.ehcache.CacheManager.getInstance();
    ehCacheManager.shutdown();
}

From source file:ch.algotrader.service.LookupServiceTest.java

@Override
@Before//from  www  . j ava2  s .  com
public void setup() throws Exception {

    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ClassPathResource("/db/h2/h2.sql"));
    DatabasePopulatorUtils.execute(dbPopulator, dataSource);

    this.session = sessionFactory.openSession();

    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(this.session));
}