View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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( true, "Package is required" );
    72  		XFormField bindingCombo = mainForm.addComboBox( BINDING, new String[] {"jaxb","xmlbeans"}, "Binding framework to use" );
    73
    74  		XFormTextField cpField = mainForm.addTextField( CLASSPATH, "Classpath to generated xmlbeans for binding", XForm.FieldType.PROJECT_FILE );
    75  		XFormTextField extBindingsField = mainForm.addTextField( EXTERNAL_BINDINGS, "External jaxb binding file(s)", XForm.FieldType.PROJECT_FILE );
    76        bindingCombo.addComponentEnabler(cpField, "xmlbeans");
    77        bindingCombo.addComponentEnabler(extBindingsField, "jaxb");
    78
    79  		mainForm.addTextField( PROFILE, "Profile to use for generating artifacts", XForm.FieldType.TEXT );
    80  		mainForm.addTextField( BASE_URI, "Base URI to use", XForm.FieldType.URL );
    81  		mainForm.addCheckBox( OVERWRITE, null );
    82  		mainForm.addCheckBox( EXPLICIT_ANNOTATION, null );
    83  		mainForm.addCheckBox( SERVER_STUBS, null );
    84
    85        buildArgsForm( builder, true, "WsGen");
    86
    87  		return builder.buildDialog( buildDefaultActions(HelpUrls.XFIRE_HELP_URL, modelItem),
    88        		"Specify arguments for XFire 1.X WsGen", UISupport.TOOL_ICON );
    89  	}
    90
    91  	protected StringToStringMap initValues(Interface modelItem, Object param)
    92  	{
    93  		StringToStringMap values = super.initValues(modelItem, param);
    94  		values.putIfMissing( BINDING, "jaxb" );
    95  		return values;
    96  	}
    97
    98  	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem) throws Exception
    99  	{
    100 		String xfireDir = SoapUI.getSettings().getString( ToolsSettings.XFIRE_LOCATION, null );
    101 		if( Tools.isEmpty( xfireDir ))
    102 		{
    103 			UISupport.showErrorMessage( "XFire 1.X directory must be set in global preferences" );
    104 			return;
    105 		}
    106
    107       String antDir = ToolsSupport.getToolLocator().getAntDir(true);
    108       if( Tools.isEmpty( antDir ))
    109 		{
    110 			UISupport.showErrorMessage( "ANT directory must be set in global preferences" );
    111 			return;
    112 		}
    113
    114 		ProcessBuilder builder = new ProcessBuilder();
    115 		ArgumentBuilder args = buildArgs( buildClasspath( xfireDir, antDir, values.get( CLASSPATH ) ), modelItem );
    116 		builder.command(args.getArgs());
    117 		builder.directory(new File(xfireDir));
    118
    119 		toolHost.run( new ProcessToolRunner( builder, "XFire 1.X WsGen", modelItem ));
    120 	}
    121
    122 	private String buildClasspath(String xfireDir, String antDir, String additional )
    123 	{
    124 		String libDir = xfireDir + File.separatorChar + "lib";
    125 		String [] xfireLibs = new File( libDir ).list( new FilenameFilter() {
    126
    127 			public boolean accept(File dir, String name)
    128 			{
    129 				if( !name.endsWith( ".jar"))
    130 					return false;
    131
    132 				if( name.startsWith( "jaxb"))
    133 					return name.indexOf( "2.0") > 0;
    134
    135 				return true;
    136 			}});
    137
    138 		String modulesDir = xfireDir + File.separatorChar + "modules";
    139 		String [] xfireJars = new File( modulesDir ).list( new FilenameFilter() {
    140
    141 			public boolean accept(File dir, String name)
    142 			{
    143 				return name.endsWith( ".jar");
    144 			}});
    145
    146 		StringBuilder classpath = new StringBuilder();
    147
    148 		for( int c = 0; c < xfireLibs.length; c++ )
    149 		{
    150 			if( c > 0 )
    151 				classpath.append( File.pathSeparatorChar );
    152
    153 			classpath.append( libDir + File.separatorChar + xfireLibs[c] );
    154 		}
    155
    156 		for( int c = 0; c < xfireJars.length; c++ )
    157 		{
    158 			classpath.append( File.pathSeparatorChar );
    159 			classpath.append( modulesDir ).append( File.separatorChar ).append( xfireJars[c] );
    160 		}
    161
    162 		classpath.append( File.pathSeparatorChar );
    163 		classpath.append( antDir ).append( File.separatorChar ).append( "lib" ).
    164 			append( File.separatorChar ).append( "ant.jar" );
    165
    166 		if( additional != null && additional.trim().length() > 0 )
    167 			classpath.append( File.pathSeparatorChar ).append( additional.trim() );
    168
    169 		return classpath.toString();
    170 	}
    171
    172 	private ArgumentBuilder buildArgs( String classpath, Interface modelItem ) throws IOException
    173 	{
    174 		StringToStringMap values = dialog.getValues();
    175 		values.put( OUTPUT, Tools.ensureDir( values.get( OUTPUT ), "" ));
    176
    177 		ArgumentBuilder builder = new ArgumentBuilder( values );
    178
    179 		builder.addArgs( "java" );
    180 		addJavaArgs( values, builder );
    181
    182 		builder.addArgs( "-cp", classpath, "org.codehaus.xfire.gen.WsGen" );
    183 		builder.addArgs( "-wsdl", getWsdlUrl( values, modelItem ) );
    184 		builder.addString( OUTPUT, "-o" );
    185 		builder.addString( PACKAGE, "-p" );
    186 		builder.addString( BINDING, "-b" );
    187 		builder.addString( EXTERNAL_BINDINGS, "-e" );
    188 		builder.addString( PROFILE, "-r" );
    189 		builder.addString( BASE_URI, "-u" );
    190 		builder.addString( OVERWRITE, "-overwrite" );
    191 		builder.addString( EXPLICIT_ANNOTATION, "-x" );
    192 		builder.addString( SERVER_STUBS, "-ss" );
    193 		addToolArgs( values, builder );
    194 		return builder;
    195 	}
    196 }