2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.baseservice.roa.util;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Enumeration;
24 import java.util.Properties;
27 * Service connection configuration util.<br/>
29 * Get host and port from the Client Configure the connection environment and service Configure
34 * @version SDNO 0.5 28-May-2016
36 public class ServiceUtil {
38 private static final Logger LOG = LoggerFactory.getLogger(ServiceUtil.class);
40 private final Properties allConfigure = new Properties();
42 private final Properties serviceConfigure;
44 private String serviceStage;
46 private String serviceName;
51 * Load profile information.
55 * @param serviceName user-specified service name.
56 * @param url invoked service url.
58 public ServiceUtil(final String serviceName, final String url) {
59 final String fomattedUrl = formatUrl(url);
60 serviceConfigure = loadProperties("service-configure.properties");
61 if(null == serviceName || serviceName.isEmpty()) {
62 this.serviceName = getServiceNameWhitUrl(fomattedUrl);
64 this.serviceName = serviceName;
70 * Get the service user-specified host.
76 public String getServiceHost() {
77 final String host = allConfigure.getProperty(serviceName + "." + serviceStage + ".host");
85 * Get the service user-specified port.
91 public int getServicePort() {
92 final String portStr = allConfigure.getProperty(serviceName + "." + serviceStage + ".port");
96 return Integer.parseInt(portStr);
99 private String getServiceNameWhitUrl(final String url) {
100 final Enumeration<?> keys = serviceConfigure.propertyNames();
101 while(keys.hasMoreElements()) {
102 final String key = (String)keys.nextElement();
103 if(key.endsWith("urls")) {
104 final String urls = serviceConfigure.getProperty(key);
105 for(String tempUrl : urls.split(",")) {
106 tempUrl = formatUrl(tempUrl);
107 if(url.startsWith(tempUrl)) {
108 return key.split("\\.")[0];
117 private static String formatUrl(final String url) {
119 if(outUrl.contains("?")) {
120 outUrl = outUrl.split("\\?")[0];
122 outUrl = outUrl.replace("\\", "/");
123 outUrl = outUrl.replaceAll("[/]{2,}", "/");
124 outUrl = outUrl.endsWith("/") ? outUrl.substring(0, outUrl.length() - 1) : outUrl;
125 outUrl = outUrl.endsWith("/*") ? outUrl.substring(0, outUrl.length() - 2) : outUrl;
130 * Loads the client and service configuration files.
135 private void loadPropertyFile() {
136 final Properties clientConfigure = loadProperties(serviceName + "-client-configure.properties");
137 allConfigure.putAll(clientConfigure);
138 allConfigure.putAll(serviceConfigure);
139 serviceStage = allConfigure.getProperty(serviceName + ".stage");
143 * Loads the client and service configuration files.
146 * @param classProperties: service profile file name.
147 * @return Service configuration.
150 private Properties loadProperties(final String classProperties) {
151 final Properties properties = new Properties();
152 InputStream inputStream = null;
153 ClassLoader classloader = null;
155 classloader = this.getClass().getClassLoader();
156 if(classloader != null) {
157 inputStream = classloader.getResourceAsStream(classProperties);
159 if(inputStream != null) {
160 properties.load(inputStream);
163 } catch(final IOException e) {
164 LOG.error("load file error: ", e);
166 if(inputStream != null) {
169 } catch(final IOException ee) {
170 LOG.error("close inputStream error: ", ee);