1 2 3 package Jt; 4 import java.io.*; 5 import java.sql.*; 6 import javax.sql.*; 7 import java.text.*; 8 import Jt.jndi.*; 9 10 13 14 public class JtJDBCAdapter extends JtAdapter { 15 private String user; 16 private String password; 17 private String url; 18 private String driver; 19 private transient Connection connection = null; 20 private String base; 21 int n = 0; 22 private transient Statement myStatement; 23 private String datasource = null; 24 private transient JtJNDIAdapter jndiAdapter = null; 25 private transient DataSource dataSource = null; 26 private transient boolean initted = false; 27 28 private DataSource locateDataSource (String datasource) { 29 JtMessage msg = new JtMessage ("JtLOOKUP"); 30 31 32 if (datasource == null) 33 return (null); 34 35 msg.setMsgContent (datasource); 36 jndiAdapter = new JtJNDIAdapter (); 37 38 return ((DataSource) jndiAdapter.processMessage (msg)); 39 40 } 41 42 private void initial () { 43 44 45 46 if (datasource != null) { 47 dataSource = locateDataSource (datasource); 48 49 if (dataSource == null) { 50 handleError ("JtJDBAdapter.connect: unable to locate datasource:" + 51 datasource); 52 connection = null; 53 return; 54 55 } 56 57 try { 58 connection = dataSource.getConnection (); 59 } catch (Exception ex) { 60 handleException (ex); 61 } 62 } 63 64 } 65 66 67 void connect () 68 { 69 handleTrace ("JtJDBCAdapter.connect ...."); 70 71 72 if (datasource != null) { 73 74 if (dataSource == null) { 75 handleWarning ("JtJDBAdapter.connect: unable to connect to Datasource: " + 76 datasource); 77 connection = null; 78 return; 79 80 } 81 82 try { 83 connection = dataSource.getConnection (); 84 handleTrace ("JtJDBCAdapter.connect: using data source ...."); 85 86 } catch (Exception ex) { 87 handleException (ex); 88 } 89 return; 90 } 91 92 if (connection != null) { 93 return; 94 } 95 96 if (driver == null) { 97 handleError ("connect: null attribute (driver)"); 98 return; 99 } 100 101 if (url == null) { 102 handleError ("connect: null attribute (url)"); 103 return; 104 } 105 106 try 107 { 108 Class.forName(driver); 109 if (user == null) 110 connection = DriverManager.getConnection(url); 111 else 112 connection = DriverManager.getConnection(url, 113 user, password); 114 } 116 catch(ClassNotFoundException cnfe) 117 { 118 handleException (cnfe); 119 } 120 catch(SQLException sqle) 121 { 122 handleException (sqle); 123 } 124 } 125 126 128 void close () { 129 130 if (connection == null) 131 return; 132 133 try { 134 connection.close (); 135 } 136 catch (SQLException sqle) { 137 handleException (sqle); 138 } 139 finally { 140 connection = null; 141 } 142 } 143 144 146 ResultSet execute_query (String query) { 147 148 if (query == null) 149 return (null); 150 151 if (connection == null) { 152 handleError ("execute_query: no database connection"); 153 return (null); 154 } 155 156 try { 157 myStatement = connection.createStatement (); 158 ResultSet results = myStatement.executeQuery (query); 159 160 if (results == null) myStatement.close (); 162 163 return (results); 165 } 167 catch(SQLException sqle) 168 { 169 handleTrace ("Message: " + sqle.getMessage ()); 170 handleTrace ("SQL State: " + sqle.getSQLState ()); 171 handleTrace ("Vendor code: " + sqle.getErrorCode ()); 172 handleException (sqle); 173 return (null); 174 } 175 185 186 } 187 188 Object execute_update (String query) { 189 int cnt = 0; 190 Statement myStatement = null; 191 192 if (query == null) 193 return (null); 194 195 if (connection == null) { 196 handleError ("execute_query: no database connection"); 197 return (null); 198 } 199 200 try { 201 myStatement = connection.createStatement (); 202 cnt = myStatement.executeUpdate (query); 203 myStatement.close (); 204 return new Integer (cnt); 205 } 206 catch(SQLException sqle) 207 { 208 handleTrace ("Message: " + sqle.getMessage ()); 209 handleTrace ("SQL State: " + sqle.getSQLState ()); 210 handleTrace ("Vendor code: " + sqle.getErrorCode ()); 211 handleException (sqle); 212 return (null); 213 } 214 215 } 216 217 219 int show_output (ResultSet res) { 220 221 int ncol, n; 222 ResultSetMetaData meta; 223 224 if (res == null) 225 return (0); 226 227 try { 228 meta = res.getMetaData (); 229 } 230 catch (SQLException e) { 231 handleException (e); 232 return (0); 233 } 234 235 236 237 try { 238 while (res.next ()) { 239 ncol = meta.getColumnCount (); 240 handleTrace ("output:ncol:"+ ncol); 241 for (n = 1; n <= ncol; n++) { 242 handleTrace ("output:" + 243 meta.getColumnName (n) + ":" + 244 res.getString (n)); 245 } 246 } 247 return (1); 248 } 249 catch (SQLException e) { 250 handleException (e); 251 return (0); 252 } 253 } 254 255 257 int map (ResultSetMetaData meta, 258 ResultSet res, String obj_name) { 259 260 int ncol, n; 261 DateFormat df = DateFormat.getDateInstance(); 262 263 if ((meta == null) || (res == null) || 264 (obj_name == null)) 265 return (0); 266 267 try { 268 ncol = meta.getColumnCount (); 269 for (n = 1; n <= ncol; n++) { 270 if (meta.getColumnTypeName(n).equals ("DATE") || 272 meta.getColumnTypeName(n).equals ("DATETIME")) { 273 Date date = res.getDate (n); 274 if (date == null || date.equals ("")) 275 continue; String sdate = df.format (date); 277 this.setValue (obj_name, 278 meta.getColumnName (n), 279 sdate); 280 continue; 281 } 282 this.setValue (obj_name, 283 meta.getColumnName (n), 284 res.getString (n)); 285 290 } 291 292 return (1); 293 } 294 catch (SQLException e) { 295 handleException (e); 296 return (0); 297 } 298 } 299 300 302 void map_query (String query, JtMessage me) { 303 ResultSetMetaData mdata; 304 ResultSet res; 305 String name; 307 JtMessage event; 308 JtObject tmp; 309 310 313 handleTrace ("JtJDBCAdapter.map_query ...."); 314 315 if (query == null) 316 return; 317 318 if (base == null) 319 return; 320 321 323 res = execute_query (query); 324 325 if (res == null) { 326 handleTrace ("map_query:res:null"); 327 return; 329 } 330 331 try { 332 mdata = res.getMetaData (); 333 } 334 catch (SQLException e) { 335 handleException (e); 336 return; 337 } 338 339 try { 340 if (mdata == null) { 341 myStatement.close (); 342 return; 344 } 345 } catch (SQLException e) { 346 handleException (e); 347 return; 348 } 349 350 try { 351 while (res.next ()) { 352 n++; 353 name = "" + n; 354 handleTrace ("creating object ..." + name); 355 tmp = (JtObject) this.createObject (base, name); 356 if (map (mdata, res, name) == 0) 357 continue; 358 event = new JtMessage (); 359 event.setMsgContent (tmp); 361 event.setMsgId ("JtOBJECT"); 362 event.setMsgSubject (me.getMsgSubject ()); 363 365 366 if (me.getMsgReplyTo () != null) { 368 this.sendMessage (me.getMsgReplyTo (), event); 372 } 373 374 } 375 myStatement.close (); 377 } 378 catch (SQLException e) { 379 handleException (e); 380 } 381 } 382 383 private PreparedStatement prepareStatement (String query) 384 { 385 PreparedStatement pst = null; 386 387 if (connection == null) { 388 handleError ("JtJDBCAdapter.prepareStatement: invalid connection (null)"); 389 return (null); 390 } 391 392 if (query == null) 393 return (null); 394 395 try { 396 pst = connection.prepareStatement (query); 397 } 398 catch (Exception ex) { 399 handleException (ex); 400 } 401 return (pst); 402 } 403 404 405 private Object executePreparedUpdate (PreparedStatement pst ) 406 { 407 int cnt = 0; 408 409 if (pst == null) { 410 return (null); 411 } 412 413 if (connection == null) { 414 handleError ("JtJDBCAdapter.prepareStatement: invalid connection (null)"); 415 return (null); 416 } 417 try { 418 cnt = pst.executeUpdate (); 419 return (new Integer (cnt)); 420 } catch (Exception ex) { 421 handleException (ex); 422 return (ex); 423 } 424 425 } 426 427 444 445 public Object processMessage (Object message) { 446 String content; 447 String query; 448 JtMessage e = (JtMessage) message; 449 Object reply; 450 451 if (e == null || (e.getMsgId() == null)) 452 return (null); 453 454 if (!initted) { 455 initial (); 456 initted = true; 457 } 458 459 if (e.getMsgId().equals("JtCONNECT")) { 461 connect (); 462 return (connection); 463 } 464 465 if (e.getMsgId().equals("JtQUERY")) { 467 connect (); 468 query = (String ) e.getMsgContent(); 469 show_output (execute_query (query)); 470 473 return (null); 474 } 475 476 if (e.getMsgId().equals("JtEXECUTE_QUERY")) { 477 query = (String ) e.getMsgContent(); 478 connect (); 479 reply = execute_query (query); 480 return (reply); 484 485 } 486 487 488 if (e.getMsgId().equals("JtPREPARE_STATEMENT")) { 489 query = (String ) e.getMsgContent(); 490 connect (); 491 reply = prepareStatement (query); 492 return (reply); 495 } 496 497 498 if (e.getMsgId().equals("JtEXECUTE_PREPARED_UPDATE")) { 499 connect (); 501 reply = executePreparedUpdate 502 ((PreparedStatement) e.getMsgContent()); 503 return (reply); 506 } 507 508 if (e.getMsgId().equals("JtUPDATE")) { 510 query = (String ) e.getMsgContent(); 511 connect (); 512 reply = execute_update (query); 513 return (reply); 516 } 517 518 if (e.getMsgId().equals("JtMAP")) { 520 query = (String ) e.getMsgContent(); 521 if (query == null) 522 return (null); 523 connect (); 524 map_query (query, e); 525 return (null); 528 } 529 530 if (e.getMsgId().equals("JtCLOSE") || e.getMsgId().equals ("JtREMOVE")) { 532 close (); 533 return (null); 534 } 535 handleError 536 ("processMessage: invalid message id:"+ 537 e.getMsgId()); 538 return (null); 539 } 540 541 545 546 public void setUser (String newUser) { 547 user = newUser; 548 } 549 550 551 554 555 public String getUser () { 556 return (user); 557 } 558 559 560 564 565 public void setPassword (String newPassword) { 566 password = newPassword; 567 } 568 569 570 573 574 public String getPassword () { 575 return (password); 576 } 577 578 579 583 584 public void setUrl (String newUrl) { 585 url = newUrl; 586 } 587 588 591 592 public String getUrl () { 593 return (url); 594 } 595 596 597 601 602 public void setDatasource (String datasource) { 603 this.datasource = datasource; 604 } 605 606 609 610 public String getDatasource () { 611 return (datasource); 612 } 613 614 615 619 620 public void setDriver (String newDriver) { 621 driver = newDriver; 622 } 623 624 627 public String getDriver () { 628 return (driver); 629 } 630 631 634 635 public void setBase (String newBase) { 636 base = newBase; 637 } 638 639 642 643 public String getBase () { 644 return (base); 645 } 646 647 648 651 652 public void setConnection (Connection newConnection) { 653 connection = newConnection; 654 } 655 656 659 660 public Connection getConnection () { 661 return (connection); 662 } 663 664 665 private static void test () { 666 JtObject main; 667 String query; 668 669 main = new JtObject (); 670 main.createObject ("Jt.JtMessage", "message"); 672 673 main.createObject ("Jt.JtJDBCAdapter", "database"); 674 main.setValue ("database", "objTrace", "1"); 675 677 679 680 query = "select email from member where email='SUPPORT@FSW.COM';"; 681 682 main.setValue ("message", "msgId", "JtQUERY"); 683 main.setValue ("message", "msgContent", query); 684 main.sendMessage ("database", "message"); 685 main.removeObject ("database"); 686 main.removeObject ("message"); 687 688 } 689 690 702 703 public static void main (String [] args) { 704 test (); 705 } 706 }
| Popular Tags
|