Java Code Examples for org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup

The following are top voted examples for showing how to use org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: cloud-spring-boot-sample   File: NeoConfig.java   Source Code and License 7 votes vote down vote up
@Bean(destroyMethod="")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException, SQLException
{
	JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
	
	DataSource ds = dataSourceLookup.getDataSource("java:comp/env/jdbc/DefaultDB");
	
	DBInformation dbInfo = new DBInformation(ds.getConnection().getMetaData());
	log.info(dbInfo.toString());
	
	return ds;
}
 
Example 2
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testMetaInfCase() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/META-INF/persistence.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement", info[0].getPersistenceUnitName());

	assertEquals(2, info[0].getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
	assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 3
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testExample2() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example2.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);

	assertEquals("OrderManagement2", info[0].getPersistenceUnitName());

	assertEquals(1, info[0].getMappingFileNames().size());
	assertEquals("mappings.xml", info[0].getMappingFileNames().get(0));
	assertEquals(0, info[0].getProperties().keySet().size());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 4
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testExample3() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example3.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement3", info[0].getPersistenceUnitName());

	assertEquals(2, info[0].getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
	assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));

	assertEquals(0, info[0].getProperties().keySet().size());
	assertNull(info[0].getJtaDataSource());
	assertNull(info[0].getNonJtaDataSource());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 5
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testExample5() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example5.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement5", info[0].getPersistenceUnitName());

	assertEquals(2, info[0].getMappingFileNames().size());
	assertEquals("order1.xml", info[0].getMappingFileNames().get(0));
	assertEquals("order2.xml", info[0].getMappingFileNames().get(1));

	assertEquals(2, info[0].getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
	assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));

	assertEquals("com.acme.AcmePersistence", info[0].getPersistenceProviderClassName());
	assertEquals(0, info[0].getProperties().keySet().size());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 6
Project: srccode   File: DbCachedServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
public List<Map<String, Object>> makeDbRequest(String jndi,
		String query) {
       JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
       dsLookup.setResourceRef(true);
      
       DataSource dataSource = dsLookup.getDataSource(jndi);
       Assert.notNull(dataSource, "DataSource is null.");
       LOGGER.info("DataSource: " + dataSource);   
       
       JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
       
	LOGGER.info("Executing query:"+query);
	LOGGER.info("JdbcTemplate:"+jdbcTemplate);
       return jdbcTemplate.queryForList(query);
}
 
Example 7
Project: srccode   File: DbCachedServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
@Cacheable("mundoCache")
public List<Map<String, Object>> makeDbOrCache1mRequest(String jndi,
		String query) {
       JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
       dsLookup.setResourceRef(true);
      
       DataSource dataSource = dsLookup.getDataSource(jndi);
       Assert.notNull(dataSource, "DataSource is null.");
       LOGGER.info("DataSource: " + dataSource);   
       
       JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
       
	LOGGER.info("Executing query:"+query);
	LOGGER.info("JdbcTemplate:"+jdbcTemplate);
       return jdbcTemplate.queryForList(query);
}
 
Example 8
Project: srccode   File: DbCachedServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
@Cacheable("mundo1hCache")
public List<Map<String, Object>> makeDbOrCache1hRequest(String jndi,
		String query) {
       JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
       dsLookup.setResourceRef(true);
      
       DataSource dataSource = dsLookup.getDataSource(jndi);
       Assert.notNull(dataSource, "DataSource is null.");
       LOGGER.info("DataSource: " + dataSource);   
       
       JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
       
	LOGGER.info("Executing query:"+query);
	LOGGER.info("JdbcTemplate:"+jdbcTemplate);
       return jdbcTemplate.queryForList(query);
}
 
