View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.actions.iface.tools.tcpmon;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.net.URL;
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import javax.swing.Action;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.impl.wsdl.WsdlInterface;
26  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
27  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
28  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
29  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
30  import com.eviware.soapui.model.iface.Interface;
31  import com.eviware.soapui.settings.ToolsSettings;
32  import com.eviware.soapui.support.Tools;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.types.StringToStringMap;
35  import com.eviware.x.form.XForm;
36  import com.eviware.x.form.XFormDialog;
37  import com.eviware.x.form.XFormDialogBuilder;
38  import com.eviware.x.form.XFormFactory;
39  
40  /***
41   * Invokes Apache TCPmon tool
42   * 
43   * @author Ole.Matzura
44   */
45  
46  public class TcpMonAction extends AbstractToolsAction<WsdlInterface>
47  {
48  	private static final String ENDPOINT = "Endpoint";
49  	private static final String PORT = "Local Port";
50  	private static final String ADD_ENDPOINT = "Add local endpoint";
51  	private XForm mainForm;
52  	public static final String SOAPUI_ACTION_ID = "TcpMonAction";
53  	
54     public TcpMonAction()
55     {
56        super( "Launch TcpMon", "Launch Tcp Mon for monitoring SOAP traffic");
57     }
58  
59     protected XFormDialog buildDialog( WsdlInterface modelItem)
60  	{
61     	if( modelItem == null )
62     		return null;
63     	
64        XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Launch TcpMon");
65  
66  		mainForm = builder.createForm( "Basic" );
67  		mainForm.addComboBox( ENDPOINT, new String[] {""}, "endpoint to forward to" );
68  		mainForm.addTextField( PORT, "Local port to listen on.", XForm.FieldType.TEXT );
69  		mainForm.addCheckBox( ADD_ENDPOINT, "adds an endpoint to the interface pointing to the started monitor" );
70        
71  		return builder.buildDialog( buildDefaultActions(HelpUrls.TCPMON_HELP_URL, modelItem),
72        		"Specify arguments for launching TcpMon", UISupport.TOOL_ICON );
73  	}
74     
75  	protected Action createRunOption( WsdlInterface modelItem)
76  	{
77  		Action action = super.createRunOption( modelItem );
78  		action.putValue( Action.NAME, "Launch" );
79  		return action;
80  	}
81  
82  	protected StringToStringMap initValues(WsdlInterface modelItem, Object param)
83  	{
84  		if( modelItem != null )
85  		{
86  			List<String> endpoints = new ArrayList<String>( Arrays.asList( modelItem.getEndpoints() ));
87  			endpoints.add( 0, null );
88  			mainForm.setOptions( ENDPOINT, endpoints.toArray() );
89  		}
90  		else if( mainForm != null )
91  		{
92  			mainForm.setOptions( ENDPOINT, new String[] { null } );
93  		}
94  		
95  		
96  		StringToStringMap values = super.initValues( modelItem, param );
97  		if( !values.isEmpty() )
98  			return values;
99  		
100 		values.put( ENDPOINT, getDefinition( modelItem ) );
101 		values.put( PORT, "8080" );
102 		
103 		return values;
104 	}
105 
106 	protected void generate(StringToStringMap values, ToolHost toolHost, WsdlInterface modelItem ) throws Exception
107 	{
108 		String tcpMonDir = SoapUI.getSettings().getString( ToolsSettings.TCPMON_LOCATION, null );
109 		if( Tools.isEmpty( tcpMonDir ))
110 		{
111 			UISupport.showErrorMessage( "TcpMon directory must be set in global preferences" );
112 			return;
113 		}
114 		
115 		ProcessBuilder builder = new ProcessBuilder();
116 		ArgumentBuilder args = buildArgs(modelItem);
117 		builder.command(args.getArgs());
118 		builder.directory(new File(tcpMonDir + File.separatorChar + "build" ));
119 		
120 		SoapUI.log( "Launching tcpmon in directory [" + builder.directory() + "] with arguments [" +
121 					args.toString() + "]" );
122 		
123 		builder.start();
124 		closeDialog(modelItem);
125 	}
126 
127 	private ArgumentBuilder buildArgs(Interface modelItem) throws IOException
128 	{
129 		if( dialog == null )
130 		{
131 			ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
132 			builder.startScript( "tcpmon", ".bat", ".sh" );
133 			return builder;
134 		}
135 		
136 		StringToStringMap values = dialog.getValues();
137 		
138 		ArgumentBuilder builder = new ArgumentBuilder( values );
139 		builder.startScript( "tcpmon", ".bat", ".sh" );
140 		
141 		builder.addArgs( values.get( PORT ));
142 		String endpoint = values.get( ENDPOINT );
143 		if( endpoint != null && !endpoint.equals( "- none available -"))
144 		{
145 			URL url = new URL( endpoint);
146 			builder.addArgs( url.getHost() );
147 			builder.addArgs( (url.getPort() == -1) ? "80" : ""+url.getPort() );
148 			
149 			if( values.getBoolean( ADD_ENDPOINT ))
150 			{
151 				modelItem.addEndpoint( "http://localhost:" + values.get( PORT ) + url.getPath() );
152 			}
153 		}
154 		
155 		addToolArgs( values, builder );
156 		return builder;
157 	}
158 }