http-service: use jndi provided mongo

This commit is contained in:
Adam
2019-08-08 16:26:51 -04:00
parent 05ca96c3db
commit 277a3ccd35
5 changed files with 28 additions and 4 deletions

View File

@@ -125,7 +125,8 @@
<dependency> <dependency>
<groupId>org.mongodb</groupId> <groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId> <artifactId>mongodb-driver-sync</artifactId>
<version>3.10.1</version> <version>3.10.2</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@@ -32,6 +32,7 @@ import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.naming.NamingException;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
@@ -55,6 +56,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.jndi.JndiTemplate;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.sql2o.Sql2o; import org.sql2o.Sql2o;
import org.sql2o.converters.Converter; import org.sql2o.converters.Converter;
@@ -160,9 +162,21 @@ public class SpringBootWebApplication extends SpringBootServletInitializer
} }
@Bean @Bean
public MongoClient mongoClient(@Value("${mongo.host}") String host) public MongoClient mongoClient(@Value("${mongo.host:}") String host, @Value("${mongo.jndiName:}") String jndiName) throws NamingException
{ {
return MongoClients.create(host); if (!Strings.isNullOrEmpty(jndiName))
{
JndiTemplate jndiTemplate = new JndiTemplate();
return jndiTemplate.lookup(jndiName, MongoClient.class);
}
else if (!Strings.isNullOrEmpty(host))
{
return MongoClients.create(host);
}
else
{
throw new RuntimeException("Either mongo.host or mongo.jndiName must be set");
}
} }
private static DataSource getDataSource(DataSourceProperties dataSourceProperties) private static DataSource getDataSource(DataSourceProperties dataSourceProperties)

View File

@@ -26,6 +26,11 @@ datasource:
username: runelite username: runelite
password: runelite password: runelite
# Development mongo
mongo:
jndiName:
host: mongodb://localhost:27017
# Development oauth callback (without proxy) # Development oauth callback (without proxy)
oauth: oauth:
callback: http://localhost:8080/account/callback callback: http://localhost:8080/account/callback

View File

@@ -31,7 +31,7 @@ redis:
host: http://localhost:6379 host: http://localhost:6379
mongo: mongo:
host: mongodb://localhost:27017 jndiName: java:comp/env/mongodb/runelite
# Twitter client for feed # Twitter client for feed
runelite: runelite:

View File

@@ -15,3 +15,7 @@ datasource:
driverClassName: org.h2.Driver driverClassName: org.h2.Driver
type: org.h2.jdbcx.JdbcDataSource type: org.h2.jdbcx.JdbcDataSource
url: jdbc:h2:mem:xptracker url: jdbc:h2:mem:xptracker
mongo:
jndiName:
host: mongodb://localhost:27017