View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db.isql; 22 23 import java.sql.Connection; 24 25 import javax.naming.Context; 26 import javax.naming.InitialContext; 27 import javax.naming.NamingEnumeration; 28 import javax.servlet.http.HttpServletRequest; 29 import javax.servlet.http.HttpServletResponse; 30 import javax.sql.DataSource; 31 32 import org.apache.log4j.Category; 33 import org.apache.struts.action.Action; 34 import org.apache.struts.action.ActionForm; 35 import org.apache.struts.action.ActionForward; 36 import org.apache.struts.action.ActionMapping; 37 38 39 /*** 40 * Implementation of Action 41 */ 42 public class TestDataSourceAction extends Action { 43 /* 44 * Member Properties 45 */ 46 47 /*** 48 * Define a static Category variable for logging 49 */ 50 private static Category logger = Category.getInstance(TestDataSourceAction.class); 51 52 /* 53 * Constructors 54 */ 55 56 /*** 57 * Default constructor 58 */ 59 public TestDataSourceAction() { 60 } 61 62 /* 63 * Public Methods 64 */ 65 66 /*** 67 * Process the specified HTTP request, and create the corresponding HTTP 68 * response (or forward to another web component that will create it). 69 * Return an ActionForward instance describing where and how 70 * control should be forwarded, or null if the response has 71 * already been completed. 72 * 73 * @param mapping The ActionMapping used to select this instance 74 * @param actionForm The optional ActionForm bean for this request (if any) 75 * @param request The HTTP request we are processing 76 * @param response The HTTP response we are creating 77 * 78 * @exception IOException if an input/output error occurs 79 * @exception ServletException if a servlet exception occurs 80 */ 81 public ActionForward execute(ActionMapping mapping, ActionForm form, 82 HttpServletRequest request, HttpServletResponse response) 83 throws Exception { 84 logger.debug("METHOD_ENTRY: perform"); 85 86 IsqlForm isqlForm = (IsqlForm) form; 87 String dataSource = isqlForm.getDataSource(); 88 89 boolean check = false; 90 Connection conn = null; 91 92 try { 93 InitialContext ic = new InitialContext(); 94 Context envContext = (Context) ic.lookup("java:comp/env"); 95 96 if (envContext != null) { 97 logger.warn("listing JDBC context:"); 98 99 for (NamingEnumeration ne = envContext.list("jdbc"); 100 ne.hasMoreElements();) { 101 logger.warn(ne.nextElement().toString()); 102 } 103 } else { 104 throw new Exception("Not able to find context: java:comp/env"); 105 } 106 107 Object obj = envContext.lookup(dataSource); 108 DataSource ds = null; 109 110 if ((obj != null) && ds instanceof DataSource) { 111 ds = (DataSource) obj; 112 } else { 113 throw new Exception("Not able to find datasource: " 114 + dataSource); 115 } 116 117 conn = ds.getConnection(); 118 119 if (conn != null) { 120 check = true; 121 } 122 } catch (Exception e) { 123 logger.error(e.getMessage(), e); 124 } finally { 125 try { 126 conn.close(); 127 conn = null; 128 } catch (Exception e) { 129 ; 130 } 131 } 132 133 isqlForm.setResponse(String.valueOf(check)); 134 135 // Forward control to the specified success URI 136 return (mapping.findForward("success")); 137 } 138 }

This page was automatically generated by Maven