Example 9
Project: srccode   File: DbCachedServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
@Cacheable("mundo12hCache")
public List<Map<String, Object>> makeDbOrCache12hRequest(String jndi,
		String query) {
       JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
       dsLookup.setResourceRef(true);
      
       DataSource dataSource = dsLookup.getDataSource(jndi);
       Assert.notNull(dataSource, "DataSource is null.");
       LOGGER.info("DataSource: " + dataSource);   
       
       JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
       
	LOGGER.info("Executing query:"+query);
	LOGGER.info("JdbcTemplate:"+jdbcTemplate);
       return jdbcTemplate.queryForList(query);
}
 
Example 10
Project: srccode   File: DbCachedServiceImpl.java   Source Code and License 6 votes vote down vote up
@Override
@Cacheable("mundo24hCache")
public List<Map<String, Object>> makeDbOrCache24hRequest(String jndi,
		String query) {
       JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
       dsLookup.setResourceRef(true);
      
       DataSource dataSource = dsLookup.getDataSource(jndi);
       Assert.notNull(dataSource, "DataSource is null.");
       LOGGER.info("DataSource: " + dataSource);   
       
       JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
       
	LOGGER.info("Executing query:"+query);
	LOGGER.info("JdbcTemplate:"+jdbcTemplate);
       return jdbcTemplate.queryForList(query);
}
 
Example 11
Project: srccode   File: PersistenceConfig.java   Source Code and License 6 votes vote down vote up
/**
 * Creates DataSource from JNDI resource.
 * @return DataSource
 */
@Bean
public DataSource dataSourceJndi() {
    
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);
    DataSource dataSource = dsLookup.getDataSource(dbJndiName);
    return dataSource;
}
 
Example 12
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testMetaInfCase() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/META-INF/persistence.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement", info[0].getPersistenceUnitName());

	assertEquals(2, info[0].getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
	assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 13
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testExample2() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example2.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);

	assertEquals("OrderManagement2", info[0].getPersistenceUnitName());

	assertEquals(1, info[0].getMappingFileNames().size());
	assertEquals("mappings.xml", info[0].getMappingFileNames().get(0));
	assertEquals(0, info[0].getProperties().keySet().size());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 14
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testExample3() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example3.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement3", info[0].getPersistenceUnitName());

	assertEquals(2, info[0].getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
	assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));

	assertEquals(0, info[0].getProperties().keySet().size());
	assertNull(info[0].getJtaDataSource());
	assertNull(info[0].getNonJtaDataSource());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 15
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 6 votes vote down vote up
@Test
public void testExample5() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example5.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement5", info[0].getPersistenceUnitName());

	assertEquals(2, info[0].getMappingFileNames().size());
	assertEquals("order1.xml", info[0].getMappingFileNames().get(0));
	assertEquals("order2.xml", info[0].getMappingFileNames().get(1));

	assertEquals(2, info[0].getJarFileUrls().size());
	assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
	assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));

	assertEquals("com.acme.AcmePersistence", info[0].getPersistenceProviderClassName());
	assertEquals(0, info[0].getProperties().keySet().size());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 16
Project: rjb-blog-multitenancy   File: SingleTenantDataSourceConfig.java   Source Code and License 5 votes vote down vote up
protected DataSource getJndiDataSource() {
	if (StringUtil.isEmpty(this.propertiesConfig.dataSourceJndiName())) {
		throw new IllegalStateException("'dataSourceJndiName' property is required");
	}
	try {
		return new JndiDataSourceLookup()
				.getDataSource(this.propertiesConfig.dataSourceJndiName());
	} catch (Exception e) {
		throw new IllegalStateException(
				"failed to look up JNDI DataSource for name=" + this.propertiesConfig.dataSourceJndiName(), e);
	}
}
 
Example 17
Project: rjb-blog-multitenancy   File: SingleTenantDataSourceConfig.java   Source Code and License 5 votes vote down vote up
protected DataSource getJndiDataSource() {
	if (StringUtil.isEmpty(this.propertiesConfig.dataSourceJndiName())) {
		throw new IllegalStateException("'dataSourceJndiName' property is required");
	}
	try {
		return new JndiDataSourceLookup()
				.getDataSource(this.propertiesConfig.dataSourceJndiName());
	} catch (Exception e) {
		throw new IllegalStateException(
				"failed to look up JNDI DataSource for name=" + this.propertiesConfig.dataSourceJndiName(), e);
	}
}
 
