@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()); }
@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()); }
@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()); }
@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()); }
@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); }
@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); }
@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); }
@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); }
@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); }
/** * 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; }
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); } }
@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; }
@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()); }
@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(); }
@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()); }
@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) { } }
@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) { } }
@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")); }
@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)); }
@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()); }
@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()); }
/** * 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); }
@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"); } }
@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); }
/** * @return Source de données */ @Bean public DataSource dataSourceEcandidat() { JndiDataSourceLookup dsLookup = new JndiDataSourceLookup(); return dsLookup.getDataSource("java:/comp/env/jdbc/dbEcandidat"); }