From 0b575b94f469cdcdb24a6f86beee5e2c86285cb1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 1 Jun 2015 14:57:26 +1200 Subject: Support JNDI data source (fix #1229) --- src/org/traccar/database/DataManager.java | 52 ++++++++++++++++++------------- 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 2b7d15b3d..2698dd378 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -34,6 +34,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; +import javax.naming.InitialContext; import javax.sql.DataSource; import org.traccar.Context; import org.traccar.helper.DriverDelegate; @@ -76,31 +77,40 @@ public class DataManager { private void initDatabase(Properties properties) throws Exception { useNewDatabase = Boolean.valueOf(properties.getProperty("http.new")); + + String jndiName = properties.getProperty("database.jndi"); - // Load driver - String driver = properties.getProperty("database.driver"); - if (driver != null) { - String driverFile = properties.getProperty("database.driverFile"); + if (jndiName != null) { - if (driverFile != null) { - URL url = new URL("jar:file:" + new File(driverFile).getAbsolutePath() + "!/"); - URLClassLoader cl = new URLClassLoader(new URL[]{url}); - Driver d = (Driver) Class.forName(driver, true, cl).newInstance(); - DriverManager.registerDriver(new DriverDelegate(d)); - } else { - Class.forName(driver); + dataSource = (DataSource) new InitialContext().lookup(jndiName); + + } else { + + // Load driver + String driver = properties.getProperty("database.driver"); + if (driver != null) { + String driverFile = properties.getProperty("database.driverFile"); + + if (driverFile != null) { + URL url = new URL("jar:file:" + new File(driverFile).getAbsolutePath() + "!/"); + URLClassLoader cl = new URLClassLoader(new URL[]{url}); + Driver d = (Driver) Class.forName(driver, true, cl).newInstance(); + DriverManager.registerDriver(new DriverDelegate(d)); + } else { + Class.forName(driver); + } } + + // Initialize data source + ComboPooledDataSource ds = new ComboPooledDataSource(); + ds.setDriverClass(properties.getProperty("database.driver")); + ds.setJdbcUrl(properties.getProperty("database.url")); + ds.setUser(properties.getProperty("database.user")); + ds.setPassword(properties.getProperty("database.password")); + ds.setIdleConnectionTestPeriod(600); + ds.setTestConnectionOnCheckin(true); + dataSource = ds; } - - // Initialize data source - ComboPooledDataSource ds = new ComboPooledDataSource(); - ds.setDriverClass(properties.getProperty("database.driver")); - ds.setJdbcUrl(properties.getProperty("database.url")); - ds.setUser(properties.getProperty("database.user")); - ds.setPassword(properties.getProperty("database.password")); - ds.setIdleConnectionTestPeriod(600); - ds.setTestConnectionOnCheckin(true); - dataSource = ds; // Load statements from configuration String query; -- cgit v1.2.3