Example 18
Project: rjb-blog-multitenancy   File: SingleTenantDataSourceConfig.java   Source Code and License 5 votes vote down vote up
protected DataSource getJndiDataSource() {
	if (StringUtil.isEmpty(this.propertiesConfig.dataSourceJndiName())) {
		throw new IllegalStateException("'dataSourceJndiName' property is required");
	}
	try {
		return new JndiDataSourceLookup()
				.getDataSource(this.propertiesConfig.dataSourceJndiName());
	} catch (Exception e) {
		throw new IllegalStateException(
				"failed to look up JNDI DataSource for name=" + this.propertiesConfig.dataSourceJndiName(), e);
	}
}
 
Example 19
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testExample1() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example1.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement", info[0].getPersistenceUnitName());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 20
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testExample4() throws Exception {
	SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
	DataSource ds = new DriverManagerDataSource();
	builder.bind("java:comp/env/jdbc/MyDB", ds);

	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement4", info[0].getPersistenceUnitName());

	assertEquals(1, info[0].getMappingFileNames().size());
	assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0));

	assertEquals(3, info[0].getManagedClassNames().size());
	assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0));
	assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1));
	assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2));

	assertTrue("Exclude unlisted should be true when no value.", info[0].excludeUnlistedClasses());

	assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType());
	assertEquals(0, info[0].getProperties().keySet().size());

	builder.clear();
}
 
Example 21
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testExample6() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example6.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
	assertEquals(1, info.length);
	assertEquals("pu", info[0].getPersistenceUnitName());
	assertEquals(0, info[0].getProperties().keySet().size());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 22
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Ignore  // not doing schema parsing anymore for JPA 2.0 compatibility
@Test
public void testInvalidPersistence() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-invalid.xml";
	try {
		reader.readPersistenceUnitInfos(resource);
		fail("expected invalid document exception");
	}
	catch (RuntimeException expected) {
	}
}
 
Example 23
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Ignore  // not doing schema parsing anymore for JPA 2.0 compatibility
@Test
public void testNoSchemaPersistence() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-no-schema.xml";
	try {
		reader.readPersistenceUnitInfos(resource);
		fail("expected invalid document exception");
	}
	catch (RuntimeException expected) {
	}
}
 
Example 24
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testPersistenceUnitRootUrl() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());

	URL url = reader.determinePersistenceUnitRootUrl(new ClassPathResource(
			"/org/springframework/orm/jpa/persistence-no-schema.xml"));
	assertNull(url);

	url = reader.determinePersistenceUnitRootUrl(new ClassPathResource("/org/springframework/orm/jpa/META-INF/persistence.xml"));
	assertTrue("the containing folder should have been returned", url.toString().endsWith("/org/springframework/orm/jpa"));
}
 
Example 25
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testPersistenceUnitRootUrlWithJar() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());

	ClassPathResource archive = new ClassPathResource("/org/springframework/orm/jpa/jpa-archive.jar");
	String newRoot = "jar:" + archive.getURL().toExternalForm() + "!/META-INF/persist.xml";
	Resource insideArchive = new UrlResource(newRoot);
	// make sure the location actually exists
	assertTrue(insideArchive.exists());
	URL url = reader.determinePersistenceUnitRootUrl(insideArchive);
	assertTrue("the archive location should have been returned", archive.getURL().sameFile(url));
}
 
