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  /*
14   * soapUI, copyright (C) 2004-2008 eviware.com
15   *
16   * soapUI is free software; you can redistribute it and/or modify it under the
17   * terms of version 2.1 of the GNU Lesser General Public License as published by
18   * the Free Software Foundation.
19   *
20   * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
21   * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22   * See the GNU Lesser General Public License for more details at gnu.org.
23   */
24  
25  /*
26   * soapUI, copyright (C) 2004-2008 eviware.com
27   *
28   * soapUI is free software; you can redistribute it and/or modify it under the
29   * terms of version 2.1 of the GNU Lesser General Public License as published by
30   * the Free Software Foundation.
31   *
32   * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
33   * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
34   * See the GNU Lesser General Public License for more details at gnu.org.
35   */
36  
37  package com.eviware.soapui.impl.wsdl.mock.dispatch;
38  
39  import com.eviware.soapui.SoapUI;
40  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
41  import com.eviware.soapui.impl.wsdl.mock.*;
42  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
43  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
44  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
45  import com.eviware.soapui.model.mock.MockRunContext;
46  import com.eviware.soapui.model.settings.Settings;
47  import com.eviware.soapui.support.StringUtils;
48  import com.eviware.soapui.support.UISupport;
49  import com.eviware.soapui.support.components.JXToolBar;
50  import com.eviware.soapui.support.scripting.ScriptEnginePool;
51  import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
52  import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
53  
54  import javax.swing.*;
55  import java.awt.*;
56  import java.awt.event.ActionEvent;
57  import java.beans.PropertyChangeEvent;
58  import java.beans.PropertyChangeListener;
59  
60  public class ScriptMockOperationDispatcher extends AbstractMockOperationDispatcher implements PropertyChangeListener
61  {
62     private ScriptEnginePool scriptEnginePool;
63     private GroovyEditor groovyEditor;
64  
65     public ScriptMockOperationDispatcher( WsdlMockOperation mockOperation )
66     {
67        super( mockOperation );
68  
69        scriptEnginePool = new ScriptEnginePool( mockOperation );
70        scriptEnginePool.setScript( mockOperation.getDispatchPath() );
71  
72        mockOperation.addPropertyChangeListener( WsdlMockOperation.DISPATCH_PATH_PROPERTY, this );
73     }
74  
75     public WsdlMockResponse selectMockResponse( WsdlMockRequest request, WsdlMockResult result ) throws DispatchException
76     {
77        String dispatchScript = getMockOperation().getDispatchPath();
78        if( StringUtils.hasContent( dispatchScript ) )
79        {
80           SoapUIScriptEngine scriptEngine = scriptEnginePool.getScriptEngine();
81  
82           try
83           {
84              WsdlMockService mockService = getMockOperation().getMockService();
85              WsdlMockRunner mockRunner = mockService.getMockRunner();
86              MockRunContext context = mockRunner == null ? new WsdlMockRunContext( mockService, null ) : mockRunner.getMockContext();
87  
88              scriptEngine.setVariable( "context", context );
89              scriptEngine.setVariable( "requestContext", request == null ? null : request.getRequestContext() );
90              scriptEngine.setVariable( "mockRequest", request );
91              scriptEngine.setVariable( "mockOperation", this );
92              scriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
93  
94              scriptEngine.setScript( dispatchScript );
95              Object retVal = scriptEngine.run();
96              return getMockOperation().getMockResponseByName( String.valueOf( retVal ) );
97           }
98           catch( Throwable e )
99           {
100             SoapUI.logError( e );
101             throw new DispatchException( "Failed to dispatch using script; " + e );
102          }
103          finally
104          {
105             scriptEnginePool.returnScriptEngine( scriptEngine );
106          }
107       }
108 
109       return null;
110    }
111 
112    @Override
113    public void release()
114    {
115       scriptEnginePool.release();
116 
117       if( groovyEditor != null )
118          groovyEditor.release();
119 
120       getMockOperation().removePropertyChangeListener( WsdlMockOperation.DISPATCH_PATH_PROPERTY, this );
121 
122       super.release();
123    }
124 
125    @Override
126    public JComponent buildEditorComponent()
127    {
128       JPanel groovyEditorPanel = new JPanel( new BorderLayout() );
129       DispatchScriptGroovyEditorModel editorModel = new DispatchScriptGroovyEditorModel();
130       groovyEditor = (GroovyEditor) UISupport.getEditorFactory().buildGroovyEditor( editorModel );
131       groovyEditorPanel.add( groovyEditor, BorderLayout.CENTER );
132       groovyEditorPanel.add( buildGroovyEditorToolbar( editorModel ), BorderLayout.PAGE_START );
133 
134       return groovyEditorPanel;
135    }
136 
137    protected JXToolBar buildGroovyEditorToolbar( DispatchScriptGroovyEditorModel editorModel )
138    {
139       JXToolBar toolbar = UISupport.createToolbar();
140       toolbar.addSpace( 3 );
141       toolbar.addFixed( UISupport.createToolbarButton( editorModel.getRunAction() ) );
142       toolbar.addGlue();
143 
144       JLabel label = new JLabel( "<html>Script is invoked with <code>log</code>, <code>context</code>, " +
145               "<code>requestContext</code>, <code>mockRequest</code> and <code>mockOperation</code> variables</html>" );
146       label.setToolTipText( label.getText() );
147       label.setMaximumSize( label.getPreferredSize() );
148 
149       toolbar.add( label );
150       toolbar.addFixed( ModelItemDesktopPanel.createActionButton( new ShowOnlineHelpAction( HelpUrls.MOCKOPERATION_SCRIPTDISPATCH_HELP_URL ), true ) );
151       return toolbar;
152    }
153 
154    public void propertyChange( PropertyChangeEvent evt )
155    {
156       scriptEnginePool.setScript( String.valueOf( evt.getNewValue() ) );
157    }
158 
159    public static class Factory implements MockOperationDispatchFactory
160    {
161       public MockOperationDispatcher build( WsdlMockOperation mockOperation )
162       {
163          return new ScriptMockOperationDispatcher( mockOperation );
164       }
165    }
166 
167    public class DispatchScriptGroovyEditorModel implements GroovyEditorModel
168    {
169       private RunScriptAction runScriptAction = new RunScriptAction();
170 
171       public String[] getKeywords()
172       {
173          return new String[]{"mockRequest", "context", "requestContext", "log", "mockOperation"};
174       }
175 
176       public Action getRunAction()
177       {
178          return runScriptAction;
179       }
180 
181       public String getScript()
182       {
183          return getMockOperation().getDispatchPath();
184       }
185 
186       public Settings getSettings()
187       {
188          return getMockOperation().getSettings();
189       }
190 
191       public void setScript( String text )
192       {
193          getMockOperation().setDispatchPath( text );
194       }
195 
196       public String getScriptName()
197       {
198          return "Dispatch";
199       }
200    }
201 
202    private class RunScriptAction extends AbstractAction
203    {
204       public RunScriptAction()
205       {
206          putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ) );
207          putValue( Action.SHORT_DESCRIPTION, "Runs this script using a mockRequest and context" );
208       }
209 
210       public void actionPerformed( ActionEvent e )
211       {
212          WsdlMockResult lastMockResult = getMockOperation().getLastMockResult();
213          WsdlMockRequest mockRequest = lastMockResult == null ? null : lastMockResult.getMockRequest();
214 
215          try
216          {
217             WsdlMockResponse retVal = selectMockResponse( mockRequest, null );
218             UISupport.showInfoMessage( "Script returned [" + ( retVal == null ? "null" : retVal.getName() ) + "]" );
219          }
220          catch( Exception e1 )
221          {
222             UISupport.showErrorMessage( e1 );
223          }
224       }
225    }
226 }