adjust the code
[vfc/nfvo/wfengine.git] / rest-client / src / main / java / org / openo / baseservice / roa / util / ServiceUtil.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.baseservice.roa.util;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Enumeration;
24 import java.util.Properties;
25
26 /**
27  * Service connection configuration util.<br/>
28  * <p>
29  * Get host and port from the Client Configure the connection environment and service Configure
30  * profile
31  * </p>
32  * 
33  * @author
34  * @version SDNO 0.5 28-May-2016
35  */
36 public class ServiceUtil {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ServiceUtil.class);
39
40     private final Properties allConfigure = new Properties();
41
42     private final Properties serviceConfigure;
43
44     private String serviceStage;
45
46     private String serviceName;
47
48     /**
49      * Constructor<br/>
50      * <p>
51      * Load profile information.
52      * </p>
53      * 
54      * @since SDNO 0.5
55      * @param serviceName user-specified service name.
56      * @param url invoked service url.
57      */
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);
63         } else {
64             this.serviceName = serviceName;
65         }
66         loadPropertyFile();
67     }
68
69     /**
70      * Get the service user-specified host.
71      * <br/>
72      * 
73      * @return host
74      * @since SDNO 0.5
75      */
76     public String getServiceHost() {
77         final String host = allConfigure.getProperty(serviceName + "." + serviceStage + ".host");
78         if(null == host) {
79             return "";
80         }
81         return host;
82     }
83
84     /**
85      * Get the service user-specified port.
86      * <br/>
87      * 
88      * @return port
89      * @since SDNO 0.5
90      */
91     public int getServicePort() {
92         final String portStr = allConfigure.getProperty(serviceName + "." + serviceStage + ".port");
93         if(null == portStr) {
94             return -1;
95         }
96         return Integer.parseInt(portStr);
97     }
98
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];
109                     }
110                 }
111
112             }
113         }
114         return "";
115     }
116
117     private static String formatUrl(final String url) {
118         String outUrl = url;
119         if(outUrl.contains("?")) {
120             outUrl = outUrl.split("\\?")[0];
121         }
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;
126         return outUrl;
127     }
128
129     /**
130      * Loads the client and service configuration files.
131      * <br/>
132      * 
133      * @since SDNO 0.5
134      */
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");
140     }
141
142     /**
143      * Loads the client and service configuration files.
144      * <br/>
145      * 
146      * @param classProperties: service profile file name.
147      * @return Service configuration.
148      * @since SDNO 0.5
149      */
150     private Properties loadProperties(final String classProperties) {
151         final Properties properties = new Properties();
152         InputStream inputStream = null;
153         ClassLoader classloader = null;
154         try {
155             classloader = this.getClass().getClassLoader();
156             if(classloader != null) {
157                 inputStream = classloader.getResourceAsStream(classProperties);
158             }
159             if(inputStream != null) {
160                 properties.load(inputStream);
161                 inputStream.close();
162             }
163         } catch(final IOException e) {
164             LOG.error("load file error: ", e);
165         } finally {
166             if(inputStream != null) {
167                 try {
168                     inputStream.close();
169                 } catch(final IOException ee) {
170                     LOG.error("close inputStream  error: ", ee);
171                 }
172                 inputStream = null;
173             }
174         }
175         return properties;
176     }
177 }