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.jaxb;
14  
15  import java.io.File;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
19  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
20  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
21  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
22  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23  import com.eviware.soapui.model.iface.Interface;
24  import com.eviware.soapui.model.project.Project;
25  import com.eviware.soapui.settings.ToolsSettings;
26  import com.eviware.soapui.support.Tools;
27  import com.eviware.soapui.support.UISupport;
28  import com.eviware.soapui.support.types.StringToStringMap;
29  import com.eviware.x.form.XForm;
30  import com.eviware.x.form.XFormDialog;
31  import com.eviware.x.form.XFormDialogBuilder;
32  import com.eviware.x.form.XFormFactory;
33  
34  /***
35   * Generates JAXB classes for given interface
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class JaxbXjcAction extends AbstractToolsAction<Interface>
41  {
42  	private final static String PACKAGE = "package";
43  	private final static String OUTPUT = "output";
44  	private final static String NOVALIDATION = "no validation";
45  	private final static String BINDINGS = "binding files";
46  	private final static String CLASSPATH = "classpath";
47  	private final static String CATALOG = "catalog";
48  	private final static String HTTPPROXY = "http proxy";
49  	private final static String READONLY = "read-only";
50  	private final static String NPA = "npa";
51  	private final static String VERBOSE = "verbose";
52  	public static final String SOAPUI_ACTION_ID = "JaxbXjcAction";
53  
54  	// Configure the behavior of this action:
55  	private String output = null;
56  
57  	public JaxbXjcAction()
58  	{
59  		super( "JAXB 2.0 Artifacts", "Generates JAXB artifacts" );
60  	}
61  
62  	@Override
63  	public boolean applies( Interface target )
64  	{
65  		Interface iface = ( Interface )target;
66  		return !iface.getProject().hasNature( Project.JBOSSWS_NATURE_ID );
67  	}
68  
69  	/***
70  	 * Set this to predefine the output directory instead of letting the user
71  	 * select.
72  	 */
73  	public void setOutput( String output )
74  	{
75  		this.output = output;
76  	}
77  
78  	protected StringToStringMap initValues( Interface modelItem, Object param )
79  	{
80  		StringToStringMap values = super.initValues( modelItem, param );
81  
82  		if( output != null )
83  			values.put( OUTPUT, output );
84  
85  		return values;
86  	}
87  
88  	protected XFormDialog buildDialog( Interface modelItem )
89  	{
90  		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "JAXB Artifacts" );
91  
92  		XForm mainForm = builder.createForm( "Basic" );
93  		addWSDLFields( mainForm, modelItem );
94  
95  		mainForm.addTextField( OUTPUT, "generated files will go into this directory", XForm.FieldType.PROJECT_FOLDER );
96  		mainForm.addTextField( PACKAGE, "the target package", XForm.FieldType.JAVA_PACKAGE );
97  
98  		mainForm.addTextField( BINDINGS, "external bindings file(s), comma-separated", XForm.FieldType.PROJECT_FILE );
99  		mainForm.addTextField( CATALOG, "catalog files to resolve external entity references",
100 				XForm.FieldType.PROJECT_FILE );
101 		mainForm.addTextField( CLASSPATH, "where to find user class files", XForm.FieldType.PROJECT_FOLDER );
102 
103 		mainForm.addTextField( HTTPPROXY, "set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost[:proxyPort]",
104 				XForm.FieldType.TEXT );
105 		mainForm.addCheckBox( READONLY, "(generated files will be in read-only mode)" );
106 		mainForm.addCheckBox( NOVALIDATION, "(do not resolve strict validation of the input schema(s))" );
107 		mainForm.addCheckBox( NPA, "(suppress generation of package level annotations (**/package-info.java))" );
108 
109 		mainForm.addCheckBox( VERBOSE, "(be extra verbose)" );
110 
111 		buildArgsForm( builder, false, "xjc" );
112 
113 		return builder.buildDialog( buildDefaultActions( HelpUrls.JABXJC_HELP_URL, modelItem ),
114 				"Specify arguments for the JAXB 2 xjc compiler", UISupport.TOOL_ICON );
115 	}
116 
117 	protected void generate( StringToStringMap values, ToolHost toolHost, Interface modelItem ) throws Exception
118 	{
119 		String jaxbDir = SoapUI.getSettings().getString( ToolsSettings.JAXB_LOCATION, null );
120 		if( Tools.isEmpty( jaxbDir ) )
121 		{
122 			UISupport.showErrorMessage( "JAXB location must be set in global preferences" );
123 			return;
124 		}
125 
126 		ProcessBuilder builder = new ProcessBuilder();
127 		ArgumentBuilder argumentBuilder = buildArgs( modelItem );
128 		builder.command( argumentBuilder.getArgs() );
129 		builder.directory( new File( jaxbDir + File.separatorChar + "bin" ) );
130 
131 		toolHost.run( new ProcessToolRunner( builder, "JAXB xjc", modelItem, argumentBuilder ) );
132 	}
133 
134 	private ArgumentBuilder buildArgs( Interface modelItem )
135 	{
136 		StringToStringMap values = dialog.getValues();
137 		ArgumentBuilder builder = new ArgumentBuilder( values );
138 
139 		builder.startScript( "xjc", ".bat", ".sh" );
140 
141 		String outputValue = ( output != null ? output : values.get( OUTPUT ) );
142 		values.put( OUTPUT, Tools.ensureDir( outputValue, "" ) );
143 
144 		builder.addString( OUTPUT, "-d" );
145 		builder.addString( PACKAGE, "-p" );
146 		builder.addString( CLASSPATH, "-classpath" );
147 		builder.addString( CATALOG, "-catalog" );
148 		builder.addString( HTTPPROXY, "-httpproxy " );
149 
150 		builder.addBoolean( NOVALIDATION, "-nv" );
151 		builder.addBoolean( NPA, "-npa" );
152 		builder.addBoolean( READONLY, "-readOnly" );
153 		builder.addBoolean( VERBOSE, "-verbose" );
154 
155 		addToolArgs( values, builder );
156 
157 		builder.addArgs( "-wsdl", getWsdlUrl( values, modelItem ) );
158 
159 		String[] bindings = values.get( BINDINGS ).split( "," );
160 		for( String binding : bindings )
161 		{
162 			if( binding.trim().length() > 0 )
163 				builder.addArgs( "-b", binding.trim() );
164 		}
165 
166 		return builder;
167 	}
168 }