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.xfire;
014:
015: import java.io.File;
016: import java.io.FilenameFilter;
017: import java.io.IOException;
018:
019: import com.eviware.soapui.SoapUI;
020: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
021: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
022: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
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.settings.ToolsSupport;
028: import com.eviware.soapui.support.Tools;
029: import com.eviware.soapui.support.UISupport;
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.eviware.x.form.XFormField;
036: import com.eviware.x.form.XFormTextField;
037:
038: /**
039: * Invokes XFire wsgen utility
040: *
041: * @author Ole.Matzura
042: */
043:
044: public class XFireAction extends AbstractToolsAction<Interface> {
045: private static final String PACKAGE = "Package";
046: private static final String OUTPUT = "Output Directory";
047: private static final String BINDING = "Binding";
048: private static final String EXTERNAL_BINDINGS = "External Bindings";
049: private static final String BASE_URI = "Base URI";
050: private static final String PROFILE = "Profile";
051: private static final String CLASSPATH = "Classpath";
052: private static final String OVERWRITE = "Overwrite previously generated files";
053: private static final String EXPLICIT_ANNOTATION = "Explicit Annotations";
054: private static final String SERVER_STUBS = "Generate Server Stubs";
055: public static final String SOAPUI_ACTION_ID = "XFireAction";
056:
057: public XFireAction() {
058: super ("XFire 1.X Stubs",
059: "Generates XFire 1.X stubs using the wsgen utility");
060: }
061:
062: protected XFormDialog buildDialog(Interface modelItem) {
063: XFormDialogBuilder builder = XFormFactory
064: .createDialogBuilder("XFire 1.X Stubs");
065:
066: XForm mainForm = builder.createForm("Basic");
067: addWSDLFields(mainForm, modelItem);
068:
069: mainForm.addTextField(OUTPUT,
070: "Root directory for all emitted files.",
071: XForm.FieldType.PROJECT_FOLDER);
072: mainForm.addTextField(PACKAGE, "Package for generated classes",
073: XForm.FieldType.JAVA_PACKAGE).setRequired(true,
074: "Package is required");
075: XFormField bindingCombo = mainForm.addComboBox(BINDING,
076: new String[] { "jaxb", "xmlbeans" },
077: "Binding framework to use");
078:
079: XFormTextField cpField = mainForm.addTextField(CLASSPATH,
080: "Classpath to generated xmlbeans for binding",
081: XForm.FieldType.PROJECT_FILE);
082: XFormTextField extBindingsField = mainForm.addTextField(
083: EXTERNAL_BINDINGS, "External jaxb binding file(s)",
084: XForm.FieldType.PROJECT_FILE);
085: bindingCombo.addComponentEnabler(cpField, "xmlbeans");
086: bindingCombo.addComponentEnabler(extBindingsField, "jaxb");
087:
088: mainForm.addTextField(PROFILE,
089: "Profile to use for generating artifacts",
090: XForm.FieldType.TEXT);
091: mainForm.addTextField(BASE_URI, "Base URI to use",
092: XForm.FieldType.URL);
093: mainForm.addCheckBox(OVERWRITE, null);
094: mainForm.addCheckBox(EXPLICIT_ANNOTATION, null);
095: mainForm.addCheckBox(SERVER_STUBS, null);
096:
097: buildArgsForm(builder, true, "WsGen");
098:
099: return builder.buildDialog(buildDefaultActions(
100: HelpUrls.XFIRE_HELP_URL, modelItem),
101: "Specify arguments for XFire 1.X WsGen",
102: UISupport.TOOL_ICON);
103: }
104:
105: protected StringToStringMap initValues(Interface modelItem) {
106: StringToStringMap values = super .initValues(modelItem);
107: values.putIfMissing(BINDING, "jaxb");
108: return values;
109: }
110:
111: protected void generate(StringToStringMap values,
112: ToolHost toolHost, Interface modelItem) throws Exception {
113: String xfireDir = SoapUI.getSettings().getString(
114: ToolsSettings.XFIRE_LOCATION, null);
115: if (Tools.isEmpty(xfireDir)) {
116: UISupport
117: .showErrorMessage("XFire 1.X directory must be set in global preferences");
118: return;
119: }
120:
121: String antDir = ToolsSupport.getToolLocator().getAntDir(true);
122: if (Tools.isEmpty(antDir)) {
123: UISupport
124: .showErrorMessage("ANT directory must be set in global preferences");
125: return;
126: }
127:
128: ProcessBuilder builder = new ProcessBuilder();
129: ArgumentBuilder args = buildArgs(buildClasspath(xfireDir,
130: antDir, values.get(CLASSPATH)), modelItem);
131: builder.command(args.getArgs());
132: builder.directory(new File(xfireDir));
133:
134: toolHost.run(new ProcessToolRunner(builder, "XFire 1.X WsGen",
135: modelItem));
136: }
137:
138: private String buildClasspath(String xfireDir, String antDir,
139: String additional) {
140: String libDir = xfireDir + File.separatorChar + "lib";
141: String[] xfireLibs = new File(libDir)
142: .list(new FilenameFilter() {
143:
144: public boolean accept(File dir, String name) {
145: if (!name.endsWith(".jar"))
146: return false;
147:
148: if (name.startsWith("jaxb"))
149: return name.indexOf("2.0") > 0;
150:
151: return true;
152: }
153: });
154:
155: String modulesDir = xfireDir + File.separatorChar + "modules";
156: String[] xfireJars = new File(modulesDir)
157: .list(new FilenameFilter() {
158:
159: public boolean accept(File dir, String name) {
160: return name.endsWith(".jar");
161: }
162: });
163:
164: StringBuilder classpath = new StringBuilder();
165:
166: for (int c = 0; c < xfireLibs.length; c++) {
167: if (c > 0)
168: classpath.append(File.pathSeparatorChar);
169:
170: classpath
171: .append(libDir + File.separatorChar + xfireLibs[c]);
172: }
173:
174: for (int c = 0; c < xfireJars.length; c++) {
175: classpath.append(File.pathSeparatorChar);
176: classpath.append(modulesDir).append(File.separatorChar)
177: .append(xfireJars[c]);
178: }
179:
180: classpath.append(File.pathSeparatorChar);
181: classpath.append(antDir).append(File.separatorChar).append(
182: "lib").append(File.separatorChar).append("ant.jar");
183:
184: if (additional != null && additional.trim().length() > 0)
185: classpath.append(File.pathSeparatorChar).append(
186: additional.trim());
187:
188: return classpath.toString();
189: }
190:
191: private ArgumentBuilder buildArgs(String classpath,
192: Interface modelItem) throws IOException {
193: StringToStringMap values = dialog.getValues();
194: values.put(OUTPUT, Tools.ensureDir(values.get(OUTPUT), ""));
195:
196: ArgumentBuilder builder = new ArgumentBuilder(values);
197:
198: builder.addArgs("java");
199: addJavaArgs(values, builder);
200:
201: builder.addArgs("-cp", classpath,
202: "org.codehaus.xfire.gen.WsGen");
203: builder.addArgs("-wsdl", getWsdlUrl(values, modelItem));
204: builder.addString(OUTPUT, "-o");
205: builder.addString(PACKAGE, "-p");
206: builder.addString(BINDING, "-b");
207: builder.addString(EXTERNAL_BINDINGS, "-e");
208: builder.addString(PROFILE, "-r");
209: builder.addString(BASE_URI, "-u");
210: builder.addString(OVERWRITE, "-overwrite");
211: builder.addString(EXPLICIT_ANNOTATION, "-x");
212: builder.addString(SERVER_STUBS, "-ss");
213: addToolArgs(values, builder);
214: return builder;
215: }
216: }
|