1 package org.ejen.ext.db; 22 23 import org.ejen.util.XSLUtil; 24 import org.ejen.util.DOMUtil; 25 import java.util.Hashtable ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.traversal.NodeIterator; 32 import org.apache.xalan.extensions.XSLProcessorContext; 33 import org.apache.xalan.extensions.ExpressionContext; 34 import org.apache.xalan.templates.ElemExtensionCall; 35 import org.apache.xml.utils.WrappedRuntimeException; 36 import org.apache.xpath.objects.XObject; 37 import org.apache.xpath.objects.XNodeSet; 38 39 96 public class BasicMetaDataConnection extends TableMetaDataNodeBuilder { 97 public static final String S_CI_CONNECTION_NODE_NAME = "connection"; 98 public static final String S_CI_DRIVER = "driver"; 99 public static final String S_CI_URL = "url"; 100 101 102 protected static Hashtable _conns = new Hashtable (); 103 104 107 protected BasicMetaDataConnection() {} 108 109 150 public static void open(XSLProcessorContext context, ElemExtensionCall elem) { 151 String name = XSLUtil.getAttribute(context, elem, "name", true); 152 String select = XSLUtil.getAttribute(context, elem, "select", true); 153 XObject xo = XSLUtil.evaluate(context, elem, select); 154 155 if (xo == null || !(xo instanceof XNodeSet)) { 156 throw new RuntimeException ("illegal 'select' attribute in 'connection' node"); 157 } 158 NodeIterator ni = null; 159 160 try { 161 ni = xo.nodeset(); 162 } catch (Exception e) { 163 throw new WrappedRuntimeException(e); 164 } 165 open(null, name, ni); 166 } 167 168 194 public static boolean open(ExpressionContext context, String name, NodeIterator connectionNi) { 195 name = XSLUtil.evaluate(context, name); 196 if (!close(null, name)) { 197 return false; 198 } 199 try { 200 boolean badNode = false; 201 Properties props = null; 202 String driver = null; 203 String url = null; 204 Node elt = connectionNi.nextNode(); 205 206 if (elt == null 207 || !S_CI_CONNECTION_NODE_NAME.equals(elt.getNodeName())) { 208 badNode = true; 209 } else { 210 driver = DOMUtil.getAttribute(elt, S_CI_DRIVER); 211 url = DOMUtil.getAttribute(elt, S_CI_URL); 212 if (driver == null || url == null) { 213 badNode = true; 214 } else { 215 props = DOMUtil.getChildProperties(elt); 216 } 217 } 218 if (badNode) { 219 throw new RuntimeException ("Illegal jdbc 'connection' node"); 220 } 221 Class.forName(driver); 222 _activeConn = DriverManager.getConnection(url, props); 223 _conns.put(name, _activeConn); 224 } catch (Exception e) { 225 appendErrorNode(e); 226 } 227 return _errors == null; 228 } 229 230 253 public static void close(XSLProcessorContext context, ElemExtensionCall elem) { 254 close(null, XSLUtil.getAttribute(context, elem, "name", true)); 255 } 256 257 282 public static boolean close(ExpressionContext context, String name) { 283 if (context != null) { 284 name = XSLUtil.evaluate(context, name); 285 } 286 _errors = null; 287 if (context != null) { 288 Connection conn = (Connection ) (_conns.get(name)); 289 290 if (conn != null) { 291 if (_activeConn == conn) { 292 _activeConn = null; 293 } 294 try { 295 conn.close(); 296 } catch (Exception e) { 297 appendErrorNode(e); 298 } 299 finally { 300 _conns.remove(name); 301 } 302 } else { 303 throw new RuntimeException ("No jdbc connection with this name: " 304 + name); 305 } 306 } 307 return _errors == null; 308 } 309 310 327 public static void setActive(XSLProcessorContext context, ElemExtensionCall elem) { 328 String name = XSLUtil.getAttribute(context, elem, "name", true); 329 Connection conn = (Connection ) (_conns.get(name)); 330 331 if (conn != null) { 332 _activeConn = conn; 333 } else { 334 throw new RuntimeException ("No jdbc connection with this name: " 335 + name); 336 } 337 } 338 339 356 public static void closeAll(XSLProcessorContext context, ElemExtensionCall elem) { 357 closeAll(null); 358 } 359 360 378 public static boolean closeAll(ExpressionContext context) { 379 _errors = null; 380 for (Enumeration e = _conns.elements(); e.hasMoreElements();) { 381 Connection conn = (Connection ) (e.nextElement()); 382 383 try { 384 conn.close(); 385 } catch (Exception f) { 386 appendErrorNode(f); 387 } 388 } 389 _conns.clear(); 390 return _errors == null; 391 } 392 } 393
| Popular Tags
|