1 25 package org.objectweb.joram.client.jms.admin; 26 27 import java.io.Reader ; 28 import java.io.FileReader ; 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.InputStreamReader ; 33 import java.io.FileNotFoundException ; 34 import java.net.ConnectException ; 35 import java.net.UnknownHostException ; 36 import java.util.Enumeration ; 37 import java.util.Hashtable ; 38 import java.util.List ; 39 import java.util.Properties ; 40 import java.util.Vector ; 41 42 import javax.jms.*; 43 44 import org.objectweb.joram.client.jms.Destination; 45 import org.objectweb.joram.client.jms.Queue; 46 import org.objectweb.joram.client.jms.Topic; 47 import org.objectweb.joram.client.jms.TopicConnectionFactory; 48 import org.objectweb.joram.client.jms.Message; 49 import org.objectweb.joram.client.jms.ha.local.TopicHALocalConnectionFactory; 50 import org.objectweb.joram.client.jms.ha.tcp.TopicHATcpConnectionFactory; 51 import org.objectweb.joram.client.jms.local.TopicLocalConnectionFactory; 52 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory; 53 import org.objectweb.joram.shared.admin.*; 54 55 import org.objectweb.joram.shared.JoramTracing; 56 import org.objectweb.util.monolog.api.BasicLevel; 57 58 63 public class AdminModule { 64 public static final String ADM_NAME_PROPERTY = "JoramAdminXML"; 65 public static final String DEFAULT_ADM_NAME = "default"; 66 67 public static final String REQUEST_TIMEOUT_PROP = 68 "org.objectweb.joram.client.jms.admin.requestTimeout"; 69 70 public static final long DEFAULT_REQUEST_TIMEOUT = 120000; 71 72 73 private static int localServer; 74 75 protected static String localHost; 76 77 protected static int localPort; 78 79 80 private static TopicConnection cnx = null; 81 82 83 private static AdminRequestor requestor; 84 85 86 private static ObjectMessage requestMsg; 87 88 private static ObjectMessage replyMsg; 89 90 91 protected static AdminReply reply; 92 93 private static int requestCounter; 94 95 private static long requestTimeout = 96 Long.getLong(REQUEST_TIMEOUT_PROP, 97 DEFAULT_REQUEST_TIMEOUT).longValue(); 98 99 100 private static boolean isHa = false; 101 102 108 public static void main(String [] args) { 109 try { 110 AdminModule.executeXMLAdmin(args[0]); 111 } catch (Exception exc) { 112 exc.printStackTrace(); 113 } 114 } 115 116 129 public static void connect(javax.jms.TopicConnectionFactory cnxFact, 130 String name, 131 String password) 132 throws ConnectException , AdminException { 133 if (cnx != null) 134 return; 135 136 try { 137 cnx = cnxFact.createTopicConnection(name, password); 138 requestor = new AdminRequestor(cnx); 139 140 cnx.start(); 141 142 org.objectweb.joram.client.jms.FactoryParameters params = null; 143 144 if (cnxFact instanceof javax.jms.XATopicConnectionFactory ) 145 params = ((org.objectweb.joram.client.jms.XAConnectionFactory) 146 cnxFact).getParameters(); 147 else 148 params = ((org.objectweb.joram.client.jms.ConnectionFactory) 149 cnxFact).getParameters(); 150 151 localHost = params.getHost(); 152 localPort = params.getPort(); 153 154 localServer = requestor.getLocalServerId(); 156 } catch (JMSSecurityException exc) { 157 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 158 JoramTracing.dbgClient.log( 159 BasicLevel.DEBUG, "", exc); 160 throw new AdminException(exc.getMessage()); 161 } catch (JMSException exc) { 162 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 163 JoramTracing.dbgClient.log( 164 BasicLevel.DEBUG, "", exc); 165 throw new ConnectException ("Connecting failed: " + exc); 166 } 167 } 168 169 185 public static void connect(String hostName, 186 int port, 187 String name, 188 String password, 189 int cnxTimer) 190 throws UnknownHostException , ConnectException , AdminException { 191 connect(hostName,port,name,password,cnxTimer, 192 "org.objectweb.joram.client.jms.tcp.ReliableTcpClient"); 193 } 194 195 212 public static void connect(String hostName, 213 int port, 214 String name, 215 String password, 216 int cnxTimer, 217 String reliableClass) 218 throws UnknownHostException , ConnectException , AdminException { 219 javax.jms.TopicConnectionFactory cnxFact =null; 220 221 if (isHa) { 222 String urlHa = "hajoram://" + hostName + ":" + port; 223 cnxFact = TopicHATcpConnectionFactory.create(urlHa); 224 225 } else { 226 cnxFact = TopicTcpConnectionFactory.create(hostName, port, reliableClass); 227 } 228 229 ((org.objectweb.joram.client.jms.ConnectionFactory) 230 cnxFact).getParameters().connectingTimer = cnxTimer; 231 232 connect(cnxFact, name, password); 233 } 234 235 249 public static void connect(String name, String password, int cnxTimer) 250 throws UnknownHostException , ConnectException , AdminException { 251 connect("localhost", 16010, name, password, cnxTimer); 252 } 253 254 269 public static void connect(String name, 270 String password, 271 int cnxTimer, 272 String reliableClass) 273 throws UnknownHostException , ConnectException , AdminException { 274 connect("localhost", 16010, name, password, cnxTimer, reliableClass); 275 } 276 277 287 public static void collocatedConnect(String name, String password) 288 throws ConnectException , AdminException { 289 JoramTracing.dbgClient.log(BasicLevel.DEBUG, "isHa=" + isHa); 290 if (isHa) { 291 connect(TopicHALocalConnectionFactory.create(), name, password); 292 } else { 293 connect(TopicLocalConnectionFactory.create(), name, password); 294 } 295 } 296 297 298 public static void disconnect() { 299 try { 300 if (cnx == null) return; 301 302 cnx.close(); 303 } catch (JMSException exc) {} 304 305 cnx = null; 306 } 307 308 318 public static void stopServer(int serverId) 319 throws ConnectException , AdminException { 320 try { 321 doRequest(new StopServerRequest(serverId)); 322 323 if (serverId == localServer) 324 cnx = null; 325 } catch (ConnectException exc) { 326 if (serverId != localServer) throw exc; 328 329 cnx = null; 330 } 331 } 332 333 339 public static void stopServer() throws ConnectException , AdminException { 340 stopServer(localServer); 341 } 342 343 355 public static void addServer(int sid, 356 String hostName, 357 String domainName, 358 int port, 359 String serverName) 360 throws ConnectException , AdminException { 361 addServer(sid, 362 hostName, domainName, port, serverName, 363 null, null); 364 } 365 366 380 public static void addServer(int sid, 381 String hostName, 382 String domainName, 383 int port, 384 String serverName, 385 String [] serviceNames, 386 String [] serviceArgs) 387 throws ConnectException , AdminException { 388 if (serviceNames != null && 389 serviceArgs != null) { 390 if (serviceNames.length != serviceArgs.length) 391 throw new AdminException( 392 "Same number of service names and arguments expected"); 393 } else { 394 if (serviceNames == null) throw new AdminException( 395 "Expected service names"); 396 if (serviceArgs == null) throw new AdminException( 397 "Expected service arguments"); 398 } 399 doRequest(new AddServerRequest( 400 sid, 401 hostName, 402 domainName, 403 port, 404 serverName, 405 serviceNames, 406 serviceArgs)); 407 } 408 409 417 public static void removeServer(int sid) 418 throws ConnectException , AdminException { 419 doRequest(new RemoveServerRequest(sid)); 420 } 421 422 434 public static void addDomain(String domainName, 435 int sid, 436 int port) 437 throws ConnectException , AdminException { 438 doRequest(new AddDomainRequest( 439 domainName, 440 sid, 441 port)); 442 } 443 444 452 public static void removeDomain(String domainName) 453 throws ConnectException , AdminException { 454 doRequest(new RemoveDomainRequest( 455 domainName)); 456 } 457 458 464 public static String getConfiguration() 465 throws ConnectException , AdminException { 466 return doRequest(new GetConfigRequest()).getInfo(); 467 } 468 469 481 public static void setDefaultDMQ(int serverId, DeadMQueue dmq) 482 throws ConnectException , AdminException 483 { 484 doRequest(new SetDefaultDMQ(serverId, dmq.getName())); 485 } 486 487 496 public static void setDefaultDMQ(DeadMQueue dmq) 497 throws ConnectException , AdminException 498 { 499 setDefaultDMQ(localServer, dmq); 500 } 501 502 514 public static void setDefaultThreshold(int serverId, int threshold) 515 throws ConnectException , AdminException 516 { 517 doRequest(new SetDefaultThreshold(serverId, threshold)); 518 } 519 520 529 public static void setDefaultThreshold(int threshold) 530 throws ConnectException , AdminException 531 { 532 setDefaultThreshold(localServer, threshold); 533 } 534 535 541 public static List getServersIds() throws ConnectException , AdminException 542 { 543 return getServersIds(null); 544 } 545 546 553 public static List getServersIds(String domainName) 554 throws ConnectException , AdminException { 555 Monitor_GetServersIds request = 556 new Monitor_GetServersIds( 557 AdminModule.getLocalServerId(), 558 domainName); 559 Monitor_GetServersIdsRep reply = 560 (Monitor_GetServersIdsRep) doRequest(request); 561 int[] serverIds = reply.getIds(); 562 Vector res = new Vector (); 563 for (int i = 0; i < serverIds.length; i++) { 564 res.addElement(new Integer (serverIds[i])); 565 } 566 return res; 567 } 568 569 public static Server[] getServers() 570 throws ConnectException , AdminException { 571 return getServers(null); 572 } 573 574 public static Server[] getServers(String domainName) 575 throws ConnectException , AdminException { 576 Monitor_GetServersIds request = 577 new Monitor_GetServersIds( 578 AdminModule.getLocalServerId(), 579 domainName); 580 Monitor_GetServersIdsRep reply = 581 (Monitor_GetServersIdsRep) doRequest(request); 582 int[] serverIds = reply.getIds(); 583 String [] serverNames = reply.getNames(); 584 String [] serverHostNames = reply.getHostNames(); 585 Server[] servers = new Server[serverIds.length]; 586 for (int i = 0; i < serverIds.length; i++) { 587 servers[i] = new Server(serverIds[i], 588 serverNames[i], 589 serverHostNames[i]); 590 } 591 return servers; 592 } 593 594 public static Server getLocalServer() 595 throws ConnectException , AdminException { 596 GetLocalServerRep reply = (GetLocalServerRep)doRequest( 597 new GetLocalServer()); 598 return new Server(reply.getId(), 599 reply.getName(), 600 reply.getHostName()); 601 } 602 603 610 public static String [] getDomainNames(int serverId) 611 throws ConnectException , AdminException { 612 GetDomainNames request = 613 new GetDomainNames(serverId); 614 GetDomainNamesRep reply = 615 (GetDomainNamesRep) doRequest(request); 616 return reply.getDomainNames(); 617 } 618 619 628 public static DeadMQueue getDefaultDMQ(int serverId) 629 throws ConnectException , AdminException 630 { 631 Monitor_GetDMQSettings request = new Monitor_GetDMQSettings(serverId); 632 Monitor_GetDMQSettingsRep reply; 633 reply = (Monitor_GetDMQSettingsRep) doRequest(request); 634 635 if (reply.getDMQName() == null) 636 return null; 637 else 638 return new DeadMQueue(reply.getDMQName()); 639 } 640 641 648 public static DeadMQueue getDefaultDMQ() 649 throws ConnectException , AdminException 650 { 651 return getDefaultDMQ(localServer); 652 } 653 654 662 public static int getDefaultThreshold(int serverId) 663 throws ConnectException , AdminException 664 { 665 Monitor_GetDMQSettings request = new Monitor_GetDMQSettings(serverId); 666 Monitor_GetDMQSettingsRep reply; 667 reply = (Monitor_GetDMQSettingsRep) doRequest(request); 668 669 if (reply.getThreshold() == null) 670 return -1; 671 else 672 return reply.getThreshold().intValue(); 673 } 674 675 681 public static int getDefaultThreshold() 682 throws ConnectException , AdminException 683 { 684 return getDefaultThreshold(localServer); 685 } 686 687 696 public static List getDestinations(int serverId) 697 throws ConnectException , AdminException 698 { 699 Monitor_GetDestinations request = new Monitor_GetDestinations(serverId); 700 Monitor_GetDestinationsRep reply = 701 (Monitor_GetDestinationsRep) doRequest(request); 702 703 Vector list = new Vector (); 704 String [] ids = reply.getIds(); 705 String [] names = reply.getNames(); 706 String [] types = reply.getTypes(); 707 for (int i = 0; i < types.length; i++) { 708 list.addElement(Destination.newInstance( 709 ids[i], names[i], types[i])); 710 } 711 return list; 712 } 713 714 721 public static List getDestinations() throws ConnectException , AdminException 722 { 723 return getDestinations(localServer); 724 } 725 726 734 public static List getDestinations(int serverId, long delay) 735 throws ConnectException , AdminException { 736 737 Monitor_GetDestinations request = new Monitor_GetDestinations(serverId); 738 Monitor_GetDestinationsRep reply = 739 (Monitor_GetDestinationsRep) doRequest(request,delay); 740 741 Vector list = new Vector (); 742 String [] ids = reply.getIds(); 743 String [] names = reply.getNames(); 744 String [] types = reply.getTypes(); 745 for (int i = 0; i < types.length; i++) { 746 list.addElement(Destination.newInstance( 747 ids[i], names[i], types[i])); 748 } 749 return list; 750 } 751 752 761 public static List getUsers(int serverId) 762 throws ConnectException , AdminException 763 { 764 Monitor_GetUsers request = new Monitor_GetUsers(serverId); 765 Monitor_GetUsersRep reply = (Monitor_GetUsersRep) doRequest(request); 766 767 Vector list = new Vector (); 768 Hashtable users = reply.getUsers(); 769 String name; 770 for (Enumeration names = users.keys(); names.hasMoreElements();) { 771 name = (String ) names.nextElement(); 772 list.add(new User(name, (String ) users.get(name))); 773 } 774 return list; 775 } 776 777 785 public static List getUsers(int serverId, long delay) 786 throws ConnectException , AdminException { 787 788 Monitor_GetUsers request = new Monitor_GetUsers(serverId); 789 Monitor_GetUsersRep reply = (Monitor_GetUsersRep) doRequest(request,delay); 790 791 Vector list = new Vector (); 792 Hashtable users = reply.getUsers(); 793 String name; 794 for (Enumeration names = users.keys(); names.hasMoreElements();) { 795 name = (String ) names.nextElement(); 796 list.add(new User(name, (String ) users.get(name))); 797 } 798 return list; 799 } 800 801 808 public static List getUsers() throws ConnectException , AdminException 809 { 810 return getUsers(localServer); 811 } 812 813 814 819 public static int getLocalServerId() throws ConnectException 820 { 821 if (cnx == null) 822 throw new ConnectException ("Administrator not connected."); 823 824 return localServer; 825 } 826 827 832 public static String getLocalHost() throws ConnectException 833 { 834 if (cnx == null) 835 throw new ConnectException ("Administrator not connected."); 836 837 return localHost; 838 } 839 840 845 public static int getLocalPort() throws ConnectException 846 { 847 if (cnx == null) 848 throw new ConnectException ("Administrator not connected."); 849 850 return localPort; 851 } 852 853 861 public static AdminReply doRequest(AdminRequest request) 862 throws AdminException, ConnectException { 863 return doRequest(request,requestTimeout); 864 } 865 866 874 public static AdminReply doRequest(AdminRequest request, long timeout) 875 throws AdminException, ConnectException { 876 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 877 JoramTracing.dbgClient.log( 878 BasicLevel.DEBUG, "AdminModule.doRequest(" + request + ')'); 879 880 if (cnx == null) 881 throw new ConnectException ("Admin connection not established."); 882 883 if (timeout < 1) 884 timeout = requestTimeout; 885 886 try { 887 replyMsg = (ObjectMessage) requestor.request( 888 request, timeout); 889 reply = (AdminReply) replyMsg.getObject(); 890 891 if (! reply.succeeded()) { 892 switch (reply.getErrorCode()) { 893 case AdminReply.NAME_ALREADY_USED: 894 throw new NameAlreadyUsedException(reply.getInfo()); 895 case AdminReply.START_FAILURE: 896 throw new StartFailureException(reply.getInfo()); 897 case AdminReply.SERVER_ID_ALREADY_USED: 898 throw new ServerIdAlreadyUsedException(reply.getInfo()); 899 case AdminReply.UNKNOWN_SERVER: 900 throw new UnknownServerException(reply.getInfo()); 901 default: 902 throw new AdminException(reply.getInfo()); 903 } 904 } else { 905 return reply; 906 } 907 } catch (JMSException exc) { 908 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 909 JoramTracing.dbgClient.log( 910 BasicLevel.DEBUG, "", exc); 911 throw new ConnectException ("Connection failed: " + exc.getMessage()); 912 } catch (ClassCastException exc) { 913 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 914 JoramTracing.dbgClient.log( 915 BasicLevel.DEBUG, "", exc); 916 throw new AdminException("Invalid server reply: " + exc.getMessage()); 917 } 918 } 919 920 public static void abortRequest() throws JMSException { 921 if (requestor != null) { 922 requestor.abort(); 923 } else throw new JMSException("Not connected"); 924 } 925 926 public static class AdminRequestor { 927 private javax.jms.TopicConnection cnx; 928 private javax.jms.TopicSession sess; 929 private javax.jms.Topic topic; 930 private TemporaryTopic tmpTopic; 931 private MessageProducer producer; 932 private MessageConsumer consumer; 933 934 public AdminRequestor(javax.jms.TopicConnection cnx) 935 throws JMSException { 936 this.cnx = cnx; 937 sess = cnx.createTopicSession( 938 false, Session.AUTO_ACKNOWLEDGE); 939 topic = sess.createTopic("#AdminTopic"); 940 producer = sess.createProducer(topic); 941 tmpTopic = sess.createTemporaryTopic(); 942 consumer = sess.createConsumer(tmpTopic); 943 } 944 945 public javax.jms.Message request(AdminRequest request, 946 long timeout) throws JMSException { 947 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 948 JoramTracing.dbgClient.log( 949 BasicLevel.DEBUG, 950 "AdminModule.AdminRequestor.request(" + 951 request + ',' + timeout + ')'); 952 953 requestMsg = sess.createObjectMessage(request); 954 requestMsg.setJMSReplyTo(tmpTopic); 955 producer.send(requestMsg); 956 String correlationId = requestMsg.getJMSMessageID(); 957 while (true) { 958 javax.jms.Message reply = consumer.receive(timeout); 959 if (reply == null) { 960 throw new JMSException("Interrupted request"); 961 } else { 962 if (correlationId.equals( 963 reply.getJMSCorrelationID())) { 964 return reply; 965 } else { 966 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 967 JoramTracing.dbgClient.log( 968 BasicLevel.DEBUG, 969 "reply id (" + reply.getJMSCorrelationID() + 970 ") != request id (" + correlationId + ")"); 971 continue; 972 } 973 } 974 } 975 } 976 977 public void abort() throws JMSException { 978 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 979 JoramTracing.dbgClient.log( 980 BasicLevel.DEBUG, "AdminModule.AdminRequestor.abort()"); 981 consumer.close(); 982 consumer = sess.createConsumer(tmpTopic); 983 } 984 985 public int getLocalServerId() { 986 try { 987 String topicName = topic.getTopicName(); 988 int ind0 = topicName.indexOf("."); 989 int ind1 = topicName.indexOf(".", ind0 + 1); 990 return Integer.parseInt(topicName.substring(ind0 + 1, ind1)); 991 } catch (JMSException exc) { 992 return -1; 993 } 994 } 995 996 public void close() throws JMSException { 997 consumer.close(); 998 producer.close(); 999 tmpTopic.delete(); 1000 sess.close(); 1001 } 1002 } 1003 1004 1013 public static boolean executeXMLAdmin(String cfgDir, 1014 String cfgFileName) 1015 throws Exception { 1016 return executeXMLAdmin(new File (cfgDir, cfgFileName).getPath()); 1017 } 1018 1019 1027 public static boolean executeXMLAdmin(String path) throws Exception { 1028 if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG)) 1029 JoramTracing.dbgAdmin.log(BasicLevel.DEBUG,"executeXMLAdmin(" + path + ")"); 1030 1031 boolean res = false; 1032 Reader reader = null; 1033 1034 File cfgFile = new File (path); 1036 try { 1037 if (!cfgFile.exists() || !cfgFile.isFile() || (cfgFile.length() == 0)) { 1038 throw new IOException (); 1039 } 1040 reader = new FileReader (cfgFile); 1041 } catch (IOException exc) { 1042 if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG)) 1045 JoramTracing.dbgAdmin.log(BasicLevel.DEBUG, 1046 "Unable to find Joram Admin configuration file \"" + 1047 cfgFile.getPath() + "\"."); 1048 reader = null; 1049 } 1050 1051 if (reader == null) { 1053 ClassLoader classLoader = null; 1054 InputStream is = null; 1055 try { 1056 classLoader = AdminModule.class.getClassLoader(); 1057 if (classLoader != null) { 1058 if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG)) 1059 JoramTracing.dbgAdmin.log(BasicLevel.DEBUG, 1060 "Trying to find [" + path + "] using " + 1061 classLoader + " class loader."); 1062 is = classLoader.getResourceAsStream(path); 1063 } 1064 } catch (Throwable t) { 1065 if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG)) 1066 JoramTracing.dbgAdmin.log(BasicLevel.DEBUG, 1067 "Can't find [" + path + "] using " + 1068 classLoader + " class loader.", 1069 t); 1070 is = null; 1071 } 1072 1073 if (is == null) { 1074 if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG)) 1076 JoramTracing.dbgAdmin.log(BasicLevel.DEBUG, 1077 "Trying to find [" + path + 1078 "] using ClassLoader.getSystemResource()."); 1079 is = ClassLoader.getSystemResourceAsStream(path); 1080 } 1081 if (is != null) { 1082 res = executeAdmin(new InputStreamReader (is)); 1083 } 1084 } else { 1085 res = executeAdmin(reader); 1086 } 1087 1088 if (!res) 1089 throw new FileNotFoundException ("xml Joram Admin configuration file not found."); 1090 1091 return res; 1092 } 1093 1094 public static boolean executeAdmin(Reader reader) 1095 throws Exception { 1096 if (JoramTracing.dbgAdmin.isLoggable(BasicLevel.DEBUG)) 1097 JoramTracing.dbgAdmin.log(BasicLevel.DEBUG, "executeAdmin(" + reader + ")"); 1098 1099 String cfgName = System.getProperty(AdminModule.ADM_NAME_PROPERTY, 1100 AdminModule.DEFAULT_ADM_NAME); 1101 1102 JoramSaxWrapper wrapper = new JoramSaxWrapper(); 1103 return wrapper.parse(reader,cfgName); 1104 } 1105 1106 1107 public static void setHa(boolean isHa) { 1108 AdminModule.isHa = isHa; 1109 } 1110} 1111 | Popular Tags |