You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/06/01 01:31:39 UTC

[GitHub] liubao68 commented on a change in pull request #741: [SCB-625] ProduceProcessor use SPI to support extends

liubao68 commented on a change in pull request #741: [SCB-625] ProduceProcessor use SPI to support extends
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/741#discussion_r192280141
 
 

 ##########
 File path: common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/produce/ProduceProcessorManager.java
 ##########
 @@ -17,24 +17,43 @@
 
 package org.apache.servicecomb.common.rest.codec.produce;
 
+import java.util.ServiceLoader;
+
 import javax.ws.rs.core.MediaType;
 
 import org.apache.servicecomb.foundation.common.RegisterManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class ProduceProcessorManager extends RegisterManager<String, ProduceProcessor> {
+  private static final Logger LOGGER = LoggerFactory.getLogger(ProduceProcessorManager.class);
+
+  private static final ServiceLoader<ProduceProcessor> produceProcessor = ServiceLoader.load(ProduceProcessor.class);
+
   private static final String NAME = "produce processor mgr";
 
   public static final String DEFAULT_TYPE = MediaType.APPLICATION_JSON;
 
   public static final ProduceProcessorManager INSTANCE = new ProduceProcessorManager();
 
-  public static final ProduceProcessor JSON_PROCESSOR = new ProduceJsonProcessor();
+  public static final ProduceProcessor JSON_PROCESSOR = chooseProduceProcessor(MediaType.APPLICATION_JSON);
 
-  public static final ProduceProcessor PLAIN_PROCESSOR = new ProduceTextPlainProcessor();
+  public static final ProduceProcessor PLAIN_PROCESSOR = chooseProduceProcessor(MediaType.TEXT_PLAIN);
 
   public static final ProduceProcessor DEFAULT_PROCESSOR = JSON_PROCESSOR;
 
   private ProduceProcessorManager() {
     super(NAME);
+    produceProcessor.forEach(processor -> register(processor.getName(), processor));
+  }
+
+  public static ProduceProcessor chooseProduceProcessor(String type) {
+    for (ProduceProcessor processor : produceProcessor) {
+      if (type.equals(processor.getName()))
+        return processor;
+    }
+    LOGGER.warn("Getting the current produceProcessor type {} failed, use default produceProcessor: application/json",
+        type);
+    return new ProduceJsonProcessor();
 
 Review comment:
   This is not fixed as mentioned in previous comment. Do we need to create a new instance or can reuse JSON_PROCESSOR ?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services