View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.ComponentEnabler;
32  import com.eviware.x.form.XForm;
33  import com.eviware.x.form.XFormDialog;
34  import com.eviware.x.form.XFormDialogBuilder;
35  import com.eviware.x.form.XFormFactory;
36  import com.eviware.x.form.XFormField;
37  
38  /***
39   * Invokes jbossws wsdl2java tools
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  	
57     public XFireAction( Interface iface )
58     {
59        super( iface, "XFire 1.X Stubs", "Generates XFire 1.X stubs using the wsgen utility");
60     }
61  
62     protected XFormDialog buildDialog()
63  	{
64        XFormDialogBuilder builder = XFormFactory.createDialogBuilder("XFire 1.X Stubs");
65  
66  		XForm mainForm = builder.createForm( "Basic" );
67  		addWSDLFields(mainForm);
68  		
69  		mainForm.addTextField( OUTPUT, "Root directory for all emitted files.", XForm.FieldType.PROJECT_FOLDER );
70  		mainForm.addTextField( PACKAGE, "Package for generated classes", XForm.FieldType.JAVA_PACKAGE ).setRequired( true, "Package is required" );
71  		XFormField bindingCombo = mainForm.addComboBox( BINDING, new String[] {"jaxb","xmlbeans"}, "Binding framework to use" );
72  		ComponentEnabler enabler = new ComponentEnabler( bindingCombo );
73  		
74  		enabler.add( mainForm.addTextField( CLASSPATH, "Classpath to generated xmlbeans for binding", XForm.FieldType.PROJECT_FILE ), "xmlbeans" );
75  		enabler.add( mainForm.addTextField( EXTERNAL_BINDINGS, "External jaxb binding file(s)", XForm.FieldType.PROJECT_FILE ), "jaxb" );
76  
77  		mainForm.addTextField( PROFILE, "Profile to use for generating artifacts", XForm.FieldType.TEXT );
78  		mainForm.addTextField( BASE_URI, "Base URI to use", XForm.FieldType.URL );
79  		mainForm.addCheckBox( OVERWRITE, null );
80  		mainForm.addCheckBox( EXPLICIT_ANNOTATION, null );
81  		mainForm.addCheckBox( SERVER_STUBS, null );
82  		
83        buildArgsForm( builder, true, "WsGen");
84        
85  		return builder.buildDialog( buildDefaultActions(HelpUrls.XFIRE_HELP_URL),
86        		"Specify arguments for XFire 1.X WsGen", UISupport.TOOL_ICON );
87  	}
88     
89  	protected StringToStringMap initValues()
90  	{
91  		StringToStringMap values = super.initValues();
92  		values.putIfMissing( BINDING, "jaxb" );
93  		return values;
94  	}
95  
96  	protected void generate(StringToStringMap values, ToolHost toolHost) throws Exception
97  	{
98  		String xfireDir = SoapUI.getSettings().getString( ToolsSettings.XFIRE_LOCATION, null );
99  		if( Tools.isEmpty( xfireDir ))
100 		{
101 			UISupport.showErrorMessage( "XFire 1.X directory must be set in global preferences" );
102 			return;
103 		}
104 		
105       String antDir = ToolsSupport.getToolLocator().getAntDir(true);
106       if( Tools.isEmpty( antDir ))
107 		{
108 			UISupport.showErrorMessage( "ANT directory must be set in global preferences" );
109 			return;
110 		}
111 		
112 		ProcessBuilder builder = new ProcessBuilder();
113 		ArgumentBuilder args = buildArgs( buildClasspath( xfireDir, antDir, values.get( CLASSPATH ) ) );
114 		builder.command(args.getArgs());
115 		builder.directory(new File(xfireDir));
116 		
117 		toolHost.run( new ProcessToolRunner( builder, "XFire 1.X WsGen", modelItem ));
118 	}
119    
120 	private String buildClasspath(String xfireDir, String antDir, String additional )
121 	{
122 		String libDir = xfireDir + File.separatorChar + "lib";
123 		String [] xfireLibs = new File( libDir ).list( new FilenameFilter() {
124 
125 			public boolean accept(File dir, String name)
126 			{
127 				if( !name.endsWith( ".jar"))
128 					return false;
129 				
130 				if( name.startsWith( "jaxb"))
131 					return name.indexOf( "2.0") > 0;
132 					
133 				return true;	
134 			}});
135 		
136 		String modulesDir = xfireDir + File.separatorChar + "modules";
137 		String [] xfireJars = new File( modulesDir ).list( new FilenameFilter() {
138 
139 			public boolean accept(File dir, String name)
140 			{
141 				return name.endsWith( ".jar");
142 			}});
143 		
144 		StringBuilder classpath = new StringBuilder();
145 		
146 		for( int c = 0; c < xfireLibs.length; c++ )
147 		{
148 			if( c > 0 )
149 				classpath.append( File.pathSeparatorChar );
150 			
151 			classpath.append( libDir + File.separatorChar + xfireLibs[c] );
152 		}
153 		
154 		for( int c = 0; c < xfireJars.length; c++ )
155 		{
156 			classpath.append( File.pathSeparatorChar );
157 			classpath.append( modulesDir ).append( File.separatorChar ).append( xfireJars[c] );
158 		}
159 		
160 		classpath.append( File.pathSeparatorChar );
161 		classpath.append( antDir ).append( File.separatorChar ).append( "lib" ).
162 			append( File.separatorChar ).append( "ant.jar" );
163 		
164 		if( additional != null && additional.trim().length() > 0 )
165 			classpath.append( File.pathSeparatorChar ).append( additional.trim() );
166 		
167 		return classpath.toString();
168 	}
169 
170 	private ArgumentBuilder buildArgs( String classpath ) throws IOException
171 	{
172 		StringToStringMap values = dialog.getValues();
173 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
174 		
175 		ArgumentBuilder builder = new ArgumentBuilder( values );
176 		
177 		builder.addArgs( "java" );
178 		addJavaArgs( values, builder );
179 		
180 		builder.addArgs( "-cp", classpath, "org.codehaus.xfire.gen.WsGen" );
181 		builder.addArgs( "-wsdl", getWsdlUrl( values ) );
182 		builder.addString( OUTPUT, "-o" );
183 		builder.addString( PACKAGE, "-p" );
184 		builder.addString( BINDING, "-b" );
185 		builder.addString( EXTERNAL_BINDINGS, "-e" );
186 		builder.addString( PROFILE, "-r" );
187 		builder.addString( BASE_URI, "-u" );
188 		builder.addString( OVERWRITE, "-overwrite" );
189 		builder.addString( EXPLICIT_ANNOTATION, "-x" );
190 		builder.addString( SERVER_STUBS, "-ss" );
191 		addToolArgs( values, builder );
192 		return builder;
193 	}
194 }