| 1 20 21 package org.jacorb.imr; 22 23 import org.apache.avalon.framework.logger.Logger; 24 import org.apache.avalon.framework.configuration.*; 25 26 import org.omg.GIOP.*; 27 28 import org.jacorb.imr.RegistrationPackage.*; 29 import org.jacorb.imr.AdminPackage.*; 30 31 import org.jacorb.orb.*; 32 import org.jacorb.orb.giop.*; 33 import org.jacorb.orb.iiop.*; 34 35 import org.jacorb.poa.util.POAUtil; 36 37 import org.jacorb.util.ObjectUtil; 38 39 import org.omg.PortableServer.*; 40 41 import java.io.*; 42 import java.net.ServerSocket ; 43 import java.net.Socket ; 44 import java.net.InetAddress ; 45 import java.lang.reflect.Method ; 46 47 57 58 public class ImplementationRepositoryImpl 59 extends ImplementationRepositoryPOA 60 { 61 64 private org.omg.CORBA.ORB orb; 65 66 private org.jacorb.config.Configuration configuration = null; 67 68 69 private Logger logger = null; 70 71 private String iorFile = null; 72 73 private File table_file; 74 private ServerTable server_table; 75 private File table_file_backup; 76 private SocketListener listener; 77 private Thread listenerThread; 78 79 private int object_activation_retries = 5; 80 private int object_activation_sleep = 50; 81 82 private boolean allow_auto_register = false; 83 private boolean check_object_liveness = false; 84 85 private int connection_timeout = 2000; 86 private long poaActivationTimeout = 120000; 88 private WriteThread wt; 89 private boolean updatePending; 90 private Shutdown shutdownThread; 91 92 93 103 public ImplementationRepositoryImpl(org.omg.CORBA.ORB orb) 104 { 105 this.orb = orb; 106 107 shutdownThread = new Shutdown (); 108 shutdownThread.setDaemon (true); 109 shutdownThread.setName ("Shutdown Thread"); 110 addShutdownHook (shutdownThread); 111 } 112 113 public void configure(Configuration myConfiguration) 114 throws ConfigurationException 115 { 116 configuration = (org.jacorb.config.Configuration)myConfiguration; 117 118 logger = configuration.getNamedLogger("jacorb.imr"); 119 120 String defaultTableFile = "table.dat"; 121 String tableFileStr = configuration.getAttribute("jacorb.imr.table_file", 122 defaultTableFile); 123 124 if (tableFileStr == defaultTableFile) 128 { 129 if (this.logger.isWarnEnabled()) 130 { 131 this.logger.warn("No file for the server table specified! Please configure the property jacorb.imr.table_file!"); 132 this.logger.warn("Will create \"table.dat\" in current directory, if necessary"); 133 } 134 } 135 136 table_file = new File(tableFileStr); 137 boolean _new_table = false; 138 139 if (! table_file.exists ()) 141 { 142 _new_table = true; 143 if (this.logger.isInfoEnabled()) 144 { 145 this.logger.info("Table file " + tableFileStr + 146 " does not exist - autocreating it."); 147 } 148 149 try 150 { 151 table_file.createNewFile (); 152 } 153 catch (IOException ex) 154 { 155 throw new ConfigurationException("Failed to create table file", ex); 156 } 157 } 158 else 159 { 160 if (table_file.isDirectory ()) 161 { 162 throw new ConfigurationException("The table file is a directory! Please check " + table_file.getAbsolutePath()); 163 } 164 165 if (! table_file.canRead()) 166 { 167 throw new ConfigurationException("The table file is not readable! Please check " + table_file.getAbsolutePath()); 168 } 169 170 if (! table_file.canWrite()) 171 { 172 throw new ConfigurationException("The table file is not writable! Please check " + table_file.getAbsolutePath()); 173 } 174 } 175 176 try 177 { 178 if (_new_table) 179 { 180 this.server_table = new ServerTable(); 181 save_server_table(table_file); 182 } 183 else 184 { 185 try 186 { 187 ObjectInputStream _in = 188 new ObjectInputStream(new FileInputStream(table_file)); 189 server_table = (ServerTable)_in.readObject(); 190 _in.close(); 191 } 192 catch (Exception ex) 193 { 194 this.logger.debug("Failed to read ServerTable", ex); 195 196 server_table = new ServerTable(); 197 save_server_table(table_file); 198 } 199 } 200 } 201 catch (FileOpFailed ex) 202 { 203 this.logger.debug("Failed to read ServerTable", ex); 204 } 205 206 207 this.iorFile = configuration.getAttribute("jacorb.imr.ior_file"); 209 210 String _backup_file_str = 211 configuration.getAttribute("jacorb.imr.backup_file", ""); 212 213 if (_backup_file_str.length() == 0) 215 { 216 this.logger.warn("No backup file specified!. No backup file will be created"); 217 } 218 219 if (_backup_file_str.length() > 0) 220 { 221 table_file_backup = new File(_backup_file_str); 222 223 if ( ! table_file_backup.exists ()) 225 { 226 _new_table = true; 227 228 if (this.logger.isInfoEnabled()) 229 { 230 this.logger.info("Backup file " + _backup_file_str + 231 " does not exist - autocreating it."); 232 } 233 234 try 235 { 236 table_file_backup.createNewFile(); 237 } 238 catch (IOException ex) 239 { 240 throw new ConfigurationException("Failed to create backup file", 241 ex); 242 } 243 } 244 else 245 { 246 if (table_file_backup.isDirectory ()) 247 { 248 throw new ConfigurationException("The backup file is a directory! Please check " + table_file_backup.getAbsolutePath()); 249 } 250 251 if (! table_file_backup.canRead()) 252 { 253 throw new ConfigurationException("The backup file is not readable! Please check " + table_file_backup.getAbsolutePath()); 254 } 255 256 if (! table_file_backup.canWrite()) 257 { 258 throw new ConfigurationException("The backup file is not writable! Please check " + table_file_backup.getAbsolutePath()); 259 } 260 } 261 } 262 263 this.object_activation_retries = 264 configuration.getAttributeAsInteger("jacorb.imr.object_activation_retries", 265 5); 266 267 this.object_activation_sleep = 268 configuration.getAttributeAsInteger("jacorb.imr.object_activation_sleep", 269 50); 270 271 this.allow_auto_register = 272 configuration.getAttributeAsBoolean("jacorb.imr.allow_auto_register", 273 false); 274 this.check_object_liveness = 275 configuration.getAttributeAsBoolean("jacorb.imr.check_object_liveness", 276 false); 277 278 this.connection_timeout = 279 configuration.getAttributeAsInteger("jacorb.imr.connection_timeout", 280 2000 ); 281 282 this.poaActivationTimeout = 283 configuration.getAttributeAsInteger( "jacorb.imr.timeout", 120000); 284 285 this.listener = new SocketListener(); 286 this.listener.configure(configuration); 287 288 this.listenerThread = new Thread (listener); 289 this.listenerThread.setPriority(Thread.MAX_PRIORITY); 290 this.listenerThread.start(); 291 292 this.wt = new WriteThread (); 293 this.wt.setName ("IMR Write Thread"); 294 this.wt.setDaemon (true); 295 this.wt.start (); 296 } 297 298 public String getIORFile() 299 { 300 return this.iorFile; 301 } 302 303 305 313 public void set_server_down(String server) 314 throws UnknownServerName 315 { 316 if (this.logger.isDebugEnabled()) 317 { 318 this.logger.debug("ImR: server " + server + " is going down... "); 319 } 320 321 ImRServerInfo _server = server_table.getServer(server); 322 _server.setDown(); 323 } 324 325 326 346 public void register_poa(String name, String server, String host, int port) 347 throws IllegalPOAName, DuplicatePOAName, UnknownServerName 348 { 349 ImRServerInfo _server = null; 350 ImRPOAInfo _poa = null; 351 boolean remap = false; 352 353 updatePending = true; 354 355 if (this.logger.isDebugEnabled()) 356 { 357 this.logger.debug("ImR: registering poa " + name + " for server: " + 358 server + " on " + host); 359 } 360 361 if( allow_auto_register && 362 ! server_table.hasServer( server )) 363 { 364 try 365 { 366 register_server( server, "", "" ); 367 } 368 catch( IllegalServerName isn ) 369 { 370 } 372 catch( DuplicateServerName dsn ) 373 { 374 } 376 } 377 378 _server = server_table.getServer(server); 379 _poa = server_table.getPOA(name); 380 381 if (_poa == null) 382 { 383 _poa = new ImRPOAInfo(name, host, port, _server, 385 poaActivationTimeout); 386 _server.addPOA(_poa); 387 server_table.putPOA(name, _poa); 388 389 this.logger.debug("ImR: new poa registered"); 390 } 391 else 392 { 393 395 if ((_poa.active) || (! server.equals(_poa.server.name))) 399 { 400 byte[] first = _poa.name.getBytes (); 401 byte[] id = new byte [ first.length + 1]; 402 System.arraycopy (first, 0, id, 0, first.length); 403 id[first.length] = org.jacorb.poa.POAConstants.OBJECT_KEY_SEP_BYTE; 404 405 if (_poa.host.equals (host) && _poa.port == port) 409 { 410 remap = true; 411 } 412 else 413 { 414 remap = ! (checkServerActive (_poa.host, _poa.port, id)); 416 } 417 418 if (remap == false) 419 { 420 throw new DuplicatePOAName 421 ( 422 "POA " + name + 423 " has already been registered " + 424 "for server " + _poa.server.name 425 ); 426 } 427 else 428 { 429 this.logger.debug("ImR: Remapping server/port"); 430 } 431 } 432 433 _poa.reactivate(host, port); 434 this.logger.debug("ImR: register_poa, reactivated"); 435 } 436 try 437 { 438 synchronized (wt) 439 { 440 wt.notify (); 441 } 442 } 443 catch (IllegalMonitorStateException e) 444 { 445 this.logger.debug("Caught Exception", e); 446 } 447 } 448 449 450 459 public void register_host(HostInfo host) 460 throws IllegalHostName, InvalidSSDRef 461 { 462 463 if (host.name == null || host.name.length() == 0) 464 throw new IllegalHostName(host.name); 465 466 try 467 { 468 host.ssd_ref.get_system_load(); 469 } 470 catch (Exception e) 471 { 472 this.logger.debug("Caught Exception", e); 473 throw new InvalidSSDRef(); 474 } 475 updatePending = true; 476 477 server_table.putHost(host.name, new ImRHostInfo(host)); 478 479 try 480 { 481 synchronized (wt) 482 { 483 wt.notify (); 484 } 485 } 486 catch (IllegalMonitorStateException e) 487 { 488 this.logger.debug("Caught Exception", e); 489 } 490 } 491 492 493 497 498 public ImRInfo get_imr_info() 499 { 500 return new ImRInfo(listener.getAddress(), listener.getPort()); 501 } 502 503 504 506 513 public HostInfo[] list_hosts() 514 { 515 return server_table.getHosts(); 516 } 517 518 524 public ServerInfo[] list_servers() 525 { 526 ServerInfo [] servers; 527 528 if (check_object_liveness) 529 { 530 this.logger.debug("ImR: Checking servers"); 531 532 servers = server_table.getServers(); 533 534 for (int k=0; k<servers.length; k++) 535 { 536 if (servers[k].active && servers[k].poas.length > 0) 537 { 538 byte[] first = servers[k].poas[0].name.getBytes (); 539 byte[] id = new byte [ first.length + 1]; 540 System.arraycopy (first, 0, id, 0, first.length); 541 id[first.length] = org.jacorb.poa.POAConstants.OBJECT_KEY_SEP_BYTE; 542 543 if ( ! checkServerActive 544 (servers[k].poas[0].host, servers[k].poas[0].port, id)) 545 { 546 try 547 { 548 if (this.logger.isDebugEnabled()) 549 { 550 this.logger.debug("ImR: Setting server " + 551 servers[k].name + " down"); 552 } 553 554 server_table.getServer(servers[k].name).setDown(); 556 557 servers[k].active = false; 559 } 560 catch (UnknownServerName e) 561 { 562 if (this.logger.isDebugEnabled()) 563 { 564 this.logger.debug("ImR: Internal error - unknown server " + servers[k].name, e); 565 } 566 } 567 } 568 } 569 } 570 } 571 else 572 { 573 servers = server_table.getServers(); 574 } 575 576 return servers; 577 } 578 579 586 public ServerInfo get_server_info(String server) 587 throws UnknownServerName 588 { 589 return server_table.getServer(server).toServerInfo(); 590 } 591 592 606 public void register_server(String name, String command, String host) 607 throws IllegalServerName, DuplicateServerName 608 { 609 updatePending = true; 610 611 ImRServerInfo _server = new ImRServerInfo(name, host, command); 612 server_table.putServer(name, _server); 613 614 if (this.logger.isDebugEnabled()) 615 { 616 this.logger.debug("ImR: server " + name + " on " + 617 host + " registered"); 618 } 619 620 try 621 { 622 synchronized (wt) 623 { 624 wt.notify (); 625 } 626 } 627 catch (IllegalMonitorStateException e) 628 { 629 this.logger.debug("Caught Exception", e); 630 } 631 } 632 633 634 641 public void unregister_server(String name) throws UnknownServerName 642 { 643 updatePending = true; 644 645 ImRServerInfo _server = server_table.getServer(name); 646 String [] _poas = _server.getPOANames(); 647 648 for (int _i = 0; _i < _poas.length; _i++) 650 server_table.removePOA(_poas[_i]); 651 652 server_table.removeServer(name); 653 654 if (this.logger.isDebugEnabled()) 655 { 656 this.logger.debug("ImR: server " + name + " unregistered"); 657 } 658 659 try 660 { 661 synchronized (wt) 662 { 663 wt.notify (); 664 } 665 } 666 catch (IllegalMonitorStateException e) 667 { 668 this.logger.debug("Caught Exception", e); 669 } 670 } 671 672 681 public void edit_server(String name, String command, String host) 682 throws UnknownServerName 683 { 684 updatePending = true; 685 686 ImRServerInfo _server = server_table.getServer(name); 687 688 _server.command = command; 689 _server.host = host; 690 691 if (this.logger.isDebugEnabled()) 692 { 693 this.logger.debug("ImR: server " + name + " edited"); 694 } 695 696 try 697 { 698 synchronized (wt) 699 { 700 wt.notify (); 701 } 702 } 703 catch (IllegalMonitorStateException e) 704 { 705 this.logger.debug("Caught Exception", e); 706 } 707 } 708 709 710 719 public void hold_server(String name) 720 throws UnknownServerName 721 { 722 ImRServerInfo _server = server_table.getServer(name); 723 _server.holding = true; 724 } 725 726 732 public void release_server(String name) 733 throws UnknownServerName 734 { 735 ImRServerInfo _server = server_table.getServer(name); 736 _server.release(); 737 } 738 739 746 public void start_server(String name) 747 throws UnknownServerName, ServerStartupFailed 748 { 749 restartServer(server_table.getServer(name)); 750 } 751 752 756 public void save_server_table() 757 throws FileOpFailed 758 { 759 if (table_file_backup != null) 760 { 761 save_server_table(table_file_backup); 762 } 763 } 764 765 774 public void shutdown(boolean wait) 775 { 776 synchronized (wt) 777 { 778 wt.shutdown (); 779 wt.notify (); 780 } 781 if (listener != null) 782 { 783 listener.stopListening (wait); 784 try 785 { 786 synchronized (listener) 787 { 788 listenerThread.join(5000); 790 } 791 } 792 catch (InterruptedException e) 793 { 794 this.logger.debug("Caught Exception", e); 795 } 796 } 797 try 798 { 799 save_server_table (); 800 } 801 catch (FileOpFailed f) 802 { 803 this.logger.debug("ImR: Failed to save backup table.", f); 804 } 805 this.logger.debug("ImR: Finished shutting down"); 806 } 807 808 815 public void unregister_host(String name) 816 throws UnknownHostName{ 817 if (server_table.removeHost(name) == null) 818 throw new UnknownHostName(name); 819 } 820 821 827 private void save_server_table(File save_to) throws FileOpFailed { 828 try{ 829 ObjectOutputStream _out = new ObjectOutputStream(new FileOutputStream(save_to)); 830 831 server_table.table_lock.gainExclusiveLock(); 832 _out.writeObject(server_table); 833 server_table.table_lock.releaseExclusiveLock(); 834 835 _out.flush(); 836 _out.close(); 837 } 838 catch (Exception e) 839 { 840 this.logger.debug("Caught Exception", e); 841 throw new FileOpFailed(); 842 } 843 updatePending = false; 844 } 845 846 847 850 public static void usage () 851 { 852 System.out.println("Usage: The following properties are useful in conjunction with the \nImplementationRepository:"); 853 System.out.println("\t \"jacorb.imr.endpoint_host\" Address to listen on for requests"); 854 System.out.println("\t \"jacorb.imr.endpoint_port\" Port to listen on for requests"); 855 System.out.println("\t \"jacorb.imr.table_file\" The file to store the server table into"); 856 System.out.println("\t \"jacorb.imr.backup_file\" The file to store the server table backup into"); 857 System.out.println("\t \"jacorb.imr.ior_file\" The file to store the ImRs IOR into"); 858 System.out.println("\t \"jacorb.imr.allow_auto_register\" if set to \"on\", servers that don't \n\talready have an entry on their first call to the imr, will get \n\tautomatically registered. Otherwise, an UnknownServer exception \n\tis thrown."); 859 System.exit(0); 860 } 861 862 867 public static void main(String [] args) 868 { 869 java.util.Properties argProps = ObjectUtil.argsToProps( args ); 872 argProps.setProperty("jacorb.implname", "the_ImR"); 873 argProps.setProperty("jacorb.use_imr", "off"); 874 875 try 877 { 878 org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init( args, argProps ); 879 880 ImplementationRepositoryImpl _imr = 881 new ImplementationRepositoryImpl(orb); 882 _imr.configure(((org.jacorb.orb.ORB) orb).getConfiguration()); 883 884 POA root_poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); 885 root_poa.the_POAManager().activate(); 886 887 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [2]; 888 889 policies[0] = 890 &
|