Example 26
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testJpa1ExcludeUnlisted() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-exclude-1.0.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals("The number of persistence units is incorrect.", 4, info.length);

	PersistenceUnitInfo noExclude = info[0];
	assertNotNull("noExclude should not be null.", noExclude);
	assertEquals("noExclude name is not correct.", "NoExcludeElement", noExclude.getPersistenceUnitName());
	assertFalse("Exclude unlisted should default false in 1.0.", noExclude.excludeUnlistedClasses());

	PersistenceUnitInfo emptyExclude = info[1];
	assertNotNull("emptyExclude should not be null.", emptyExclude);
	assertEquals("emptyExclude name is not correct.", "EmptyExcludeElement", emptyExclude.getPersistenceUnitName());
	assertTrue("emptyExclude should be true.", emptyExclude.excludeUnlistedClasses());

	PersistenceUnitInfo trueExclude = info[2];
	assertNotNull("trueExclude should not be null.", trueExclude);
	assertEquals("trueExclude name is not correct.", "TrueExcludeElement", trueExclude.getPersistenceUnitName());
	assertTrue("trueExclude should be true.", trueExclude.excludeUnlistedClasses());

	PersistenceUnitInfo falseExclude = info[3];
	assertNotNull("falseExclude should not be null.", falseExclude);
	assertEquals("falseExclude name is not correct.", "FalseExcludeElement", falseExclude.getPersistenceUnitName());
	assertFalse("falseExclude should be false.", falseExclude.excludeUnlistedClasses());
}
 
Example 27
Project: spring4-understanding   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testJpa2ExcludeUnlisted() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-exclude-2.0.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals("The number of persistence units is incorrect.", 4, info.length);

	PersistenceUnitInfo noExclude = info[0];
	assertNotNull("noExclude should not be null.", noExclude);
	assertEquals("noExclude name is not correct.", "NoExcludeElement", noExclude.getPersistenceUnitName());
	assertFalse("Exclude unlisted still defaults to false in 2.0.", noExclude.excludeUnlistedClasses());

	PersistenceUnitInfo emptyExclude = info[1];
	assertNotNull("emptyExclude should not be null.", emptyExclude);
	assertEquals("emptyExclude name is not correct.", "EmptyExcludeElement", emptyExclude.getPersistenceUnitName());
	assertTrue("emptyExclude should be true.", emptyExclude.excludeUnlistedClasses());

	PersistenceUnitInfo trueExclude = info[2];
	assertNotNull("trueExclude should not be null.", trueExclude);
	assertEquals("trueExclude name is not correct.", "TrueExcludeElement", trueExclude.getPersistenceUnitName());
	assertTrue("trueExclude should be true.", trueExclude.excludeUnlistedClasses());

	PersistenceUnitInfo falseExclude = info[3];
	assertNotNull("falseExclude should not be null.", falseExclude);
	assertEquals("falseExclude name is not correct.", "FalseExcludeElement", falseExclude.getPersistenceUnitName());
	assertFalse("falseExclude should be false.", falseExclude.excludeUnlistedClasses());
}
 
Example 28
Project: herd   File: DaoEnvSpringModuleConfig.java   Source Code and License 5 votes vote down vote up
/**
 * The data source for the application.
 *
 * @return the data source.
 */
@Bean
public static DataSource herdDataSource()
{
    // Access the environment using the application context holder since we're in a static method that doesn't have access to the environment in any
    // other way.
    Environment environment = ApplicationContextHolder.getApplicationContext().getEnvironment();

    // Get the configuration property for the data source JNDI name.
    String dataSourceJndiName = ConfigurationHelper.getProperty(ConfigurationValue.HERD_DATA_SOURCE_JNDI_NAME, environment);

    // Return a new JNDI data source.
    return new JndiDataSourceLookup().getDataSource(dataSourceJndiName);
}
 
