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.actions;
14  
15  import java.io.IOException;
16  
17  import org.apache.log4j.Logger;
18  
19  import com.eviware.soapui.DefaultSoapUICore;
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.impl.wsdl.WsdlProject;
22  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23  import com.eviware.soapui.settings.ProjectSettings;
24  import com.eviware.soapui.support.UISupport;
25  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
26  import com.eviware.soapui.tools.MockAsWar;
27  import com.eviware.x.form.XFormDialog;
28  import com.eviware.x.form.XFormField;
29  import com.eviware.x.form.XFormFieldListener;
30  import com.eviware.x.form.support.ADialogBuilder;
31  import com.eviware.x.form.support.AField;
32  import com.eviware.x.form.support.AForm;
33  import com.eviware.x.form.support.AField.AFieldType;
34  import com.eviware.x.form.validators.RequiredValidator;
35  
36  public class MockAsWarAction extends AbstractSoapUIAction<WsdlProject>
37  {
38  	private XFormDialog dialog;
39  	private Logger log = Logger.getLogger( MockAsWarAction.class );
40  
41  	public MockAsWarAction()
42  	{
43  		super( "Deploy As War", "Deploys Project MockServices as a WAR file" );
44  	}
45  
46  	public void perform( WsdlProject project, Object param )
47  	{
48  		// check for mockservices
49  		if( project.getMockServiceCount() == 0 )
50  		{
51  			UISupport.showErrorMessage( "Project does not have any MockServices to deploy" );
52  			return;
53  		}
54  
55  		// create war file
56  		if( dialog == null )
57  		{
58  			dialog = ADialogBuilder.buildDialog( MockAsWarDialog.class );
59  			dialog.getFormField( MockAsWarDialog.GLOBAL_SETTINGS ).addFormFieldListener( new XFormFieldListener()
60  			{
61  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
62  				{
63  					dialog.getFormField( MockAsWarDialog.SETTINGS_FILE ).setEnabled( Boolean.valueOf( newValue ) );
64  				}
65  			} );
66  
67  			dialog.getFormField( MockAsWarDialog.WAR_DIRECTORY ).addFormFieldValidator(
68  					new RequiredValidator( "WAR Directory is required" ) );
69  		}
70  
71  		XFormField settingFile = dialog.getFormField( MockAsWarDialog.SETTINGS_FILE );
72  		settingFile.setValue( ( ( DefaultSoapUICore )SoapUI.getSoapUICore() ).getSettingsFile() );
73  		settingFile.setEnabled( dialog.getBooleanValue( MockAsWarDialog.GLOBAL_SETTINGS ) );
74  
75  		XFormField warDirectory = dialog.getFormField( MockAsWarDialog.WAR_DIRECTORY );
76  		XFormField warFile = dialog.getFormField( MockAsWarDialog.WAR_FILE );
77  
78  		String passwordForEncryption = project.getSettings().getString( ProjectSettings.SHADOW_PASSWORD, null );
79  		project.getSettings().setString( ProjectSettings.SHADOW_PASSWORD, null );
80  
81  		if( dialog.show() )
82  		{
83  			project.beforeSave();
84  			try
85  			{
86  				project.save();
87  			}
88  			catch( IOException e )
89  			{
90  				log.error( e.getMessage(), e );
91  			}
92  			finally
93  			{
94  				project.getSettings().setString( ProjectSettings.SHADOW_PASSWORD, passwordForEncryption );
95  			}
96  
97  			MockAsWar mockAsWar = new MockAsWar( project.getPath(), dialog
98  					.getBooleanValue( MockAsWarDialog.GLOBAL_SETTINGS ) ? settingFile.getValue() : "", warDirectory
99  					.getValue(), warFile.getValue(), dialog.getBooleanValue( MockAsWarDialog.EXT_LIBS ), dialog
100 					.getBooleanValue( MockAsWarDialog.ACTIONS ), dialog.getBooleanValue( MockAsWarDialog.LISTENERS ), dialog
101 					.getValue( MockAsWarDialog.MOCKSERVICE_ENDPOINT ), dialog.getBooleanValue( MockAsWarDialog.ENABLE_WEBUI ) );
102 			mockAsWar.createMockAsWarArchive();
103 		}
104 	}
105 
106 	@AForm( description = "Configure what to include in generated WAR", name = "Deploy Project as WAR", helpUrl = HelpUrls.MOCKASWAR_HELP_URL )
107 	protected interface MockAsWarDialog
108 	{
109 		@AField( description = "Specify if global settings should be included", name = "Include Global Settings", type = AFieldType.BOOLEAN )
110 		public final static String GLOBAL_SETTINGS = "Include Global Settings";
111 
112 		@AField( description = "Specify Settings File", name = "Settings", type = AFieldType.FILE )
113 		public final static String SETTINGS_FILE = "Settings";
114 
115 		@AField( description = "Specify if action extensions should be included", name = "Include Actions", type = AFieldType.BOOLEAN )
116 		public final static String ACTIONS = "Include Actions";
117 
118 		@AField( description = "Specify if listener extensions should be included", name = "Include Listeners", type = AFieldType.BOOLEAN )
119 		public final static String LISTENERS = "Include Listeners";
120 
121 		@AField( description = "Include jar files from ext folder", name = "Include External Jar Files", type = AFieldType.BOOLEAN )
122 		public final static String EXT_LIBS = "Include External Jar Files";
123 
124 		@AField( description = "Check to enable WebUI", name = "WebUI", type = AFieldType.BOOLEAN )
125 		public final static String ENABLE_WEBUI = "WebUI";
126 
127 		@AField( description = "Local endpoint that will be used for WSDL endpoints/includes/imports", name = "MockService Endpoint", type = AFieldType.STRING )
128 		public final static String MOCKSERVICE_ENDPOINT = "MockService Endpoint";
129 
130 		@AField( description = "Specify name of target War File", name = "War File", type = AFieldType.FILE )
131 		public final static String WAR_FILE = "War File";
132 
133 		@AField( description = "Specify a directory where War file structure will be created", name = "War Directory", type = AFieldType.FOLDER )
134 		public final static String WAR_DIRECTORY = "War Directory";
135 
136 	}
137 }