networkservercontrolimpl.java

来自「derby database source code.good for you.」· Java 代码 · 共 2,300 行 · 第 1/5 页

JAVA
2,300
字号
	 *	 * @param args	arguments to search	 *	 * @return  command	 */	private int findCommand(String [] args) throws Exception	{		try {			// process the dashArgs and pull out the command args 			int i = 0;			int newpos = 0;			while (i < args.length)			{				if (args[i].startsWith("-"))				{					newpos = processDashArg(i, args);					if (newpos == i)						commandArgs.addElement(args[i++]);					else						i = newpos;				}				else					commandArgs.addElement(args[i++]);			}								// look up command			if (commandArgs.size() > 0)			{				for (i = 0; i < COMMANDS.length; i++)				{					if (StringUtil.SQLEqualsIgnoreCase(COMMANDS[i], 													   (String)commandArgs.firstElement()))					{						commandArgs.removeElementAt(0);						return i;					}				}			}			// didn't find command			consolePropertyMessage("DRDA_UnknownCommand.U", 				(String) commandArgs.firstElement());		} catch (Exception e) {			if (e.getMessage().equals(NetworkServerControlImpl.UNEXPECTED_ERR))				throw e;			//Ignore expected exceptions, they will have been									//handled by the consolePropertyMessage routine		}		return COMMAND_UNKNOWN;	}	/**	 * Get the dash argument. Optional arguments are formated as -x value.	 *	 * @param pos	starting point	 * @param args	arguments to search	 *	 * @return  command	 *	 * @exception Exception	thrown if an error occurs	 */	private int processDashArg(int pos, String[] args)		throws Exception	{		//check for a negative number		char c = args[pos].charAt(1);		if (c >= '0' && c <= '9')			return pos;		int dashArg = -1;		for (int i = 0; i < DASHARGS.length; i++)		{			if (DASHARGS[i].equals(args[pos].substring(1)))			{				dashArg = i;				pos++;				break;			}		}		if (dashArg == -1)			consolePropertyMessage("DRDA_UnknownArgument.U", args[pos]);		switch (dashArg)		{			case DASHARG_PORT:				if (pos < args.length)				{            		try{                		portNumber = Integer.parseInt(args[pos]);            		}catch(NumberFormatException e){						consolePropertyMessage("DRDA_InvalidValue.U", 							new String [] {args[pos], "DRDA_PortNumber.I"});            		}				}				else					consolePropertyMessage("DRDA_MissingValue.U", "DRDA_PortNumber.I");				break;			case DASHARG_HOST:				if (pos < args.length)				{					hostArg = args[pos];				}				else					consolePropertyMessage("DRDA_MissingValue.U", "DRDA_Host.I");				break;			case DASHARG_DATABASE:				if (pos < args.length)					databaseArg = args[pos];				else					consolePropertyMessage("DRDA_MissingValue.U", 						"DRDA_DatabaseDirectory.I");				break;			case DASHARG_USER:				if (pos < args.length)				{					userArg = args[pos++];					if (pos < args.length)						passwordArg = args[pos];					else						consolePropertyMessage("DRDA_MissingValue.U", 							"DRDA_Password.I");				}				else					consolePropertyMessage("DRDA_MissingValue.U", "DRDA_User.I");				break;			case DASHARG_ENCALG:				if (pos < args.length)					encAlgArg = args[pos];				else					consolePropertyMessage("DRDA_MissingValue.U", 						"DRDA_EncryptionAlgorithm.I");				break;			case DASHARG_ENCPRV:				if (pos < args.length)					encPrvArg = args[pos];				else					consolePropertyMessage("DRDA_MissingValue.U", 						"DRDA_EncryptionProvider.I");				break;			case DASHARG_LOADSYSIBM:				break;			case DASHARG_SESSION:				if (pos < args.length)            		try{                		sessionArg = Integer.parseInt(args[pos]);            		}catch(NumberFormatException e){						consolePropertyMessage("DRDA_InvalidValue.U", 							new String [] {args[pos], "DRDA_Session.I"});            		}				else					consolePropertyMessage("DRDA_MissingValue.U", "DRDA_Session.I");				break;			default:				//shouldn't get here		}		return pos+1;	}	/**	 * Is string "on" or "off"	 *	 * @param arg	string to check	 *	 * @return  true if string is "on", false if string is "off"	 *	 * @exception Exception	thrown if string is not one of "on" or "off"	 */	private boolean isOn(String arg)		throws Exception	{		if (StringUtil.SQLEqualsIgnoreCase(arg, "on"))			return true;		else if (!StringUtil.SQLEqualsIgnoreCase(arg, "off"))			consolePropertyMessage("DRDA_OnOffValue.U", arg);		return false;	}	/**	 * Set up client socket to send a command to the network server	 *   	 * @exception Exception	thrown if exception encountered	 */	private void setUpSocket() throws Exception	{				try {            clientSocket = (Socket) AccessController.doPrivileged(								new PrivilegedExceptionAction() {																			public Object run() throws UnknownHostException,IOException									{										if (hostAddress == null)											hostAddress = InetAddress.getByName(hostArg);										// JDK131 can't connect with a client										// socket with 0.0.0.0 (all addresses) so we will										// getLocalHost() which will suffice.										InetAddress connectAddress;										if (JVMInfo.JDK_ID <= JVMInfo.J2SE_13 &&											hostAddress.getHostAddress().equals("0.0.0.0"))											connectAddress = InetAddress.getLocalHost();										else											connectAddress = hostAddress;										return new Socket(connectAddress, portNumber);									}								}							);		} catch (PrivilegedActionException pae) {			Exception e1 = pae.getException();        	if (e1 instanceof UnknownHostException) {					consolePropertyMessage("DRDA_UnknownHost.S", hostArg);			}        	else if (e1 instanceof IOException) {					consolePropertyMessage("DRDA_NoIO.S",						new String [] {hostArg, (new Integer(portNumber)).toString()});			}		} catch (Exception e) {		// If we find other (unexpected) errors, we ultimately exit--so make		// sure we print the error message before doing so (Beetle 5033).			throwUnexpectedException(e);		}        try        {	       clientIs = clientSocket.getInputStream();	       clientOs = clientSocket.getOutputStream();		} catch (IOException e) {			consolePropertyMessage("DRDA_NoInputStream.I");			throw e;        }	}		private void checkAddressIsLocal(InetAddress inetAddr) throws UnknownHostException,Exception	{		for(int i = 0; i < localAddresses.size(); i++)		{			if (inetAddr.equals((InetAddress)localAddresses.get(i)))			{				return;			}		}		consolePropertyMessage("DRDA_NeedLocalHost.S", new String[] {inetAddr.getHostName(),serverSocket.getInetAddress().getHostName()});	}	/**	 * Build local address list to allow admin commands.	 *	 * @param bindAddr Address on which server was started	 * 	 * Note: Some systems may not support localhost.	 * In that case a console message will print for the localhost entries,	 * but the server will continue to start.	 **/	private void buildLocalAddressList(InetAddress bindAddr) 	{			localAddresses = new ArrayList(3);			localAddresses.add(bindAddr);			try {				localAddresses.add(InetAddress.getLocalHost());				localAddresses.add(InetAddress.getByName("localhost"));			}catch(UnknownHostException uhe)			{				try {					consolePropertyMessage("DRDA_UnknownHostWarning.I",uhe.getMessage());				} catch (Exception e)				{ // just a warning shouldn't actually throw an exception				}			}				}		/**	 * Routines for writing commands for NetworkServerControlImpl being used as a client	 * to a server	 */	/**	 * Write command header consisting of command header string and protocol	 * version and command	 *	 * @param command	command to be written	 *	 * @exception Exception	throws an exception if an error occurs	 */	private void writeCommandHeader(int command) throws Exception	{		try {			writeString(COMMAND_HEADER);			commandOs.writeByte((byte)((PROTOCOL_VERSION & 0xf0) >> 8 ));			commandOs.writeByte((byte)(PROTOCOL_VERSION & 0x0f));			if (clientLocale != null && clientLocale != DEFAULT_LOCALE)			{				commandOs.writeByte(clientLocale.length());				commandOs.writeBytes(clientLocale);			}			else				commandOs.writeByte((byte) 0);			commandOs.writeByte((byte) 0);			commandOs.writeByte((byte) command);		}		catch (IOException e)		{			clientSocketError(e);		}	}	/**	 * Write length delimited string string	 *	 * @param msg	string to be written	 *	 * @exception Exception	throws an exception if an error occurs	 */	private void writeLDString(String msg) throws Exception	{		try {			if (msg == null)			{				commandOs.writeShort(0);			}			else			{				commandOs.writeShort(msg.length());				writeString(msg);			}		}		catch (IOException e)		{			clientSocketError(e);		}	}	/** Write string	 *	 * @param msg String to write	 */	protected void writeString(String msg) throws Exception	{		byte[] msgBytes = msg.getBytes(DEFAULT_ENCODING);		commandOs.write(msgBytes,0,msgBytes.length);	}	/**	 * Write short	 *	 * @param value	value to be written	 *	 * @exception Exception	throws an exception if an error occurs	 */	private void writeShort(int value) throws Exception	{		try {			commandOs.writeByte((byte)((value & 0xf0) >> 8 ));			commandOs.writeByte((byte)(value & 0x0f));		}		catch (IOException e)		{			clientSocketError(e);		}	}	/**	 * Write byte	 *	 * @param value	value to be written	 *	 * @exception Exception	throws an exception if an error occurs	 */	private void writeByte(int value) throws Exception	{		try {			commandOs.writeByte((byte)(value & 0x0f));		}		catch (IOException e)		{			clientSocketError(e);		}	}	/**	 * Send client message to server	 *	 *	 * @exception Exception	throws an exception if an error occurs	 */	private void send() throws Exception	{		try {			byteArrayOs.writeTo(clientOs);			commandOs.flush();			byteArrayOs.reset();	//discard anything currently in the byte array		}		catch (IOException e)		{			clientSocketError(e);		}	}	/**	 * Stream error writing to client socket 	 */	private void clientSocketError(IOException e) throws IOException	{		try {			consolePropertyMessage("DRDA_ClientSocketError.S", e.getMessage());		} catch (Exception ce) {} // catch the exception consolePropertyMessage will								 // throw since we also want to print a stack trace		consoleExceptionPrintTrace(e);			throw e;	}	/**	 * Read result from sending client message to server	 *	 * @exception Exception	throws an exception if an error occurs	 */	private void readResult() throws Exception	{		fillReplyBuffer();		readCommandReplyHeader();		if (replyBufferPos >= replyBufferCount)			consolePropertyMessage("DRDA_InvalidReplyTooShort.S");		int messageType = replyBuffer[replyBufferPos++] & 0xFF;		if (messageType == OK)		// O.K.			return;		// get error and display and throw exception		String message =  readLDString();		if (messageType == SQLERROR)			wrapSQLError(message);		else if (messageType == SQLWARNING)			wrapSQLWarning(message);		else			consolePropertyMessage(message);	}		/**	 * Ensure the reply buffer is at large enought to hold all the data;	 * don't just rely on OS level defaults	 *	 *	 * @param minimumBytesNeeded	size of buffer required		 * @exception Exception throws an exception if a problem reading the reply	 */	private void ensureDataInBuffer(int minimumBytesNeeded) throws Exception	{		// make sure the buffer is large enough		while ((replyBufferCount - replyBufferPos) < minimumBytesNeeded)		{			try {				int bytesRead = clientIs.read(replyBuffer, replyBufferCount, replyBuffer.length - replyBufferCount);				replyBufferCount += bytesRead;					} catch (IOException e)			{				clientSocketError(e);			}		}	}	/**	 * Fill the reply buffer with the reply allocates a reply buffe

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?