Created
February 29, 2012 16:45
-
-
Save webdevwilson/1942363 to your computer and use it in GitHub Desktop.
JndiDataSource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.inject.AbstractModule; | |
import javax.naming.InitialContext; | |
public class DataSource | |
{ | |
public static AbstractModule fromJndi(final String jndiName) | |
{ | |
return new AbstractModule() | |
{ | |
@Override | |
protected void configure() | |
{ | |
try | |
{ | |
final InitialContext ic = new InitialContext(); | |
final javax.sql.DataSource dataSource = (javax.sql.DataSource) ic.lookup(jndiName); | |
bind(javax.sql.DataSource.class).toInstance(dataSource); | |
} | |
catch (final Exception e) | |
{ | |
throw new RuntimeException("Error retrieving datasource '" + jndiName + "'", e); | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment