Skip to content

Commit

Permalink
Made de jndi key under which the datasources are looked up configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rvs-fluid-it committed Jun 22, 2015
1 parent 564ba66 commit 1644e6a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
* {@link DataSource} configured in your J2EE Container.
*/
public class JEEDataSourceConfiguration {
// The default key matches Tomcat
// (Dependending on the target application server used you should eventually override the default)
// See https://tomcat.apache.org/tomcat-8.0-doc/jndi-resources-howto.html
@NotNull
private String datasourcesJndKey = "java:comp/env";

@NotNull
private String name;

Expand All @@ -39,4 +45,11 @@ public String getName() {
return name;
}

/**
* @return JNDI key under which the datasources are registered.
*/
@JsonProperty
public String getDatasourcesJndiKey() {
return this.datasourcesJndKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Map<String, String> getProperties() {
@Override
public ManagedDataSource build(MetricRegistry metricRegistry, String name) {
try {
return new JEEManagedDataSource(this.configuration.getName());
return new JEEManagedDataSource(this.configuration.getDatasourcesJndiKey(), this.configuration.getName());
} catch (NamingException e) {
throw new IllegalStateException("An error has occured while opening datasource " + name, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
import io.dropwizard.db.ManagedDataSource;

/**
* A {@link DataSource} configured in JEE Container available through JNDI
* under java:comp/env context.
* A {@link DataSource} configured in JEE Container available through JNDI.
*/
public class JEEManagedDataSource implements ManagedDataSource {
private DataSource jeeDatasource;

public JEEManagedDataSource(String dataSourceName) throws NamingException {
public JEEManagedDataSource(String datasourcesJndiKey, String dataSourceName) throws NamingException {
InitialContext ic = new InitialContext();
Context envCtx = (Context) ic.lookup("java:comp/env");
Context envCtx = (Context) ic.lookup(datasourcesJndiKey);
jeeDatasource = (DataSource) envCtx.lookup(dataSourceName);
}

Expand Down

0 comments on commit 1644e6a

Please sign in to comment.