View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.actions.iface.tools.xfire;
14  
15  import java.io.File;
16  import java.io.FilenameFilter;
17  import java.io.IOException;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
21  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
22  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
23  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
24  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
25  import com.eviware.soapui.model.iface.Interface;
26  import com.eviware.soapui.settings.ToolsSettings;
27  import com.eviware.soapui.settings.ToolsSupport;
28  import com.eviware.soapui.support.Tools;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.soapui.support.types.StringToStringMap;
31  import com.eviware.x.form.XForm;
32  import com.eviware.x.form.XFormDialog;
33  import com.eviware.x.form.XFormDialogBuilder;
34  import com.eviware.x.form.XFormFactory;
35  import com.eviware.x.form.XFormField;
36  import com.eviware.x.form.XFormTextField;
37  
38  /***
39   * Invokes XFire wsgen utility
40   * 
41   * @author Ole.Matzura
42   */
43  
44  public class XFireAction extends AbstractToolsAction<Interface>
45  {
46  	private static final String PACKAGE = "Package";
47  	private static final String OUTPUT = "Output Directory";
48  	private static final String BINDING = "Binding";
49  	private static final String EXTERNAL_BINDINGS = "External Bindings";
50  	private static final String BASE_URI = "Base URI";
51  	private static final String PROFILE = "Profile";
52  	private static final String CLASSPATH = "Classpath";
53  	private static final String OVERWRITE = "Overwrite previously generated files";
54  	private static final String EXPLICIT_ANNOTATION = "Explicit Annotations";
55  	private static final String SERVER_STUBS = "Generate Server Stubs";
56  	public static final String SOAPUI_ACTION_ID = "XFireAction";
57  
58  	public XFireAction()
59  	{
60  		super( "XFire 1.X Stubs", "Generates XFire 1.X stubs using the wsgen utility" );
61  	}
62  
63  	protected XFormDialog buildDialog( Interface modelItem )
64  	{
65  		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "XFire 1.X Stubs" );
66  
67  		XForm mainForm = builder.createForm( "Basic" );
68  		addWSDLFields( mainForm, modelItem );
69  
70  		mainForm.addTextField( OUTPUT, "Root directory for all emitted files.", XForm.FieldType.PROJECT_FOLDER );
71  		mainForm.addTextField( PACKAGE, "Package for generated classes", XForm.FieldType.JAVA_PACKAGE ).setRequired(
72  				true, "Package is required" );
73  		XFormField bindingCombo = mainForm.addComboBox( BINDING, new String[] { "jaxb", "xmlbeans" },
74  				"Binding framework to use" );
75  
76  		XFormTextField cpField = mainForm.addTextField( CLASSPATH, "Classpath to generated xmlbeans for binding",
77  				XForm.FieldType.PROJECT_FILE );
78  		XFormTextField extBindingsField = mainForm.addTextField( EXTERNAL_BINDINGS, "External jaxb binding file(s)",
79  				XForm.FieldType.PROJECT_FILE );
80  		bindingCombo.addComponentEnabler( cpField, "xmlbeans" );
81  		bindingCombo.addComponentEnabler( extBindingsField, "jaxb" );
82  
83  		mainForm.addTextField( PROFILE, "Profile to use for generating artifacts", XForm.FieldType.TEXT );
84  		mainForm.addTextField( BASE_URI, "Base URI to use", XForm.FieldType.URL );
85  		mainForm.addCheckBox( OVERWRITE, null );
86  		mainForm.addCheckBox( EXPLICIT_ANNOTATION, null );
87  		mainForm.addCheckBox( SERVER_STUBS, null );
88  
89  		buildArgsForm( builder, true, "WsGen" );
90  
91  		return builder.buildDialog( buildDefaultActions( HelpUrls.XFIRE_HELP_URL, modelItem ),
92  				"Specify arguments for XFire 1.X WsGen", UISupport.TOOL_ICON );
93  	}
94  
95  	protected StringToStringMap initValues( Interface modelItem, Object param )
96  	{
97  		StringToStringMap values = super.initValues( modelItem, param );
98  		values.putIfMissing( BINDING, "jaxb" );
99  		return values;
100 	}
101 
102 	protected void generate( StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
103 	{
104 		String xfireDir = SoapUI.getSettings().getString( ToolsSettings.XFIRE_LOCATION, null );
105 		if( Tools.isEmpty( xfireDir ) )
106 		{
107 			UISupport.showErrorMessage( "XFire 1.X directory must be set in global preferences" );
108 			return;
109 		}
110 
111 		String antDir = ToolsSupport.getToolLocator().getAntDir( true );
112 		if( Tools.isEmpty( antDir ) )
113 		{
114 			UISupport.showErrorMessage( "ANT directory must be set in global preferences" );
115 			return;
116 		}
117 
118 		ProcessBuilder builder = new ProcessBuilder();
119 		ArgumentBuilder args = buildArgs( buildClasspath( xfireDir, antDir, values.get( CLASSPATH ) ), modelItem );
120 		builder.command( args.getArgs() );
121 		builder.directory( new File( xfireDir ) );
122 
123 		toolHost.run( new ProcessToolRunner( builder, "XFire 1.X WsGen", modelItem ) );
124 	}
125 
126 	private String buildClasspath( String xfireDir, String antDir, String additional )
127 	{
128 		String libDir = xfireDir + File.separatorChar + "lib";
129 		String[] xfireLibs = new File( libDir ).list( new FilenameFilter()
130 		{
131 
132 			public boolean accept( File dir, String name )
133 			{
134 				if( !name.endsWith( ".jar" ) )
135 					return false;
136 
137 				if( name.startsWith( "jaxb" ) )
138 					return name.indexOf( "2.0" ) > 0;
139 
140 				return true;
141 			}
142 		} );
143 
144 		String modulesDir = xfireDir + File.separatorChar + "modules";
145 		String[] xfireJars = new File( modulesDir ).list( new FilenameFilter()
146 		{
147 
148 			public boolean accept( File dir, String name )
149 			{
150 				return name.endsWith( ".jar" );
151 			}
152 		} );
153 
154 		StringBuilder classpath = new StringBuilder();
155 
156 		for( int c = 0; c < xfireLibs.length; c++ )
157 		{
158 			if( c > 0 )
159 				classpath.append( File.pathSeparatorChar );
160 
161 			classpath.append( libDir + File.separatorChar + xfireLibs[c] );
162 		}
163 
164 		for( int c = 0; c < xfireJars.length; c++ )
165 		{
166 			classpath.append( File.pathSeparatorChar );
167 			classpath.append( modulesDir ).append( File.separatorChar ).append( xfireJars[c] );
168 		}
169 
170 		classpath.append( File.pathSeparatorChar );
171 		classpath.append( antDir ).append( File.separatorChar ).append( "lib" ).append( File.separatorChar ).append(
172 				"ant.jar" );
173 
174 		if( additional != null && additional.trim().length() > 0 )
175 			classpath.append( File.pathSeparatorChar ).append( additional.trim() );
176 
177 		return classpath.toString();
178 	}
179 
180 	private ArgumentBuilder buildArgs( String classpath, Interface modelItem ) throws IOException
181 	{
182 		StringToStringMap values = dialog.getValues();
183 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ) );
184 
185 		ArgumentBuilder builder = new ArgumentBuilder( values );
186 
187 		builder.addArgs( "java" );
188 		addJavaArgs( values, builder );
189 
190 		builder.addArgs( "-cp", classpath, "org.codehaus.xfire.gen.WsGen" );
191 		builder.addArgs( "-wsdl", getWsdlUrl( values, modelItem ) );
192 		builder.addString( OUTPUT, "-o" );
193 		builder.addString( PACKAGE, "-p" );
194 		builder.addString( BINDING, "-b" );
195 		builder.addString( EXTERNAL_BINDINGS, "-e" );
196 		builder.addString( PROFILE, "-r" );
197 		builder.addString( BASE_URI, "-u" );
198 		builder.addString( OVERWRITE, "-overwrite" );
199 		builder.addString( EXPLICIT_ANNOTATION, "-x" );
200 		builder.addString( SERVER_STUBS, "-ss" );
201 		addToolArgs( values, builder );
202 		return builder;
203 	}
204 }