Skip to content

Commit

Permalink
Added basic support for jndi lookup of application server managed data
Browse files Browse the repository at this point in the history
sources.
  • Loading branch information
skenny committed Mar 19, 2015
1 parent db0fa3e commit a4d9098
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions WebContent/conf/Spiracle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ java.password=test

jdbc.fetchsize=25

default.connection=jndi

spring.path=/path/to/spring-context.xml

application.loggingEnabled=True
Expand Down
6 changes: 6 additions & 0 deletions src/com/waratek/spiracle/init/SpiracleInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void contextInitialized(ServletContextEvent arg0) {
logServerInfo(application);
ComboPooledDataSource ds = getConnectionPool(props);
setConnectionPool(application, ds);
setDefaultConnection(application, props);
setFetchSize(application, props);
}

Expand Down Expand Up @@ -128,6 +129,11 @@ private void setFetchSize(ServletContext application, Properties props) {
}
}

private void setDefaultConnection(ServletContext application, Properties props) {
String defaultConnection = (String) props.get("default.connection");
application.setAttribute("defaultConnection", defaultConnection);
}

void logServerInfo(ServletContext application) {
logger.info("Application Server Name: " + application.getServerInfo());
logger.info("Application Context Path:" + application.getRealPath(""));
Expand Down
30 changes: 30 additions & 0 deletions src/com/waratek/spiracle/sql/jndi/CreateJndiConnectionPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.waratek.spiracle.sql.jndi;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

@WebListener
public class CreateJndiConnectionPool implements ServletContextListener {
private static final Logger logger = Logger.getLogger(CreateJndiConnectionPool.class);

@Resource(name = "jdbc/oracle")
DataSource ds;

@Override
public void contextDestroyed(ServletContextEvent arg0) {

}

@Override
public void contextInitialized(ServletContextEvent arg0) {
ServletContext application = arg0.getServletContext();
application.setAttribute("jndiConnectionPool", ds);
logger.info("Added jndi connection pool " + ds + " to application context.");
}
}
4 changes: 4 additions & 0 deletions src/com/waratek/spiracle/sql/util/ConnectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.context.support.FileSystemXmlApplicationContext;
Expand All @@ -42,6 +43,9 @@ public static Connection getConnection(ServletContext application, String connec
FileSystemXmlApplicationContext context = (FileSystemXmlApplicationContext)application.getAttribute("springContext");
DriverManagerDataSource dmds = (DriverManagerDataSource)context.getBean("dataSource");
con = dmds.getConnection();
} else if(connectionType.equals("jndi")) {
DataSource ds = (DataSource) application.getAttribute("jndiConnectionPool");
con = ds.getConnection();
}

logger.info("Returning connection: " + con.toString());
Expand Down
3 changes: 2 additions & 1 deletion src/com/waratek/spiracle/sql/util/SelectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static void executeQuery(String sql, ServletContext application, HttpServ
String connectionType = null;
Connection con = null;
int fetchSize = (Integer) application.getAttribute("fetchSize");
String defaultConnection = (String) application.getAttribute("defaultConnection");

PreparedStatement stmt = null;
ResultSet rs = null;
Expand All @@ -50,7 +51,7 @@ public static void executeQuery(String sql, ServletContext application, HttpServ
try {
//Checking if connectionType is set, defaulting it to c3p0 if not set.
if(request.getParameter("connectionType") == null) {
connectionType = "c3p0";
connectionType = defaultConnection;
} else {
connectionType = request.getParameter("connectionType");
}
Expand Down

0 comments on commit a4d9098

Please sign in to comment.