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.support;
014:
015: import java.awt.event.ActionEvent;
016: import java.io.File;
017: import java.io.FilenameFilter;
018:
019: import javax.swing.AbstractAction;
020: import javax.swing.Action;
021:
022: import org.apache.log4j.Logger;
023:
024: import com.eviware.soapui.SoapUI;
025: import com.eviware.soapui.actions.SoapUIPreferencesAction;
026: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
027: import com.eviware.soapui.impl.wsdl.support.wsdl.CachedWsdlLoader;
028: import com.eviware.soapui.model.ModelItem;
029: import com.eviware.soapui.model.iface.Interface;
030: import com.eviware.soapui.settings.ProjectSettings;
031: import com.eviware.soapui.support.Tools;
032: import com.eviware.soapui.support.UISupport;
033: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
034: import com.eviware.soapui.support.action.swing.ActionList;
035: import com.eviware.soapui.support.action.swing.DefaultActionList;
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.XFormField;
041: import com.eviware.x.form.XFormTextField;
042:
043: /**
044: * Abstract base class for Tool Actions
045: *
046: * @author Ole.Matzura
047: */
048:
049: public abstract class AbstractToolsAction<T extends ModelItem> extends
050: AbstractSoapUIAction {
051: @SuppressWarnings("unused")
052: private static final Logger log = Logger
053: .getLogger(AbstractToolsAction.class);
054:
055: protected static final String WSDL = "WSDL";
056: protected static final String CACHED_WSDL = "Use cached WSDL";
057: protected static final String JAVA_ARGS = "Java Args";
058: protected static final String TOOL_ARGS = "Tool Args";
059:
060: protected XFormDialog dialog;
061: protected String valuesSettingID;
062: private XFormField useCached;
063:
064: // Configure behavior of this action:
065: private boolean fixedWSDL = false;
066: private Action toolsSettingsAction = new ShowIntegratedToolsSettingsAction();;
067:
068: public AbstractToolsAction(String name, String description) {
069: super (name, description);
070: }
071:
072: public String getValuesSettingID() {
073: return valuesSettingID;
074: }
075:
076: public void setValuesSettingID(String valuesSettingID) {
077: this .valuesSettingID = valuesSettingID;
078: }
079:
080: /**
081: * Set this to true to not let the user edit the WSDL.
082: *
083: * @param b
084: */
085: public void setFixedWSDL(boolean b) {
086: this .fixedWSDL = b;
087: }
088:
089: @SuppressWarnings("unchecked")
090: public void perform(ModelItem target, Object param) {
091: this .valuesSettingID = this .getClass().getName() + "@values";
092: if (target == null)
093: this .valuesSettingID += "-global";
094: else
095: this .valuesSettingID += "-local";
096:
097: // Could reuse the dialog in Swing, but not in Eclipse.
098: // if( dialog == null )
099: dialog = buildDialog((T) target);
100:
101: if (dialog == null) {
102: try {
103: generate(initValues((T) target), UISupport
104: .getToolHost(), (T) target);
105: } catch (Exception e1) {
106: UISupport.showErrorMessage(e1);
107: }
108: } else {
109: StringToStringMap values = initValues((T) target);
110:
111: dialog.setValues(values);
112: dialog.setVisible(true);
113: }
114: }
115:
116: protected StringToStringMap initValues(T modelItem) {
117: String settingValues = modelItem == null ? SoapUI.getSettings()
118: .getString(valuesSettingID, null) : modelItem
119: .getSettings().getString(valuesSettingID, null);
120:
121: StringToStringMap result = settingValues == null ? new StringToStringMap()
122: : StringToStringMap.fromXml(settingValues);
123:
124: if (modelItem instanceof Interface) {
125: initWSDL(result, (Interface) modelItem);
126: }
127:
128: if (dialog != null && modelItem != null) {
129: String projectRoot = modelItem.getSettings().getString(
130: ProjectSettings.PROJECT_ROOT, null);
131: if (projectRoot != null)
132: dialog.setFormFieldProperty(
133: ProjectSettings.PROJECT_ROOT, projectRoot);
134: }
135:
136: return result;
137: }
138:
139: protected XFormDialog buildDialog(T modelItem) {
140: return null;
141: }
142:
143: protected void addWSDLFields(XForm mainForm, T modelItem) {
144: if (!fixedWSDL) {
145: XFormTextField tf = mainForm.addTextField(WSDL,
146: "url to wsdl", XForm.FieldType.URL);
147:
148: if (modelItem instanceof Interface) {
149: useCached = mainForm.addCheckBox(CACHED_WSDL, null);
150: useCached.addComponentEnabler(tf, "false");
151: }
152: } else {
153: if (modelItem instanceof Interface) {
154: useCached = mainForm.addCheckBox(CACHED_WSDL, null);
155: }
156: }
157: }
158:
159: protected void initWSDL(StringToStringMap values, Interface iface) {
160: boolean cached = iface.isCached();
161: if (useCached != null)
162: useCached.setEnabled(cached);
163:
164: if (!values.containsKey(CACHED_WSDL))
165: values.put(CACHED_WSDL, Boolean.toString(cached));
166:
167: if (values.getBoolean(CACHED_WSDL) || !values.hasValue(WSDL))
168: values.put(WSDL, iface.getDefinition());
169: }
170:
171: protected abstract void generate(StringToStringMap values,
172: ToolHost toolHost, T modelItem) throws Exception;
173:
174: public void run(ToolHost toolHost, T modelItem) throws Exception {
175: generate(initValues(modelItem), toolHost, modelItem);
176: }
177:
178: /**
179: * To be overridden..
180: */
181:
182: public void onClose(T modelItem) {
183: if (dialog == null)
184: return;
185:
186: if (modelItem == null) {
187: SoapUI.getSettings().setString(valuesSettingID,
188: dialog.getValues().toXml());
189: } else {
190: modelItem.getSettings().setString(valuesSettingID,
191: dialog.getValues().toXml());
192: }
193: }
194:
195: protected String getWsdlUrl(StringToStringMap values, T modelItem) {
196: String wsdl = values.get(WSDL);
197: boolean useCached = values.getBoolean(CACHED_WSDL);
198:
199: if (wsdl == null && !useCached
200: && modelItem instanceof Interface)
201: return ((Interface) modelItem).getDefinition();
202:
203: Interface iface = (Interface) modelItem;
204: if (useCached && iface.isCached()) {
205: try {
206: File tempFile = File.createTempFile("tempdir", null);
207: String path = tempFile.getAbsolutePath();
208: tempFile.delete();
209: CachedWsdlLoader loader = (CachedWsdlLoader) iface
210: .createWsdlLoader();
211: wsdl = loader.saveDefinition(path);
212: } catch (Exception e) {
213: SoapUI.logError(e);
214: }
215: }
216:
217: return wsdl;
218: }
219:
220: protected String buildClasspath(File jarDir) {
221: String[] jars = jarDir.list(new FilenameFilter() {
222:
223: public boolean accept(File dir, String name) {
224: return name.endsWith(".jar");
225: }
226: });
227:
228: StringBuilder classpath = new StringBuilder();
229:
230: for (int c = 0; c < jars.length; c++) {
231: if (c > 0)
232: classpath.append(File.pathSeparatorChar);
233:
234: classpath.append(jars[c]);
235: }
236: return classpath.toString();
237: }
238:
239: protected ActionList buildDefaultActions(String helpUrl, T modelItem) {
240: ActionList actions = new DefaultActionList("Actions");
241:
242: if (helpUrl != null) {
243: actions.addAction(new ShowOnlineHelpAction(helpUrl));
244: actions.addSeparator();
245: }
246:
247: actions.addAction(createRunOption(modelItem));
248: actions.addAction(new CloseAction(modelItem));
249:
250: if (toolsSettingsAction != null)
251: actions.addAction(toolsSettingsAction);
252:
253: return actions;
254: }
255:
256: public Action getToolsSettingsAction() {
257: return toolsSettingsAction;
258: }
259:
260: public void setToolsSettingsAction(Action toolsSettingsAction) {
261: this .toolsSettingsAction = toolsSettingsAction;
262: }
263:
264: protected Action createRunOption(T modelItem) {
265: return new GenerateAction(modelItem);
266: }
267:
268: protected String getDefinition(T modelItem) {
269: if (modelItem == null)
270: return "";
271:
272: String definition = ((Interface) modelItem).getDefinition();
273: if (definition.startsWith("file:"))
274: definition = definition.substring(5);
275:
276: return definition;
277: }
278:
279: protected void addJavaArgs(StringToStringMap values,
280: ArgumentBuilder builder) {
281: String[] javaArgs = Tools.tokenizeArgs(values.get(JAVA_ARGS));
282: if (javaArgs != null)
283: builder.addArgs(javaArgs);
284: }
285:
286: protected void addToolArgs(StringToStringMap values,
287: ArgumentBuilder builder) {
288: String[] toolArgs = Tools.tokenizeArgs(values.get(TOOL_ARGS));
289: if (toolArgs != null)
290: builder.addArgs(toolArgs);
291: }
292:
293: protected XForm buildArgsForm(XFormDialogBuilder builder,
294: boolean addJavaArgs, String toolName) {
295: XForm argsForm = builder.createForm("Custom Args");
296: if (addJavaArgs)
297: argsForm.addTextField(JAVA_ARGS,
298: "additional arguments to java",
299: XForm.FieldType.TEXT);
300:
301: argsForm.addTextField(TOOL_ARGS, "additional arguments to "
302: + toolName, XForm.FieldType.TEXT);
303: return argsForm;
304: }
305:
306: public static final class ShowIntegratedToolsSettingsAction extends
307: AbstractAction {
308: public ShowIntegratedToolsSettingsAction() {
309: super ("Tools");
310: }
311:
312: public void actionPerformed(ActionEvent e) {
313: SoapUIPreferencesAction.getInstance().show(
314: SoapUIPreferencesAction.INTEGRATED_TOOLS);
315: }
316: }
317:
318: protected final class CloseAction extends AbstractAction {
319: private final T modelItem;
320:
321: public CloseAction(T modelItem) {
322: super ("Close");
323: this .modelItem = modelItem;
324: }
325:
326: public void actionPerformed(ActionEvent e) {
327: closeDialog(modelItem);
328: }
329: }
330:
331: public void closeDialog(T modelItem) {
332: onClose(modelItem);
333: if (dialog != null)
334: dialog.setVisible(false);
335: }
336:
337: protected final class GenerateAction extends AbstractAction {
338: private final T modelItem;
339:
340: public GenerateAction(T modelItem) {
341: super ("Generate");
342: this .modelItem = modelItem;
343: }
344:
345: public void actionPerformed(ActionEvent e) {
346: try {
347: if (dialog.validate()) {
348: generate(dialog.getValues(), UISupport
349: .getToolHost(), modelItem);
350: }
351: } catch (Exception e1) {
352: UISupport.showErrorMessage(e1);
353: }
354: }
355: }
356: }
|