001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.actions.iface.tools.tcpmon;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.net.URL;
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.List;
021:
022: import javax.swing.Action;
023:
024: import org.apache.log4j.Logger;
025:
026: import com.eviware.soapui.SoapUI;
027: import com.eviware.soapui.impl.wsdl.WsdlInterface;
028: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
029: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
030: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
031: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
032: import com.eviware.soapui.model.iface.Interface;
033: import com.eviware.soapui.settings.ToolsSettings;
034: import com.eviware.soapui.support.Tools;
035: import com.eviware.soapui.support.UISupport;
036: import com.eviware.soapui.support.types.StringToStringMap;
037: import com.eviware.x.form.XForm;
038: import com.eviware.x.form.XFormDialog;
039: import com.eviware.x.form.XFormDialogBuilder;
040: import com.eviware.x.form.XFormFactory;
041:
042: /**
043: * Invokes Apache TCPmon tool
044: *
045: * @author Ole.Matzura
046: */
047:
048: public class TcpMonAction extends AbstractToolsAction<WsdlInterface> {
049: private static final String ENDPOINT = "Endpoint";
050: private static final String PORT = "Local Port";
051: private static final String ADD_ENDPOINT = "Add local endpoint";
052: private XForm mainForm;
053: private final static Logger log = Logger
054: .getLogger(TcpMonAction.class);
055: public static final String SOAPUI_ACTION_ID = "TcpMonAction";
056:
057: public TcpMonAction() {
058: super ("Launch TcpMon",
059: "Launch Tcp Mon for monitoring SOAP traffic");
060: }
061:
062: protected XFormDialog buildDialog(WsdlInterface modelItem) {
063: if (modelItem == null)
064: return null;
065:
066: XFormDialogBuilder builder = XFormFactory
067: .createDialogBuilder("Launch TcpMon");
068:
069: mainForm = builder.createForm("Basic");
070: mainForm.addComboBox(ENDPOINT, new String[] { "" },
071: "endpoint to forward to");
072: mainForm.addTextField(PORT, "Local port to listen on.",
073: XForm.FieldType.TEXT);
074: mainForm
075: .addCheckBox(ADD_ENDPOINT,
076: "adds an endpoint to the interface pointing to the started monitor");
077:
078: return builder.buildDialog(buildDefaultActions(
079: HelpUrls.TCPMON_HELP_URL, modelItem),
080: "Specify arguments for launching TcpMon",
081: UISupport.TOOL_ICON);
082: }
083:
084: protected Action createRunOption(WsdlInterface modelItem) {
085: Action action = super .createRunOption(modelItem);
086: action.putValue(Action.NAME, "Launch");
087: return action;
088: }
089:
090: protected StringToStringMap initValues(WsdlInterface modelItem) {
091: if (modelItem != null) {
092: List<String> endpoints = new ArrayList<String>(Arrays
093: .asList(modelItem.getEndpoints()));
094: endpoints.add(0, null);
095: mainForm.setOptions(ENDPOINT, endpoints.toArray());
096: } else if (mainForm != null) {
097: mainForm.setOptions(ENDPOINT, new String[] { null });
098: }
099:
100: StringToStringMap values = super .initValues(modelItem);
101: if (!values.isEmpty())
102: return values;
103:
104: values.put(ENDPOINT, getDefinition(modelItem));
105: values.put(PORT, "8080");
106:
107: return values;
108: }
109:
110: protected void generate(StringToStringMap values,
111: ToolHost toolHost, WsdlInterface modelItem)
112: throws Exception {
113: String tcpMonDir = SoapUI.getSettings().getString(
114: ToolsSettings.TCPMON_LOCATION, null);
115: if (Tools.isEmpty(tcpMonDir)) {
116: UISupport
117: .showErrorMessage("TcpMon directory must be set in global preferences");
118: return;
119: }
120:
121: ProcessBuilder builder = new ProcessBuilder();
122: ArgumentBuilder args = buildArgs(modelItem);
123: builder.command(args.getArgs());
124: builder.directory(new File(tcpMonDir + File.separatorChar
125: + "build"));
126:
127: if (log.isDebugEnabled())
128: log.debug("Launching tcpmon in directory ["
129: + builder.directory() + "] with arguments ["
130: + args.toString() + "]");
131:
132: builder.start();
133: closeDialog(modelItem);
134: }
135:
136: private ArgumentBuilder buildArgs(Interface modelItem)
137: throws IOException {
138: if (dialog == null) {
139: ArgumentBuilder builder = new ArgumentBuilder(
140: new StringToStringMap());
141: builder.startScript("tcpmon", ".bat", ".sh");
142: return builder;
143: }
144:
145: StringToStringMap values = dialog.getValues();
146:
147: ArgumentBuilder builder = new ArgumentBuilder(values);
148: builder.startScript("tcpmon", ".bat", ".sh");
149:
150: builder.addArgs(values.get(PORT));
151: String endpoint = values.get(ENDPOINT);
152: if (endpoint != null && !endpoint.equals("- none available -")) {
153: URL url = new URL(endpoint);
154: builder.addArgs(url.getHost());
155: builder.addArgs((url.getPort() == -1) ? "80" : ""
156: + url.getPort());
157:
158: if (values.getBoolean(ADD_ENDPOINT)) {
159: modelItem.addEndpoint("http://localhost:"
160: + values.get(PORT) + url.getPath());
161: }
162: }
163:
164: addToolArgs(values, builder);
165: return builder;
166: }
167: }
|