summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzhouruiyu <zhouruiyu@huawei.com>2016-09-27 10:30:25 +0800
committerzhouruiyu <zhouruiyu@huawei.com>2016-09-27 10:30:25 +0800
commitbc536c771a675bae597d0f8d9533e0a6022adb2b (patch)
tree762c41dc38f173a46c0f5b304f349787d73b90ef
parentb44388f5e377f0cb96610b91e609110620246f2c (diff)
add the logic for the register service CBB to call the msb
Change-Id: I928f7593dd1a5494b25d013a8aada2e3b904c42a Signed-off-by: zhouruiyu <zhouruiyu@huawei.com>
-rw-r--r--common-util/src/main/java/org/openo/baseservice/bus/util/BusConstant.java48
-rw-r--r--common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java112
-rw-r--r--common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java22
3 files changed, 109 insertions, 73 deletions
diff --git a/common-util/src/main/java/org/openo/baseservice/bus/util/BusConstant.java b/common-util/src/main/java/org/openo/baseservice/bus/util/BusConstant.java
new file mode 100644
index 0000000..ab2bcd1
--- /dev/null
+++ b/common-util/src/main/java/org/openo/baseservice/bus/util/BusConstant.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2016 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openo.baseservice.bus.util;
+
+/**
+ * <p>
+ * An class which holds the all the constant values for the Bus register CBB.
+ * </p>
+ * <br/>
+ *
+ * @author
+ * @version
+ */
+public final class BusConstant {
+
+ public static final String JSON = "json";
+
+ public static final String BUS_CONFIGURE_FILE = "/etc/microservice.ini";
+
+ public static final String BUS_SERVICE_URL = "/openoapi/microservices/v1/services";
+
+ public static final String BUS_ADDRESS_KEY = "msb.address";
+
+ public static final String APPLICATION_JSON_HEADER = "application/json";
+
+ public static final String CREATE_OR_UPDATE = "createOrUpdate";
+
+ public static final String MICROSERVICE_DEFAULT = "msb.openo.org:80";
+
+ public static final String MICROSERVICE_PATH = "/etc/microservice";
+
+ public static final String POST_METHOD = "POST";
+
+ public static final String HTTP_HEAD = "http://";
+}
diff --git a/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java
index 1e0ef33..3a84781 100644
--- a/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java
+++ b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java
@@ -30,126 +30,119 @@ import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
/**
- * Provide the service register cbb for common use.
- * <br/>
+ * Provide the service register cbb for common use. <br/>
* <p>
* </p>
*
* @author
- * @version
+ * @version
*/
public class RegisterService {
private static final Logger LOGGER = LoggerFactory.getLogger(RegisterService.class);
- private static final String BUS_CONFIGURE_FILE = "/etc/microservice.ini";
-
- private static final String BUS_SERVICE_URL = "/openoapi/microservices/v1/services";
-
- private static final String BUS_ADDRESS_KEY = "msb.address";
-
private static String busPath = null;
/**
* Constructor<br/>
* <p>
* </p>
- * @throws IOException
*
- * @since
+ * @throws IOException
+ *
+ * @since
*/
private RegisterService() {
}
/**
- * register the micro service.
- * <br/>
+ * register the micro service. <br/>
*
- * @param jsonPath: the service json object to register to the bus.
- * @param createOrUpdate: true, create and update the old ip port. false, create and delete the
- * old one;
+ * @param jsonPath:
+ * the service json object to register to the bus.
+ * @param createOrUpdate:
+ * true, create and update the old ip port. false, create and
+ * delete the old one;
* @return
* @throws IOException
- * @since
+ * @since
*/
public static Response registerService(String jsonPath, boolean createOrUpdate) throws IOException {
-
+
String serviceInfo = getServiceModel(jsonPath);
-
+
WebClient client = initializeClient();
-
- client.type("application/json");
-
- client.accept("application/json");
-
- client.path(BUS_SERVICE_URL);
-
- client.query("createOrUpdate", createOrUpdate);
-
- LOGGER.info("Connecting bus address : " + busPath + BUS_SERVICE_URL);
-
- return client.invoke("POST", serviceInfo);
-
+
+ client.type(BusConstant.APPLICATION_JSON_HEADER);
+
+ client.accept(BusConstant.APPLICATION_JSON_HEADER);
+
+ client.path(BusConstant.BUS_SERVICE_URL);
+
+ client.query(BusConstant.CREATE_OR_UPDATE, createOrUpdate);
+
+ LOGGER.info("Connecting bus address : " + busPath + BusConstant.BUS_SERVICE_URL);
+
+ return client.invoke(BusConstant.POST_METHOD, serviceInfo);
+
}
/**
- * get the service's model. and return it as a string ;
- * <br/>
+ * get the service's model. and return it as a string ; <br/>
*
* @param jsonPath
* @return
- * @since
+ * @since
*/
private static String getServiceModel(String jsonPath) {
-
+
String serviceInfo = "";
-
+
try {
LOGGER.info("begin to read file micro service json " + jsonPath);
-
+
FileInputStream busFile = new FileInputStream(jsonPath);
-
+
int size = busFile.available();
-
+
byte[] buffer = new byte[size];
-
+
busFile.read(buffer);
-
+
busFile.close();
-
+
serviceInfo = new String(buffer);
LOGGER.info("finished to read micro service json file. ");
- } catch(Exception ex) {
+ } catch (Exception ex) {
LOGGER.error("Read the micro service json file error :", ex);
}
return serviceInfo;
}
+
/**
- * initialize the bus ip and port.
- * <br/>
+ * initialize the bus ip and port. <br/>
*
* @return
* @throws IOException
- * @since
+ * @since
*/
private static String getBusAdderss() throws IOException {
LOGGER.info("begin to get the bus baseurl.");
FileInputStream busFile = null;
- String url = "msb.openo.org:80";
+ String url = BusConstant.MICROSERVICE_DEFAULT;
- String filePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BUS_CONFIGURE_FILE;
+ String filePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BusConstant.BUS_CONFIGURE_FILE;
LOGGER.info("bus base url file:" + filePath);
-
+
Properties properties = new Properties();
try {
busFile = new FileInputStream(filePath);
properties.load(busFile);
- url = properties.getProperty(BUS_ADDRESS_KEY);
- } catch(IOException e) {
+ url = properties.getProperty(BusConstant.BUS_ADDRESS_KEY);
+ } catch (IOException e) {
if (busFile != null) {
busFile.close();
}
@@ -157,19 +150,18 @@ public class RegisterService {
}
LOGGER.info("initialize the bus baseurl is: " + url);
- return "http://" + url;
+ return BusConstant.HTTP_HEAD + url;
}
-
+
/**
- * get the bus's client's address. and initialize the web client.
- * <br/>
+ * get the bus's client's address. and initialize the web client. <br/>
*
* @return
- * @throws IOException
- * @since
+ * @throws IOException
+ * @since
*/
private static WebClient initializeClient() throws IOException {
-
+
final List<Object> providers = new ArrayList<Object>();
JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider();
diff --git a/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java
index eb2e122..c4e1417 100644
--- a/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java
+++ b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java
@@ -17,6 +17,7 @@
package org.openo.baseservice.bus.util;
import java.io.File;
+import java.io.IOException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
@@ -26,42 +27,37 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Initialize the service register listener.
- * <br/>
- * <p>
+ * Initialize the service register listener. <br/>
+ * <p>
* </p>
*
* @author
- * @version
+ * @version
*/
public class RegisterServiceListener implements ServletContextListener {
private static final Logger LOGGER = LoggerFactory.getLogger(RegisterServiceListener.class);
-
- private static final String JSON = "json";
@Override
public void contextInitialized(ServletContextEvent sce) {
- String servicePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + "/etc/microservice";
+ String servicePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BusConstant.MICROSERVICE_PATH;
LOGGER.info("microservices json file path is" + servicePath);
File file = new File(servicePath);
File[] fileList = file.listFiles();
- for(File tempFile : fileList) {
+ for (File tempFile : fileList) {
String fileName = tempFile.getName();
- if (fileName.substring(fileName.lastIndexOf('.') + 1).equalsIgnoreCase(JSON)) {
+ if (fileName.substring(fileName.lastIndexOf('.') + 1).equalsIgnoreCase(BusConstant.JSON)) {
LOGGER.info("begin to initialize the service file" + tempFile.getAbsolutePath());
-
- /** now because ZTE do not provide the service bus.commont this code first.
+
try {
RegisterService.registerService(tempFile.getAbsolutePath(), true);
- } catch(IOException e) {
+ } catch (IOException e) {
LOGGER.error("Faile to register the service file :" + tempFile.getPath() + ", exception:" + e);
}
- */
}
}
}