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: import java.net.MalformedURLException;
018: import java.net.URL;
019:
020: import org.jboss.jbosswsTools.ConfigurationDocument;
021: import org.jboss.jbosswsTools.ConfigurationType;
022: import org.jboss.jbosswsTools.GlobalType;
023: import org.jboss.jbosswsTools.PkgNSType;
024: import org.jboss.jbosswsTools.WsdlToJavaType;
025: import org.jboss.jbosswsTools.WsxmlType;
026: import org.jboss.jbosswsTools.WsdlToJavaType.ParameterStyle;
027: import org.w3c.dom.Element;
028:
029: import com.eviware.soapui.SoapUI;
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.ShowConfigFileAction;
034: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
035: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
036: import com.eviware.soapui.model.ModelItem;
037: import com.eviware.soapui.model.iface.Interface;
038: import com.eviware.soapui.model.project.Project;
039: import com.eviware.soapui.settings.ToolsSettings;
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: import com.eviware.x.form.XFormField;
049: import com.eviware.x.form.XFormFieldListener;
050: import com.eviware.x.form.XFormTextField;
051:
052: /**
053: * Invokes jbossws wsdl2java tools
054: *
055: * @author Ole.Matzura
056: */
057:
058: public class WSToolsWsdl2JavaAction extends
059: AbstractToolsAction<Interface> {
060: public static final String SOAPUI_ACTION_ID = "WSToolsWsdl2JavaAction";
061:
062: private static final String NAMESPACE_MAPPING = "Namespace mapping";
063: private static final String OUTPUT = "Output Directory";
064: private static final String MAPPING = "Mapping file";
065: private static final String UNWRAP = "Unwrap";
066: private static final String APPEND = "Append";
067: private static final String SERVLET_LINK = "Servlet Link";
068: private static final String EJB_LINK = "EJB Link";
069: private XFormTextField ejbLinkField;
070: private XFormTextField servletLinkField;
071: private XFormField appendField;
072:
073: public WSToolsWsdl2JavaAction() {
074: super ("JBossWS Artifacts",
075: "Generates JBossWS artifacts using the jboss wstools utility");
076: }
077:
078: @Override
079: public boolean applies(ModelItem target) {
080: Interface iface = (Interface) target;
081: return !iface.getProject().hasNature(Project.JBOSSWS_NATURE_ID);
082: }
083:
084: @Override
085: protected StringToStringMap initValues(Interface modelItem) {
086: StringToStringMap values = super .initValues(modelItem);
087:
088: boolean hasEjbLink = values.get(EJB_LINK, "").length() > 0;
089: boolean hasServletLink = values.get(SERVLET_LINK, "").length() > 0;
090:
091: if (!hasEjbLink && !hasServletLink) {
092: ejbLinkField.setEnabled(true);
093: servletLinkField.setEnabled(true);
094: } else {
095: ejbLinkField.setEnabled(hasEjbLink && !hasServletLink);
096: servletLinkField.setEnabled(hasServletLink && !hasEjbLink);
097:
098: if (hasEjbLink && hasServletLink)
099: values.put(SERVLET_LINK, "");
100: }
101:
102: appendField.setEnabled(hasEjbLink || hasServletLink);
103:
104: return values;
105: }
106:
107: protected XFormDialog buildDialog(Interface modelItem) {
108: XFormDialogBuilder builder = XFormFactory
109: .createDialogBuilder("JBossWS Artifacts");
110:
111: XForm mainForm = builder.createForm("Basic");
112: addWSDLFields(mainForm, modelItem);
113:
114: mainForm.addTextField(OUTPUT,
115: "The root directory for all emitted files.",
116: XForm.FieldType.PROJECT_FOLDER);
117: mainForm.addTextField(MAPPING, "mapping file to generate",
118: XForm.FieldType.PROJECT_FILE);
119: mainForm.addCheckBox(UNWRAP, "unwrap doc-literal operations");
120:
121: mainForm.addNameSpaceTable(NAMESPACE_MAPPING, modelItem);
122:
123: mainForm.addSeparator("webservices.xml generation options");
124: ejbLinkField = mainForm
125: .addTextField(
126: EJB_LINK,
127: "The ejb-jar.xml ejb-link for Stateless Session Bean endpoints",
128: XForm.FieldType.TEXT);
129: ejbLinkField.addFormFieldListener(new XFormFieldListener() {
130: public void valueChanged(XFormField sourceField,
131: String newValue, String oldValue) {
132: servletLinkField.setEnabled(newValue.length() == 0);
133: appendField.setEnabled(newValue.length() > 0);
134: }
135: });
136:
137: servletLinkField = mainForm
138: .addTextField(
139: SERVLET_LINK,
140: "The web.xml servlet-link that is used by Java Service Endpoints (WAR)",
141: XForm.FieldType.TEXT);
142: servletLinkField.addFormFieldListener(new XFormFieldListener() {
143: public void valueChanged(XFormField sourceField,
144: String newValue, String oldValue) {
145: ejbLinkField.setEnabled(newValue.length() == 0);
146: appendField.setEnabled(newValue.length() > 0);
147: }
148: });
149:
150: appendField = mainForm.addCheckBox(APPEND,
151: "append to existing file");
152: appendField.setEnabled(false);
153: buildArgsForm(builder, false, "wstools");
154:
155: ActionList actions = buildDefaultActions(
156: HelpUrls.WSTOOLS_HELP_URL, modelItem);
157: actions.addAction(new JBossWSShowConfigFileAction(
158: "JBossWS Wsdl2Java",
159: "Contents of generated wsconfig.xml file", modelItem));
160: return builder
161: .buildDialog(
162: actions,
163: "Specify arguments for JBossWS wstools wsdl2java functionality",
164: UISupport.TOOL_ICON);
165: }
166:
167: protected void generate(StringToStringMap values,
168: ToolHost toolHost, Interface modelItem) throws Exception {
169: String wstoolsDir = SoapUI.getSettings().getString(
170: ToolsSettings.JBOSSWS_WSTOOLS_LOCATION, null);
171: if (Tools.isEmpty(wstoolsDir)) {
172: UISupport
173: .showErrorMessage("wstools directory must be set in global preferences");
174: return;
175: }
176:
177: String wsToolsExtension = UISupport.isWindows() ? ".bat"
178: : ".sh";
179:
180: File wstoolsFile = new File(wstoolsDir + File.separatorChar
181: + "wstools" + wsToolsExtension);
182: if (!wstoolsFile.exists()) {
183: UISupport
184: .showErrorMessage("Could not find wstools script at ["
185: + wstoolsFile + "]");
186: return;
187: }
188:
189: ProcessBuilder builder = new ProcessBuilder();
190: ArgumentBuilder args = buildArgs(UISupport.isWindows(),
191: modelItem);
192: builder.command(args.getArgs());
193: builder.directory(new File(wstoolsDir));
194:
195: toolHost.run(new ProcessToolRunner(builder, "JBossWS wstools",
196: modelItem));
197: }
198:
199: private ArgumentBuilder buildArgs(boolean isWindows,
200: Interface modelItem) throws IOException {
201: StringToStringMap values = dialog.getValues();
202:
203: values.put(OUTPUT, Tools.ensureDir(values.get(OUTPUT), ""));
204:
205: ArgumentBuilder builder = new ArgumentBuilder(values);
206: builder.startScript("wstools");
207:
208: builder.addArgs("-config", buildConfigFile(values, modelItem));
209: builder.addString(OUTPUT, "-dest");
210: addToolArgs(values, builder);
211: return builder;
212: }
213:
214: private String buildConfigFile(StringToStringMap values,
215: Interface modelItem) throws IOException {
216: File file = File.createTempFile("wstools-config", ".xml");
217: ConfigurationDocument configDocument = createConfigFile(values,
218: modelItem);
219:
220: configDocument.save(file);
221:
222: return file.getAbsolutePath();
223: }
224:
225: private ConfigurationDocument createConfigFile(
226: StringToStringMap values, Interface modelItem) {
227: ConfigurationDocument configDocument = ConfigurationDocument.Factory
228: .newInstance();
229: ConfigurationType config = configDocument.addNewConfiguration();
230:
231: try {
232: StringToStringMap nsMappings = StringToStringMap
233: .fromXml(values.get(NAMESPACE_MAPPING));
234: if (!nsMappings.isEmpty()) {
235: GlobalType global = config.addNewGlobal();
236:
237: for (String namespace : nsMappings.keySet()) {
238: PkgNSType entry = global.addNewPackageNamespace();
239: entry.setNamespace(namespace);
240: entry.setPackage(nsMappings.get(namespace));
241: }
242: }
243: } catch (Exception e) {
244: SoapUI.logError(e);
245: }
246:
247: WsdlToJavaType wsdl2Java = config.addNewWsdlJava();
248:
249: String wsdlUrl = getWsdlUrl(values, modelItem);
250: try {
251: new URL(wsdlUrl);
252: wsdl2Java.setLocation(wsdlUrl);
253: } catch (MalformedURLException e) {
254: ((Element) wsdl2Java.getDomNode()).setAttribute("file",
255: wsdlUrl);
256: }
257:
258: if (values.getBoolean(UNWRAP))
259: wsdl2Java.setParameterStyle(ParameterStyle.BARE);
260: else
261: wsdl2Java.setParameterStyle(ParameterStyle.WRAPPED);
262:
263: if (values.get(EJB_LINK) != null
264: && values.get(EJB_LINK).length() > 0) {
265: WsxmlType webservices = wsdl2Java.addNewWebservices();
266: webservices.setEjbLink(values.get(EJB_LINK));
267: webservices.setAppend(values.getBoolean(APPEND));
268: } else if (values.get(SERVLET_LINK) != null
269: && values.get(SERVLET_LINK).length() > 0) {
270: WsxmlType webservices = wsdl2Java.addNewWebservices();
271: webservices.setServletLink(values.get(SERVLET_LINK));
272: webservices.setAppend(values.getBoolean(APPEND));
273: }
274:
275: String mappingFile = values.get(MAPPING).toString().trim();
276: if (mappingFile.length() > 0) {
277: wsdl2Java.addNewMapping().setFile(mappingFile);
278: }
279: return configDocument;
280: }
281:
282: private final class JBossWSShowConfigFileAction extends
283: ShowConfigFileAction {
284: private final Interface modelItem;
285:
286: private JBossWSShowConfigFileAction(String title,
287: String description, Interface modelItem) {
288: super (title, description);
289: this .modelItem = modelItem;
290: }
291:
292: protected String getConfigFile() {
293: ConfigurationDocument configDocument = createConfigFile(
294: dialog.getValues(), modelItem);
295: return configDocument.toString();
296: }
297: }
298:
299: }
|