1 21 package org.jacorb.imr.util; 22 23 import org.apache.avalon.framework.logger.Logger; 24 import org.apache.avalon.framework.configuration.*; 25 26 import org.jacorb.imr.*; 27 import org.jacorb.imr.AdminPackage.*; 28 29 import java.util.*; 30 31 import javax.swing.tree.*; 32 import javax.swing.*; 33 import javax.swing.table.*; 34 35 43 44 public class ImRModel 45 { 46 private Admin m_admin; 47 private org.jacorb.orb.ORB m_orb; 48 49 private org.jacorb.config.Configuration configuration = null; 50 51 52 private Logger logger = null; 53 54 private ServerInfo[] m_servers; 55 private ImRInfo m_imr_info; 56 57 private DefaultMutableTreeNode m_top_node; 58 private JTree m_tree; 59 private DefaultTreeModel m_tree_model; 60 61 private ImRPOATableModel m_poa_model; 62 private ImRServerTableModel m_server_model; 63 64 private Hashtable m_server_names; 65 66 private JComboBox m_host_selector; 67 private DefaultComboBoxModel m_host_selector_model; 68 private Hashtable m_host_names; 69 70 private RefreshThread m_refresh_thread; 71 protected int m_current_refresh_interval = 20000; 72 protected boolean m_refresh_disabled = false; 73 74 private Vector m_server_nodes; 75 private Vector m_poa_nodes; 77 80 81 public ImRModel() 82 { 83 m_orb = (org.jacorb.orb.ORB)org.omg.CORBA.ORB.init(new String [0], null); 84 configuration = m_orb.getConfiguration(); 85 logger = configuration.getNamedLogger("jacorb.imr.model"); 86 87 try 88 { 89 m_admin = AdminHelper.narrow( m_orb.resolve_initial_references("ImplementationRepository")); 90 } 91 catch( org.omg.CORBA.ORBPackage.InvalidName in ) 92 { 93 if (logger.isWarnEnabled()) 94 logger.warn("Could not contact Impl. Repository!"); 95 return; 96 } 97 98 fetchImRInfo(); 99 100 m_top_node = new DefaultMutableTreeNode(m_imr_info); 101 m_tree_model = new DefaultTreeModel(m_top_node, false); 102 m_tree = new JTree(m_tree_model); 103 104 m_server_model = new ImRServerTableModel(this); 105 m_poa_model = new ImRPOATableModel(); 106 107 m_server_names = new Hashtable(); 108 109 m_server_nodes = new Vector(); 110 m_poa_nodes = new Vector(); 111 112 m_host_names = new Hashtable(); 113 m_host_selector_model = new DefaultComboBoxModel(); 114 m_host_selector = new JComboBox(m_host_selector_model); 115 m_host_selector.setEditable(true); 116 117 fetchServers(); 118 m_tree.expandRow(0); 119 120 m_refresh_thread = new RefreshThread(m_current_refresh_interval); 121 } 122 123 128 public void connectTo(String ior_url) 129 { 130 try 131 { 132 m_admin = AdminHelper.narrow( m_orb.resolve_initial_references("ImplementationRepository")); 133 } 134 catch( org.omg.CORBA.ORBPackage.InvalidName in ) 135 { 136 if (logger.isWarnEnabled()) 137 logger.warn("Could not contact Impl. Repository!"); 138 return; 139 } 140 141 fetchImRInfo(); 142 143 m_top_node.setUserObject(m_imr_info); 144 145 fetchServers(); 146 147 setRefreshInterval(m_current_refresh_interval); 148 } 149 150 155 public JComboBox getHostSelector(){ 156 return m_host_selector; 157 } 158 159 164 public TableModel getPOATableModel(){ 165 return m_poa_model; 166 } 167 168 173 public TableModel getServerTableModel(){ 174 return m_server_model; 175 } 176 177 183 public void poaTableForServer(ServerInfo server){ 184 m_poa_model.setPOAs(server.poas); 185 } 186 187 190 public void fetchServers(){ 191 m_servers = m_admin.list_servers(); 192 193 m_server_model.setServers(m_servers); 194 195 m_server_names.clear(); 196 for (int _i = 0; _i < m_servers.length; _i++) 197 m_server_names.put(m_servers[_i].name, new Integer (_i)); 198 199 String _server = m_poa_model.getServerName(); 200 if (_server != null) 201 m_poa_model.setPOAs(m_servers[indexForServerName(_server)].poas); 202 203 buildTree(); 204 buildHostSelectorModel(); 205 } 206 207 212 public void removeServer(String name){ 213 removeServer(indexForServerName(name)); 214 } 215 216 221 public void removeServer(int server_row){ 222 try{ 223 m_admin.unregister_server(m_servers[server_row].name); 224 }catch(Exception _e){ 225 handleException (_e); 226 } 227 228 fetchServers(); 229 } 230 231 236 public void holdServer(String name){ 237 holdServer(indexForServerName(name)); 238 } 239 240 245 public void holdServer(int server_row){ 246 try{ 247 m_admin.hold_server(m_servers[server_row].name); 248 }catch(Exception _e){ 249 handleException (_e); 250 } 251 252 refreshServer(server_row); 253 } 254 255 260 public void releaseServer(String name){ 261 releaseServer(indexForServerName(name)); 262 } 263 264 269 public void releaseServer(int server_row){ 270 try{ 271 m_admin.release_server(m_servers[server_row].name); 272 }catch(Exception _e){ 273 handleException (_e); 274 } 275 276 refreshServer(server_row); 277 } 278 279 284 public void refreshServer(String name){ 285 refreshServer(indexForServerName(name)); 286 } 287 288 293 public void refreshServer(int index){ 294 try{ 295 ServerInfo _server = m_admin.get_server_info(m_servers[index].name); 296 297 m_servers[index] = _server; 298 299 buildServerNode(index); 300 301 m_server_model.serverRefreshed(index); 302 303 if (m_host_names.put(m_servers[index].host, m_servers[index].host) == null) 304 m_host_selector_model.addElement(m_servers[index].host); 305 306 if ( _server.name.equals(m_poa_model.getServerName())) 307 m_poa_model.setPOAs(_server.poas); 308 309 }catch(Exception _e){ 310 handleException (_e); 311 } 312 } 313 314 319 public void setServerDown(String name){ 320 setServerDown(indexForServerName(name)); 321 } 322 323 328 public void setServerDown(int server_row){ 329 Registration _reg = RegistrationHelper.narrow(m_admin); 330 331 try{ 332 _reg.set_server_down(m_servers[server_row].name); 333 }catch (Exception _e){ 334 handleException (_e); 335 } 336 337 refreshServer(m_servers[server_row].name); 338 } 339 340 348 public void addServer(String name, String command, String host){ 349 try{ 350 m_admin.register_server(name, command, host); 351 }catch (Exception _e){ 352 handleException (_e); 353 } 354 355 fetchServers(); 356 } 357 358 363 public JTree getTree(){ 364 return m_tree; 365 } 366 367 373 public void imrShutdown(boolean wait){ 374 disableRefresh(); 375 376 try{ 377 m_admin.shutdown(wait); 378 379 m_top_node.removeAllChildren(); 380 m_servers = null; 381 }catch (Exception _e){ 382 handleException (_e); 383 } 384 } 385 386 389 public void saveTable(){ 390 try{ 391 m_admin.save_server_table(); 392 }catch (Exception _e){ 393 handleException (_e); 394 } 395 } 396 397 403 public int getRow(ServerInfo server, POAInfo poa){ 404 for(int _i = 0; _i < server.poas.length; _i++){ 405 if (server.poas[_i] == poa) 406 return _i; 407 } 408 409 return -1; 410 } 411 412 417 public void setRefreshInterval(int interval){ 418 m_current_refresh_interval = interval; 419 m_refresh_disabled = false; 420 m_refresh_thread.setInterval(interval); 421 } 422 423 426 public void disableRefresh(){ 427 m_refresh_disabled = true; 428 m_refresh_thread.setInterval(0); 429 } 430 431 439 protected void updateServer(int server_row, String field_name, Object new_value){ 440 String _host = m_servers[server_row].host; 441 String _cmd = m_servers[server_row].command; 442 443 if (new_value instanceof String ){ 444 if (field_name.equals("Host")){ 445 _host = (String ) new_value; 446 if (m_host_names.put(new_value, new_value) == null) 447 m_host_selector_model.addElement(new_value); 448 } 449 else if (field_name.equals("Command")) 450 _cmd = (String ) new_value; 451 452 try{ 453 m_admin.edit_server(m_servers[server_row].name, _cmd, _host); 454 }catch (Exception _e){ 455 handleException (_e); 456 } 457 } 458 else if(new_value instanceof Boolean ){ 459 if (field_name.equals("active")){ 460 if (! ((Boolean ) new_value).booleanValue()) 461 setServerDown(m_servers[server_row].name); 462 } 463 else if (field_name.equals("holding")){ 464 try{ 465 if (((Boolean ) new_value).booleanValue()) 466 m_admin.hold_server(m_servers[server_row].name); 467 else 468 m_admin.release_server(m_servers[server_row].name); 469 470 }catch (Exception _e){ 471 handleException (_e); 472 } 473 } 474 } 475 476 refreshServer(m_servers[server_row].name); 477 } 478 479 484 485 private void handleException (Exception e) 486 { 487 if (e instanceof org.omg.CORBA.UserException ) 488 { 489 String _msg = e.toString(); 490 if (e instanceof IllegalServerName) 491 _msg = "The specified server name is not allowed"; 492 else if (e instanceof DuplicateServerName) 493 _msg = "A server with name " + 494 ((DuplicateServerName) e).name + 495 " has already been registered with the repository"; 496 else if (e instanceof FileOpFailed) 497 _msg = "The backup operation failed"; 498 499 JOptionPane.showMessageDialog(new JFrame(), _msg, 500 "An error occurred", 501 JOptionPane.ERROR_MESSAGE); 502 } 503 else 504 { 505 if (logger.isWarnEnabled()) 506 logger.warn("Exception: " + e.getMessage()); 507 } 508 } 509 510 511 516 517 private void buildHostSelectorModel() 518 { 519 HostInfo[] _hosts = m_admin.list_hosts(); 520 521 for (int _i = 0; _i < _hosts.length; _i++){ 522 try{ 523 ServerStartupDaemon _ssd = ServerStartupDaemonHelper.narrow(m_orb.string_to_object(_hosts[_i].ior_string)); 524 _ssd.get_system_load(); 525 _ssd._release(); 526 527 if (m_host_names.put(_hosts[_i].name, _hosts[_i].name) == null) 529 m_host_selector_model.addElement(_hosts[_i].name); 530 } catch (Exception _e){ 531 } 533 } 534 535 for (int _i = 0; _i < m_servers.length; _i++) 536 if (m_host_names.put(m_servers[_i].host, m_servers[_i].host) == null) 537 m_host_selector_model.addElement(m_servers[_i].host); 538 } 539 540 546 private int indexForServerName(String name){ 547 return ((Integer ) m_server_names.get(name)).intValue(); 548 } 549 550 553 private void fetchImRInfo(){ 554 Registration _reg = RegistrationHelper.narrow(m_admin); 555 556 m_imr_info = _reg.get_imr_info(); 557 } 558 559 565 private void buildServerNode(int index){ 566 DefaultMutableTreeNode _server_node; 567 POAInfo[] _poa_array = m_servers[index].poas; 568 Vector _poas; 569 if (index < m_server_nodes.size()){ 570 _server_node = (DefaultMutableTreeNode) m_server_nodes.elementAt(index); 572 _poas = (Vector) m_poa_nodes.elementAt(index); 573 574 } 575 else{ 576 _server_node = new DefaultMutableTreeNode(m_servers[index]); 578 m_server_nodes.addElement(_server_node); 579 m_tree_model.insertNodeInto(_server_node, m_top_node, index); 580 m_tree.scrollPathToVisible(new TreePath(_server_node.getPath())); 581 582 _poas = new Vector(); 583 m_poa_nodes.addElement(_poas); 584 } 585 586 int _i; 587 for(_i = 0; _i < _poas.size(); _i++){ 589 if (_i < _poa_array.length){ 590 DefaultMutableTreeNode _poa = (DefaultMutableTreeNode) _poas.elementAt(_i); 591 _poa.setUserObject(_poa_array[_i]); 592 } 593 else 594 break; 595 } 596 if (_i >= _poa_array.length){ 597 for (int _j = _poas.size() - 1; _j >= _i; _j--){ 599 DefaultMutableTreeNode _poa = (DefaultMutableTreeNode) _poas.elementAt(_j); 600 _poas.removeElementAt(_j); 601 m_tree_model.removeNodeFromParent(_poa); 602 } 603 } 604 else{ 605 for (int _j = _i; _j < _poa_array.length; _j++){ 607 DefaultMutableTreeNode _poa = new DefaultMutableTreeNode(_poa_array[_j]); 608 _poas.addElement(_poa); 609 m_tree_model.insertNodeInto(_poa, _server_node, _j); 610 } 611 } 612 } 613 614 619 private void removeServerNode(int index){ 620 DefaultMutableTreeNode _server_node = (DefaultMutableTreeNode) m_server_nodes.elementAt(index); 621 Vector _poas = (Vector) m_poa_nodes.elementAt(index); 622 623 for (int _j = _poas.size() - 1; _j >= 0; _j--){ 624 DefaultMutableTreeNode _poa = (DefaultMutableTreeNode) _poas.elementAt(_j); 625 _poas.removeElementAt(_j); 626 m_tree_model.removeNodeFromParent(_poa); 627 } 628 629 m_server_nodes.removeElementAt(index); 630 m_tree_model.removeNodeFromParent(_server_node); 631 } 632 633 634 635 639 private void buildTree(){ 640 int _i; 641 for(_i = 0; _i < m_server_nodes.size(); _i++){ 643 if (_i < m_servers.length){ 644 DefaultMutableTreeNode _server = (DefaultMutableTreeNode) m_server_nodes.elementAt(_i); 645 _server.setUserObject(m_servers[_i]); 646 buildServerNode(_i); 647 } 648 else 649 break; 650 } 651 652 if (_i >= m_servers.length){ 653 for (int _j = m_server_nodes.size() - 1; _j >= _i; _j--) 655 removeServerNode(_j); 656 } 657 else{ 658 for (int _j = _i; _j < m_servers.length; _j++) 660 buildServerNode(_j); 661 } 662 } 663 664 private class RefreshThread extends Thread { 665 private long m_interval; 666 private boolean m_run = true; 667 668 public RefreshThread(long interval){ 669 m_interval = interval; 670 671 start(); 672 } 673 674 public synchronized void run(){ 675 while (true){ 676 while (m_interval <= 0){ 677 try{ 678 this.wait(); 679 }catch (Exception _e){ 680 handleException(_e); 681 } 682 } 683 try{ 684 fetchServers(); 685 }catch (Exception _e){ 686 handleException(_e); 687 } 688 689 try{ 690 this.wait(m_interval); 691 }catch (Exception _e){ 692 handleException(_e); 693 } 694 } 695 } 696 697 public synchronized void setInterval(long interval) 698 { 699 m_interval = interval; 700 701 this.notifyAll(); 702 } 703 } 704 } 706 707 708 709 710 711 712 713 | Popular Tags |