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