/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.servicecomb.core.provider.producer;

import java.io.Closeable;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.apache.commons.io.IOUtils;
import org.apache.servicecomb.core.BootListener;
import org.apache.servicecomb.core.ProducerProvider;
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.definition.MicroserviceMeta;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.apache.servicecomb.core.definition.SchemaMeta;
import org.apache.servicecomb.core.definition.SchemaUtils;
import org.apache.servicecomb.serviceregistry.RegistryUtils;
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProducerProviderManager implements BootListener {
  private static final Logger LOGGER = LoggerFactory.getLogger(ProducerProviderManager.class);

  @Autowired(required = false)
  private List<ProducerProvider> producerProviderList = Collections.emptyList();

  private MicroserviceMeta microserviceMeta;

  public void init() throws Exception {
    for (ProducerProvider provider : producerProviderList) {
      provider.init();
    }
    Microservice microservice = RegistryUtils.getMicroservice();
    microserviceMeta = SCBEngine.getInstance().getProducerMicroserviceMeta();
    for (SchemaMeta schemaMeta : microserviceMeta.getSchemaMetas()) {
      String content = SchemaUtils.swaggerToString(schemaMeta.getSwagger());
      microservice.addSchema(schemaMeta.getSchemaId(), content);
    }
  }

  @Override
  public void onBootEvent(BootEvent event) {
    if (!EventType.AFTER_CLOSE.equals(event.getEventType())) {
      return;
    }
    if (microserviceMeta == null) {
      return;
    }
    for (OperationMeta operationMeta : microserviceMeta.getOperations()) {
      if (ExecutorService.class.isInstance(operationMeta.getExecutor())) {
        ((ExecutorService) operationMeta.getExecutor()).shutdown();
        continue;
      }

      if (Closeable.class.isInstance(operationMeta.getExecutor())) {
        IOUtils.closeQuietly((Closeable) operationMeta.getExecutor());
        continue;
      }

      LOGGER.warn("Executor {} do not support close or shutdown, it may block service shutdown.",
          operationMeta.getExecutor().getClass().getName());
    }
  }
}

@@ -1709,16 +1709,84 @@
 onent;%0A%0A
+import io.swagger.models.Scheme;%0Aimport io.swagger.models.Swagger;%0A%0A
 @Compone
@@ -2242,70 +2242,611 @@
 %7D%0A  
-  Microservice microservice = RegistryUtils.getMicroservice();
+%7D%0A%0A  @Override%0A  public void onBootEvent(BootEvent event) %7B%0A    switch (event.getEventType()) %7B%0A      case AFTER_TRANSPORT:%0A        registerSchemaToMicroservice();%0A        break;%0A      case AFTER_CLOSE:%0A        onClose();%0A        break;%0A    %7D%0A  %7D%0A%0A  private void registerSchemaToMicroservice() %7B%0A    Microservice microservice = RegistryUtils.getMicroservice();%0A%0A    String swaggerSchema = %22http%22;%0A    for (String endpoint : microservice.getInstance().getEndpoints()) %7B%0A      if (endpoint.startsWith(%22rest://%22) && endpoint.indexOf(%22sslEnabled=true%22) %3E 0) %7B%0A        swaggerSchema = %22https%22;%0A      %7D%0A    %7D%0A
 %0A   
@@ -2986,24 +2986,130 @@
 aMetas()) %7B%0A
+      Swagger swagger = schemaMeta.getSwagger();%0A      swagger.addScheme(Scheme.forValue(swaggerSchema));%0A
       String
@@ -3148,38 +3148,22 @@
 String(s
-chemaMeta.getS
 wagger
-()
 );%0A     
@@ -3239,145 +3239,32 @@
 %0A%0A  
-@Override%0A  public void onBootEvent(BootEvent event) %7B%0A    if (!EventType.AFTER_CLOSE.equals(event.getEventType())) %7B%0A      return;%0A    %7D
+private void onClose() %7B
 %0A   

add http or https declare in swagger