/*
 * 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.sampler;

import java.net.URL;

import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.samplers.Interruptible;

/**
 * Proxy class that dispatches to the appropriate HTTP sampler.
 * <p>
 * This class is stored in the test plan, and holds all the configuration settings.
 * The actual implementation is created at run-time, and is passed a reference to this class
 * so it can get access to all the settings stored by HTTPSamplerProxy.
 */
public final class HTTPSamplerProxy extends HTTPSamplerBase implements Interruptible {

    private static final long serialVersionUID = 1L;

    private transient HTTPAbstractImpl impl;
    
    private transient Exception initException;

    public HTTPSamplerProxy(){
        super();
    }
    
    /**
     * Convenience method used to initialise the implementation.
     * 
     * @param impl the implementation to use.
     */
    public HTTPSamplerProxy(String impl){
        super();
        setImplementation(impl);
    }
        
    /** {@inheritDoc} */
    @Override
    protected HTTPSampleResult sample(URL u, String method, boolean areFollowingRedirect, int depth) {
        if(impl != null) {
            return impl.sample(u, method, areFollowingRedirect, depth);
        } else {
            return errorResult(initException, new HTTPSampleResult());
        }
    }

    // N.B. It's not possible to forward threadStarted() to the implementation class.
    // This is because Config items are not processed until later, and HTTPDefaults may define the implementation

    @Override
    public void threadFinished(){
        if (impl != null){
            impl.threadFinished(); // Forward to sampler
        }
    }

    @Override
    public boolean interrupt() {
        if (impl != null) {
            return impl.interrupt(); // Forward to sampler
        }
        return false;
    }

    /* (non-Javadoc)
     * @see org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase#testIterationStart(org.apache.jmeter.engine.event.LoopIterationEvent)
     */
    @Override
    public void testIterationStart(LoopIterationEvent event) {
        if (impl == null) { // Not called from multiple threads, so this is OK
            try {
                impl = HTTPSamplerFactory.getImplementation(getImplementation(), this);
                initException=null;
            } catch (Exception ex) {
                initException = ex;
            }
        } 
        if(impl != null) {
            // see https://issues.apache.org/bugzilla/show_bug.cgi?id=51380
            impl.testIterationStart(event);
        }
    }
}

@@ -1502,56 +1502,8 @@
    %0A
-    private transient Exception initException;%0A%0A
     
@@ -1943,39 +1943,40 @@
 th) %7B%0A        if
+ 
 (impl 
-!
+=
 = null) %7B%0A      
@@ -1972,98 +1972,207 @@
 l) %7B
-%0A            return impl.sample(u, method, areFollowingRedirect, depth);%0A        %7D else %7B%0A
+ // Not called from multiple threads, so this is OK%0A            try %7B%0A                impl = HTTPSamplerFactory.getImplementation(getImplementation(), this);%0A            %7D catch (Exception ex) %7B%0A    
     
@@ -2198,29 +2198,18 @@
 rResult(
-initException
+ex
 , new HT
@@ -2227,33 +2227,115 @@
 ult());%0A        
-%7D
+    %7D%0A        %7D%0A        return impl.sample(u, method, areFollowingRedirect, depth);
 %0A    %7D%0A%0A    // N
@@ -3105,345 +3105,298 @@
     
-if (impl == null) %7B // Not called from multiple threads, so this is OK%0A            try %7B%0A                impl = HTTPSamplerFactory.getImplementation(getImplementation(), this);%0A                initException=null;%0A            %7D catch (Exception ex) %7B%0A                initException = ex;%0A            %7D%0A        %7D %0A        if(impl != null) %7B
+try %7B%0A            // This class is cloned per thread, and testIterationStart is called from a different thread from samplers%0A            // so we need to fetch the implementation separately%0A            HTTPAbstractImpl temp = HTTPSamplerFactory.getImplementation(getImplementation(), this);
 %0A   
@@ -3480,20 +3480,20 @@
         
-i
+te
 mp
-l
 .testIte
@@ -3512,16 +3512,49 @@
 event);%0A
+        %7D catch (Exception ex) %7B%0A
         

Fix bug introduced in http://svn.apache.org/r1489603 The testIterationStart method is called from a different thread from any of the samples