Example 29
Project: egd-web   File: DatabaseConfiguration.java   Source Code and License 5 votes vote down vote up
@Primary
@Bean(destroyMethod = "")
public DataSource dataSource() {
    log.debug("Configuring Datasource");

    if (propertyResolver.getProperty("url") == null && propertyResolver.getProperty("jndi") == null) {
        log.error("Your database connection pool configuration is incorrect! The application" +
            "cannot start. Please check your Spring profile, current profiles are: {}");

        throw new ApplicationContextException("Database connection pool is not configured correctly");
    }

    try {
        String jndi = propertyResolver.getProperty("jndi");

        if (jndi != null) {
            log.debug("Getting datasource from JNDI global resource link {}", jndi);
            JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
            return dataSourceLookup.getDataSource(jndi);
        } else {

            String url = propertyResolver.getProperty("url");
            String username = propertyResolver.getProperty("username");

            log.debug("Initializing PGPoolingDataSource: url={}, username={}", url, username);
            PGPoolingDataSource source = new PGPoolingDataSource();
            source.setUrl(url);
            source.setUser(username);
            source.setPassword(propertyResolver.getProperty("password"));
            source.setMaxConnections(10);
            return source;
        }
    } catch (Exception e) {
        log.error("dataSource: msg=" + e.getMessage(), e);
        throw new ApplicationContextException("Database connection pool creation resulted in error");
    }
}
 
Example 30
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testExample1() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example1.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement", info[0].getPersistenceUnitName());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 31
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testExample4() throws Exception {
	SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
	DataSource ds = new DriverManagerDataSource();
	builder.bind("java:comp/env/jdbc/MyDB", ds);

	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals(1, info.length);
	assertEquals("OrderManagement4", info[0].getPersistenceUnitName());

	assertEquals(1, info[0].getMappingFileNames().size());
	assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0));

	assertEquals(3, info[0].getManagedClassNames().size());
	assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0));
	assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1));
	assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2));

	assertTrue("Exclude unlisted should be true when no value.", info[0].excludeUnlistedClasses());

	assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType());
	assertEquals(0, info[0].getProperties().keySet().size());

	builder.clear();
}
 
Example 32
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testExample6() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-example6.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
	assertEquals(1, info.length);
	assertEquals("pu", info[0].getPersistenceUnitName());
	assertEquals(0, info[0].getProperties().keySet().size());

	assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
 
Example 33
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Ignore  // not doing schema parsing anymore for JPA 2.0 compatibility
@Test
public void testInvalidPersistence() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-invalid.xml";
	try {
		reader.readPersistenceUnitInfos(resource);
		fail("expected invalid document exception");
	}
	catch (RuntimeException expected) {
	}
}
 
Example 34
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Ignore  // not doing schema parsing anymore for JPA 2.0 compatibility
@Test
public void testNoSchemaPersistence() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-no-schema.xml";
	try {
		reader.readPersistenceUnitInfos(resource);
		fail("expected invalid document exception");
	}
	catch (RuntimeException expected) {
	}
}
 
Example 35
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testPersistenceUnitRootUrl() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());

	URL url = reader.determinePersistenceUnitRootUrl(new ClassPathResource(
			"/org/springframework/orm/jpa/persistence-no-schema.xml"));
	assertNull(url);

	url = reader.determinePersistenceUnitRootUrl(new ClassPathResource("/org/springframework/orm/jpa/META-INF/persistence.xml"));
	assertTrue("the containing folder should have been returned", url.toString().endsWith("/org/springframework/orm/jpa"));
}
 
Example 36
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testPersistenceUnitRootUrlWithJar() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());

	ClassPathResource archive = new ClassPathResource("/org/springframework/orm/jpa/jpa-archive.jar");
	String newRoot = "jar:" + archive.getURL().toExternalForm() + "!/META-INF/persist.xml";
	Resource insideArchive = new UrlResource(newRoot);
	// make sure the location actually exists
	assertTrue(insideArchive.exists());
	URL url = reader.determinePersistenceUnitRootUrl(insideArchive);
	assertTrue("the archive location should have been returned", archive.getURL().sameFile(url));
}
 
