2 * Copyright 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.
17 package org.openo.baseservice.bus.util;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Properties;
25 import javax.ws.rs.core.Response;
27 import org.apache.cxf.jaxrs.client.WebClient;
28 import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
29 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Provide the service register cbb for common use. <br/>
41 public class RegisterService {
43 private static final Logger LOGGER = LoggerFactory.getLogger(RegisterService.class);
45 private static String busPath = null;
56 private RegisterService() {
60 * register the micro service. <br/>
63 * the service json object to register to the bus.
64 * @param createOrUpdate:
65 * true, create and update the old ip port. false, create and
71 public static Response registerService(String jsonPath, boolean createOrUpdate) throws IOException {
73 String serviceInfo = getServiceModel(jsonPath);
75 WebClient client = initializeClient();
77 client.type(BusConstant.APPLICATION_JSON_HEADER);
79 client.accept(BusConstant.APPLICATION_JSON_HEADER);
81 client.path(BusConstant.BUS_SERVICE_URL);
83 client.query(BusConstant.CREATE_OR_UPDATE, createOrUpdate);
85 LOGGER.info("Connecting bus address : " + busPath + BusConstant.BUS_SERVICE_URL);
87 return client.invoke(BusConstant.POST_METHOD, serviceInfo);
92 * get the service's model. and return it as a string ; <br/>
98 private static String getServiceModel(String jsonPath) {
100 String serviceInfo = "";
103 LOGGER.info("begin to read file micro service json " + jsonPath);
105 FileInputStream busFile = new FileInputStream(jsonPath);
107 int size = busFile.available();
109 byte[] buffer = new byte[size];
111 busFile.read(buffer);
115 serviceInfo = new String(buffer);
116 LOGGER.info("finished to read micro service json file. ");
117 } catch (Exception ex) {
118 LOGGER.error("Read the micro service json file error :", ex);
124 * initialize the bus ip and port. <br/>
127 * @throws IOException
130 private static String getBusAddress() throws IOException {
132 LOGGER.info("begin to get the bus baseurl.");
133 FileInputStream busFile = null;
134 String url = BusConstant.MICROSERVICE_DEFAULT;
136 String filePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BusConstant.BUS_CONFIGURE_FILE;
137 LOGGER.info("bus base url file:" + filePath);
139 Properties properties = new Properties();
142 busFile = new FileInputStream(filePath);
143 properties.load(busFile);
144 url = properties.getProperty(BusConstant.BUS_ADDRESS_KEY);
145 } catch (IOException e) {
146 if (busFile != null) {
149 LOGGER.error("Read the bus url failed: ", e);
152 LOGGER.info("initialize the bus baseurl is: " + url);
153 return BusConstant.HTTP_HEAD + url;
157 * get the bus's client's address. and initialize the web client. <br/>
160 * @throws IOException
163 private static WebClient initializeClient() throws IOException {
165 final List<Object> providers = new ArrayList<Object>();
167 JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider();
169 providers.add(jacksonJsonProvider);
171 if (busPath == null) {
172 busPath = getBusAddress();
175 return WebClient.create(busPath, providers);