SofaRegistryHelper { public static ProviderInfo parseProviderInfo(String originUrl) { String url = originUrl; String host = null; int port = 80; String path = null; String schema = null; int i = url.indexOf(": if (i > 0) { schema = url.substring(0, i); url = url.substring(i + 3); } Map<String, String> parameters = new HashMap<String, String>(); i = url.indexOf('?'); if (i >= 0) { String[] parts = url.substring(i + 1).split("\\&"); for (String part : parts) { part = part.trim(); if (part.length() > 0) { int j = part.indexOf('='); if (j >= 0) { parameters.put(part.substring(0, j), part.substring(j + 1)); } else { parameters.put(part, part); } } } url = url.substring(0, i); } i = url.indexOf('/'); if (i >= 0) { path = url.substring(i + 1); url = url.substring(0, i); } i = url.indexOf(':'); if (i >= 0 && i < url.length() - 1) { port = Integer.parseInt(url.substring(i + 1)); url = url.substring(0, i); } if (url.length() > 0) { host = url; } ProviderInfo providerInfo = new ProviderInfo(); providerInfo.setOriginUrl(originUrl); providerInfo.setHost(host); if (port != 80) { providerInfo.setPort(port); } if (path != null) { providerInfo.setPath(path); } if (schema != null) { providerInfo.setProtocolType(schema); } String protocolStr = getValue(parameters, RPC_REMOTING_PROTOCOL); if (schema == null && protocolStr != null) { if ((RemotingConstants.PROTOCOL_BOLT + "").equals(protocolStr)) { protocolStr = PROTOCOL_TYPE_BOLT; } else if ((RemotingConstants.PROTOCOL_TR + "").equals(protocolStr)) { protocolStr = PROTOCOL_TYPE_TR; } try { providerInfo.setProtocolType(protocolStr); } catch (Exception e) { LOGGER.error(LogCodes.getLog(LogCodes.ERROR_INVALID_ATTRIBUTE, "protocol", originUrl)); } } String timeoutStr = getValue(parameters, ATTR_TIMEOUT, TIMEOUT); if (timeoutStr != null) { removeOldKeys(parameters, ATTR_TIMEOUT, TIMEOUT); try { providerInfo.setDynamicAttr(ATTR_TIMEOUT, Integer.parseInt(timeoutStr)); } catch (Exception e) { LOGGER.error(LogCodes.getLog(LogCodes.ERROR_INVALID_ATTRIBUTE, "timeout", originUrl)); } } String serializationStr = getValue(parameters, ATTR_SERIALIZATION, SERIALIZE_TYPE_KEY); if (serializationStr != null) { removeOldKeys(parameters, ATTR_SERIALIZATION, SERIALIZE_TYPE_KEY); if ((RemotingConstants.SERIALIZE_CODE_HESSIAN + "").equals(serializationStr)) { serializationStr = SERIALIZE_HESSIAN; } else if ((RemotingConstants.SERIALIZE_CODE_JAVA + "").equals(serializationStr)) { serializationStr = SERIALIZE_JAVA; } else if ((RemotingConstants.SERIALIZE_CODE_HESSIAN2 + "").equals(serializationStr)) { serializationStr = SERIALIZE_HESSIAN2; } else if ((RemotingConstants.SERIALIZE_CODE_PROTOBUF + "").equals(serializationStr)) { serializationStr = SERIALIZE_PROTOBUF; } providerInfo.setSerializationType(serializationStr); } String appNameStr = getValue(parameters, ATTR_APP_NAME, APP_NAME, SofaRegistryConstants.SELF_APP_NAME); if (appNameStr != null) { removeOldKeys(parameters, APP_NAME, SofaRegistryConstants.SELF_APP_NAME); providerInfo.setStaticAttr(ATTR_APP_NAME, appNameStr); } String connections = getValue(parameters, ATTR_CONNECTIONS, SofaRegistryConstants.CONNECTI_NUM); if (connections != null) { removeOldKeys(parameters, SofaRegistryConstants.CONNECTI_NUM); providerInfo.setStaticAttr(ATTR_CONNECTIONS, connections); } String rpcVersion = getValue(parameters, ATTR_RPC_VERSION); providerInfo.setRpcVersion(CommonUtils.parseInt(rpcVersion, providerInfo.getRpcVersion())); String weightStr = getValue(parameters, ATTR_WEIGHT, WEIGHT_KEY); if (weightStr != null) { removeOldKeys(parameters, ATTR_WEIGHT, WEIGHT_KEY); try { int weight = Integer.parseInt(weightStr); providerInfo.setWeight(weight); providerInfo.setStaticAttr(ATTR_WEIGHT, weightStr); } catch (Exception e) { LOGGER.error(LogCodes.getLog(LogCodes.ERROR_INVALID_ATTRIBUTE, "weight", originUrl)); } } String warmupTimeStr = getValue(parameters, ATTR_WARMUP_TIME, SofaRegistryConstants.WARMUP_TIME_KEY); int warmupTime = 0; if (warmupTimeStr != null) { removeOldKeys(parameters, ATTR_WARMUP_TIME, SofaRegistryConstants.WARMUP_TIME_KEY); try { warmupTime = Integer.parseInt(warmupTimeStr); providerInfo.setStaticAttr(ATTR_WARMUP_TIME, warmupTimeStr); } catch (Exception e) { LOGGER.error(LogCodes.getLog(LogCodes.ERROR_INVALID_ATTRIBUTE, "warmupTime", originUrl)); } } String warmupWeightStr = getValue(parameters, ATTR_WARMUP_WEIGHT, SofaRegistryConstants.WARMUP_WEIGHT_KEY); int warmupWeight = 0; if (warmupWeightStr != null) { removeOldKeys(parameters, ATTR_WARMUP_WEIGHT, SofaRegistryConstants.WARMUP_WEIGHT_KEY); try { warmupWeight = Integer.parseInt(warmupWeightStr); providerInfo.setStaticAttr(ATTR_WARMUP_WEIGHT, warmupWeightStr); } catch (Exception e) { LOGGER.error(LogCodes.getLog(LogCodes.ERROR_INVALID_ATTRIBUTE, "warmupWeight", originUrl)); } } String startTimeStr = getValue(parameters, ATTR_START_TIME); long startTime = 0L; if (startTimeStr != null) { try { startTime = Long.parseLong(startTimeStr); } catch (Exception e) { LOGGER.error(LogCodes.getLog(LogCodes.ERROR_INVALID_ATTRIBUTE, "startTime", originUrl)); } } if (startTime == 0) { startTime = System.currentTimeMillis(); } if (StringUtils.isNotBlank(warmupTimeStr) && StringUtils.isNotBlank(warmupWeightStr)) { if (warmupTime > 0) { providerInfo.setStatus(ProviderStatus.WARMING_UP); providerInfo.setDynamicAttr(ATTR_WARMUP_WEIGHT, warmupWeight); providerInfo.setDynamicAttr(ATTR_WARM_UP_END_TIME, startTime + warmupTime); } } String hostMachineName = getValue(parameters, HOST_MACHINE_KEY); if (StringUtils.isNotBlank(hostMachineName)) { providerInfo.setDynamicAttr(ATTR_HOST_MACHINE, hostMachineName); } List<String> methodKeys = new ArrayList<String>(); Map<String, Object> methodParameters = new HashMap<String, Object>(); for (Map.Entry<String, String> entry : parameters.entrySet()) { if (entry.getKey().startsWith("[") && entry.getKey().endsWith("]") && entry.getValue().startsWith("[") && entry.getValue().endsWith("]")) { String key = entry.getKey(); methodKeys.add(key); String methodName = key.substring(1, key.length() - 1); parseMethodInfo(methodParameters, methodName, entry.getValue()); } } for (String methodKey : methodKeys) { parameters.remove(methodKey); } providerInfo.getStaticAttrs().putAll(parameters); providerInfo.getDynamicAttrs().putAll(methodParameters); providerInfo.setStaticAttr(ProviderInfoAttrs.ATTR_SOURCE, "sofa"); return providerInfo; }  static String buildListDataId(AbstractInterfaceConfig config, String protocol); static String convertProviderToUrls(ProviderConfig providerConfig, ServerConfig server); static List<ProviderInfo> parseProviderInfos(List<String> urls); static ProviderInfo parseProviderInfo(String originUrl); static final String PUBLISHER_PREFIX; static final String SUBSCRIBER_PREFIX; static final String SUBSCRIBER_LIST_GROUP_ID; static final String SUBSCRIBER_CONFIG_GROUP_ID; static final String PROTOCOL_TYPE_OVERRIDE; }
@Test public void parseProviderInfo() throws Exception { String defaultProtocol = RpcConfigs.getStringValue(RpcOptions.DEFAULT_PROTOCOL); String url = "10.244.22.1:8080?zone=GZ00A&self_app_name=icardcenter&app_name=icardcenter&_TIMEOUT=3000"; ProviderInfo provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue(defaultProtocol.equals(provider.getProtocolType())); Assert.assertTrue("10.244.22.1".equals(provider.getHost())); Assert.assertTrue(provider.getPort() == 8080); Assert.assertTrue("GZ00A".equals(provider.getAttr("zone"))); Assert.assertTrue("icardcenter".equals(provider.getAttr(ProviderInfoAttrs.ATTR_APP_NAME))); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT) == 3000); url = "11.166.0.239:12200?_TIMEOUT=3000&p=1&_SERIALIZETYPE=hessian2&app_name=iptcore&zone=GZ00B&_IDLETIMEOUT=27&_MAXREADIDLETIME=30&v=4.0"; provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue(RpcConstants.PROTOCOL_TYPE_BOLT.equals(provider.getProtocolType())); Assert.assertTrue(RpcConstants.SERIALIZE_HESSIAN2.equals(provider.getSerializationType())); Assert.assertTrue("11.166.0.239".equals(provider.getHost())); Assert.assertTrue(provider.getPort() == 12200); Assert.assertTrue("GZ00B".equals(provider.getAttr("zone"))); Assert.assertTrue("iptcore".equals(provider.getAttr(ProviderInfoAttrs.ATTR_APP_NAME))); Assert.assertTrue("27".equals(provider.getAttr("_IDLETIMEOUT"))); Assert.assertTrue("30".equals(provider.getAttr("_MAXREADIDLETIME"))); Assert.assertTrue("4.0".equals(provider.getAttr("v"))); Assert.assertTrue("1".equals(provider.getAttr("p"))); url = "10.209.80.104:12200?zone=GZ00A&self_app_name=icif&_SERIALIZETYPE=java&app_name=icif&_TIMEOUT=3000"; provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue(defaultProtocol.equals(provider.getProtocolType())); Assert.assertTrue(RpcConstants.SERIALIZE_JAVA.equals(provider.getSerializationType())); Assert.assertTrue("10.209.80.104".equals(provider.getHost())); Assert.assertTrue(provider.getPort() == 12200); Assert.assertTrue("GZ00A".equals(provider.getAttr("zone"))); Assert.assertTrue("icif".equals(provider.getAttr(ProviderInfoAttrs.ATTR_APP_NAME))); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT) == 3000); Assert.assertTrue(provider.getAttr("v") == null); Assert.assertTrue(provider.getAttr("p") == null); url = "10.209.76.82:12200?_TIMEOUT=3000&p=13&_SERIALIZETYPE=11&app_name=ipayprocess&zone=GZ00A&_IDLETIMEOUT=27&_MAXREADIDLETIME=30&v=4.0&[xx]=[_AUTORECONNECT#false@_TIMEOUT#2000]"; provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue(RpcConstants.PROTOCOL_TYPE_TR.equals(provider.getProtocolType())); Assert.assertTrue(RpcConstants.SERIALIZE_PROTOBUF.equals(provider.getSerializationType())); Assert.assertTrue("10.209.76.82".equals(provider.getHost())); Assert.assertTrue(provider.getPort() == 12200); Assert.assertTrue("GZ00A".equals(provider.getAttr("zone"))); Assert.assertTrue("ipayprocess".equals(provider.getAttr(ProviderInfoAttrs.ATTR_APP_NAME))); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT) == 3000); Assert.assertTrue("27".equals(provider.getAttr("_IDLETIMEOUT"))); Assert.assertTrue("30".equals(provider.getAttr("_MAXREADIDLETIME"))); Assert.assertTrue("4.0".equals(provider.getAttr("v"))); Assert.assertTrue("13".equals(provider.getAttr("p"))); Assert.assertTrue((Integer) provider.getDynamicAttr(".xx.timeout") == 2000); Assert.assertTrue("false".equals(provider.getAttr(".xx._AUTORECONNECT"))); url = "tri: provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue(RpcConstants.PROTOCOL_TYPE_TRIPLE.equals(provider.getProtocolType())); Assert.assertTrue(RpcConstants.SERIALIZE_PROTOBUF.equals(provider.getSerializationType())); Assert.assertTrue("10.15.232.229".equals(provider.getHost())); Assert.assertTrue(provider.getPort() == 55555); Assert.assertTrue("test".equals(provider.getAttr(ProviderInfoAttrs.ATTR_APP_NAME))); Assert.assertTrue("1".equals(provider.getAttr(ProviderInfoAttrs.ATTR_CONNECTIONS))); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT) == 4000); Assert.assertTrue("4.0".equals(provider.getAttr("v"))); Assert.assertTrue("1".equals(provider.getAttr("p"))); url = "10.15.232.229:12222?_TIMEOUT=3333&p=1&_SERIALIZETYPE=4&_CONNECTIONNUM=1&_WARMUPTIME=6&_WARMUPWEIGHT=5&app_name=test-server&v=4.0&_WEIGHT=2000&[cd]=[]&[echoStr]=[clientTimeout#4444]"; provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue(RpcConstants.PROTOCOL_TYPE_BOLT.equals(provider.getProtocolType())); Assert.assertTrue(RpcConstants.SERIALIZE_HESSIAN2.equals(provider.getSerializationType())); Assert.assertTrue("10.15.232.229".equals(provider.getHost())); Assert.assertTrue(provider.getPort() == 12222); Assert.assertTrue("test-server".equals(provider.getAttr(ProviderInfoAttrs.ATTR_APP_NAME))); Assert.assertTrue("1".equals(provider.getAttr(ProviderInfoAttrs.ATTR_CONNECTIONS))); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT) == 3333); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT) == 5); Assert.assertTrue(provider.getDynamicAttr(ProviderInfoAttrs.ATTR_WARM_UP_END_TIME) != null); Assert.assertEquals(provider.getStaticAttr(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT), "5"); Assert.assertEquals(provider.getStaticAttr(ProviderInfoAttrs.ATTR_WARMUP_TIME), "6"); Assert.assertTrue(provider.getWeight() == 5); Assert.assertTrue(provider.getStatus() == ProviderStatus.WARMING_UP); try { Thread.sleep(10); } catch (Exception e) { } Assert.assertTrue(provider.getWeight() == 2000); Assert.assertTrue(provider.getStatus() == ProviderStatus.AVAILABLE); Assert.assertTrue(provider.getDynamicAttr(ProviderInfoAttrs.ATTR_WARM_UP_END_TIME) == null); Assert.assertTrue("4.0".equals(provider.getAttr("v"))); Assert.assertTrue("1".equals(provider.getAttr("p"))); Assert.assertTrue(provider.getAttr(".cd.timeout") == null); Assert.assertTrue((Integer) provider.getDynamicAttr(".echoStr.timeout") == 4444); url = "10.15.232.229:12222?_TIMEOUT=3333&p=1&_SERIALIZETYPE=4&_CONNECTIONNUM=1&_WARMUPTIME=6&_WARMUPWEIGHT=5&startTime=123456"; provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT) == 3333); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT) == 5); Assert.assertTrue(provider.getDynamicAttr(ProviderInfoAttrs.ATTR_WARM_UP_END_TIME) != null); Assert.assertEquals(provider.getStaticAttr(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT), "5"); Assert.assertEquals(provider.getStaticAttr(ProviderInfoAttrs.ATTR_WARMUP_TIME), "6"); Assert.assertTrue(provider.getWeight() == 100); Assert.assertTrue(provider.getStatus() == ProviderStatus.AVAILABLE); url = "bolt: provider = SofaRegistryHelper.parseProviderInfo(url); Assert.assertTrue(RpcConstants.PROTOCOL_TYPE_BOLT.equals(provider.getProtocolType())); Assert.assertTrue(RpcConstants.SERIALIZE_HESSIAN2.equals(provider.getSerializationType())); Assert.assertTrue("10.244.22.1".equals(provider.getHost())); Assert.assertTrue(provider.getPort() == 8080); Assert.assertTrue("GZ00A".equals(provider.getAttr("zone"))); Assert.assertTrue("icardcenter".equals(provider.getAttr(ProviderInfoAttrs.ATTR_APP_NAME))); Assert.assertTrue((Integer) provider.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT) == 3000); }