Example 37
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testJpa1ExcludeUnlisted() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-exclude-1.0.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals("The number of persistence units is incorrect.", 4, info.length);

	PersistenceUnitInfo noExclude = info[0];
	assertNotNull("noExclude should not be null.", noExclude);
	assertEquals("noExclude name is not correct.", "NoExcludeElement", noExclude.getPersistenceUnitName());
	assertFalse("Exclude unlisted should default false in 1.0.", noExclude.excludeUnlistedClasses());

	PersistenceUnitInfo emptyExclude = info[1];
	assertNotNull("emptyExclude should not be null.", emptyExclude);
	assertEquals("emptyExclude name is not correct.", "EmptyExcludeElement", emptyExclude.getPersistenceUnitName());
	assertTrue("emptyExclude should be true.", emptyExclude.excludeUnlistedClasses());

	PersistenceUnitInfo trueExclude = info[2];
	assertNotNull("trueExclude should not be null.", trueExclude);
	assertEquals("trueExclude name is not correct.", "TrueExcludeElement", trueExclude.getPersistenceUnitName());
	assertTrue("trueExclude should be true.", trueExclude.excludeUnlistedClasses());

	PersistenceUnitInfo falseExclude = info[3];
	assertNotNull("falseExclude should not be null.", falseExclude);
	assertEquals("falseExclude name is not correct.", "FalseExcludeElement", falseExclude.getPersistenceUnitName());
	assertFalse("falseExclude should be false.", falseExclude.excludeUnlistedClasses());
}
 
Example 38
Project: class-guard   File: PersistenceXmlParsingTests.java   Source Code and License 5 votes vote down vote up
@Test
public void testJpa2ExcludeUnlisted() throws Exception {
	PersistenceUnitReader reader = new PersistenceUnitReader(
			new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
	String resource = "/org/springframework/orm/jpa/persistence-exclude-2.0.xml";
	PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);

	assertNotNull(info);
	assertEquals("The number of persistence units is incorrect.", 4, info.length);

	PersistenceUnitInfo noExclude = info[0];
	assertNotNull("noExclude should not be null.", noExclude);
	assertEquals("noExclude name is not correct.", "NoExcludeElement", noExclude.getPersistenceUnitName());
	assertFalse("Exclude unlisted still defaults to false in 2.0.", noExclude.excludeUnlistedClasses());

	PersistenceUnitInfo emptyExclude = info[1];
	assertNotNull("emptyExclude should not be null.", emptyExclude);
	assertEquals("emptyExclude name is not correct.", "EmptyExcludeElement", emptyExclude.getPersistenceUnitName());
	assertTrue("emptyExclude should be true.", emptyExclude.excludeUnlistedClasses());

	PersistenceUnitInfo trueExclude = info[2];
	assertNotNull("trueExclude should not be null.", trueExclude);
	assertEquals("trueExclude name is not correct.", "TrueExcludeElement", trueExclude.getPersistenceUnitName());
	assertTrue("trueExclude should be true.", trueExclude.excludeUnlistedClasses());

	PersistenceUnitInfo falseExclude = info[3];
	assertNotNull("falseExclude should not be null.", falseExclude);
	assertEquals("falseExclude name is not correct.", "FalseExcludeElement", falseExclude.getPersistenceUnitName());
	assertFalse("falseExclude should be false.", falseExclude.excludeUnlistedClasses());
}
 
Example 39
Project: spring-batch-tools   File: BatchDataSourceConfig.java   Source Code and License 5 votes vote down vote up
@Bean
@BatchDataSource
public DataSource batchDataSource() throws IllegalArgumentException, NamingException {
	final String jndiName = env.getProperty("batch.dataSource.jndiName", "java:comp/env/jdbc/Batch");

	return new JndiDataSourceLookup().getDataSource(jndiName);
}
 
Example 40
Project: esup-ecandidat   File: JpaConfigEcandidat.java   Source Code and License 4 votes vote down vote up
/**
 * @return Source de données
 */
