1 21 22 package org.apache.derby.impl.drda; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.DataOutputStream ; 27 import java.io.FilterOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.PrintStream ; 32 import java.io.PrintWriter ; 33 import java.net.InetAddress ; 34 import java.net.ServerSocket ; 35 import java.net.Socket ; 36 import java.net.UnknownHostException ; 37 import java.security.AccessController ; 38 import java.security.PrivilegedActionException ; 39 import java.security.PrivilegedExceptionAction ; 40 import java.sql.Connection ; 41 import java.sql.Driver ; 42 import java.sql.DriverManager ; 43 import java.sql.SQLException ; 44 import java.sql.SQLWarning ; 45 import java.util.ArrayList ; 46 import java.util.Enumeration ; 47 import java.util.Hashtable ; 48 import java.util.Properties ; 49 import java.util.StringTokenizer ; 50 import java.util.Vector ; 51 52 import org.apache.derby.drda.NetworkServerControl; 53 import org.apache.derby.iapi.error.StandardException; 54 import org.apache.derby.iapi.jdbc.DRDAServerStarter; 55 import org.apache.derby.iapi.reference.Attribute; 56 import org.apache.derby.iapi.reference.DRDAConstants; 57 import org.apache.derby.iapi.reference.Property; 58 import org.apache.derby.iapi.reference.SQLState; 59 import org.apache.derby.iapi.services.i18n.MessageService; 60 import org.apache.derby.iapi.services.info.JVMInfo; 61 import org.apache.derby.iapi.services.info.ProductGenusNames; 62 import org.apache.derby.iapi.services.info.ProductVersionHolder; 63 import org.apache.derby.iapi.services.monitor.Monitor; 64 import org.apache.derby.iapi.services.property.PropertyUtil; 65 import org.apache.derby.iapi.services.sanity.SanityManager; 66 import org.apache.derby.iapi.tools.i18n.LocalizedOutput; 67 import org.apache.derby.iapi.tools.i18n.LocalizedResource; 68 import org.apache.derby.iapi.util.CheapDateFormatter; 69 import org.apache.derby.iapi.util.StringUtil; 70 import org.apache.derby.impl.jdbc.EmbedSQLException; 71 72 78 public final class NetworkServerControlImpl { 79 private final static int NO_USAGE_MSGS= 12; 80 private final static String [] COMMANDS = 81 {"start","shutdown","trace","tracedirectory","ping", 82 "logconnections", "sysinfo", "runtimeinfo", "maxthreads", "timeslice"}; 83 private final static int [] COMMAND_ARGS = 85 {0, 0, 1, 1, 0, 1, 0, 0, 1, 1}; 86 private final static int COMMAND_START = 0; 87 private final static int COMMAND_SHUTDOWN = 1; 88 private final static int COMMAND_TRACE = 2; 89 private final static int COMMAND_TRACEDIRECTORY = 3; 90 private final static int COMMAND_TESTCONNECTION = 4; 91 private final static int COMMAND_LOGCONNECTIONS = 5; 92 private final static int COMMAND_SYSINFO = 6; 93 private final static int COMMAND_RUNTIME_INFO = 7; 94 private final static int COMMAND_MAXTHREADS = 8; 95 private final static int COMMAND_TIMESLICE = 9; 96 private final static int COMMAND_PROPERTIES = 10; 97 private final static int COMMAND_UNKNOWN = -1; 98 private final static String [] DASHARGS = 99 {"p","d","u","ld","ea","ep", "b", "h", "s"}; 100 private final static int DASHARG_PORT = 0; 101 private final static int DASHARG_DATABASE = 1; 102 private final static int DASHARG_USER = 2; 103 private final static int DASHARG_LOADSYSIBM = 3; 104 private final static int DASHARG_ENCALG = 4; 105 private final static int DASHARG_ENCPRV = 5; 106 private final static int DASHARG_BOOTPASSWORD = 6; 107 private final static int DASHARG_HOST = 7; 108 private final static int DASHARG_SESSION = 8; 109 110 private final static int PROTOCOL_VERSION = 1; 113 private final static String COMMAND_HEADER = "CMD:"; 114 private final static String REPLY_HEADER = "RPY:"; 115 private final static int REPLY_HEADER_LENGTH = REPLY_HEADER.length(); 116 private final static int OK = 0; 117 private final static int WARNING = 1; 118 private final static int ERROR = 2; 119 private final static int SQLERROR = 3; 120 private final static int SQLWARNING = 4; 121 122 private final static String DRDA_PROP_MESSAGES = "org.apache.derby.loc.drda.messages"; 123 private final static String DRDA_PROP_DEBUG = "derby.drda.debug"; 124 private final static String CLOUDSCAPE_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; 125 126 public final static String UNEXPECTED_ERR = "Unexpected exception"; 127 128 private final static int MIN_MAXTHREADS = -1; 129 private final static int MIN_TIMESLICE = -1; 130 private final static int USE_DEFAULT = -1; 131 private final static int DEFAULT_MAXTHREADS = 0; private final static int DEFAULT_TIMESLICE = 0; 134 private final static String DEFAULT_HOST = "localhost"; 135 private final static String DRDA_MSG_PREFIX = "DRDA_"; 136 private final static String DEFAULT_LOCALE= "en"; 137 private final static String DEFAULT_LOCALE_COUNTRY="US"; 138 139 private final static int SHUTDOWN_CHECK_ATTEMPTS = 20; 141 private final static int SHUTDOWN_CHECK_INTERVAL= 500; 142 143 private final static int MAXREPLY = 32767; 145 146 protected static String att_srvclsnm; 148 protected final static String ATT_SRVNAM = "NetworkServerControl"; 149 150 protected static String att_extnam; 151 protected static String att_srvrlslv; 152 protected static String prdId; 153 protected static byte[] prdIdBytes_; 154 155 private static String buildNumber; 156 private static String versionString; 157 protected final static int CCSIDSBC = 1208; protected final static int CCSIDMBC = 1208; protected final static String DEFAULT_ENCODING = "UTF8"; protected final static int DEFAULT_CCSID = 1208; 165 protected final static byte SPACE_CHAR = 32; 166 167 168 protected final static int [] MGR_LEVELS = { 7, 4, 0, 0, 5, 0, 7, 0, 7, 7, 0, 0, 7 }; 184 185 186 protected PrintWriter logWriter; protected PrintWriter cloudscapeLogWriter; private static Driver cloudscapeDriver; 189 190 private final static int ERRTYPE_SEVERE = 1; 192 private final static int ERRTYPE_USER = 2; 193 private final static int ERRTYPE_INFO = 3; 194 private final static int ERRTYPE_UNKNOWN = -1; 195 196 private Vector commandArgs = new Vector (); 198 private String databaseArg; 199 private String userArg; 200 private String passwordArg; 201 private String bootPasswordArg; 202 private String encAlgArg; 203 private String encPrvArg; 204 private String hostArg = DEFAULT_HOST; 205 private InetAddress hostAddress; 206 private int sessionArg; 207 208 private memCheck mc; 210 211 private byte [] replyBuffer; 213 private int replyBufferCount; private int replyBufferPos; 216 private int portNumber = NetworkServerControl.DEFAULT_PORTNUMBER; 222 private String traceDirectory; private Object traceDirectorySync = new Object (); private boolean traceAll; private Object traceAllSync = new Object (); private Object serverStartSync = new Object (); private boolean logConnections; private Object logConnectionsSync = new Object (); private int minThreads; private int maxThreads; private Object threadsSync = new Object (); private int timeSlice; private Object timeSliceSync = new Object (); 240 private boolean keepAlive = true; private int minPoolSize; private int maxPoolSize; private Object poolSync = new Object (); 245 protected boolean debugOutput = false; 246 private boolean cleanupOnStart = false; private boolean restartFlag = false; 248 249 protected final static int INVALID_OR_NOTSET_SECURITYMECHANISM = -1; 250 private int allowOnlySecurityMechanism = INVALID_OR_NOTSET_SECURITYMECHANISM; 254 private Socket clientSocket = null; 258 private InputStream clientIs = null; 259 private OutputStream clientOs = null; 260 private ByteArrayOutputStream byteArrayOs = new ByteArrayOutputStream (); 261 private DataOutputStream commandOs = new DataOutputStream (byteArrayOs); 262 263 private Object shutdownSync = new Object (); 264 private boolean shutdown; 265 private int connNum; private ServerSocket serverSocket; 267 private NetworkServerControlImpl serverInstance; 268 private LocalizedResource langUtil; 269 public String clientLocale; 270 ArrayList localAddresses; 273 private Hashtable sessionTable = new Hashtable (); 275 276 private Session currentSession; 278 private Vector threadList = new Vector (); 280 281 private Vector runQueue = new Vector (); 284 285 private int freeThreads; 287 288 private Hashtable appRequesterTable = new Hashtable (); 290 291 private String propertyFileName; 293 private Runnable acceptClients; 294 295 296 private boolean shutdownDatabasesOnShutdown = false; 299 300 304 private static boolean SUPPORTS_EUSRIDPWD = false; 305 306 313 static 314 { 315 try 316 { 317 new DecryptionManager(); 323 SUPPORTS_EUSRIDPWD = true; 324 }catch(Exception e) 325 { 326 SUPPORTS_EUSRIDPWD = false; 330 } 331 } 332 333 public NetworkServerControlImpl() throws Exception 335 { 336 init(); 337 getPropertyInfo(); 338 } 339 340 341 348 public NetworkServerControlImpl(InetAddress address, int portNumber) throws Exception 349 { 350 init(); 351 getPropertyInfo(); 352 this.hostAddress = address; 353 this.portNumber = (portNumber <= 0) ? 354 this.portNumber: portNumber; 355 this.hostArg = address.getHostAddress(); 356 } 357 358 private void init() throws Exception 359 { 360 361 langUtil = new LocalizedResource(null,null,DRDA_PROP_MESSAGES); 363 364 serverInstance = this; 365 366 ProductVersionHolder myPVH = getNetProductVersionHolder(); 368 att_extnam = ATT_SRVNAM + " " + java.lang.Thread.currentThread().getName(); 369 370 att_srvclsnm = myPVH.getProductName(); 371 versionString = myPVH.getVersionBuildString(true); 372 373 String majorStr = String.valueOf(myPVH.getMajorVersion()); 374 String minorStr = String.valueOf(myPVH.getMinorVersion()); 375 String drdaMaintStr = String.valueOf(myPVH.getDrdaMaintVersion()); 378 379 384 prdId = DRDAConstants.DERBY_DRDA_SERVER_ID; 385 if (majorStr.length() == 1) 386 prdId += "0"; 387 prdId += majorStr; 388 389 if (minorStr.length() == 1) 390 prdId += "0"; 391 392 prdId += minorStr; 393 394 prdId += drdaMaintStr; 395 att_srvrlslv = prdId + "/" + myPVH.getVersionBuildString(true); 396 prdIdBytes_ = prdId.getBytes(DEFAULT_ENCODING); 398 399 if (SanityManager.DEBUG) 400 { 401 if (majorStr.length() > 2 || 402 minorStr.length() > 2 || 403 drdaMaintStr.length() > 1) 404 SanityManager.THROWASSERT("version values out of expected range for PRDID"); 405 } 406 407 buildNumber = myPVH.getBuildNumber(); 408 } 409 410 private PrintWriter makePrintWriter( OutputStream out) 411 { 412 if (out != null) 413 return new PrintWriter (out, true ); 414 else 415 return null; 416 } 417 418 protected static Driver getDriver() 419 { 420 return cloudscapeDriver; 421 } 422 423 424 431 432 433 439 public void setLogWriter(PrintWriter outWriter) 440 { 441 if ( outWriter != null ) 446 logWriter = new PrintWriter (outWriter,true); 447 else 448 logWriter = outWriter; 449 } 450 451 452 453 460 public void consoleError(String msg) 461 throws Exception 462 { 463 consoleMessage(msg); 464 throw new Exception (msg); 465 } 466 467 473 public void consoleExceptionPrint(Exception e) 474 { 475 if (debugOutput == true) 476 consoleExceptionPrintTrace(e); 477 478 return; 479 } 480 481 487 public void consoleExceptionPrintTrace(Throwable e) 488 { 489 consoleMessage(e.getMessage()); 490 if (logWriter != null) 491 { 492 synchronized (logWriter) { 493 e.printStackTrace(logWriter); 494 } 495 } 496 else 497 { 498 e.printStackTrace(); 499 } 500 501 if (cloudscapeLogWriter != null) 502 { 503 synchronized(cloudscapeLogWriter) { 504 e.printStackTrace(cloudscapeLogWriter); 505 } 506 } 507 } 508 509 510 511 512 517 public void consoleMessage(String msg) 518 { 519 if (logWriter != null) 521 { 522 synchronized(logWriter) { 523 logWriter.println(msg); 524 } 525 } 526 if (cloudscapeLogWriter != null) 528 synchronized(cloudscapeLogWriter) 529 { 530 Monitor.logMessage(msg); 531 } 532 } 533 534 535 536 548 public void start(PrintWriter consoleWriter) 549 throws Exception 550 { 551 DRDAServerStarter starter = new DRDAServerStarter(); 552 starter.setStartInfo(hostAddress,portNumber,consoleWriter); 553 startNetworkServer(); 554 starter.boot(false,null); 555 } 556 557 558 567 public void blockingStart(PrintWriter consoleWriter) 568 throws Exception 569 { 570 startNetworkServer(); 571 setLogWriter(consoleWriter); 572 cloudscapeLogWriter = Monitor.getStream().getPrintWriter(); 573 if (SanityManager.DEBUG && debugOutput) 574 { 575 memCheck.showmem(); 576 mc = new memCheck(200000); 577 mc.start(); 578 } 579 try{ 581 serverSocket = (ServerSocket ) AccessController.doPrivileged( 582 new PrivilegedExceptionAction () { 583 public Object run() throws IOException ,UnknownHostException 584 { 585 if (hostAddress == null) 586 hostAddress = InetAddress.getByName(hostArg); 587 buildLocalAddressList(hostAddress); 591 return new ServerSocket (portNumber 592 ,0, 593 hostAddress); 594 } 595 } 596 ); 597 } catch (PrivilegedActionException e) { 598 Exception e1 = e.getException(); 599 if (e1 instanceof IOException ) 600 consolePropertyMessage("DRDA_ListenPort.S", 601 new String [] { 602 Integer.toString(portNumber), 603 hostArg}); 604 if (e1 instanceof UnknownHostException ) { 605 consolePropertyMessage("DRDA_UnknownHost.S", hostArg); 606 } 607 else 608 throw e1; 609 } catch (Exception e) { 610 throwUnexpectedException(e); 613 } 614 615 consolePropertyMessage("DRDA_Ready.I", new String [] 616 {Integer.toString(portNumber), att_srvclsnm, versionString, 617 getFormattedTimestamp()}); 618 619 acceptClients = (Runnable )new ClientThread(this, serverSocket); 622 Thread clientThread = (Thread ) AccessController.doPrivileged( 623 new PrivilegedExceptionAction () { 624 public Object run() throws Exception 625 { 626 return new Thread (acceptClients); 627 } 628 } 629 ); 630 clientThread.start(); 631 632 synchronized(shutdownSync) { 634 try { 635 shutdownSync.wait(); 636 } 637 catch (InterruptedException e) 638 { 639 shutdown = true; 640 } 641 } 642 643 if (mc != null) 645 mc.interrupt(); 646 647 clientThread.interrupt(); 649 650 synchronized(sessionTable) { 652 for (Enumeration e = sessionTable.elements(); e.hasMoreElements(); ) 653 { 654 Session session = (Session) e.nextElement(); 655 session.close(); 656 } 657 } 658 659 synchronized (threadList) 660 { 661 for (int i = 0; i < threadList.size(); i++) 663 { 664 ((DRDAConnThread)threadList.get(i)).close(); 665 ((DRDAConnThread)threadList.get(i)).interrupt(); 666 } 667 threadList.clear(); 668 } 669 670 671 672 673 try{ 675 serverSocket.close(); 676 }catch(IOException e){ 677 consolePropertyMessage("DRDA_ListenerClose.S"); 678 } 679 680 681 synchronized (runQueue) { 684 runQueue.notifyAll(); 685 } 686 687 if (shutdownDatabasesOnShutdown) { 688 689 try { 691 if (cloudscapeDriver != null) 692 cloudscapeDriver.connect("jdbc:derby:;shutdown=true", 693 (Properties ) null); 694 } catch (SQLException sqle) { 695 String expectedState = 699 StandardException.getSQLStateFromIdentifier( 700 SQLState.CLOUDSCAPE_SYSTEM_SHUTDOWN); 701 if (!expectedState.equals(sqle.getSQLState())) { 702 consolePropertyMessage("DRDA_ShutdownWarning.I", 703 sqle.getMessage()); 704 } 705 } 706 } 707 708 consolePropertyMessage("DRDA_ShutdownSuccess.I", new String [] 709 {att_srvclsnm, versionString, 710 getFormattedTimestamp()}); 711 712 713 } 714 715 722 723 724 725 726 protected void startNetworkServer() throws Exception 727 { 728 729 boolean restartCheck = this.restartFlag; 731 synchronized (serverStartSync) { 732 733 if (restartCheck == this.restartFlag) { 734 739 try { 740 741 if (cleanupOnStart) { 742 745 synchronized (runQueue) { 747 for (int i = 0; i < runQueue.size(); i++) { 748 Session s = (Session) runQueue.get(i); 749 s.close(); 750 removeFromSessionTable(s.getConnNum()); 751 } 752 runQueue.clear(); 753 } 754 755 762 cloudscapeDriver = null; System.gc(); 765 } 766 767 Class.forName(CLOUDSCAPE_DRIVER).newInstance(); 769 cloudscapeDriver = DriverManager.getDriver(Attribute.PROTOCOL); 770 771 } 772 catch (Exception e) { 773 consolePropertyMessage("DRDA_LoadException.S", e.getMessage()); 774 } 775 cleanupOnStart = true; 776 this.restartFlag = !this.restartFlag; 777 } 778 } 783 } 784 785 790 public void shutdown() 791 throws Exception 792 { 793 setUpSocket(); 794 writeCommandHeader(COMMAND_SHUTDOWN); 795 send(); 796 readResult(); 797 PrintWriter savWriter = logWriter; 800 FilterOutputStream silentStream = new FilterOutputStream (null) { 804 public void write(int b) { } 805 public void flush() { } 806 public void close() { } 807 }; 808 setLogWriter(new PrintWriter (silentStream)); 809 int ntry; 810 for (ntry = 0; ntry < SHUTDOWN_CHECK_ATTEMPTS; ntry++) 811 { 812 Thread.sleep(SHUTDOWN_CHECK_INTERVAL); 813 try { 814 ping(); 815 } catch (Exception e) 816 { 817 if (ntry == SHUTDOWN_CHECK_ATTEMPTS) 819 consolePropertyMessage("DRDA_ShutdownError.S", new String [] { 820 Integer.toString(portNumber), 821 hostArg}); 822 break; 823 } 824 } 825 logWriter= savWriter; 826 return; 827 } 828 829 833 public void directShutdown() { 834 shutdown = true; 835 synchronized(shutdownSync) { 836 shutdownSync.notifyAll(); 838 } 839 840 } 841 842 843 845 public boolean isServerStarted() throws Exception 846 { 847 try { 848 ping(); 849 } 850 catch (Exception e) { 851 return false; 852 } 853 return true; 854 } 855 856 public void ping() throws Exception 857 { 858 String database = null; String user = null; 863 String password = null; 864 865 setUpSocket(); 866 writeCommandHeader(COMMAND_TESTCONNECTION); 867 writeLDString(database); 868 writeLDString(user); 869 writeLDString(password); 870 send(); 871 readResult(); 872 873 } 874 875 876 883 public void trace(boolean on) 884 throws Exception 885 { 886 trace(0, on); 887 } 888 889 897 public void trace(int connNum, boolean on) 898 throws Exception 899 { 900 setUpSocket(); 901 writeCommandHeader(COMMAND_TRACE); 902 commandOs.writeInt(connNum); 903 writeByte(on ? 1 : 0); 904 send(); 905 readResult(); 906 consoleTraceMessage(connNum, on); 907 } 908 909 916 private void consoleTraceMessage(int connNum, boolean on) 917 throws Exception 918 { 919 if (connNum == 0) 920 consolePropertyMessage("DRDA_TraceChangeAll.I", on ? "DRDA_ON.I" : "DRDA_OFF.I"); 921 else 922 { 923 String [] args = new String [2]; 924 args[0] = on ? "DRDA_ON.I" : "DRDA_OFF.I"; 925 args[1] = new Integer (connNum).toString(); 926 consolePropertyMessage("DRDA_TraceChangeOne.I", args); 927 } 928 } 929 930 938 public void logConnections(boolean on) 939 throws Exception 940 { 941 setUpSocket(); 942 writeCommandHeader(COMMAND_LOGCONNECTIONS); 943 writeByte(on ? 1 : 0); 944 send(); 945 readResult(); 946 } 947 948 951 public void sendSetTraceDirectory(String traceDirectory) 952 throws Exception 953 { 954 setUpSocket(); 955 writeCommandHeader(COMMAND_TRACEDIRECTORY); 956 writeLDString(traceDirectory); 957 send(); 958 readResult(); 959 } 960 961 964 public String sysinfo() 965 throws Exception 966 { 967 setUpSocket(); 968 writeCommandHeader(COMMAND_SYSINFO); 969 send(); 970 return readStringReply("DRDA_SysInfoError.S"); 971 } 972 973 976 public String runtimeInfo() 977 throws Exception 978 { 979 setUpSocket(); 980 writeCommandHeader(COMMAND_RUNTIME_INFO); 981 send(); 982 return readStringReply("DRDA_RuntimeInfoError.S"); 983 } 984 985 989 public void usage() 990 { 991 try { 992 for (int i = 1; i <= NO_USAGE_MSGS; i++) 993 consolePropertyMessage("DRDA_Usage"+i+".I"); 994 } catch (Exception e) {} } 996 997 1006 public void netSetMaxThreads(int max) throws Exception 1007 { 1008 setUpSocket(); 1009 writeCommandHeader(COMMAND_MAXTHREADS); 1010 commandOs.writeInt(max); 1011 send(); 1012 readResult(); 1013 int newval = readInt(); 1014 consolePropertyMessage("DRDA_MaxThreadsChange.I", 1015 new Integer (newval).toString()); 1016 } 1017 1018 1026 public void netSetTimeSlice(int timeslice) 1027 throws Exception 1028 { 1029 setUpSocket(); 1030 writeCommandHeader(COMMAND_TIMESLICE); 1031 commandOs.writeInt(timeslice); 1032 send(); 1033 readResult(); 1034 int newval = readInt(); 1035 consolePropertyMessage("DRDA_TimeSliceChange.I", 1036 new Integer (newval).toString()); 1037 } 1038 1039 1045 public Properties getCurrentProperties() 1046 throws Exception 1047 { 1048 setUpSocket(); 1049 writeCommandHeader(COMMAND_PROPERTIES); 1050 send(); 1051 byte [] val = readBytesReply("DRDA_PropertyError.S"); 1052 Properties p = new Properties (); 1053 try { 1054 ByteArrayInputStream bs = new ByteArrayInputStream (val); 1055 p.load(bs); 1056 } catch (IOException io) { 1057 consolePropertyMessage("DRDA_IOException.S", 1058 io.getMessage()); 1059 } 1060 return p; 1061 } 1062 1063 1078 public static void setUniqueThreadName(Thread thrd, String newName) { 1079 1080 if (thrd.getName().indexOf("Thread-") == -1) { 1084 return; 1086 } 1087 else { 1088 String oldName = thrd.getName(); 1089 thrd.setName(newName + "_" + 1090 oldName.substring(oldName.indexOf("-")+1, oldName.length())); 1091 } 1093 return; 1094 1095 } 1096 1097 1098 1099 1100 1105 protected void removeFromSessionTable(int sessionid) 1106 { 1107 sessionTable.remove(new Integer (sessionid)); 1108 } 1109 1110 1225 protected synchronized void processCommands(DDMReader reader, DDMWriter writer, 1226 Session session) throws Throwable 1227 { 1228 try { 1229 String protocolStr = reader.readCmdString(4); 1230 String locale = DEFAULT_LOCALE; 1231 String codeset = null; 1232 int version = reader.readNetworkShort(); 1234 if (version <= 0 || version > PROTOCOL_VERSION) 1235 throw new Throwable (langUtil.getTextMessage("DRDA_UnknownProtocol.S", new Integer (version).toString())); 1236 int localeLen = reader.readByte(); 1237 if (localeLen > 0) 1238 { 1239 currentSession = session; 1240 locale = reader.readCmdString(localeLen); 1241 session.langUtil = new LocalizedResource(codeset,locale,DRDA_PROP_MESSAGES); 1242 } 1243 String notLocalMessage = null; 1244 int codesetLen = reader.readByte(); 1246 int command = reader.readByte(); 1247 if (command != COMMAND_TESTCONNECTION) 1248 { 1249 try { 1250 checkAddressIsLocal(session.clientSocket.getInetAddress()); 1251 }catch (Exception e) 1252 { 1253 notLocalMessage = e.getMessage(); 1254 } 1255 } 1256 if (notLocalMessage != null) 1257 { 1258 sendMessage(writer, ERROR,notLocalMessage); 1259 session.langUtil = null; 1260 currentSession = null; 1261 return; 1262 } 1263 1264 switch(command) 1265 { 1266 case COMMAND_SHUTDOWN: 1267 sendOK(writer); 1268 directShutdown(); 1269 break; 1270 case COMMAND_TRACE: 1271 sessionArg = reader.readNetworkInt(); 1272 boolean on = (reader.readByte() == 1); 1273 if (setTrace(on)) 1274 { 1275 sendOK(writer); 1276 } 1277 else 1278 { 1279 sendMessage(writer, ERROR, 1280 localizeMessage("DRDA_SessionNotFound.U", 1281 (session.langUtil == null) ? langUtil : session.langUtil, 1282 new String [] {new Integer (sessionArg).toString()})); 1283 } 1284 break; 1285 case COMMAND_TRACEDIRECTORY: 1286 setTraceDirectory(reader.readCmdString()); 1287 sendOK(writer); 1288 consolePropertyMessage("DRDA_TraceDirectoryChange.I", traceDirectory); 1289 break; 1290 case COMMAND_TESTCONNECTION: 1291 databaseArg = reader.readCmdString(); 1292 userArg = reader.readCmdString(); 1293 passwordArg = reader.readCmdString(); 1294 if (databaseArg != null) 1295 connectToDatabase(writer, databaseArg, userArg, passwordArg); 1296 else 1297 sendOK(writer); 1298 break; 1299 case COMMAND_LOGCONNECTIONS: 1300 boolean log = (reader.readByte() == 1); 1301 setLogConnections(log); 1302 sendOK(writer); 1303 consolePropertyMessage("DRDA_LogConnectionsChange.I", 1304 (log ? "DRDA_ON.I" : "DRDA_OFF.I")); 1305 break; 1306 case COMMAND_SYSINFO: 1307 sendSysInfo(writer); 1308 break; 1309 case COMMAND_PROPERTIES: 1310 sendPropInfo(writer); 1311 break; 1312 case COMMAND_RUNTIME_INFO: 1313 sendRuntimeInfo(writer); 1314 break; 1315 case COMMAND_MAXTHREADS: 1316 int max = reader.readNetworkInt(); 1317 try { 1318 setMaxThreads(max); 1319 }catch (Exception e) { 1320 sendMessage(writer, ERROR, e.getMessage()); 1321 return; 1322 } 1323 int newval = getMaxThreads(); 1324 sendOKInt(writer, newval); 1325 consolePropertyMessage("DRDA_MaxThreadsChange.I", 1326 new Integer (newval).toString()); 1327 break; 1328 case COMMAND_TIMESLICE: 1329 int timeslice = reader.readNetworkInt(); 1330 try { 1331 setTimeSlice(timeslice); 1332 }catch (Exception e) { 1333 sendMessage(writer, ERROR, e.getMessage()); 1334 return; 1335 } 1336 newval = getTimeSlice(); 1337 sendOKInt(writer, newval); 1338 consolePropertyMessage("DRDA_TimeSliceChange.I", 1339 new Integer (newval).toString()); 1340 break; 1341 } 1342 } catch (DRDAProtocolException e) { 1343 consoleExceptionPrintTrace(e); 1345 1346 } catch (Exception e) { 1347 consoleExceptionPrintTrace(e); 1348 } 1349 finally { 1350 session.langUtil = null; 1351 currentSession = null; 1352 } 1353 } 1354 1365 protected Session getNextSession(Session currentSession) 1366 { 1367 Session retval = null; 1368 if (shutdown == true) 1369 return retval; 1370 synchronized (runQueue) 1371 { 1372 try { 1373 if (runQueue.size() == 0) 1375 { 1376 if (currentSession == null) 1378 { 1379 while (runQueue.size() == 0) 1380 { 1381 freeThreads++; 1384 runQueue.wait(); 1385 if (shutdown == true) 1386 return null; 1387 freeThreads--; 1388 } 1389 } 1390 else 1391 return currentSession; 1392 } 1393 retval = (Session) runQueue.elementAt(0); 1394 runQueue.removeElementAt(0); 1395 if (currentSession != null) 1396 runQueueAdd(currentSession); 1397 } catch (InterruptedException e) { 1398 freeThreads--; 1403 } 1404 } 1405 return retval; 1406 } 1407 1414 protected AppRequester getAppRequester(AppRequester appRequester) 1415 { 1416 AppRequester s = null; 1417 1418 if (SanityManager.DEBUG) { 1419 if (appRequester == null) 1420 SanityManager.THROWASSERT("null appRequester in getAppRequester"); 1421 } 1422 1423 if (!appRequesterTable.isEmpty()) 1424 s = (AppRequester)appRequesterTable.get(appRequester.prdid); 1425 1426 if (s == null) 1427 { 1428 appRequesterTable.put(appRequester.prdid, appRequester); 1429 return appRequester; 1430 } 1431 else 1432 { 1433 if (s.equals(appRequester)) 1436 return s; 1437 else 1438 return appRequester; 1439 } 1440 } 1441 1447 protected int getManagerLevel(int manager) 1448 { 1449 int mindex = CodePoint.getManagerIndex(manager); 1450 if (SanityManager.DEBUG) { 1451 if (mindex == CodePoint.UNKNOWN_MANAGER) 1452 SanityManager.THROWASSERT("manager out of bounds"); 1453 } 1454 return MGR_LEVELS[mindex]; 1455 } 1456 1462 protected boolean supportsCCSID(int ccsid) 1463 { 1464 try { 1465 CharacterEncodings.getJavaEncoding(ccsid); 1466 } 1467 catch (Exception e) { 1468 return false; 1469 } 1470 return true; 1471 } 1472 1479 protected void consolePropertyMessage(String msgProp) 1480 throws Exception 1481 { 1482 consolePropertyMessageWork(msgProp, null); 1483 } 1484 1492 protected void consolePropertyMessage(String msgProp, String arg) 1493 throws Exception 1494 { 1495 consolePropertyMessageWork(msgProp, new String [] {arg}); 1496 } 1497 1505 protected void consolePropertyMessage(String msgProp, String [] args) 1506 throws Exception 1507 { 1508 consolePropertyMessageWork(msgProp, args); 1509 } 1510 1515 protected static boolean isCmd(String val) 1516 { 1517 if (val.equals(COMMAND_HEADER)) 1518 return true; 1519 else 1520 return false; 1521 } 1522 1523 1524 1525 1526 1533 private void writeCommandReplyHeader(DDMWriter writer) throws Exception 1534 { 1535 writer.setCMDProtocol(); 1536 writer.writeString(REPLY_HEADER); 1537 } 1538 1539 1546 private void sendOK(DDMWriter writer) throws Exception 1547 { 1548 writeCommandReplyHeader(writer); 1549 writer.writeByte(OK); 1550 writer.flush(); 1551 } 1552 1560 private void sendOKInt(DDMWriter writer, int val) throws Exception 1561 { 1562 writeCommandReplyHeader(writer); 1563 writer.writeByte(OK); 1564 writer.writeNetworkInt(val); 1565 writer.flush(); 1566 } 1567 1576 private void sendMessage(DDMWriter writer, int messageType, String message) 1577 throws Exception 1578 { 1579 writeCommandReplyHeader(writer); 1580 writer.writeByte(messageType); 1581 writer.writeLDString(message); 1582 writer.flush(); 1583 } 1584 1593 private void sendSQLMessage(DDMWriter writer, SQLException se, int type) 1594 throws Exception 1595 { 1596 StringBuffer locMsg = new StringBuffer (); 1597 while (se != null) 1599 { 1600 if (currentSession != null && currentSession.langUtil != null && 1601 se instanceof EmbedSQLException) 1602 { 1603 locMsg.append(se.getSQLState()+":"+ 1604 MessageService.getLocalizedMessage( 1605 currentSession.langUtil.getLocale(), ((EmbedSQLException)se).getMessageId(), 1606 ((EmbedSQLException)se).getArguments())); 1607 } 1608 else 1609 locMsg.append(se.getSQLState()+":"+se.getMessage()); 1610 se = se.getNextException(); 1611 if (se != null) 1612 locMsg.append("\n"); 1613 } 1614 sendMessage(writer, type, locMsg.toString()); 1615 } 1616 1623 private void sendSysInfo(DDMWriter writer) throws Exception 1624 { 1625 StringBuffer sysinfo = new StringBuffer (); 1626 sysinfo.append(getNetSysInfo()); 1627 sysinfo.append(getCLSSysInfo()); 1628 try { 1629 writeCommandReplyHeader(writer); 1630 writer.writeByte(0); writer.writeLDString(sysinfo.toString()); 1632 } catch (DRDAProtocolException e) { 1633 consolePropertyMessage("DRDA_SysInfoWriteError.S", e.getMessage()); 1634 } 1635 writer.flush(); 1636 } 1637 1638 1645 private void sendRuntimeInfo(DDMWriter writer) throws Exception 1646 { 1647 try { 1648 writeCommandReplyHeader(writer); 1649 writer.writeByte(0); writer.writeLDString(getRuntimeInfo()); 1651 } catch (DRDAProtocolException e) { 1652 consolePropertyMessage("DRDA_SysInfoWriteError.S", e.getMessage()); 1653 } 1654 writer.flush(); 1655 } 1656 1657 1658 1659 1666 private void sendPropInfo(DDMWriter writer) throws Exception 1667 { 1668 try { 1669 ByteArrayOutputStream out = new ByteArrayOutputStream (); 1670 Properties p = getPropertyValues(); 1671 p.store(out, "NetworkServerControl properties"); 1672 try { 1673 writeCommandReplyHeader(writer); 1674 writer.writeByte(0); writer.writeLDBytes(out.toByteArray()); 1676 } catch (DRDAProtocolException e) { 1677 consolePropertyMessage("DRDA_PropInfoWriteError.S", e.getMessage()); 1678 } 1679 writer.flush(); 1680 } 1681 catch (Exception e) { 1682 consoleExceptionPrintTrace(e); 1683 } 1684 } 1685 1686 1691 private String getNetSysInfo() 1692 { 1693 StringBuffer sysinfo = new StringBuffer (); 1694 LocalizedResource localLangUtil = langUtil; 1695 if (currentSession != null && currentSession.langUtil != null) 1696 localLangUtil = currentSession.langUtil; 1697 sysinfo.append(localLangUtil.getTextMessage("DRDA_SysInfoBanner.I")+ "\n"); 1698 sysinfo.append(localLangUtil.getTextMessage("DRDA_SysInfoVersion.I")+ " " + att_srvrlslv); 1699 sysinfo.append(" "); 1700 sysinfo.append(localLangUtil.getTextMessage("DRDA_SysInfoBuild.I")+ " " + buildNumber); 1701 sysinfo.append(" "); 1702 sysinfo.append(localLangUtil.getTextMessage("DRDA_SysInfoDrdaPRDID.I")+ " " + prdId); 1703 if (SanityManager.DEBUG) 1704 { 1705 sysinfo.append(" ** SANE BUILD **"); 1706 } 1707 sysinfo.append("\n"); 1708 Properties p = getPropertyValues(); 1710 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 1711 PrintStream ps = new PrintStream (bos); 1712 p.list(ps); 1713 sysinfo.append(bos.toString()); 1714 return sysinfo.toString(); 1715 } 1716 1717 1720 private String getRuntimeInfo() 1721 { 1722 return buildRuntimeInfo(langUtil); 1723 } 1724 1725 1732 private String getCLSSysInfo() throws IOException 1733 { 1734 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 1735 LocalizedResource localLangUtil = langUtil; 1736 if (currentSession != null && currentSession.langUtil != null) 1737 localLangUtil = currentSession.langUtil; 1738 LocalizedOutput aw = localLangUtil.getNewOutput(bos); 1739 org.apache.derby.impl.tools.sysinfo.Main.getMainInfo(aw, false); 1740 return bos.toString(); 1741 } 1742 1743 1744 1752 public void executeWork(String args[]) throws Exception 1753 { 1754 logWriter = makePrintWriter(System.out); 1756 1757 int command = 0; 1758 if (args.length > 0) 1759 command = findCommand(args); 1760 else 1761 { 1762 consolePropertyMessage("DRDA_NoArgs.U"); 1763 } 1764 1765 if (command == COMMAND_UNKNOWN) 1767 return; 1768 1769 if (commandArgs.size() != COMMAND_ARGS[command]) 1771 consolePropertyMessage("DRDA_InvalidNoArgs.U", COMMANDS[command]); 1772 int min; 1773 int max; 1774 1775 1776 switch (command) 1777 { 1778 case COMMAND_START: 1779 shutdownDatabasesOnShutdown = true; 1782 blockingStart(makePrintWriter(System.out)); 1783 break; 1784 case COMMAND_SHUTDOWN: 1785 shutdown(); 1786 consolePropertyMessage("DRDA_ShutdownSuccess.I", new String [] 1787 {att_srvclsnm, versionString, 1788 getFormattedTimestamp()}); 1789 break; 1790 case COMMAND_TRACE: 1791 { 1792 boolean on = isOn((String )commandArgs.elementAt(0)); 1793 trace(sessionArg, on); 1794 consoleTraceMessage(sessionArg, on); 1795 break; 1796 } 1797 case COMMAND_TRACEDIRECTORY: 1798 setTraceDirectory((String ) commandArgs.elementAt(0)); 1799 consolePropertyMessage("DRDA_TraceDirectoryChange.I", traceDirectory); 1800 break; 1801 case COMMAND_TESTCONNECTION: 1802 ping(); 1803 consolePropertyMessage("DRDA_ConnectionTested.I", new String [] 1804 {hostArg, (new Integer (portNumber)).toString()}); 1805 break; 1806 case COMMAND_LOGCONNECTIONS: 1807 { 1808 boolean on = isOn((String )commandArgs.elementAt(0)); 1809 logConnections(on); 1810 consolePropertyMessage("DRDA_LogConnectionsChange.I", on ? "DRDA_ON.I" : "DRDA_OFF.I"); 1811 break; 1812 } 1813 case COMMAND_SYSINFO: 1814 { 1815 String info = sysinfo(); 1816 consoleMessage(info); 1817 break; 1818 } 1819 case COMMAND_MAXTHREADS: 1820 max = 0; 1821 try{ 1822 max = Integer.parseInt((String )commandArgs.elementAt(0)); 1823 }catch(NumberFormatException e){ 1824 consolePropertyMessage("DRDA_InvalidValue.U", new String [] 1825 {(String )commandArgs.elementAt(0), "maxthreads"}); 1826 } 1827 if (max < MIN_MAXTHREADS) 1828 consolePropertyMessage("DRDA_InvalidValue.U", new String [] 1829 {new Integer (max).toString(), "maxthreads"}); 1830 netSetMaxThreads(max); 1831 1832 break; 1833 case COMMAND_RUNTIME_INFO: 1834 String reply = runtimeInfo(); 1835 consoleMessage(reply); 1836 break; 1837 case COMMAND_TIMESLICE: 1838 int timeslice = 0; 1839 String timeSliceArg = (String )commandArgs.elementAt(0); 1840 try{ 1841 timeslice = Integer.parseInt(timeSliceArg); 1842 }catch(NumberFormatException e){ 1843 consolePropertyMessage("DRDA_InvalidValue.U", new String [] 1844 {(String )commandArgs.elementAt(0), "timeslice"}); 1845 } 1846 if (timeslice < MIN_TIMESLICE) 1847 consolePropertyMessage("DRDA_InvalidValue.U", new String [] 1848 {new Integer (timeslice).toString(), "timeslice"}); 1849 netSetTimeSlice(timeslice); 1850 1851 break; 1852 default: 1853 if (SanityManager.DEBUG) 1855 SanityManager.THROWASSERT("Invalid command in switch:"+ command); 1856 } 1857 } 1858 1859 1860 1865 private void runQueueAdd(Session clientSession) 1866 { 1867 synchronized(runQueue) 1868 { 1869 runQueue.addElement(clientSession); 1870 runQueue.notify(); 1871 } 1872 } 1873 1882 private int findCommand(String [] args) throws Exception 1883 { 1884 try { 1885 int i = 0; 1887 int newpos = 0; 1888 while (i < args.length) 1889 { 1890 if (args[i].startsWith("-")) 1891 { 1892 newpos = processDashArg(i, args); 1893 if (newpos == i) 1894 commandArgs.addElement(args[i++]); 1895 else 1896 i = newpos; 1897 } 1898 else 1899 commandArgs.addElement(args[i++]); 1900 } 1901 1902 if (commandArgs.size() > 0) 1904 { 1905 for (i = 0; i < COMMANDS.length; i++) 1906 { 1907 if (StringUtil.SQLEqualsIgnoreCase(COMMANDS[i], 1908 (String )commandArgs.firstElement())) 1909 { 1910 commandArgs.removeElementAt(0); 1911 return i; 1912 } 1913 } 1914 } 1915 consolePropertyMessage("DRDA_UnknownCommand.U", 1917 (String ) commandArgs.firstElement()); 1918 } catch (Exception e) { 1919 if (e.getMessage().equals(NetworkServerControlImpl.UNEXPECTED_ERR)) 1920 throw e; 1921 } 1924 return COMMAND_UNKNOWN; 1925 } 1926 1936 private int processDashArg(int pos, String [] args) 1937 throws Exception 1938 { 1939 char c = args[pos].charAt(1); 1941 if (c >= '0' && c <= '9') 1942 return pos; 1943 int dashArg = -1; 1944 for (int i = 0; i < DASHARGS.length; i++) 1945 { 1946 if (DASHARGS[i].equals(args[pos].substring(1))) 1947 { 1948 dashArg = i; 1949 pos++; 1950 break; 1951 } 1952 } 1953 if (dashArg == -1) 1954 consolePropertyMessage("DRDA_UnknownArgument.U", args[pos]); 1955 switch (dashArg) 1956 { 1957 case DASHARG_PORT: 1958 if (pos < args.length) 1959 { 1960 try{ 1961 portNumber = Integer.parseInt(args[pos]); 1962 }catch(NumberFormatException e){ 1963 consolePropertyMessage("DRDA_InvalidValue.U", 1964 new String [] {args[pos], "DRDA_PortNumber.I"}); 1965 } 1966 } 1967 else 1968 consolePropertyMessage("DRDA_MissingValue.U", "DRDA_PortNumber.I"); 1969 break; 1970 case DASHARG_HOST: 1971 if (pos < args.length) 1972 { 1973 hostArg = args[pos]; 1974 } 1975 else 1976 consolePropertyMessage("DRDA_MissingValue.U", "DRDA_Host.I"); 1977 break; 1978 case DASHARG_DATABASE: 1979 if (pos < args.length) 1980 databaseArg = args[pos]; 1981 else 1982 consolePropertyMessage("DRDA_MissingValue.U", 1983 "DRDA_DatabaseDirectory.I"); 1984 break; 1985 case DASHARG_USER: 1986 if (pos < args.length) 1987 { 1988 userArg = args[pos++]; 1989 if (pos < args.length) 1990 passwordArg = args[pos]; 1991 else 1992 consolePropertyMessage("DRDA_MissingValue.U", 1993 "DRDA_Password.I"); 1994 } 1995 else 1996 consolePropertyMessage("DRDA_MissingValue.U", "DRDA_User.I"); 1997 break; 1998 case DASHARG_ENCALG: 1999 if (pos < args.length) 2000 encAlgArg = args[pos]; 2001 else 2002 consolePropertyMessage("DRDA_MissingValue.U", 2003 "DRDA_EncryptionAlgorithm.I"); 2004 break; 2005 case DASHARG_ENCPRV: 2006 if (pos < args.length) 2007 encPrvArg = args[pos]; 2008 else 2009 consolePropertyMessage("DRDA_MissingValue.U", 2010 "DRDA_EncryptionProvider.I"); 2011 break; 2012 case DASHARG_LOADSYSIBM: 2013 break; 2014 case DASHARG_SESSION: 2015 if (pos < args.length) 2016 try{ 2017 sessionArg = Integer.parseInt(args[pos]); 2018 }catch(NumberFormatException e){ 2019 consolePropertyMessage("DRDA_InvalidValue.U", 2020 new String [] {args[pos], "DRDA_Session.I"}); 2021 } 2022 else 2023 consolePropertyMessage("DRDA_MissingValue.U", "DRDA_Session.I"); 2024 break; 2025 default: 2026 } 2028 return pos+1; 2029 } 2030 2031 2040 private boolean isOn(String arg) 2041 throws Exception 2042 { 2043 if (StringUtil.SQLEqualsIgnoreCase(arg, "on")) 2044 return true; 2045 else if (!StringUtil.SQLEqualsIgnoreCase(arg, "off")) 2046 consolePropertyMessage("DRDA_OnOffValue.U", arg); 2047 return false; 2048 } 2049 2050 2055 private void setUpSocket() throws Exception 2056 { 2057 2058 try { 2059 clientSocket = (Socket ) AccessController.doPrivileged( 2060 new PrivilegedExceptionAction () { 2061 2062 public Object run() throws UnknownHostException ,IOException 2063 { 2064 if (hostAddress == null) 2065 hostAddress = InetAddress.getByName(hostArg); 2066 2067 InetAddress connectAddress; 2071 if (JVMInfo.JDK_ID <= JVMInfo.J2SE_13 && 2072 hostAddress.getHostAddress().equals("0.0.0.0")) 2073 connectAddress = InetAddress.getLocalHost(); 2074 else 2075 connectAddress = hostAddress; 2076 2077 return new Socket (connectAddress, portNumber); 2078 } 2079 } 2080 ); 2081 } catch (PrivilegedActionException pae) { 2082 Exception e1 = pae.getException(); 2083 if (e1 instanceof UnknownHostException ) { 2084 consolePropertyMessage("DRDA_UnknownHost.S", hostArg); 2085 } 2086 else if (e1 instanceof IOException ) { 2087 consolePropertyMessage("DRDA_NoIO.S", 2088 new String [] {hostArg, (new Integer (portNumber)).toString()}); 2089 } 2090 } catch (Exception e) { 2091 throwUnexpectedException(e); 2094 } 2095 2096 try 2097 { 2098 clientIs = clientSocket.getInputStream(); 2099 clientOs = clientSocket.getOutputStream(); 2100 } catch (IOException e) { 2101 consolePropertyMessage("DRDA_NoInputStream.I"); 2102 throw e; 2103 } 2104 } 2105 2106 2107 private void checkAddressIsLocal(InetAddress inetAddr) throws UnknownHostException ,Exception 2108 { 2109 for(int i = 0; i < localAddresses.size(); i++) 2110 { 2111 if (inetAddr.equals((InetAddress )localAddresses.get(i))) 2112 { 2113 return; 2114 } 2115 } 2116 consolePropertyMessage("DRDA_NeedLocalHost.S", new String [] {inetAddr.getHostName(),serverSocket.getInetAddress().getHostName()}); 2117 2118 } 2119 2120 2121 2130 private void buildLocalAddressList(InetAddress bindAddr) 2131 { 2132 localAddresses = new ArrayList (3); 2133 localAddresses.add(bindAddr); 2134 try { 2135 localAddresses.add(InetAddress.getLocalHost()); 2136 localAddresses.add(InetAddress.getByName("localhost")); 2137 }catch(UnknownHostException uhe) 2138 { 2139 try { 2140 consolePropertyMessage("DRDA_UnknownHostWarning.I",uhe.getMessage()); 2141 } catch (Exception e) 2142 { } 2144 } 2145 } 2146 2147 2151 2152 2160 private void writeCommandHeader(int command) throws Exception 2161 { 2162 try { 2163 writeString(COMMAND_HEADER); 2164 commandOs.writeByte((byte)((PROTOCOL_VERSION & 0xf0) >> 8 )); 2165 commandOs.writeByte((byte)(PROTOCOL_VERSION & 0x0f)); 2166 2167 if (clientLocale != null && clientLocale != DEFAULT_LOCALE) 2168 { 2169 commandOs.writeByte(clientLocale.length()); 2170 commandOs.writeBytes(clientLocale); 2171 } 2172 else 2173 commandOs.writeByte((byte) 0); 2174 commandOs.writeByte((byte) 0); 2175 commandOs.writeByte((byte) command); 2176 } 2177 catch (IOException e) 2178 { 2179 clientSocketError(e); 2180 } 2181 } 2182 2189 private void writeLDString(String msg) throws Exception 2190 { 2191 try { 2192 if (msg == null) 2193 { 2194 commandOs.writeShort(0); 2195 } 2196 else 2197 { 2198 commandOs.writeShort(msg.length()); 2199 writeString(msg); 2200 } 2201 } 2202 catch (IOException e) 2203 { 2204 clientSocketError(e); 2205 } 2206 } 2207 2208 2212 2213 protected void writeString(String msg) throws Exception 2214 { 2215 byte[] msgBytes = msg.getBytes(DEFAULT_ENCODING); 2216 commandOs.write(msgBytes,0,msgBytes.length); 2217 } 2218 2219 2226 private void writeShort(int value) throws Exception 2227 { 2228 try { 2229 commandOs.writeByte((byte)((value & 0xf0) >> 8 )); 2230 commandOs.writeByte((byte)(value & 0x0f)); 2231 } 2232 catch (IOException e) 2233 { 2234 clientSocketError(e); 2235 } 2236 } 2237 2244 private void writeByte(int value) throws Exception 2245 { 2246 try { 2247 commandOs.writeByte((byte)(value & 0x0f)); 2248 } 2249 catch (IOException e) 2250 { 2251 clientSocketError(e); 2252 } 2253 } 2254 2260 private void send() throws Exception 2261 { 2262 try { 2263 byteArrayOs.writeTo(clientOs); 2264 commandOs.flush(); 2265 byteArrayOs.reset(); } 2267 catch (IOException e) 2268 { 2269 clientSocketError(e); 2270 } 2271 } 2272 2275 private void clientSocketError(IOException e) throws IOException 2276 { 2277 try { 2278 consolePropertyMessage("DRDA_ClientSocketError.S", e.getMessage()); 2279 } catch (Exception ce) {} consoleExceptionPrintTrace(e); 2282 throw e; 2283 } 2284 2289 private void readResult() throws Exception 2290 { 2291 fillReplyBuffer(); 2292 readCommandReplyHeader(); 2293 if (replyBufferPos >= replyBufferCount) 2294 consolePropertyMessage("DRDA_InvalidReplyTooShort.S"); 2295 int messageType = replyBuffer[replyBufferPos++] & 0xFF; 2296 if (messageType == OK) return; 2298 String message = readLDString(); 2300 if (messageType == SQLERROR) 2301 wrapSQLError(message); 2302 else if (messageType == SQLWARNING) 2303 wrapSQLWarning(message); 2304 else 2305 consolePropertyMessage(message); 2306 } 2307 2308 2309 2310 2318 private void ensureDataInBuffer(int minimumBytesNeeded) throws Exception 2319 { 2320 while ((replyBufferCount - replyBufferPos) < minimumBytesNeeded) 2322 { 2323 try { 2324 int bytesRead = clientIs.read(replyBuffer, replyBufferCount, replyBuffer.length - replyBufferCount); 2325 replyBufferCount += bytesRead; 2326 2327 } catch (IOException e) 2328 { 2329 clientSocketError(e); 2330 } 2331 } 2332 } 2333 2334 2335 2342 private void fillReplyBuffer() throws Exception 2343 { 2344 if (replyBuffer == null) 2345 replyBuffer = new byte[MAXREPLY]; 2346 try { 2347 replyBufferCount = clientIs.read(replyBuffer); 2348 } 2349 catch (IOException e) 2350 { 2351 clientSocketError(e); 2352 } 2353 if (replyBufferCount == -1) 2354 consolePropertyMessage("DRDA_InvalidReplyTooShort.S"); 2355 replyBufferPos = 0; 2356 } 2357 2362 private void readCommandReplyHeader() throws Exception 2363 { 2364 ensureDataInBuffer(REPLY_HEADER_LENGTH); 2365 if (replyBufferCount < REPLY_HEADER_LENGTH) 2366 { 2367 consolePropertyMessage("DRDA_InvalidReplyHeader1.S", Integer.toString(replyBufferCount)); 2368 } 2369 String header = new String (replyBuffer, 0, REPLY_HEADER_LENGTH, DEFAULT_ENCODING); 2370 if (!header.equals(REPLY_HEADER)) 2371 { 2372 consolePropertyMessage("DRDA_InvalidReplyHeader2.S", header); 2373 } 2374 replyBufferPos += REPLY_HEADER_LENGTH; 2375 } 2376 2380 private int readShort() throws Exception 2381 { 2382 ensureDataInBuffer(2); 2383 if (replyBufferPos + 2 > replyBufferCount) 2384 consolePropertyMessage("DRDA_InvalidReplyTooShort.S"); 2385 return ((replyBuffer[replyBufferPos++] & 0xff) << 8) + 2386 (replyBuffer[replyBufferPos++] & 0xff); 2387 } 2388 2392 private int readInt() throws Exception 2393 { 2394 ensureDataInBuffer(4); 2395 if (replyBufferPos + 4 > replyBufferCount) 2396 consolePropertyMessage("DRDA_InvalidReplyTooShort.S"); 2397 return ((replyBuffer[replyBufferPos++] & 0xff) << 24) + 2398 ((replyBuffer[replyBufferPos++] & 0xff) << 16) + 2399 ((replyBuffer[replyBufferPos++] & 0xff) << 8) + 2400 (replyBuffer[replyBufferPos++] & 0xff); 2401 } 2402 2409 private String readStringReply(String msgKey) throws Exception 2410 { 2411 fillReplyBuffer(); 2412 readCommandReplyHeader(); 2413 if (replyBuffer[replyBufferPos++] == 0) return readLDString(); 2415 else 2416 consolePropertyMessage(msgKey); 2417 return null; 2418 2419 } 2420 2421 2422 2423 2424 2430 private String readLDString() throws Exception 2431 { 2432 int strlen = readShort(); 2433 ensureDataInBuffer(strlen); 2434 if (replyBufferPos + strlen > replyBufferCount) 2435 consolePropertyMessage("DRDA_InvalidReplyTooShort.S"); 2436 String retval= new String (replyBuffer, replyBufferPos, strlen, DEFAULT_ENCODING); 2437 replyBufferPos += strlen; 2438 return retval; 2439 } 2440 2447 private byte [] readBytesReply(String msgKey) throws Exception 2448 { 2449 fillReplyBuffer(); 2450 readCommandReplyHeader(); 2451 if (replyBuffer[replyBufferPos++] == 0) return readLDBytes(); 2453 else 2454 consolePropertyMessage(msgKey); 2455 return null; 2456 2457 } 2458 2464 private byte[] readLDBytes() throws Exception 2465 { 2466 int len = readShort(); 2467 ensureDataInBuffer(len); 2468 if (replyBufferPos + len > replyBufferCount) 2469 consolePropertyMessage("DRDA_InvalidReplyTooShort.S"); 2470 byte [] retval = new byte[len]; 2471 for (int i = 0; i < len; i++) 2472 retval[i] = replyBuffer[replyBufferPos++]; 2473 return retval; 2474 } 2475 2476 2480 private void getPropertyInfo() throws Exception 2481 { 2482 2484 String directory = PropertyUtil.getSystemProperty(Property.SYSTEM_HOME_PROPERTY); 2485 String propval = PropertyUtil.getSystemProperty( 2486 Property.DRDA_PROP_LOGCONNECTIONS); 2487 if (propval != null && StringUtil.SQLEqualsIgnoreCase(propval,"true")) 2488 setLogConnections(true); 2489 propval = PropertyUtil.getSystemProperty(Property.DRDA_PROP_TRACEALL); 2490 if (propval != null && StringUtil.SQLEqualsIgnoreCase(propval, 2491 "true")) 2492 setTraceAll(true); 2493 2494 propval = PropertyUtil.getSystemProperty(Property.DRDA_PROP_TRACEDIRECTORY,directory); 2499 if(propval != null){ 2500 if(propval.equals("")) 2501 propval = directory; 2502 setTraceDirectory(propval); 2503 } 2504 2505 propval = PropertyUtil.getSystemProperty( 2508 Property.DRDA_PROP_MINTHREADS); 2509 if (propval != null){ 2510 if(propval.equals("")) 2511 propval = "0"; 2512 setMinThreads(getIntPropVal(Property.DRDA_PROP_MINTHREADS, propval)); 2513 } 2514 2515 propval = PropertyUtil.getSystemProperty( 2516 Property.DRDA_PROP_MAXTHREADS); 2517 if (propval != null){ 2518 if(propval.equals("")) 2519 propval = "0"; 2520 setMaxThreads(getIntPropVal(Property.DRDA_PROP_MAXTHREADS, propval)); 2521 } 2522 2523 2524 propval = PropertyUtil.getSystemProperty( 2525 Property.DRDA_PROP_TIMESLICE); 2526 if (propval != null){ 2527 if(propval.equals("")) 2528 propval = "0"; 2529 setTimeSlice(getIntPropVal(Property.DRDA_PROP_TIMESLICE, propval)); 2530 } 2531 2532 propval = PropertyUtil.getSystemProperty( 2533 Property.DRDA_PROP_PORTNUMBER); 2534 if (propval != null){ 2535 if(propval.equals("")) 2536 propval = String.valueOf(NetworkServerControl.DEFAULT_PORTNUMBER); 2537 portNumber = getIntPropVal(Property.DRDA_PROP_PORTNUMBER, propval); 2538 } 2539 2540 propval = PropertyUtil.getSystemProperty( 2541 Property.DRDA_PROP_KEEPALIVE); 2542 if (propval != null && 2543 StringUtil.SQLEqualsIgnoreCase(propval,"false")) 2544 keepAlive = false; 2545 2546 propval = PropertyUtil.getSystemProperty( 2547 Property.DRDA_PROP_HOSTNAME); 2548 if (propval != null){ 2549 if(propval.equals("")) 2550 hostArg = DEFAULT_HOST; 2551 else 2552 hostArg = propval; 2553 } 2554 propval = PropertyUtil.getSystemProperty( 2555 NetworkServerControlImpl.DRDA_PROP_DEBUG); 2556 if (propval != null && StringUtil.SQLEqualsIgnoreCase(propval, "true")) 2557 debugOutput = true; 2558 2559 propval = PropertyUtil.getSystemProperty( 2560 Property.DRDA_PROP_SECURITYMECHANISM); 2561 if (propval != null){ 2562 setSecurityMechanism(propval); 2563 } 2564 2565 } 2566 2567 2577 private int getSecMecValue(String s) 2578 { 2579 int secmec = INVALID_OR_NOTSET_SECURITYMECHANISM; 2580 2581 if( StringUtil.SQLEqualsIgnoreCase(s,"USER_ONLY_SECURITY")) 2582 secmec = CodePoint.SECMEC_USRIDONL; 2583 else if( StringUtil.SQLEqualsIgnoreCase(s,"CLEAR_TEXT_PASSWORD_SECURITY")) 2584 secmec = CodePoint.SECMEC_USRIDPWD; 2585 else if( StringUtil.SQLEqualsIgnoreCase(s,"ENCRYPTED_USER_AND_PASSWORD_SECURITY")) 2586 secmec = CodePoint.SECMEC_EUSRIDPWD; 2587 else if( StringUtil.SQLEqualsIgnoreCase(s,"STRONG_PASSWORD_SUBSTITUTE_SECURITY")) 2588 secmec = CodePoint.SECMEC_USRSSBPWD; 2589 2590 return secmec; 2591 } 2592 2593 2600 private String getStringValueForSecMec(int secmecVal) 2601 { 2602 switch(secmecVal) 2603 { 2604 case CodePoint.SECMEC_USRIDONL: 2605 return "USER_ONLY_SECURITY"; 2606 2607 case CodePoint.SECMEC_USRIDPWD: 2608 return "CLEAR_TEXT_PASSWORD_SECURITY"; 2609 2610 case CodePoint.SECMEC_EUSRIDPWD: 2611 return "ENCRYPTED_USER_AND_PASSWORD_SECURITY"; 2612 2613 case CodePoint.SECMEC_USRSSBPWD: 2614 return "STRONG_PASSWORD_SUBSTITUTE_SECURITY"; 2615 } 2616 return null; 2617 } 2618 2619 2625 boolean supportsEUSRIDPWD() 2626 { 2627 return SUPPORTS_EUSRIDPWD; 2628 } 2629 2630 2639 private int getIntPropVal(String propName, String propVal) 2640 throws Exception 2641 { 2642 int val = 0; 2643 try { 2644 val = (new Integer (propVal)).intValue(); 2645 } catch (Exception e) 2646 { 2647 consolePropertyMessage("DRDA_InvalidPropVal.S", new String [] 2648 {propName, propVal}); 2649 } 2650 return val; 2651 } 2652 2653 2663 private void consolePropertyMessageWork(String messageKey, String [] args) 2664 throws Exception 2665 { 2666 String locMsg = null; 2667 2668 int type = getMessageType(messageKey); 2669 2670 if (type == ERRTYPE_UNKNOWN) 2671 locMsg = messageKey; 2672 else 2673 locMsg = localizeMessage(messageKey, langUtil, args); 2674 2675 consoleMessage(locMsg); 2677 2678 if (type == ERRTYPE_USER) 2680 usage(); 2681 2682 if (currentSession != null && 2685 currentSession.langUtil != null && 2686 type != ERRTYPE_UNKNOWN) 2687 locMsg = localizeMessage(messageKey, currentSession.langUtil, args); 2688 2689 if (type == ERRTYPE_SEVERE || type == ERRTYPE_USER) 2691 { 2692 if (messageKey.equals("DRDA_SQLException.S")) 2693 throwSQLException(args[0]); 2694 else if (messageKey.equals("DRDA_SQLWarning.I")) 2695 throwSQLWarning(args[0]); 2696 else 2697 throw new Exception (messageKey+":"+locMsg); 2698 } 2699 2700 if (type == ERRTYPE_UNKNOWN) 2703 throw new Exception (locMsg); 2704 2705 return; 2706 2707 } 2708 2716 private void throwSQLException(String msg) throws SQLException 2717 { 2718 SQLException se = null; 2719 SQLException ne; 2720 SQLException ce = null; 2721 StringBuffer strbuf = new StringBuffer (); 2722 StringTokenizer tokenizer = new StringTokenizer (msg, "\n"); 2723 String sqlstate = null; 2724 String str; 2725 while (tokenizer.hasMoreTokens()) 2726 { 2727 str = tokenizer.nextToken(); 2728 if (str.charAt(5) == ':') 2730 { 2731 if (strbuf.length() > 0) 2732 { 2733 if (se == null) 2734 { 2735 se = new SQLException (strbuf.toString(), sqlstate); 2736 ce = se; 2737 } 2738 else 2739 { 2740 ne = new SQLException (strbuf.toString(), sqlstate); 2741 ce.setNextException(ne); 2742 ce = ne; 2743 } 2744 strbuf = new StringBuffer (); 2745 } 2746 strbuf.append(str.substring(6)); 2747 sqlstate = str.substring(0,5); 2748 } 2749 else 2750 strbuf.append(str); 2751 } 2752 if (strbuf.length() > 0) 2753 { 2754 if (se == null) 2755 { 2756 se = new SQLException (strbuf.toString(), sqlstate); 2757 ce = se; 2758 } 2759 else 2760 { 2761 ne = new SQLException (strbuf.toString(), sqlstate); 2762 ce.setNextException(ne); 2763 ce = ne; 2764 } 2765 } 2766 throw se; 2767 } 2768 2776 private void throwSQLWarning(String msg) throws SQLWarning 2777 { 2778 SQLWarning se = null; 2779 SQLWarning ne; 2780 SQLWarning ce = null; 2781 StringBuffer strbuf = new StringBuffer (); 2782 StringTokenizer tokenizer = new StringTokenizer (msg, "\n"); 2783 String sqlstate = null; 2784 String str; 2785 while (tokenizer.hasMoreTokens()) 2786 { 2787 str = tokenizer.nextToken(); 2788 if (str.charAt(5) == ':') 2790 { 2791 if (strbuf.length() > 0) 2792 { 2793 if (se == null) 2794 { 2795 se = new SQLWarning (strbuf.toString(), sqlstate); 2796 ce = se; 2797 } 2798 else 2799 { 2800 ne = new SQLWarning (strbuf.toString(), sqlstate); 2801 ce.setNextException(ne); 2802 ce = ne; 2803 } 2804 strbuf = new StringBuffer (); 2805 } 2806 strbuf.append(str.substring(6)); 2807 sqlstate = str.substring(0,5); 2808 } 2809 else 2810 strbuf.append(str); 2811 } 2812 if (strbuf.length() > 0) 2813 { 2814 if (se == null) 2815 { 2816 se = new SQLWarning (strbuf.toString(), sqlstate); 2817 ce = se; 2818 } 2819 else 2820 { 2821 ne = new SQLWarning (strbuf.toString(), sqlstate); 2822 ce.setNextException(ne); 2823 ce = ne; 2824 } 2825 } 2826 throw se; 2827 } 2828 2829 2838 private void throwUnexpectedException(Exception e) 2839 throws Exception { 2840 2841 consoleExceptionPrintTrace(e); 2842 throw new Exception (UNEXPECTED_ERR); 2843 2844 } 2845 2846 2854 private String localizeMessage(String msgProp, LocalizedResource localLangUtil, String [] args) 2855 { 2856 String locMsg = null; 2857 if (args != null) 2859 { 2860 String [] argMsg = new String [args.length]; 2861 for (int i = 0; i < args.length; i++) 2862 { 2863 if (isMsgProperty(args[i])) 2864 argMsg[i] = localLangUtil.getTextMessage(args[i]); 2865 else 2866 argMsg[i] = args[i]; 2867 } 2868 switch (args.length) 2869 { 2870 case 1: 2871 locMsg = localLangUtil.getTextMessage(msgProp, argMsg[0]); 2872 break; 2873 case 2: 2874 locMsg = localLangUtil.getTextMessage(msgProp, argMsg[0], argMsg[1]); 2875 break; 2876 case 3: 2877 locMsg = localLangUtil.getTextMessage(msgProp, argMsg[0], argMsg[1], argMsg[2]); 2878 break; 2879 case 4: 2880 locMsg = localLangUtil.getTextMessage(msgProp, argMsg[0], argMsg[1], argMsg[2], argMsg[3]); 2881 break; 2882 default: 2883 } 2885 } 2886 else 2887 locMsg = localLangUtil.getTextMessage(msgProp); 2888 return locMsg; 2889 } 2890 2897 private int getMessageType(String msg) 2898 { 2899 if (!msg.startsWith(DRDA_MSG_PREFIX)) 2901 return ERRTYPE_UNKNOWN; 2902 int startpos = msg.indexOf('.')+1; 2903 if (startpos >= msg.length()) 2904 return ERRTYPE_UNKNOWN; 2905 if (msg.length() > (startpos + 1)) 2906 return ERRTYPE_UNKNOWN; 2907 char type = msg.charAt(startpos); 2908 if (type == 'S') 2909 return ERRTYPE_SEVERE; 2910 if (type == 'U') 2911 return ERRTYPE_USER; 2912 if (type == 'I') 2913 return ERRTYPE_INFO; 2914 return ERRTYPE_UNKNOWN; 2915 } 2916 2924 private boolean isMsgProperty(String msg) 2925 { 2926 if (msg.startsWith(DRDA_MSG_PREFIX)) 2927 return true; 2928 else 2929 return false; 2930 } 2931 2936 public boolean getLogConnections() 2937 { 2938 synchronized(logConnectionsSync) { 2939 return logConnections; 2940 } 2941 } 2942 2947 private void setLogConnections(boolean value) 2948 { 2949 synchronized(logConnectionsSync) { 2950 logConnections = value; 2951 } 2952 synchronized(threadList) { 2954 for (Enumeration e = threadList.elements(); e.hasMoreElements(); ) 2955 { 2956 DRDAConnThread thread = (DRDAConnThread)e.nextElement(); 2957 thread.setLogConnections(value); 2958 } 2959 } 2960 } 2961 2962 2972 private void setSecurityMechanism(String s) 2973 throws Exception 2974 { 2975 allowOnlySecurityMechanism = getSecMecValue(s); 2976 2977 if ((allowOnlySecurityMechanism == INVALID_OR_NOTSET_SECURITYMECHANISM) || 2981 (allowOnlySecurityMechanism == CodePoint.SECMEC_EUSRIDPWD && 2982 !SUPPORTS_EUSRIDPWD)) 2983 consolePropertyMessage("DRDA_InvalidValue.U", new String [] 2984 {s, Property.DRDA_PROP_SECURITYMECHANISM}); 2985 } 2986 2987 2995 protected int getSecurityMechanism() 2996 { 2997 return allowOnlySecurityMechanism; 2998 } 2999 3006 private boolean setTrace(boolean on) 3007 { 3008 if (sessionArg == 0) 3009 { 3010 setTraceAll(on); 3011 synchronized(sessionTable) { 3012 for (Enumeration e = sessionTable.elements(); e.hasMoreElements(); ) 3013 { 3014 Session session = (Session) e.nextElement(); 3015 if (on) 3016 session.setTraceOn(traceDirectory); 3017 else 3018 session.setTraceOff(); 3019 } 3020 } 3021 } 3022 else 3023 { 3024 Session session = (Session) sessionTable.get(new Integer (sessionArg)); 3025 if (session != null) 3026 { 3027 if (on) 3028 session.setTraceOn(traceDirectory); 3029 else 3030 session.setTraceOff(); 3031 } 3032 else 3033 return false; 3034 } 3035 return true; 3036 } 3037 3038 3039 3044 protected int getTimeSlice() 3045 { 3046 return timeSlice; 3047 } 3048 3054 private void setTimeSlice(int value) 3055 throws Exception 3056 { 3057 if (value < MIN_TIMESLICE) 3058 consolePropertyMessage("DRDA_InvalidValue.U", new String [] 3059 {new Integer (value).toString(), "timeslice"}); 3060 if (value == USE_DEFAULT) 3061 value = DEFAULT_TIMESLICE; 3062 synchronized(timeSliceSync) { 3063 timeSlice = value; 3064 } 3065 } 3066 3067 3071 protected boolean getKeepAlive() 3072 { 3073 return keepAlive; 3074 } 3075 3076 3081 private int getMinThreads() 3082 { 3083 synchronized(threadsSync) { 3084 return minThreads; 3085 } 3086 } 3087 3092 private void setMinThreads(int value) 3093 { 3094 synchronized(threadsSync) { 3095 minThreads = value; 3096 } 3097 } 3098 3103 private int getMaxThreads() 3104 { 3105 synchronized(threadsSync) { 3106 return maxThreads; 3107 } 3108 } 3109 3115 private void setMaxThreads(int value) throws Exception 3116 { 3117 if (value < MIN_MAXTHREADS) 3118 consolePropertyMessage("DRDA_InvalidValue.U", new String [] 3119 {new Integer (value).toString(), "maxthreads"}); 3120 if (value == USE_DEFAULT) 3121 value = DEFAULT_MAXTHREADS; 3122 synchronized(threadsSync) { 3123 maxThreads = value; 3124 } 3125 } 3126 3127 3132 protected boolean getTraceAll() 3133 { 3134 synchronized(traceAllSync) { 3135 return traceAll; 3136 } 3137 } 3138 3143 private void setTraceAll(boolean value) 3144 { 3145 synchronized(traceAllSync) { 3146 traceAll = value; 3147 } 3148 } 3149 3154 protected String getTraceDirectory() 3155 { 3156 synchronized(traceDirectorySync) { 3157 return traceDirectory; 3158 } 3159 } 3160 3165 private void setTraceDirectory(String value) 3166 { 3167 synchronized(traceDirectorySync) { 3168 traceDirectory = value; 3169 } 3170 } 3171 3172 3173 3174 3182 private void connectToDatabase(DDMWriter writer, String database, String user, 3183 String password) throws Exception 3184 { 3185 Properties p = new Properties (); 3186 if (user != null) 3187 p.put("user", user); 3188 if (password != null) 3189 p.put("password", password); 3190 try { 3191 Class.forName(CLOUDSCAPE_DRIVER); 3192 } 3193 catch (Exception e) { 3194 sendMessage(writer, ERROR, e.getMessage()); 3195 return; 3196 } 3197 try { 3198 Connection conn = DriverManager.getConnection(Attribute.PROTOCOL+database, p); 3201 SQLWarning warn = conn.getWarnings(); 3203 if (warn != null) 3204 sendSQLMessage(writer, warn, SQLWARNING); 3205 else 3206 sendOK(writer); 3207 conn.close(); 3208 return; 3209 } catch (SQLException se) { 3210 sendSQLMessage(writer, se, SQLERROR); 3211 } 3212 } 3213 3224 private void startDatabase(DDMWriter writer, String database, 3225 String bootPassword, String encPrv, String encAlg, String user, 3226 String password) throws Exception 3227 { 3228 Properties p = new Properties (); 3229 if (bootPassword != null) 3230 p.put(Attribute.BOOT_PASSWORD, bootPassword); 3231 if (encPrv != null) 3232 p.put(Attribute.CRYPTO_PROVIDER, encPrv); 3233 if (encAlg != null) 3234 p.put(Attribute.CRYPTO_ALGORITHM, encAlg); 3235 if (user != null) 3236 p.put(Attribute.USERNAME_ATTR, user); 3237 if (password != null) 3238 p.put(Attribute.PASSWORD_ATTR, password); 3239 try { 3240 Class.forName(CLOUDSCAPE_DRIVER); 3241 } 3242 catch (Exception e) { 3243 sendMessage(writer, ERROR, e.getMessage()); 3244 return; 3245 } 3246 try { 3247 Connection conn = DriverManager.getConnection(Attribute.PROTOCOL+database, p); 3250 SQLWarning warn = conn.getWarnings(); 3251 if (warn != null) 3252 sendSQLMessage(writer, warn, SQLWARNING); 3253 else 3254 sendOK(writer); 3255 conn.close(); 3256 } catch (SQLException se) { 3257 sendSQLMessage(writer, se, SQLERROR); 3258 } catch (Exception e) { 3259 sendMessage(writer, ERROR, e.getMessage()); 3260 } 3261 } 3262 3270 private void shutdownDatabase(DDMWriter writer, String database, String user, 3271 String password) throws Exception 3272 { 3273 3274 StringBuffer url = new StringBuffer (Attribute.PROTOCOL + database); 3275 if (user != null) 3276 url.append(";user="+user); 3277 if (password != null) 3278 url.append(";password="+password); 3279 url.append(";shutdown=true"); 3280 try { 3281 Class.forName(CLOUDSCAPE_DRIVER); 3282 } 3283 catch (Exception e) { 3284 sendMessage(writer, ERROR, e.getMessage()); 3285 return; 3286 } 3287 try { 3288 Connection conn = DriverManager.getConnection(url.toString()); 3289 SQLWarning warn = conn.getWarnings(); 3290 if (warn != null) 3291 sendSQLMessage(writer, warn, SQLWARNING); 3292 else 3293 sendOK(writer); 3294 conn.close(); 3295 } catch (SQLException se) { 3296 String expectedState = 3298 StandardException. 3299 getSQLStateFromIdentifier(SQLState.SHUTDOWN_DATABASE); 3300 if (!expectedState.equals(se.getSQLState())) 3301 { 3302 sendSQLMessage(writer, se, SQLERROR); 3303 return; 3304 } 3305 sendOK(writer); 3306 } 3307 } 3308 3315 private void wrapSQLError(String messageKey) 3316 throws Exception 3317 { 3318 consolePropertyMessage("DRDA_SQLException.S", messageKey); 3319 } 3320 3321 3328 private void wrapSQLWarning(String messageKey) 3329 throws Exception 3330 { 3331 consolePropertyMessage("DRDA_SQLWarning.I", messageKey); 3332 } 3333 private Properties getPropertyValues() 3334 { 3335 Properties retval = new Properties (); 3336 retval.put(Property.DRDA_PROP_PORTNUMBER, new Integer (portNumber).toString()); 3337 retval.put(Property.DRDA_PROP_HOSTNAME, hostArg); 3338 retval.put(Property.DRDA_PROP_KEEPALIVE, new Boolean (keepAlive).toString()); 3339 3340 String tracedir = getTraceDirectory(); 3341 if (tracedir != null) 3342 retval.put(Property.DRDA_PROP_TRACEDIRECTORY, tracedir); 3343 retval.put(Property.DRDA_PROP_TRACEALL, new Boolean (getTraceAll()).toString()); 3344 retval.put(Property.DRDA_PROP_MINTHREADS, new Integer (getMinThreads()).toString()); 3345 retval.put(Property.DRDA_PROP_MAXTHREADS, new Integer (getMaxThreads()).toString()); 3346 retval.put(Property.DRDA_PROP_TIMESLICE, new Integer (getTimeSlice()).toString()); 3347 3348 retval.put(Property.DRDA_PROP_TIMESLICE, new Integer (getTimeSlice()).toString()); 3349 retval.put(Property.DRDA_PROP_LOGCONNECTIONS, new Boolean (getLogConnections()).toString()); 3350 String startDRDA = PropertyUtil.getSystemProperty(Property.START_DRDA); 3351 if(startDRDA!=null && startDRDA.equals("")) 3354 startDRDA = "false"; 3355 3356 retval.put(Property.START_DRDA, (startDRDA == null)? "false" : startDRDA); 3357 3358 if ( getSecurityMechanism() != INVALID_OR_NOTSET_SECURITYMECHANISM ) 3362 retval.put( Property.DRDA_PROP_SECURITYMECHANISM, getStringValueForSecMec(getSecurityMechanism())); 3363 3364 if (!getTraceAll()) 3366 { 3367 synchronized(sessionTable) { 3368 for (Enumeration e = sessionTable.elements(); e.hasMoreElements(); ) 3369 { 3370 Session session = (Session) e.nextElement(); 3371 if (session.isTraceOn()) 3372 retval.put(Property.DRDA_PROP_TRACE+"."+session.getConnNum(), "true"); 3373 } 3374 } 3375 } 3376 return retval; 3377 } 3378 3379 3380 3392 void addSession(Socket clientSocket) throws Exception { 3393 3394 int connectionNumber = ++connNum; 3395 3396 if (getLogConnections()) { 3397 consolePropertyMessage("DRDA_ConnNumber.I", 3398 Integer.toString(connectionNumber)); 3399 } 3400 3401 Session session = new Session(connectionNumber, clientSocket, 3405 getTraceDirectory(), getTraceAll()); 3406 3407 sessionTable.put(new Integer (connectionNumber), session); 3408 3409 boolean enoughThreads; 3412 synchronized (runQueue) { 3413 enoughThreads = (runQueue.size() < freeThreads); 3414 } 3415 3421 DRDAConnThread thread = null; 3422 3423 if (!enoughThreads) { 3425 synchronized (threadsSync) { 3429 if ((maxThreads == 0) || (threadList.size() < maxThreads)) { 3432 thread = new DRDAConnThread(session, this, getTimeSlice(), 3433 getLogConnections()); 3434 threadList.add(thread); 3435 thread.start(); 3436 } 3437 } 3438 } 3439 3440 if (thread == null) { 3442 runQueueAdd(session); 3443 } 3444 } 3445 3446 3452 void removeThread(DRDAConnThread thread) { 3453 threadList.remove(thread); 3454 } 3455 3456 protected Object getShutdownSync() { return shutdownSync; } 3457 protected boolean getShutdown() { return shutdown; } 3458 3459 3460 public String buildRuntimeInfo(LocalizedResource locallangUtil) 3461 { 3462 3463 String s = locallangUtil.getTextMessage("DRDA_RuntimeInfoBanner.I")+ "\n"; 3464 int sessionCount = 0; 3465 s += locallangUtil.getTextMessage("DRDA_RuntimeInfoSessionBanner.I") + "\n"; 3466 for (int i = 0; i < threadList.size(); i++) 3467 { 3468 String sessionInfo = ((DRDAConnThread) 3469 threadList.get(i)).buildRuntimeInfo("",locallangUtil) ; 3470 if (!sessionInfo.equals("")) 3471 { 3472 sessionCount ++; 3473 s += sessionInfo + "\n"; 3474 } 3475 } 3476 int waitingSessions = 0; 3477 for (int i = 0; i < runQueue.size(); i++) 3478 { 3479 s += ((Session)runQueue.get(i)).buildRuntimeInfo("", locallangUtil); 3480 waitingSessions ++; 3481 } 3482 s+= "-------------------------------------------------------------\n"; 3483 s += locallangUtil.getTextMessage("DRDA_RuntimeInfoNumThreads.I") + 3484 threadList.size() + "\n"; 3485 s += locallangUtil.getTextMessage("DRDA_RuntimeInfoNumActiveSessions.I") + 3486 sessionCount +"\n"; 3487 s +=locallangUtil.getTextMessage("DRDA_RuntimeInfoNumWaitingSessions.I") + 3488 + waitingSessions + "\n\n"; 3489 3490 Runtime rt = Runtime.getRuntime(); 3491 rt.gc(); 3492 long totalmem = rt.totalMemory(); 3493 long freemem = rt.freeMemory(); 3494 s += locallangUtil.getTextMessage("DRDA_RuntimeInfoTotalMemory.I") + 3495 + totalmem + "\t"; 3496 s += locallangUtil.getTextMessage("DRDA_RuntimeInfoFreeMemory.I") + 3497 + freemem + "\n\n"; 3498 3499 return s; 3500 } 3501 3502 3503 protected void setClientLocale(String locale) 3504 { 3505 clientLocale = locale; 3506 } 3507 3508 3515 private ProductVersionHolder getNetProductVersionHolder() throws Exception 3516 { 3517 ProductVersionHolder myPVH= null; 3518 try { 3519 myPVH = (ProductVersionHolder) AccessController.doPrivileged( 3520 new PrivilegedExceptionAction () { 3521 3522 public Object run() throws UnknownHostException ,IOException 3523 { 3524 InputStream versionStream = getClass().getResourceAsStream(ProductGenusNames.NET_INFO); 3525 3526 return ProductVersionHolder.getProductVersionHolderFromMyEnv(versionStream); 3527 } 3528 }); 3529 3530} 3531 catch(PrivilegedActionException e) { 3532 Exception e1 = e.getException(); 3533 consolePropertyMessage("DRDA_ProductVersionReadError.S", e1.getMessage()); 3534 } 3535 return myPVH; 3536 } 3537 3538 3546 private String getFormattedTimestamp(){ 3547 long currentTime = System.currentTimeMillis(); 3548 return CheapDateFormatter.formatDate(currentTime); 3549 } 3550} 3551 3552 3553 3554 3555 3556 3557 | Popular Tags |