📄 socketurl.java
字号:
/*------------------------------------------------------------------------------Name: SocketUrl.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: SocketUrl knows how to parse the URL notation of our SOCKET protocol------------------------------------------------------------------------------*/package org.xmlBlaster.util.protocol.socket;import org.xmlBlaster.util.Global;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.def.ErrorCode;import org.xmlBlaster.util.property.PropInt;import org.xmlBlaster.util.property.PropString;import org.xmlBlaster.util.qos.address.AddressBase;import org.xmlBlaster.util.FileLocator;import java.net.InetAddress;import java.net.URL;import java.io.InputStream;/** * This knows how to parse the URL notation of our SOCKET protocol. * It holds the hostname and the port. * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.socket.html">The protocol.socket requirement</a> * @see org.xmlBlaster.test.classtest.SocketUrlTest */public class SocketUrl{ private String ME = "SocketUrl"; private Global glob; private static Logger log = Logger.getLogger(SocketUrl.class.getName()); /** The string representation like "192.168.1.1", useful if multihomed computer */ private String hostname = null; private PropString hostnameProp; /** xmlBlaster server host */ private java.net.InetAddress inetAddress = null; /** The port */ private int port = SocketUrl.DEFAULT_SERVER_PORT; private PropInt portProp; private boolean isLocal = false; /** Flag to use TCP/IP */ public static final boolean SOCKET_TCP = false; /** Flag to use UDP */ public static final boolean SOCKET_UDP = true; // Default port of xmlBlaster socket local side is 0 (choosen by OS) */ //public static final int DEFAULT_SERVER_CBPORT = 0; /** Default port of xmlBlaster socket server is 7607 */ public static final int DEFAULT_SERVER_PORT = 7607; private static boolean firstKey = true; private static boolean firstTrust = true; private boolean isEnforced = false; /** * @param hostname if null or empty the local IP will be used * @param port any port, not checked */ public SocketUrl(Global glob, String hostname, int port) throws XmlBlasterException { this.glob = glob; this.hostname = hostname; if (this.hostname == null || this.hostname.length() < 1) { this.hostname = glob.getLocalIP(); } this.port = port; createInetAddress(); // first check } /** * Parse the given url. * @param url e.g. "socket://127.168.1.1:7607" or only "127.168.1.1:7607" or "" (choose default settings) * @exception XmlBlasterException if url is null or invalid */ public SocketUrl(Global glob, String url) throws XmlBlasterException { this.glob = glob; parse(url); createInetAddress(); // first check } /** * Extract "hostname" and "port" from environment, if not found use the local host * for hostname and the default port 7607. * <br /> * Updates the raw address from AddressBase */ public SocketUrl(Global glob, AddressBase address) throws XmlBlasterException { this(glob, address, false, SocketUrl.DEFAULT_SERVER_PORT); } /** * Extract hostname and port from environment, if not found use the local host * for hostname and the given default port (usually ExecutorBase.DEFAULT_SERVER_PORT=7607). * <br /> * Updates the raw address from AddressBase if isLocal==false * @param isLocal If local is set to true "localHostname" and "localPort" will be extracted */ public SocketUrl(Global glob, AddressBase address, boolean isLocal, int defaultServerPort) throws XmlBlasterException { this.glob = glob; if (isLocal) { this.isLocal = true; this.portProp = address.getEnv("localPort", defaultServerPort); this.port = this.portProp.getValue(); this.hostnameProp = address.getEnv("localHostname", glob.getLocalIP()); this.hostname = this.hostnameProp.getValue(); } else { if (address.getRawAddress() != null && address.getRawAddress().length() > 2) { parse(address.getRawAddress()); createInetAddress(); // first check this.isEnforced = true; return; } this.portProp = address.getEnv("port", defaultServerPort); this.port = this.portProp.getValue(); this.hostnameProp = address.getEnv("hostname", glob.getLocalIP()); this.hostname = this.hostnameProp.getValue(); address.setRawAddress(getUrl()); } createInetAddress(); // first check } /** @return true if host or port was given by user configuration, false if default is chosen */ public boolean isEnforced() { if (this.isEnforced) return true; boolean enforcedHost = (this.hostnameProp == null) ? false : !this.hostnameProp.isDefault(); boolean enforcedPort = (this.portProp == null) ? false : !this.portProp.isDefault(); return enforcedHost || enforcedPort; } public String getHostname() { return this.hostname; } public int getPort() { return this.port; } /** * @return for example "socket://myServer.com:7607" */ public String getUrl() { return "socket://" + this.hostname + ":" + this.port; } public String toString() { return getUrl(); } private void parse(String url) throws XmlBlasterException { if (url == null) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION_ADDRESS, ME, "Your given SOCKET url '" + url + "' is invalid"); } String urlLowerCase = url.toLowerCase(); if (urlLowerCase.startsWith("socket://")) { url = url.substring("socket://".length()); } else if (urlLowerCase.startsWith("socket:")) { url = url.substring("socket:".length()); } int pos = url.indexOf(":"); String portStr = null; if (pos > -1) { this.hostname = url.substring(0, pos); portStr = url.substring(pos+1); if (portStr != null && portStr.length() > 0) { pos = portStr.indexOf("/"); if (pos > -1) { portStr = portStr.substring(0, pos); // strip path e.g. "socket://myHost:8000/path/subpath" } try { this.port = (new Integer(portStr)).intValue(); } catch (NumberFormatException e) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION_ADDRESS, ME, "Your given SOCKET url '" + url + "' port '" + portStr + "' is invalid"); } } } else { this.hostname = url; } if (this.hostname == null || this.hostname.length() < 1) { this.hostname = glob.getLocalIP(); } } public InetAddress getInetAddress() { return this.inetAddress; } private void createInetAddress() throws XmlBlasterException { try { this.inetAddress = java.net.InetAddress.getByName(this.hostname); } catch(java.net.UnknownHostException e) { Thread.dumpStack(); String txt = "The hostname [" + this.hostname + "] of url '" + getUrl() + "' is invalid, check your '-plugin/socket/" + (isLocal ? "localHostname" : "hostname") + " <ip>' setting: " + e.toString(); log.warning(txt); // throw new XmlBlasterException(glob, ErrorCode.RESOURCE_CONFIGURATION_ADDRESS, ME, txt); throw new XmlBlasterException(glob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, txt); } } public boolean equals(SocketUrl other) { if (this.port == other.getPort() && getInetAddress().equals(other.getInetAddress())) { //log.error(ME, "DEBUG ONLY: EQUAL: " + getUrl() + " - " + other.getUrl()); return true; } //log.error(ME, "DEBUG ONLY: NOT EQUAL: " + getUrl() + " - " + other.getUrl()); return false; } /** * Helper to create a server side SSL socket, uses reflection to compile with JDK 1.3 * SSL support can't be used with a standard JDK 1.3 * <p /> * Setup: * <pre>keytool -genkey -keystore testStore -keyalg RSA (using password 'testtest')java org.xmlBlaster.Main -plugin/socket/SSL true -plugin/socket/keyStore testStore -plugin/socket/keyStorePassword testtest
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -