View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2008 eviware.com
3    *
4    * soapUI is free software; you can redistribute it and/or modify it under the
5    * terms of version 2.1 of the GNU Lesser General Public License as published by
6    * the Free Software Foundation.
7    *
8    * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   * See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.mock.dispatch;
14  
15  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
16  import com.eviware.soapui.impl.wsdl.mock.WsdlMockRequest;
17  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
18  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
19  import com.eviware.soapui.model.mock.MockResult;
20  import com.eviware.soapui.model.mock.MockRunListener;
21  import com.eviware.soapui.model.mock.MockRunner;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  public class SequenceMockOperationDispatcher extends AbstractMockOperationDispatcher implements MockRunListener
27  {
28     private int currentDispatchIndex;
29  
30     public SequenceMockOperationDispatcher( WsdlMockOperation mockOperation )
31     {
32        super( mockOperation );
33  
34        mockOperation.getMockService().addMockRunListener( this );
35     }
36  
37     public WsdlMockResponse selectMockResponse( WsdlMockRequest request, WsdlMockResult result )
38     {
39        synchronized( result.getMockOperation() )
40        {
41           if( currentDispatchIndex >= getMockOperation().getMockResponseCount() )
42              currentDispatchIndex = 0;
43  
44           WsdlMockResponse mockResponse = getMockOperation().getMockResponseAt( currentDispatchIndex );
45  
46           currentDispatchIndex++;
47           return mockResponse;
48        }
49     }
50  
51     @Override
52     public void release()
53     {
54        getMockOperation().getMockService().removeMockRunListener( this );
55        super.release();
56     }
57  
58     public void onMockRunnerStart( MockRunner mockRunner )
59     {
60        currentDispatchIndex = 0;
61     }
62  
63     public void onMockResult( MockResult result )
64     {
65     }
66  
67     public void onMockRunnerStop( MockRunner mockRunner )
68     {
69     }
70  
71     public MockResult onMockRequest( MockRunner runner, HttpServletRequest request, HttpServletResponse response )
72     {
73        return null;
74     }
75  
76     public static class Factory implements MockOperationDispatchFactory
77     {
78        public MockOperationDispatcher build( WsdlMockOperation mockOperation )
79        {
80           return new SequenceMockOperationDispatcher( mockOperation );
81        }
82     }
83  }