/*
 * 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.jmeter.protocol.http.proxy;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.jmeter.util.JMeterUtils;
import org.slf4j.LoggerFactory;
import org.apache.jorphan.reflect.ClassFinder;
import org.slf4j.Logger;

/**
 * {@link SamplerCreator} factory
 */
public class SamplerCreatorFactory {
    private static final Logger log = LoggerFactory.getLogger(SamplerCreatorFactory.class);

    private static final SamplerCreator DEFAULT_SAMPLER_CREATOR = new DefaultSamplerCreator();

    private final Map<String, SamplerCreator> samplerCreatorMap = new HashMap<>();

    /**
     * 
     */
    public SamplerCreatorFactory() {
        init();
    }
    
    /**
     * Initialize factory from classpath
     */
    private void init() { // WARNING: called from ctor so must not be overridden (i.e. must be private or final)
        try {
            List<String> listClasses = ClassFinder.findClassesThatExtend(
                    JMeterUtils.getSearchPaths(), 
                    new Class[] {SamplerCreator.class }); 
            for (String strClassName : listClasses) {
                try {
                    if(log.isDebugEnabled()) {
                        log.debug("Loading class: "+ strClassName);
                    }
                    Class<?> commandClass = Class.forName(strClassName);
                    if (!Modifier.isAbstract(commandClass.getModifiers())) {
                        if(log.isDebugEnabled()) {
                            log.debug("Instantiating: "+ commandClass.getName());
                        }
                            SamplerCreator creator = (SamplerCreator) commandClass.newInstance();
                            String[] contentTypes = creator.getManagedContentTypes();
                            for (String contentType : contentTypes) {
                                if(log.isDebugEnabled()) {
                                    log.debug("Registering samplerCreator "+commandClass.getName()+" for content type:"+contentType);
                                }
                                SamplerCreator oldSamplerCreator = samplerCreatorMap.put(contentType, creator);
                                if(oldSamplerCreator!=null) {
                                    log.warn("A sampler creator was already registered for:"+contentType+", class:"+oldSamplerCreator.getClass()
                                            + ", it will be replaced");
                                }
                            }                        
                    }
                } catch (Exception e) {
                    log.error("Exception registering "+SamplerCreator.class.getName() + " with implementation:"+strClassName, e);
                }
            }
        } catch (IOException e) {
            log.error("Exception finding implementations of "+SamplerCreator.class, e);
        }
    }

    /**
     * Gets {@link SamplerCreator} for content type, if none is found returns {@link DefaultSamplerCreator}
     * @param request {@link HttpRequestHdr} from which the content type should be used
     * @param pageEncodings Map of pageEncodings
     * @param formEncodings  Map of formEncodings
     * @return SamplerCreator for the content type of the <code>request</code>, or {@link DefaultSamplerCreator} when none is found
     */
    public SamplerCreator getSamplerCreator(HttpRequestHdr request,
            Map<String, String> pageEncodings, Map<String, String> formEncodings) {
        SamplerCreator creator = samplerCreatorMap.get(request.getContentType());
        if(creator == null) {
            return DEFAULT_SAMPLER_CREATOR;
        }
        return creator;
    }
}

@@ -2109,18 +2109,20 @@
  class: 
-%22+
+%7B%7D%22,
  strClas
@@ -2406,18 +2406,20 @@
 iating: 
-%22+
+%7B%7D%22,
  command
@@ -2454,36 +2454,32 @@
               %7D%0A
-    
                 
@@ -2580,20 +2580,16 @@
         
-    
 String%5B%5D
@@ -2634,28 +2634,24 @@
 entTypes();%0A
-    
             
@@ -2724,36 +2724,32 @@
                 
-    
 if(log.isDebugEn
@@ -2783,36 +2783,32 @@
                 
-    
 log.debug(%22Regis
@@ -2829,18 +2829,82 @@
 Creator 
-%22+
+%7B%7D for content type:%7B%7D%22, %0A                                        
 commandC
@@ -2921,30 +2921,10 @@
 me()
-+%22 for content type:%22+
+, 
 cont
@@ -2957,34 +2957,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -3099,20 +3099,16 @@
         
-    
 if(oldSa
@@ -3157,28 +3157,24 @@
             
-    
 log.warn(%22A 
@@ -3220,131 +3220,126 @@
 for:
-%22+contentType+%22, class:%22+oldSamplerCreator.getClass()%0A                                            + %22, it will be replaced%22
+%7B%7D, class:%7B%7D, it will be replaced%22, %0A                                        contentType, oldSamplerCreator.getClass()
 );%0A 
@@ -3361,34 +3361,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -3532,18 +3532,72 @@
 stering 
-%22+
+%7B%7D with implementation:%7B%7D%22,%0A                            
 SamplerC
@@ -3622,35 +3622,9 @@
 me()
- + %22 with implementation:%22+
+,
 strC
@@ -3767,10 +3767,13 @@
  of 
-%22+
+%7B%7D%22, 
 Samp

Improve logs