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