@Bean
public DataSource dataSourceEcandidat() {
	JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
	return dsLookup.getDataSource("java:/comp/env/jdbc/dbEcandidat");
}
 
Example 41
Project: azeroth   File: MutiRouteDataSource.java   Source Code and License 4 votes vote down vote up
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
    this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}
 
Example 42
Project: bookManager   File: RootApplicationContextConfig.java   Source Code and License 4 votes vote down vote up
@Bean
public DataSource bookManagerDataSource() {
	JndiDataSourceLookup lookup = new JndiDataSourceLookup();
	return lookup.getDataSource("jdbc/bookManager");
}
 
Example 43
Project: jeesuite-libs   File: MutiRouteDataSource.java   Source Code and License 4 votes vote down vote up
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
	this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}
 
Example 44
Project: atsy   File: PersistenceConfiguration.java   Source Code and License 4 votes vote down vote up
@Bean
public DataSource dataSource() {
  final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
  dsLookup.setResourceRef(true);
  return dsLookup.getDataSource(JNDI_DATA_SOURCE);
}
 
Example 45
Project: OrcidHub   File: DatabaseConfiguration.java   Source Code and License 4 votes vote down vote up
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingClass(name = "it.cineca.pst.huborcid.config.HerokuDatabaseConfiguration")
@Profile("!" + Constants.SPRING_PROFILE_CLOUD)
public DataSource dataSource() {
    log.debug("Configuring Datasource");
    if (dataSourcePropertyResolver.getProperty("url") == null && dataSourcePropertyResolver.getProperty("databaseName") == null) {
        log.error("Your database connection pool configuration is incorrect! The application" +
                " cannot start. Please check your Spring profile, current profiles are: {}",
                Arrays.toString(env.getActiveProfiles()));

        throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    if(StringUtils.isEmpty(dataSourcePropertyResolver.getProperty("jndi"))){
     config.setDataSourceClassName(dataSourcePropertyResolver.getProperty("dataSourceClassName"));
     if(StringUtils.isEmpty(dataSourcePropertyResolver.getProperty("url"))) {
         config.addDataSourceProperty("databaseName", dataSourcePropertyResolver.getProperty("databaseName"));
         config.addDataSourceProperty("serverName", dataSourcePropertyResolver.getProperty("serverName"));
     } else {
         config.addDataSourceProperty("url", dataSourcePropertyResolver.getProperty("url"));
     }
     config.addDataSourceProperty("user", dataSourcePropertyResolver.getProperty("username"));
     config.addDataSourceProperty("password", dataSourcePropertyResolver.getProperty("password"));
     config.setConnectionTimeout(10000);
     config.setMaximumPoolSize(25);
    }else{
    	final JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        dataSourceLookup.setResourceRef(true);
    	DataSource dataSourceTemp = null;
        try {
            dataSourceTemp = dataSourceLookup.getDataSource(dataSourcePropertyResolver.getProperty("jndi"));
        } catch (DataSourceLookupFailureException e) {
            log.error("DataSource not found.");
        }
        config.setDataSource(dataSourceTemp);
    }

    if (metricRegistry != null) {
        config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
}
 
Example 46
Project: ubrew_v0.1   File: JndiDataSourceConfig.java   Source Code and License 4 votes vote down vote up
@Bean
public DataSource dataSource() {
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);
    return dsLookup.getDataSource(dataSourceName());
}
 
Example 47
Project: uncode-dal-all   File: DynamicDataSource.java   Source Code and License 4 votes vote down vote up
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
	this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}
 
Example 48
Project: 2fa-demo   File: PersistenceJpaConfig.java   Source Code and License 4 votes vote down vote up
@Bean
public DataSource dataSource() {
	JndiDataSourceLookup lookup = new JndiDataSourceLookup();
	return lookup.getDataSource(jndiDatasource);
}
 
