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