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.wscompile;
014:
015: import java.io.File;
016: import java.io.IOException;
017:
018: import com.eviware.soapui.SoapUI;
019: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
020: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
021: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
022: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ShowConfigFileAction;
023: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
024: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
025: import com.eviware.soapui.model.iface.Interface;
026: import com.eviware.soapui.settings.ToolsSettings;
027: import com.eviware.soapui.support.Tools;
028: import com.eviware.soapui.support.UISupport;
029: import com.eviware.soapui.support.action.swing.ActionList;
030: import com.eviware.soapui.support.types.StringToStringMap;
031: import com.eviware.x.form.XForm;
032: import com.eviware.x.form.XFormDialog;
033: import com.eviware.x.form.XFormDialogBuilder;
034: import com.eviware.x.form.XFormFactory;
035: import com.sun.java.xml.ns.jaxRpc.ri.config.ConfigurationDocument;
036: import com.sun.java.xml.ns.jaxRpc.ri.config.NamespaceMappingRegistryType;
037: import com.sun.java.xml.ns.jaxRpc.ri.config.NamespaceMappingType;
038: import com.sun.java.xml.ns.jaxRpc.ri.config.WsdlType;
039: import com.sun.java.xml.ns.jaxRpc.ri.config.ConfigurationDocument.Configuration;
040:
041: /**
042: * Invokes JWSDP wscompile
043: *
044: * @author Ole.Matzura
045: */
046:
047: public class WSCompileAction extends AbstractToolsAction<Interface> {
048: private final class WSCompileShowConfigFileAction extends
049: ShowConfigFileAction {
050: private final Interface modelItem;
051:
052: private WSCompileShowConfigFileAction(String title,
053: String description, Interface modelItem) {
054: super (title, description);
055: this .modelItem = modelItem;
056: }
057:
058: protected String getConfigFile() {
059: ConfigurationDocument configDocument = createConfigFile(
060: dialog.getValues(), modelItem);
061: return configDocument.toString();
062: }
063: }
064:
065: private static final String OUTPUT = "directory";
066: private static final String DATAHANDLERONLY = "datahandleronly";
067: private static final String DONOTUNWRAP = "donotunwrap";
068: private static final String PACKAGE = "package";
069: private static final String KEEP = "keep";
070: private static final String MAPPING = "mapping";
071: private static final String SOURCE = "source";
072: private static final String OPTIMIZE = "optimize";
073: private static final String SOURCE_VERSION = "source version";
074: private static final String MODEL = "model";
075: private static final String NONCLASS = "non-class";
076: private static final String SECURITY = "security";
077: private static final String DEBUG = "debug";
078: private static final String EXPLICITCONTEXT = "explicitcontext";
079: private static final String JAXBENUMTYPE = "jaxbenumtype";
080: private static final String NODATABINDING = "nodatabinding";
081: private static final String NOENCODEDTYPES = "noencodedtypes";
082: private static final String NOMULTIREFS = "nomultirefs";
083: private static final String NORPCSTRUCTURES = "norpcstructures";
084: private static final String NOVALIDATION = "novalidation";
085: private static final String RESOLVEIDREF = "resolveidref";
086: private static final String SEARCHSCHEMA = "searchschema";
087: private static final String SERIALIZEINTERFACES = "serializeinterfaces";
088: private static final String STRICT = "strict";
089: private static final String UNWRAP = "unwrap";
090: private static final String WSI = "wsi";
091: private static final String PROXY = "proxy";
092: private static final String NAMESPACE_MAPPING = "Namespace mapping";
093: public static final String SOAPUI_ACTION_ID = "WSCompileAction";
094:
095: public WSCompileAction() {
096: super ("JAX-RPC Artifacts",
097: "Generates JAX-RPC artifacts using wscompile");
098: }
099:
100: protected XFormDialog buildDialog(Interface modelItem) {
101: XFormDialogBuilder builder = XFormFactory
102: .createDialogBuilder("WSCompile");
103:
104: XForm mainForm = builder.createForm("Basic");
105: addWSDLFields(mainForm, modelItem);
106:
107: mainForm.addTextField(PACKAGE,
108: "the package of the classes generated by wscompile",
109: XForm.FieldType.JAVA_PACKAGE);
110: mainForm.addTextField(OUTPUT,
111: "where to place generated output files",
112: XForm.FieldType.PROJECT_FOLDER);
113: mainForm.addCheckBox(KEEP, "(Keep generated files)");
114: mainForm.addTextField(MAPPING,
115: "Generate a J2EE mapping.xml file",
116: XForm.FieldType.PROJECT_FILE);
117: mainForm.addTextField(MODEL,
118: "Write the internal model to the given file",
119: XForm.FieldType.PROJECT_FILE);
120: mainForm.addTextField(SOURCE,
121: "Where to place generated source files",
122: XForm.FieldType.PROJECT_FOLDER);
123: mainForm.addTextField(NONCLASS,
124: "Where to place non-class generated files",
125: XForm.FieldType.PROJECT_FOLDER);
126: mainForm.addCheckBox(OPTIMIZE, "(Optimize generated code)");
127: mainForm.addCheckBox(DEBUG, "(Generate debugging info)");
128: mainForm.addComboBox(SOURCE_VERSION, new String[] { "1.0.1",
129: "1.0.3", "1.1", "1.1.1", "1.1.2" },
130: "Generate code for the specified JAX-RPC SI version");
131: mainForm
132: .addTextField(
133: SECURITY,
134: "Security configuration file to generate security code",
135: XForm.FieldType.PROJECT_FILE);
136: mainForm.addTextField(PROXY, "Specify a HTTP proxy server",
137: XForm.FieldType.URL);
138:
139: XForm featuresForm = builder.createForm("Features");
140:
141: featuresForm.addCheckBox(DATAHANDLERONLY,
142: "(Always map attachments to the DataHandler type)");
143: featuresForm
144: .addCheckBox(DONOTUNWRAP,
145: "(Disable unwrapping of document/literal wrapper elements in WSI mode)");
146: featuresForm.addCheckBox(EXPLICITCONTEXT,
147: "(Turn on explicit service context mapping)");
148: featuresForm.addCheckBox(JAXBENUMTYPE,
149: "(Map anonymous enumeration to its base type)");
150: featuresForm.addCheckBox(NODATABINDING,
151: "(Turn off data binding for literal encoding)");
152: featuresForm.addCheckBox(NOENCODEDTYPES,
153: "(Turn off encoding type information)");
154: featuresForm.addCheckBox(NOMULTIREFS,
155: "(Turn off support for multiple references)");
156: featuresForm.addCheckBox(NORPCSTRUCTURES,
157: "(Do not generate RPC structures)");
158: featuresForm
159: .addCheckBox(NOVALIDATION,
160: "(Turn off full validation of imported WSDL documents)");
161: featuresForm.addCheckBox(RESOLVEIDREF, "(Resolve xsd:IDREF)");
162: featuresForm.addCheckBox(SEARCHSCHEMA,
163: "(Search schema aggressively for types)");
164: featuresForm.addCheckBox(SERIALIZEINTERFACES,
165: "(Turn on direct serialization of interface types)");
166: featuresForm.addCheckBox(STRICT,
167: "(Generate code strictly compliant with JAXRPC spec)");
168: featuresForm
169: .addCheckBox(UNWRAP,
170: "(Enable unwrapping of document/literal wrapper elements in WSI mode)");
171: featuresForm
172: .addCheckBox(
173: WSI,
174: "(Enable WSI-Basic Profile features, to be used for document/literal and rpc/literal)");
175:
176: XForm advForm = builder.createForm("Advanced");
177: advForm.addNameSpaceTable(NAMESPACE_MAPPING, modelItem);
178:
179: buildArgsForm(builder, false, "wscompile");
180:
181: ActionList actions = buildDefaultActions(
182: HelpUrls.WSCOMPILE_HELP_URL, modelItem);
183: actions.addAction(new WSCompileShowConfigFileAction(
184: "JAX-RPC wscompile",
185: "Contents of generated config.xml file", modelItem));
186:
187: return builder.buildDialog(actions,
188: "Specify arguments for JAX-RPC wscompile",
189: UISupport.TOOL_ICON);
190: }
191:
192: protected StringToStringMap initValues(Interface modelItem) {
193: StringToStringMap values = super .initValues(modelItem);
194: values.putIfMissing(SOURCE_VERSION, "1.1.2");
195: values.putIfMissing(WSI, Boolean.toString(true));
196:
197: return values;
198: }
199:
200: protected void generate(StringToStringMap values,
201: ToolHost toolHost, Interface modelItem) throws Exception {
202: String wscompileDir = SoapUI.getSettings().getString(
203: ToolsSettings.JWSDP_WSCOMPILE_LOCATION, null);
204: if (Tools.isEmpty(wscompileDir)) {
205: UISupport
206: .showErrorMessage("wscompile directory must be set in global preferences");
207: return;
208: }
209:
210: String wscompileExtension = UISupport.isWindows() ? ".bat"
211: : ".sh";
212:
213: File wscompileFile = new File(wscompileDir + File.separatorChar
214: + "wscompile" + wscompileExtension);
215: if (!wscompileFile.exists()) {
216: UISupport
217: .showErrorMessage("Could not find wscompile script at ["
218: + wscompileFile + "]");
219: return;
220: }
221:
222: ProcessBuilder builder = new ProcessBuilder();
223: ArgumentBuilder args = buildArgs(UISupport.isWindows(),
224: modelItem);
225: builder.command(args.getArgs());
226: builder.directory(new File(wscompileDir));
227:
228: toolHost.run(new ProcessToolRunner(builder,
229: "JAX-RPC wscompile", modelItem));
230: }
231:
232: private ArgumentBuilder buildArgs(boolean isWindows,
233: Interface modelItem) throws IOException {
234: StringToStringMap values = dialog.getValues();
235: ArgumentBuilder builder = new ArgumentBuilder(values);
236: builder.startScript("wscompile");
237:
238: values.put(OUTPUT, Tools.ensureDir(values.get(OUTPUT), ""));
239:
240: values.put(SOURCE, Tools.ensureDir(values.get(SOURCE), values
241: .get(OUTPUT)));
242: values.put(NONCLASS, Tools.ensureDir(values.get(NONCLASS),
243: values.get(OUTPUT)));
244:
245: values.put(MAPPING, Tools.ensureFileDir(values.get(MAPPING),
246: values.get(OUTPUT)));
247: values.put(MODEL, Tools.ensureFileDir(values.get(MODEL), values
248: .get(OUTPUT)));
249:
250: builder.addString(OUTPUT, "-d");
251: builder.addBoolean(KEEP, "-keep");
252: builder.addString(MAPPING, "-mapping");
253: builder.addString(MODEL, "-model");
254: builder.addString(SOURCE, "-s");
255: builder.addString(NONCLASS, "-nd");
256: builder.addBoolean(OPTIMIZE, "-O");
257: builder.addBoolean(DEBUG, "-g");
258: builder.addString(SOURCE_VERSION, "-source");
259: builder.addString(SECURITY, "-security");
260: builder.addString(PROXY, "httpproxy", ":");
261:
262: builder.addBoolean(DATAHANDLERONLY, "-f:datahandleronly");
263: builder.addBoolean(DONOTUNWRAP, "-f:donotunwrap");
264: builder.addBoolean(EXPLICITCONTEXT, "-f:explicitcontext");
265: builder.addBoolean(JAXBENUMTYPE, "-f:jaxbenumtype");
266: builder.addBoolean(NODATABINDING, "-f:nodatabinding");
267: builder.addBoolean(NOENCODEDTYPES, "-f:noencodedtypes");
268: builder.addBoolean(NOMULTIREFS, "-f:nomultirefs");
269: builder.addBoolean(NORPCSTRUCTURES, "-f:norpcstructures");
270: builder.addBoolean(NOVALIDATION, "-f:novalidation");
271: builder.addBoolean(RESOLVEIDREF, "-f:resolveidref");
272: builder.addBoolean(SEARCHSCHEMA, "-f:searchschema");
273: builder.addBoolean(SERIALIZEINTERFACES,
274: "-f:serializeinterfaces");
275: builder.addBoolean(STRICT, "-f:strict");
276: builder.addBoolean(UNWRAP, "-f:unwrap");
277: builder.addBoolean(WSI, "-f:wsi");
278:
279: builder.addArgs("-import");
280: builder.addArgs("-verbose");
281: addToolArgs(values, builder);
282: builder.addArgs(buildConfigFile(values, modelItem));
283: return builder;
284: }
285:
286: private String buildConfigFile(StringToStringMap values,
287: Interface modelItem) throws IOException {
288: File file = File.createTempFile("wscompile-config", ".xml");
289: ConfigurationDocument configDocument = createConfigFile(values,
290: modelItem);
291: configDocument.save(file);
292: return file.getAbsolutePath();
293: }
294:
295: private ConfigurationDocument createConfigFile(
296: StringToStringMap values, Interface modelItem) {
297: ConfigurationDocument configDocument = ConfigurationDocument.Factory
298: .newInstance();
299: Configuration config = configDocument.addNewConfiguration();
300:
301: WsdlType wsdl = config.addNewWsdl();
302: wsdl.setLocation(getWsdlUrl(values, modelItem));
303: wsdl.setPackageName(values.get(PACKAGE).toString());
304:
305: try {
306: StringToStringMap nsMappings = StringToStringMap
307: .fromXml(values.get(NAMESPACE_MAPPING));
308: if (!nsMappings.isEmpty()) {
309: NamespaceMappingRegistryType nsMappingRegistry = wsdl
310: .addNewNamespaceMappingRegistry();
311:
312: for (String namespace : nsMappings.keySet()) {
313: String packageName = nsMappings.get(namespace);
314:
315: NamespaceMappingType newMapping = nsMappingRegistry
316: .addNewNamespaceMapping();
317: newMapping.setNamespace(namespace);
318: newMapping.setPackageName(packageName);
319: }
320: }
321: } catch (Exception e) {
322: SoapUI.logError(e);
323: }
324: return configDocument;
325: }
326: }
|