2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.msb.wrapper;
\r
19 import java.util.ArrayList;
\r
20 import java.util.Iterator;
\r
21 import java.util.List;
\r
22 import java.util.Set;
\r
23 import java.util.regex.Matcher;
\r
24 import java.util.regex.Pattern;
\r
26 import org.openo.msb.api.ServiceAccessInfo;
\r
27 import org.openo.msb.wrapper.util.JedisUtil;
\r
28 import org.openo.msb.wrapper.util.RouteUtil;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
32 import redis.clients.jedis.Jedis;
\r
34 public class ServiceAccessWrapper {
\r
35 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAccessWrapper.class);
\r
37 private static ServiceAccessWrapper instance = new ServiceAccessWrapper();
\r
39 private ServiceAccessWrapper() {}
\r
41 public static ServiceAccessWrapper getInstance() {
\r
45 public ServiceAccessInfo getApiServiceAccessAddr(String serviceName, String version, String host) {
\r
47 ServiceAccessInfo apiRouteAccessInfo = null;
\r
51 jedis = JedisUtil.borrowJedisInstance();
\r
52 if (jedis == null) {
\r
53 throw new Exception("fetch from jedis pool failed,null object!");
\r
56 if ("null".equals(version)) {
\r
61 RouteUtil.getPrefixedKey("",RouteUtil.APIROUTE, serviceName, version,
\r
62 RouteUtil.ROUTE_PATH_INFO);
\r
63 Boolean isExist = jedis.exists(routekey);
\r
65 apiRouteAccessInfo = new ServiceAccessInfo();
\r
66 apiRouteAccessInfo.setServiceName(serviceName);
\r
67 apiRouteAccessInfo.setVersion(version);
\r
68 String accessAddr = "http://" + host + "/api/" + serviceName + "/" + version;
\r
69 apiRouteAccessInfo.setAccessAddr(accessAddr);
\r
71 } catch (Exception e) {
\r
72 LOGGER.error("call redis throw exception", e);
\r
74 JedisUtil.returnJedisInstance(jedis);
\r
76 return apiRouteAccessInfo;
\r
80 public List<ServiceAccessInfo> getApiRouteAccessAddr(String serviceType, String serviceName,
\r
81 String version, String host) {
\r
82 List<ServiceAccessInfo> serviceList = new ArrayList<ServiceAccessInfo>();
\r
85 jedis = JedisUtil.borrowJedisInstance();
\r
86 if (jedis == null) {
\r
87 throw new Exception("fetch from jedis pool failed,null object!");
\r
90 String keyPattern = this.getRedisSearchPattern(serviceType, serviceName, version);
\r
91 Set<String> infoKeys = jedis.keys(keyPattern);
\r
92 Pattern pattern = this.getKeyPattern();
\r
93 for (Iterator<String> iterator = infoKeys.iterator(); iterator.hasNext();) {
\r
94 String infoKey = (String) iterator.next();
\r
95 Matcher matcher = pattern.matcher(infoKey);
\r
96 if (matcher.matches()) {
\r
97 ServiceAccessInfo serviceAccessInfo = new ServiceAccessInfo();
\r
98 serviceAccessInfo.setServiceType(matcher.group("servicetype"));
\r
99 serviceAccessInfo.setServiceName(matcher.group("servicename"));
\r
100 serviceAccessInfo.setVersion(matcher.group("version"));
\r
101 this.buildServiceAccessAddr(serviceAccessInfo, host, infoKey, jedis);
\r
102 serviceList.add(serviceAccessInfo);
\r
105 } catch (Exception e) {
\r
106 LOGGER.error("call redis throw exception", e);
\r
108 JedisUtil.returnJedisInstance(jedis);
\r
110 return serviceList;
\r
114 private void buildServiceAccessAddr(ServiceAccessInfo serviceAccessInfo, String host,
\r
115 String infoKey, Jedis jedis) {
\r
116 String serviceType = serviceAccessInfo.getServiceType();
\r
117 StringBuffer accessAddr = new StringBuffer();
\r
118 switch (serviceType) {
\r
119 case RouteUtil.APIROUTE:
\r
120 accessAddr.append("http://").append(host).append(":").append(JedisUtil.serverPort)
\r
121 .append("/").append(serviceAccessInfo.getServiceType()).append("/")
\r
122 .append(serviceAccessInfo.getServiceName()).append("/")
\r
123 .append(serviceAccessInfo.getVersion());
\r
124 serviceAccessInfo.setAccessAddr(accessAddr.toString());
\r
126 case RouteUtil.IUIROUTE:
\r
127 accessAddr.append("http://").append(host).append(":").append(JedisUtil.serverPort)
\r
128 .append("/").append(serviceAccessInfo.getServiceType()).append("/")
\r
129 .append(serviceAccessInfo.getServiceName());
\r
130 serviceAccessInfo.setAccessAddr(accessAddr.toString());
\r
132 case RouteUtil.CUSTOMROUTE:
\r
133 accessAddr.append("http://").append(host).append(":").append(JedisUtil.serverPort)
\r
134 .append(serviceAccessInfo.getServiceName());
\r
135 serviceAccessInfo.setAccessAddr(accessAddr.toString());
\r
137 case RouteUtil.P2PROUTE:
\r
138 accessAddr.append(jedis.hget(infoKey, "url"));
\r
139 serviceAccessInfo.setAccessAddr(accessAddr.toString());
\r
142 serviceAccessInfo.setAccessAddr("not supported now");
\r
147 private String getRedisSearchPattern(String serviceType, String serviceName, String version) {
\r
148 StringBuffer sb = new StringBuffer();
\r
149 sb.append(RouteUtil.ROUTE_PATH);
\r
150 if (null != serviceType && !"".equals(serviceType)) {
\r
151 sb.append(":").append(serviceType);
\r
153 sb.append(":").append("*");
\r
155 sb.append(":").append(serviceName);
\r
156 if (null != version && !"".equals(version)) {
\r
158 sb.append(version);
\r
163 sb.append(RouteUtil.ROUTE_PATH_INFO);
\r
164 return sb.toString();
\r
167 private Pattern getKeyPattern() {
\r
169 "conductor:routing:(?<servicetype>api|iui|custom|p2p):(?<servicename>[^:]+)(:(?<version>[^:]*))?:info";
\r
170 return Pattern.compile(pStr);
\r