1 48 49 50 package org.exolab.jms.tools.admin; 51 52 import java.awt.Component ; 53 import java.sql.Connection ; 54 import java.util.Enumeration ; 55 import java.util.Hashtable ; 56 57 import javax.naming.Context ; 58 import javax.naming.InitialContext ; 59 import javax.swing.JFileChooser ; 60 import javax.swing.JOptionPane ; 61 import javax.transaction.TransactionManager ; 62 63 import org.exolab.jms.authentication.User; 64 import org.exolab.jms.client.JmsDestination; 65 import org.exolab.jms.client.JmsQueue; 66 import org.exolab.jms.client.JmsTopic; 67 import org.exolab.jms.config.Configuration; 68 import org.exolab.jms.config.ConfigurationManager; 69 import org.exolab.jms.config.DatabaseConfiguration; 70 import org.exolab.jms.persistence.DatabaseService; 71 import org.exolab.jms.persistence.PersistenceException; 72 73 74 88 public class OfflineConnection extends AbstractAdminConnection { 89 90 private Component _parent; 92 93 private Context _context = null; 95 96 106 public OfflineConnection(Component parent) 107 throws OfflineConnectionException { 108 try { 109 if (_instance == null) { 110 _parent = parent; 111 112 Configuration config = ConfigurationManager.getConfig(); 113 114 DatabaseConfiguration dbconfig = 115 config.getDatabaseConfiguration(); 116 117 if (dbconfig.getRdbmsDatabaseConfiguration() != null) { 120 DatabaseService.instance().getAdapter(); 121 _instance = this; 122 } else { 123 JFileChooser chooser = new JFileChooser ("."); 124 chooser.setDialogTitle 125 ("Select OpenJMS Database to connect to"); 126 chooser.setFileFilter(new DatabaseFilter()); 127 int returnVal = chooser.showOpenDialog(parent); 128 129 if (returnVal == JFileChooser.APPROVE_OPTION) { 130 DatabaseService.instance().getAdapter(); 131 _instance = this; 132 } 133 } 134 } else { 135 throw new org.exolab.jms.tools.admin.OfflineConnectionException("Already connected"); 136 } 137 } catch (Exception err) { 138 throw new org.exolab.jms.tools.admin.OfflineConnectionException 139 ("Database Error: " + err.getMessage()); 140 } 141 } 142 143 public void close() { 145 DatabaseService.getAdapter().close(); 146 _instance = null; 147 } 148 149 public boolean addDurableConsumer(String topic, String name) { 151 boolean result = false; 152 Connection connection = null; 153 154 try { 155 connection = DatabaseService.getConnection(); 156 DatabaseService.getAdapter().addDurableConsumer(connection, topic, 157 name); 158 connection.commit(); 159 result = true; 160 161 } catch (PersistenceException exception) { 162 try { 163 connection.rollback(); 164 } catch (Exception nested) { 165 } 167 } catch (Exception exception) { 168 } finally { 170 if (connection != null) { 171 try { 172 connection.close(); 173 } catch (Exception nested) { 174 } 176 } 177 } 178 179 return result; 180 } 181 182 public boolean removeDurableConsumer(String name) { 184 boolean result = false; 185 Connection connection = null; 186 187 try { 188 connection = DatabaseService.getConnection(); 189 190 DatabaseService.getAdapter().removeDurableConsumer(connection, name); 191 connection.commit(); 192 result = true; 193 194 } catch (PersistenceException exception) { 195 try { 196 connection.rollback(); 197 } catch (Exception nested) { 198 } 200 } catch (Exception exception) { 201 } finally { 203 if (connection != null) { 204 try { 205 connection.close(); 206 } catch (Exception nested) { 207 } 209 } 210 } 211 212 return result; 213 } 214 215 public boolean unregisterConsumer(String name) { 217 return false; 218 } 219 220 public boolean isConnected(String name) { 222 return false; 223 } 224 225 public Enumeration getAllDestinations() { 227 Enumeration result = null; 228 Connection connection = null; 229 230 try { 231 connection = DatabaseService.getConnection(); 232 233 result = DatabaseService.getAdapter().getAllDestinations( 234 connection); 235 connection.commit(); 236 } catch (PersistenceException exception) { 237 try { 238 connection.rollback(); 239 } catch (Exception nested) { 240 } 242 } catch (Exception exception) { 243 } finally { 245 if (connection != null) { 246 try { 247 connection.close(); 248 } catch (Exception nested) { 249 } 251 } 252 } 253 254 return result; 255 } 256 257 public boolean addDestination(String destination, boolean isQueue) { 259 Connection connection = null; 260 boolean success = false; 261 262 try { 263 connection = DatabaseService.getConnection(); 264 265 DatabaseService.getAdapter().addDestination(connection, 266 destination, isQueue); 267 if (_context == null) { 268 Hashtable props = new Hashtable (); 271 props.put(Context.INITIAL_CONTEXT_FACTORY, 272 "org.exolab.jms.jndi.intravm.IntravmJndiServer"); 273 _context = new InitialContext (props); 274 } 275 276 Object ob = (isQueue ? (Object ) (new JmsQueue(destination)) : 277 (Object ) (new JmsTopic(destination))); 278 279 if (ob instanceof JmsQueue) { 280 ((JmsDestination) ob).setPersistent(true); 281 } 282 283 _context.rebind(destination, ob); 284 connection.commit(); 285 success = true; 286 } catch (PersistenceException exception) { 287 System.err.println("Failed to add destination " + destination + 288 " b/c " + exception.toString()); 289 try { 290 connection.rollback(); 291 } catch (Exception nested) { 292 } 294 } catch (javax.naming.NamingException err) { 295 System.err.println("Failed to add " + destination + 296 " in JNDI context"); 297 try { 298 connection.rollback(); 299 } catch (Exception nested) { 300 } 302 } catch (Exception exception) { 303 } finally { 305 if (connection != null) { 306 try { 307 connection.close(); 308 } catch (Exception nested) { 309 } 311 } 312 } 313 314 return success; 315 } 316 317 public int getDurableConsumerMessageCount(String topic, String name) { 319 int count = -1; 320 Connection connection = null; 321 322 try { 323 connection = DatabaseService.getConnection(); 324 325 count = DatabaseService.getAdapter().getDurableConsumerMessageCount( 326 connection, topic, name); 327 connection.commit(); 328 } catch (PersistenceException exception) { 329 System.err.println("Failed to get message count for " + topic + 330 " b/c " + exception.toString()); 331 try { 332 connection.rollback(); 333 } catch (Exception nested) { 334 } 336 } catch (Exception exception) { 337 } finally { 339 if (connection != null) { 340 try { 341 connection.close(); 342 } catch (Exception nested) { 343 } 345 } 346 } 347 348 return count; 349 } 350 351 public int getQueueMessageCount(String queue) { 353 int count = -1; 354 Connection connection = null; 355 356 try { 357 connection = DatabaseService.getConnection(); 358 359 count = DatabaseService.getAdapter().getQueueMessageCount( 360 connection, queue); 361 connection.commit(); 362 } catch (PersistenceException exception) { 363 System.err.println("Failed to get message count for " + queue + 364 " b/c " + exception.toString()); 365 try { 366 connection.rollback(); 367 } catch (Exception nested) { 368 } 370 } catch (Exception exception) { 371 } finally { 373 if (connection != null) { 374 try { 375 connection.close(); 376 } catch (Exception nested) { 377 } 379 } 380 } 381 382 return count; 383 } 384 385 public boolean durableConsumerExists(String name) { 387 boolean result = false; 388 Connection connection = null; 389 390 try { 391 connection = DatabaseService.getConnection(); 392 393 result = DatabaseService.getAdapter().durableConsumerExists( 394 connection, name); 395 connection.commit(); 396 } catch (PersistenceException exception) { 397 System.err.println("Failed on consumer exists for " + name + 398 " b/c " + exception.toString()); 399 try { 400 connection.rollback(); 401 } catch (Exception nested) { 402 } 404 } catch (Exception exception) { 405 } finally { 407 if (connection != null) { 408 try { 409 connection.close(); 410 } catch (Exception nested) { 411 } 413 } 414 } 415 416 return result; 417 } 418 419 public Enumeration getDurableConsumers(String topic) { 421 Enumeration result = null; 422 Connection connection = null; 423 424 try { 425 connection = DatabaseService.getConnection(); 426 427 result = DatabaseService.getAdapter().getDurableConsumers( 428 connection, topic); 429 connection.commit(); 430 } catch (PersistenceException exception) { 431 System.err.println("Failed on getDurableConsumers for " + topic + 432 " b/c " + exception.toString()); 433 try { 434 connection.rollback(); 435 } catch (Exception nested) { 436 } 438 } catch (Exception exception) { 439 } finally { 441 if (connection != null) { 442 try { 443 connection.close(); 444 } catch (Exception nested) { 445 } 447 } 448 } 449 450 return result; 451 } 452 453 public boolean removeDestination(String destination) { 455 boolean result = false; 456 Connection connection = null; 457 458 try { 459 connection = DatabaseService.getConnection(); 460 461 DatabaseService.getAdapter().removeDestination(connection, 462 destination); 463 if (_context == null) { 464 Hashtable props = new Hashtable (); 467 props.put(Context.INITIAL_CONTEXT_FACTORY, 468 "org.exolab.jms.jndi.intravm.IntravmJndiServer"); 469 _context = new InitialContext (props); 470 } 471 _context.unbind(destination); 472 connection.commit(); 473 result = true; 474 } catch (PersistenceException exception) { 475 System.err.println("Failed on getDurableConsumers for " + 476 destination + " b/c " + exception.toString()); 477 try { 478 connection.rollback(); 479 } catch (Exception nested) { 480 } 482 } catch (javax.naming.NamingException err) { 483 System.err.println("Failed to removeDestination " + destination + 484 " b/c" + err.toString()); 485 try { 486 connection.rollback(); 487 } catch (Exception nested) { 488 } 490 } catch (Exception exception) { 491 } finally { 493 if (connection != null) { 494 try { 495 connection.close(); 496 } catch (Exception nested) { 497 } 499 } 500 } 501 502 return result; 503 } 504 505 public int purgeMessages() { 507 return DatabaseService.getAdapter().purgeMessages(); 508 } 509 510 public void stopServer() { 512 JOptionPane.showMessageDialog 513 (_parent, "Not available in offline mode", 514 "Shutdown Error", JOptionPane.ERROR_MESSAGE); 515 } 516 517 public boolean addUser(String username, String password) { 519 Connection connection = null; 520 boolean success = false; 521 522 try { 523 connection = DatabaseService.getConnection(); 524 525 DatabaseService.getAdapter().addUser(connection, 526 new User(username, password)); 527 connection.commit(); 528 success = true; 529 } catch (PersistenceException exception) { 530 System.err.println("Failed to add user " + username + 531 exception.toString()); 532 try { 533 connection.rollback(); 534 } catch (Exception nested) { 535 } 537 } catch (Exception exception) { 538 } finally { 540 if (connection != null) { 541 try { 542 connection.close(); 543 } catch (Exception nested) { 544 } 546 } 547 } 548 549 return success; 550 } 551 552 public boolean changePassword(String username, String password) { 554 Connection connection = null; 555 boolean success = false; 556 557 try { 558 connection = DatabaseService.getConnection(); 559 560 DatabaseService.getAdapter().updateUser(connection, 561 new User(username, password)); 562 connection.commit(); 563 success = true; 564 } catch (PersistenceException exception) { 565 System.err.println("Failed to add user " + username + 566 exception.toString()); 567 try { 568 connection.rollback(); 569 } catch (Exception nested) { 570 } 572 } catch (Exception exception) { 573 } finally { 575 if (connection != null) { 576 try { 577 connection.close(); 578 } catch (Exception nested) { 579 } 581 } 582 } 583 584 return success; 585 } 586 587 public boolean removeUser(String username) { 589 boolean result = false; 590 Connection connection = null; 591 592 try { 593 connection = DatabaseService.getConnection(); 594 595 DatabaseService.getAdapter().removeUser(connection, 596 new User(username, null)); 597 connection.commit(); 598 result = true; 599 } catch (PersistenceException exception) { 600 System.err.println("Failed on remove user for " + 601 username + exception.toString()); 602 try { 603 connection.rollback(); 604 } catch (Exception nested) { 605 } 607 } catch (Exception exception) { 608 } finally { 610 if (connection != null) { 611 try { 612 connection.close(); 613 } catch (Exception nested) { 614 } 616 } 617 } 618 619 return result; 620 } 621 622 public Enumeration getAllUsers() { 624 Enumeration result = null; 625 Connection connection = null; 626 627 try { 628 connection = DatabaseService.getConnection(); 629 630 result = DatabaseService.getAdapter().getAllUsers( 631 connection); 632 connection.commit(); 633 } catch (PersistenceException exception) { 634 System.err.println("Failed on getAllUsers " 635 + exception.toString()); 636 try { 637 connection.rollback(); 638 } catch (Exception nested) { 639 } 641 } catch (Exception exception) { 642 } finally { 644 if (connection != null) { 645 try { 646 connection.close(); 647 } catch (Exception nested) { 648 } 650 } 651 } 652 653 return result; 654 } 655 } | Popular Tags |