package net.winstone.testApplication.servlets;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Check DataSource
 * 
 * @author Jerome Guibert
 */
public class JndiDataSourceServlet extends HttpServlet {

	private static final long serialVersionUID = -2418327420316254139L;
	public static final String JNDI_DATASOURCE = "jdbc/myDatasource";

	@Override
	protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
		try {
			final InitialContext initialContext = new InitialContext();
			final Context envCtx = (Context) initialContext.lookup("java:comp/env");
			final DataSource dataSource = (DataSource) envCtx.lookup(JndiDataSourceServlet.JNDI_DATASOURCE);
			if (dataSource == null) {
				throw new ServletException("Datasource 'jdbc/myDatasource' should be found");
			}
		} catch (final NamingException ex) {
			throw new ServletException(ex);
		}
	}
}

@@ -68,16 +68,74 @@
 ception;
+%0Aimport java.sql.Connection;%0Aimport java.sql.SQLException;
 %0A%0Aimport
@@ -1181,16 +1181,457 @@
 );%0A%09%09%09%7D%0A
+%09%09%09log(%22%5C%22Datasource 'jdbc/myDatasource' was found%22);%0A%09%09%09%0A%09%09%09Connection connection = null;%0A%09%09%09try %7B%0A%09%09%09%09connection = dataSource.getConnection();%0A%09%09%09%09log(%22%5C%22Datasource 'jdbc/myDatasource' connection was open%22);%0A%09%09%09%7D catch (SQLException e) %7B%0A%09%09%09%7Dfinally %7B%0A%09%09%09%09if (connection!=null) %7B%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09connection.close();%0A%09%09%09%09%09%09log(%22%5C%22Datasource 'jdbc/myDatasource' connection was close%22);%0A%09%09%09%09%09%7D catch (SQLException e) %7B%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%7D catc

add information testing