public static DataSource getDataSource ( final String jndiName ) throws FactoryException { Validate . notBlank ( jndiName , "The validated character sequence 'jndiName' is null or empty" ) ; // no need for defensive copies of Strings try { // the initial context is created from the provided JNDI settings final Context context = new InitialContext ( ) ; // retrieve a data source object, close the context as it is no longer needed, and return the data source final Object namedObject = context . lookup ( jndiName ) ; if ( DataSource . class . isInstance ( namedObject ) ) { final DataSource dataSource = ( DataSource ) context . lookup ( jndiName ) ; context . close ( ) ; return dataSource ; } else { final String error = "The JNDI name '" + jndiName + "' does not reference a SQL DataSource." + " This is a configuration issue." ; LOG . warn ( error ) ; throw new FactoryException ( error ) ; } } catch ( NamingException e ) { final String error = "Error retrieving JDBC date source from JNDI: " + jndiName ; LOG . warn ( error ) ; throw new FactoryException ( error , e ) ; } }
Return a DataSource instance for a JNDI managed JDBC data source .