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;
35 * Provide the service register cbb for common use.
43 public class RegisterService {
45 private static final Logger LOGGER = LoggerFactory.getLogger(RegisterService.class);
47 private static final String BUS_CONFIGURE_FILE = "/etc/microservice.ini";
49 private static final String BUS_SERVICE_URL = "/openoapi/microservices/v1/services";
51 private static final String BUS_ADDRESS_KEY = "msb.address";
53 private static String busPath = null;
63 private RegisterService() {
67 * register the micro service.
70 * @param jsonPath: the service json object to register to the bus.
71 * @param createOrUpdate: true, create and update the old ip port. false, create and delete the
77 public static Response registerService(String jsonPath, boolean createOrUpdate) throws IOException {
79 String serviceInfo = getServiceModel(jsonPath);
81 WebClient client = initializeClient();
83 client.type("application/json");
85 client.accept("application/json");
87 client.path(BUS_SERVICE_URL);
89 client.query("createOrUpdate", createOrUpdate);
91 LOGGER.info("Connecting bus address : " + busPath + BUS_SERVICE_URL);
93 return client.invoke("POST", serviceInfo);
98 * get the service's model. and return it as a string ;
105 private static String getServiceModel(String jsonPath) {
107 String serviceInfo = "";
110 LOGGER.info("begin to read file micro service json " + jsonPath);
112 FileInputStream busFile = new FileInputStream(jsonPath);
114 int size = busFile.available();
116 byte[] buffer = new byte[size];
118 busFile.read(buffer);
122 serviceInfo = new String(buffer);
123 LOGGER.info("finished to read micro service json file. ");
124 } catch(Exception ex) {
125 LOGGER.error("Read the micro service json file error :", ex);
130 * initialize the bus ip and port.
134 * @throws IOException
137 private static String getBusAdderss() throws IOException {
139 LOGGER.info("begin to get the bus baseurl.");
140 FileInputStream busFile = null;
141 String url = "msb.openo.org:80";
143 String filePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BUS_CONFIGURE_FILE;
144 LOGGER.info("bus base url file:" + filePath);
146 Properties properties = new Properties();
149 busFile = new FileInputStream(filePath);
150 properties.load(busFile);
151 url = properties.getProperty(BUS_ADDRESS_KEY);
152 } catch(IOException e) {
153 if (busFile != null) {
156 LOGGER.error("Read the bus url failed: ", e);
159 LOGGER.info("initialize the bus baseurl is: " + url);
160 return "http://" + url;
164 * get the bus's client's address. and initialize the web client.
168 * @throws IOException
171 private static WebClient initializeClient() throws IOException {
173 final List<Object> providers = new ArrayList<Object>();
175 JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider();
177 providers.add(jacksonJsonProvider);
179 if (busPath == null) {
180 busPath = getBusAdderss();
183 return WebClient.create(busPath, providers);