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.jbossws;
014:
015: import java.io.File;
016: import java.io.IOException;
017:
018: import org.jboss.jbosswsTools.ConfigurationDocument;
019: import org.jboss.jbosswsTools.ConfigurationType;
020: import org.jboss.jbosswsTools.JavaToWsdlType;
021: import org.jboss.jbosswsTools.MappingType;
022: import org.jboss.jbosswsTools.NamespacesType;
023: import org.jboss.jbosswsTools.ServiceType;
024: import org.jboss.jbosswsTools.WsxmlType;
025: import org.jboss.jbosswsTools.ServiceType.ParameterStyle;
026: import org.jboss.jbosswsTools.ServiceType.Style;
027:
028: import com.eviware.soapui.SoapUI;
029: import com.eviware.soapui.impl.wsdl.WsdlProject;
030: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
031: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
032: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
033: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
034: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ShowConfigFileAction;
035: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
036: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
037: import com.eviware.soapui.model.iface.Interface;
038: import com.eviware.soapui.settings.ToolsSettings;
039: import com.eviware.soapui.support.SoapUIException;
040: import com.eviware.soapui.support.Tools;
041: import com.eviware.soapui.support.UISupport;
042: import com.eviware.soapui.support.action.swing.ActionList;
043: import com.eviware.soapui.support.types.StringToStringMap;
044: import com.eviware.x.form.XForm;
045: import com.eviware.x.form.XFormDialog;
046: import com.eviware.x.form.XFormDialogBuilder;
047: import com.eviware.x.form.XFormFactory;
048:
049: /**
050: * Invokes jbossws java2wsdl tools
051: *
052: * @author Ole.Matzura
053: */
054:
055: public class WSToolsJava2WsdlAction extends
056: AbstractToolsAction<WsdlProject> {
057: public static final String SOAPUI_ACTION_ID = "WSToolsJava2WsdlAction";
058:
059: private static final String CLASSPATH = "Classpath";
060: private static final String OUTPUT = "Output Directory";
061: private static final String ENDPOINT = "Endpoint";
062: private static final String MAPPING = "Mapping file";
063: private static final String SERVICE_NAME = "Service Name";
064: private static final String STYLE = "Style";
065: private static final String PARAMETER_STYLE = "Parameter Style";
066: private static final String TARGET_NAMESPACE = "Target Namespace";
067: private static final String TYPES_NAMESPACE = "Types Namespace";
068: private static final String EJB_LINK = "ejb-link";
069: private static final String SERVLET_LINK = "servlet-link";
070:
071: public WSToolsJava2WsdlAction() {
072: super ("Generate WSDL with JBossWS",
073: "Generates WSDL with the jbossws wstools utility");
074: }
075:
076: protected XFormDialog buildDialog(WsdlProject project) {
077: XFormDialogBuilder builder = XFormFactory
078: .createDialogBuilder("Generate JBossWS WSDL Artifacts");
079:
080: XForm mainForm = builder.createForm("Basic");
081:
082: mainForm.addTextField(ENDPOINT, "Serice Endpoint Interface",
083: XForm.FieldType.JAVA_CLASS);
084: mainForm.addTextField(SERVICE_NAME,
085: "The name of the generated Service",
086: XForm.FieldType.TEXT);
087: mainForm.addComboBox(STYLE, new String[] {
088: Style.DOCUMENT.toString(), Style.RPC.toString() },
089: "The style to use");
090: mainForm
091: .addComboBox(PARAMETER_STYLE, new String[] {
092: ParameterStyle.BARE.toString(),
093: ParameterStyle.WRAPPED.toString() },
094: "The style to use");
095: mainForm.addTextField(CLASSPATH, "Classpath to use",
096: XForm.FieldType.PROJECT_FOLDER);
097: mainForm.addTextField(OUTPUT,
098: "The root directory for all emitted files.",
099: XForm.FieldType.PROJECT_FOLDER);
100: mainForm.addTextField(MAPPING, "mapping file to generate",
101: XForm.FieldType.PROJECT_FILE);
102: mainForm.addTextField(TARGET_NAMESPACE,
103: "The target namespace for the generated WSDL",
104: XForm.FieldType.TEXT);
105: mainForm.addTextField(TYPES_NAMESPACE,
106: "The namespace for the generated types",
107: XForm.FieldType.TEXT);
108: mainForm.addTextField(EJB_LINK,
109: "The name of the source EJB to link to",
110: XForm.FieldType.TEXT);
111: mainForm.addTextField(SERVLET_LINK,
112: "The name of the source Servlet to link to",
113: XForm.FieldType.TEXT);
114:
115: buildArgsForm(builder, false, "wstools");
116:
117: ActionList actions = buildDefaultActions(
118: HelpUrls.WSTOOLS_HELP_URL, project);
119: actions.addAction(new ShowConfigFileAction("JBossWS Java2Wsdl",
120: "Contents of generated wsconfig.xml file") {
121: protected String getConfigFile() {
122: ConfigurationDocument configDocument = createConfigFile(dialog
123: .getValues());
124: return configDocument.toString();
125: }
126: });
127:
128: return builder
129: .buildDialog(
130: actions,
131: "Specify arguments for JBossWS wstools java2wsdl functionality",
132: UISupport.TOOL_ICON);
133: }
134:
135: protected void generate(StringToStringMap values,
136: ToolHost toolHost, WsdlProject project) throws Exception {
137: String wstoolsDir = SoapUI.getSettings().getString(
138: ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null);
139: if (Tools.isEmpty(wstoolsDir)) {
140: UISupport
141: .showErrorMessage("wstools directory must be set in global preferences");
142: return;
143: }
144:
145: String wsToolsExtension = UISupport.isWindows() ? ".bat"
146: : ".sh";
147:
148: File wstoolsFile = new File(wstoolsDir + File.separatorChar
149: + "wstools" + wsToolsExtension);
150: if (!wstoolsFile.exists()) {
151: UISupport
152: .showErrorMessage("Could not find wstools script at ["
153: + wstoolsFile + "]");
154: return;
155: }
156:
157: ProcessBuilder builder = new ProcessBuilder();
158: ArgumentBuilder args = buildArgs(UISupport.isWindows());
159: builder.command(args.getArgs());
160: builder.directory(new File(wstoolsDir));
161:
162: toolHost.run(new ToolRunner(builder, new File(values
163: .get(OUTPUT)), values.get(SERVICE_NAME), project));
164: }
165:
166: private ArgumentBuilder buildArgs(boolean isWindows)
167: throws IOException {
168: StringToStringMap values = dialog.getValues();
169: values.put(OUTPUT, Tools.ensureDir(values.get(OUTPUT), ""));
170:
171: ArgumentBuilder builder = new ArgumentBuilder(values);
172: builder.startScript("wstools");
173:
174: builder.addString(CLASSPATH, "-cp");
175: builder.addArgs("-config", buildConfigFile(values));
176: builder.addString(OUTPUT, "-dest");
177: addToolArgs(values, builder);
178: return builder;
179: }
180:
181: private String buildConfigFile(StringToStringMap values)
182: throws IOException {
183: File file = File.createTempFile("wstools-config", ".xml");
184: ConfigurationDocument configDocument = createConfigFile(values);
185:
186: configDocument.save(file);
187:
188: return file.getAbsolutePath();
189: }
190:
191: private ConfigurationDocument createConfigFile(
192: StringToStringMap values) {
193: ConfigurationDocument configDocument = ConfigurationDocument.Factory
194: .newInstance();
195: ConfigurationType config = configDocument.addNewConfiguration();
196:
197: JavaToWsdlType java2Wsdl = config.addNewJavaWsdl();
198: ServiceType service = java2Wsdl.addNewService();
199: service.setEndpoint(values.get(ENDPOINT));
200: service.setStyle(Style.Enum.forString(values.get(STYLE)));
201: service.setParameterStyle(ParameterStyle.Enum.forString(values
202: .get(PARAMETER_STYLE)));
203: service.setName(values.get(SERVICE_NAME));
204:
205: MappingType mapping = java2Wsdl.addNewMapping();
206: mapping.setFile(values.get(MAPPING));
207:
208: NamespacesType namespaces = java2Wsdl.addNewNamespaces();
209: namespaces.setTargetNamespace(values.get(TARGET_NAMESPACE));
210: namespaces.setTypeNamespace(values.get(TYPES_NAMESPACE));
211:
212: WsxmlType webservices = java2Wsdl.addNewWebservices();
213: if (values.get(EJB_LINK) != null
214: && values.get(EJB_LINK).length() > 0)
215: webservices.setEjbLink(values.get(EJB_LINK));
216: if (values.get(SERVLET_LINK) != null
217: && values.get(SERVLET_LINK).length() > 0)
218: webservices.setServletLink(values.get(SERVLET_LINK));
219: return configDocument;
220: }
221:
222: private class ToolRunner extends ProcessToolRunner {
223: private final File outDir;
224: private final String serviceName;
225: private final WsdlProject modelItem;
226:
227: public ToolRunner(ProcessBuilder builder, File outDir,
228: String serviceName, WsdlProject modelItem) {
229: super (builder, "JBossWS wstools", modelItem);
230: this .outDir = outDir;
231: this .serviceName = serviceName;
232: this .modelItem = modelItem;
233: }
234:
235: protected void afterRun(RunnerContext context) {
236: if (context.getStatus() != RunnerContext.RunnerStatus.FINISHED)
237: return;
238:
239: try {
240: String wsdlUrl = "file:" + outDir.getAbsolutePath()
241: + File.separatorChar + "wsdl"
242: + File.separatorChar + serviceName + ".wsdl";
243: Interface[] ifaces = modelItem
244: .importWsdl(wsdlUrl, true);
245:
246: if (ifaces.length > 0) {
247: context.log("Added Interface ["
248: + ifaces[0].getName() + "] to project");
249: ifaces[0].getSettings().setString(
250: WSToolsRegenerateJava2WsdlAction.class
251: .getName()
252: + "@values",
253: dialog.getValues().toXml());
254: UISupport.select(ifaces[0]);
255: }
256: } catch (SoapUIException e) {
257: SoapUI.logError(e);
258: }
259: }
260: }
261: }
|