add the logic for the register service CBB to call the msb
[vfc/nfvo/wfengine.git] / common-util / src / main / java / org / openo / baseservice / bus / util / RegisterServiceListener.java
1 /*
2  * Copyright 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
17 package org.openo.baseservice.bus.util;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import javax.servlet.ServletContextEvent;
23 import javax.servlet.ServletContextListener;
24
25 import org.openo.baseservice.util.impl.SystemEnvVariablesFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Initialize the service register listener. <br/>
31  * <p>
32  * </p>
33  * 
34  * @author
35  * @version
36  */
37 public class RegisterServiceListener implements ServletContextListener {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(RegisterServiceListener.class);
40
41     @Override
42     public void contextInitialized(ServletContextEvent sce) {
43
44         String servicePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BusConstant.MICROSERVICE_PATH;
45         LOGGER.info("microservices json file path is" + servicePath);
46
47         File file = new File(servicePath);
48
49         File[] fileList = file.listFiles();
50
51         for (File tempFile : fileList) {
52             String fileName = tempFile.getName();
53             if (fileName.substring(fileName.lastIndexOf('.') + 1).equalsIgnoreCase(BusConstant.JSON)) {
54                 LOGGER.info("begin to initialize the service file" + tempFile.getAbsolutePath());
55
56                 try {
57                     RegisterService.registerService(tempFile.getAbsolutePath(), true);
58                 } catch (IOException e) {
59                     LOGGER.error("Faile to register the service file :" + tempFile.getPath() + ", exception:" + e);
60                 }
61             }
62         }
63     }
64
65     @Override
66     public void contextDestroyed(ServletContextEvent sce) {
67         // TODO Auto-generated method stub
68
69     }
70 }