View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  		StringToStringMap values = super.initValues( modelItem, param );
95  		if( !values.isEmpty() )
96  			return values;
97  
98  		values.put( ENDPOINT, getDefinition( modelItem ) );
99  		values.put( PORT, "8080" );
100 
101 		return values;
102 	}
103 
104 	protected void generate( StringToStringMap values, ToolHost toolHost, WsdlInterface modelItem ) throws Exception
105 	{
106 		String tcpMonDir = SoapUI.getSettings().getString( ToolsSettings.TCPMON_LOCATION, null );
107 		if( Tools.isEmpty( tcpMonDir ) )
108 		{
109 			UISupport.showErrorMessage( "TcpMon directory must be set in global preferences" );
110 			return;
111 		}
112 
113 		ProcessBuilder builder = new ProcessBuilder();
114 		ArgumentBuilder args = buildArgs( modelItem );
115 		builder.command( args.getArgs() );
116 		builder.directory( new File( tcpMonDir + File.separatorChar + "build" ) );
117 
118 		SoapUI.log( "Launching tcpmon in directory [" + builder.directory() + "] with arguments [" + args.toString()
119 				+ "]" );
120 
121 		builder.start();
122 		closeDialog( modelItem );
123 	}
124 
125 	private ArgumentBuilder buildArgs( WsdlInterface modelItem ) throws IOException
126 	{
127 		if( dialog == null )
128 		{
129 			ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
130 			builder.startScript( "tcpmon", ".bat", ".sh" );
131 			return builder;
132 		}
133 
134 		StringToStringMap values = dialog.getValues();
135 
136 		ArgumentBuilder builder = new ArgumentBuilder( values );
137 		builder.startScript( "tcpmon", ".bat", ".sh" );
138 
139 		builder.addArgs( values.get( PORT ) );
140 		String endpoint = values.get( ENDPOINT );
141 		if( endpoint != null && !endpoint.equals( "- none available -" ) )
142 		{
143 			URL url = new URL( endpoint );
144 			builder.addArgs( url.getHost() );
145 			builder.addArgs( ( url.getPort() == -1 ) ? "80" : "" + url.getPort() );
146 
147 			if( values.getBoolean( ADD_ENDPOINT ) )
148 			{
149 				modelItem.addEndpoint( "http://localhost:" + values.get( PORT ) + url.getPath() );
150 			}
151 		}
152 
153 		addToolArgs( values, builder );
154 		return builder;
155 	}
156 }