Add wso2bpel-ext code
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / wso2bpel-mgr / src / main / java / org / openo / carbon / bpel / Wso2BpelApplication.java
1 /**
2  * Copyright 2016 [ZTE] and others.
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.carbon.bpel;
17
18 import io.dropwizard.Application;
19 import io.dropwizard.assets.AssetsBundle;
20 import io.dropwizard.configuration.EnvironmentVariableSubstitutor;
21 import io.dropwizard.configuration.SubstitutingSourceProvider;
22 import io.dropwizard.server.SimpleServerFactory;
23 import io.dropwizard.setup.Bootstrap;
24 import io.dropwizard.setup.Environment;
25 import io.swagger.jaxrs.config.BeanConfig;
26 import io.swagger.jaxrs.listing.ApiListingResource;
27
28 import org.glassfish.jersey.media.multipart.MultiPartFeature;
29 import org.openo.carbon.bpel.common.Config;
30 import org.openo.carbon.bpel.common.ServiceRegistrer;
31 import org.openo.carbon.bpel.resources.BpsPackage;
32 import org.openo.carbon.bpel.resources.BpsProcess;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.fasterxml.jackson.annotation.JsonInclude;
37
38
39
40 public class Wso2BpelApplication extends Application<Wso2BpelConfiguration> {
41   private static final Logger LOGGER = LoggerFactory.getLogger(Wso2BpelApplication.class);
42   private static final String API_RESOURCE = "org.openo.carbon.bpel.resources";
43
44   public static void main(String[] args) throws Exception {
45     new Wso2BpelApplication().run(args);
46   }
47
48
49   @Override
50   public String getName() {
51     return "hello-wso2bpel";
52   }
53
54   @Override
55   public void initialize(Bootstrap<Wso2BpelConfiguration> bootstrap) {
56     bootstrap.setConfigurationSourceProvider(new SubstitutingSourceProvider(
57         bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false)));
58     // binding jar static resource
59     bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
60   }
61   
62   private void initService() {
63     Thread registerWso2bpelService = new Thread(new ServiceRegistrer());
64     registerWso2bpelService.setName("register wso2bpel service to Microservice Bus");
65     registerWso2bpelService.start();
66   }
67
68
69
70   @Override
71   public void run(Wso2BpelConfiguration configuration, Environment environment) {
72     environment.jersey().register(MultiPartFeature.class);
73     environment.jersey().register(new BpsPackage());
74     environment.jersey().register(new BpsProcess());
75     // init Swagger conf
76     initSwaggerConfig(environment, configuration);
77     Config.setConfigration(configuration);
78     initService();
79   }
80
81   private void initSwaggerConfig(Environment environment, Wso2BpelConfiguration configuration) {
82     // register swagger scan class
83     environment.jersey().register(new ApiListingResource());
84     environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
85
86     BeanConfig config = new BeanConfig();
87     config.setTitle(configuration.getApiDescription());
88     config.setVersion("1.0");
89     config.setResourcePackage(API_RESOURCE);
90
91     // read rootPath config from yml
92     SimpleServerFactory simpleServerFactory =
93         (SimpleServerFactory) configuration.getServerFactory();
94     String basePath = simpleServerFactory.getApplicationContextPath();
95     String rootPath = simpleServerFactory.getJerseyRootPath();
96
97     // set basepath for rest api
98     rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
99     basePath = basePath.equals("/") ? rootPath
100         : (new StringBuilder()).append(basePath).append(rootPath).toString();
101
102     LOGGER.info("getApplicationContextPath:" + basePath);
103     config.setBasePath(basePath);
104     config.setScan(true);
105   }
106 }