Example 49
Project: spring-scaffy   File: SpringJNDIDatasourceConfig.java   Source Code and License 3 votes vote down vote up
@Bean
 public DataSource dataSource() {
		
JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
   
dsLookup.setResourceRef(true);
     
     return dsLookup.getDataSource(jndiDatasource);
 }
 
Example 50
Project: lams   File: DefaultPersistenceUnitManager.java   Source Code and License 2 votes vote down vote up
/**
 * Specify the JDBC DataSourceLookup that provides DataSources for the
 * persistence provider, resolving data source names in {@code persistence.xml}
 * against Spring-managed DataSource instances.
 * <p>Default is JndiDataSourceLookup, which resolves DataSource names as
 * JNDI names (as defined by standard JPA). Specify a BeanFactoryDataSourceLookup
 * instance if you want DataSource names to be resolved against Spring bean names.
 * <p>Alternatively, consider passing in a map from names to DataSource instances
 * via the "dataSources" property. If the {@code persistence.xml} file
 * does not define DataSource names at all, specify a default DataSource
 * via the "defaultDataSource" property.
 * @see org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup
 * @see org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup
 * @see #setDataSources
 * @see #setDefaultDataSource
 */
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
	this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}
 
Example 51
Project: holon-jdbc   File: JNDIDataSourceFactory.java   Source Code and License 2 votes vote down vote up
/**
 * Lookup a DataSource with given JNDI name in context.
 * @param jndiName JNDI name
 * @return DataSource instance
 * @throws NamingException Failed to obtain a DataSource with given name
 */
private static DataSource lookupJNDIDataSource(String jndiName) throws NamingException {
	JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
	return dataSourceLookup.getDataSource(jndiName);
}
 
Example 52
Project: spring4-understanding   File: DefaultPersistenceUnitManager.java   Source Code and License 2 votes vote down vote up
/**
 * Specify the JDBC DataSourceLookup that provides DataSources for the
 * persistence provider, resolving data source names in {@code persistence.xml}
 * against Spring-managed DataSource instances.
 * <p>Default is JndiDataSourceLookup, which resolves DataSource names as
 * JNDI names (as defined by standard JPA). Specify a BeanFactoryDataSourceLookup
 * instance if you want DataSource names to be resolved against Spring bean names.
 * <p>Alternatively, consider passing in a map from names to DataSource instances
 * via the "dataSources" property. If the {@code persistence.xml} file
 * does not define DataSource names at all, specify a default DataSource
 * via the "defaultDataSource" property.
 * @see org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup
 * @see org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup
 * @see #setDataSources
 * @see #setDefaultDataSource
 */
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
	this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}
 
Example 53
Project: class-guard   File: DefaultPersistenceUnitManager.java   Source Code and License 2 votes vote down vote up
/**
 * Specify the JDBC DataSourceLookup that provides DataSources for the
 * persistence provider, resolving data source names in {@code persistence.xml}
 * against Spring-managed DataSource instances.
 * <p>Default is JndiDataSourceLookup, which resolves DataSource names as
 * JNDI names (as defined by standard JPA). Specify a BeanFactoryDataSourceLookup
 * instance if you want DataSource names to be resolved against Spring bean names.
 * <p>Alternatively, consider passing in a map from names to DataSource instances
 * via the "dataSources" property. If the {@code persistence.xml} file
 * does not define DataSource names at all, specify a default DataSource
 * via the "defaultDataSource" property.
 * @see org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup
 * @see org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup
 * @see #setDataSources
 * @see #setDefaultDataSource
 */
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
	this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}
 
Example 54
Project: argo   File: MasterSlaveRoutingDataSource.java   Source Code and License 2 votes vote down vote up
/**
 * Set the DataSourceLookup implementation to use for resolving data source
 * name Strings in the {@link #setTargetDataSources targetDataSources} map.
 * <p>Default is a {@link JndiDataSourceLookup}, allowing the JNDI names
 * of application server DataSources to be specified directly.
 */
public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {
       this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
}