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.tools;
14  
15  import java.io.BufferedReader;
16  import java.io.BufferedWriter;
17  import java.io.File;
18  import java.io.FileFilter;
19  import java.io.FileOutputStream;
20  import java.io.FilenameFilter;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.OutputStreamWriter;
24  import java.net.URL;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  
28  import org.apache.log4j.Logger;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.support.StringUtils;
32  import com.eviware.soapui.support.Tools;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.x.dialogs.XProgressDialog;
35  import com.eviware.x.dialogs.XProgressMonitor;
36  import com.eviware.x.dialogs.Worker.WorkerAdapter;
37  
38  public class MockAsWar
39  {
40  	protected static final String SOAPUI_SETTINGS = "[soapUISettings]";
41  	protected static final String PROJECT_FILE_NAME = "[ProjectFileName]";
42  	protected static final String MOCKSERVICE_ENDPOINT = "[mockServiceEndpoint]";
43  
44  	protected File projectFile;
45  	protected File settingsFile;
46  	protected File warDir;
47  	private File warFile;
48  	protected File webInf;
49  	private File lib;
50  	private File soapuiDir;
51  
52  	protected Logger log = Logger.getLogger( MockAsWar.class );
53  	private boolean includeExt;
54  	protected boolean includeActions;
55  	protected boolean includeListeners;
56  	private File actionsDir;
57  	private File listenersDir;
58  	protected final String localEndpoint;
59  	protected boolean enableWebUI;
60  
61  	public MockAsWar( String projectPath, String settingsPath, String warDir, String warFile, boolean includeExt,
62  			boolean actions, boolean listeners, String localEndpoint, boolean enableWebUI )
63  	{
64  		this.localEndpoint = localEndpoint;
65  		this.projectFile = new File( projectPath );
66  		this.settingsFile = StringUtils.hasContent( settingsPath ) ? new File( settingsPath ) : null;
67  		this.warDir = warDir.length() > 0 ? new File( warDir ) : new File( System.getProperty( "java.io.tmpdir" ),
68  				"warasmock" );
69  		if( !this.warDir.exists() )
70  		{
71  			this.warDir.mkdir();
72  		}
73  		this.warFile = warFile.length() == 0 ? null : new File( warFile );
74  		this.includeExt = includeExt;
75  		this.includeActions = actions;
76  		this.includeListeners = listeners;
77  		this.enableWebUI = enableWebUI;
78  	}
79  
80  	public void createMockAsWarArchive()
81  	{
82  		XProgressDialog progressDialog = UISupport.getDialogs().createProgressDialog( "Creating War File", 3,
83  				"Building war file..", false );
84  		WorkerAdapter warWorker = new WorkerAdapter()
85  		{
86  
87  			public Object construct( XProgressMonitor monitor )
88  			{
89  				if( prepareWarFile() )
90  				{
91  					createWebXml();
92  
93  					if( warFile != null )
94  					{
95  						ArrayList<File> files = getAllFilesFrom( webInf );
96  						files.add( new File( warDir, "stylesheet.css" ) );
97  						files.add( new File( warDir, "header_logo.jpg" ) );
98  
99  						File[] filez = files.toArray( new File[files.size()] );
100 						JarPackager.createJarArchive( warFile, warDir, filez );
101 					}
102 				}
103 				return null;
104 			}
105 		};
106 		try
107 		{
108 			progressDialog.run( warWorker );
109 		}
110 		catch( Exception e )
111 		{
112 			log.error( e.getMessage(), e );
113 		}
114 
115 	}
116 
117 	private ArrayList<File> getAllFilesFrom( File dir )
118 	{
119 		ArrayList<File> result = new ArrayList<File>();
120 		if( dir.isDirectory() )
121 		{
122 			result.addAll( Arrays.asList( dir.listFiles() ) );
123 			ArrayList<File> toAdd = new ArrayList<File>();
124 			for( File f : result )
125 			{
126 				if( f.isDirectory() )
127 					toAdd.addAll( getAllFilesFrom( f ) );
128 			}
129 			result.addAll( toAdd );
130 		}
131 		return result;
132 	}
133 
134 	protected void createWebXml()
135 	{
136 		URL url = SoapUI.class.getResource( "/com/eviware/soapui/resources/mockaswar/web.xml" );
137 		try
138 		{
139 			BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream() ) );
140 			String inputLine;
141 			StringBuilder content = new StringBuilder();
142 
143 			while( ( inputLine = in.readLine() ) != null )
144 				content.append( inputLine + "\n" );
145 
146 			createContent( content );
147 
148 			BufferedWriter out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File( webInf,
149 					"web.xml" ) ) ) );
150 			out.write( content.toString() );
151 			out.flush();
152 			out.close();
153 		}
154 		catch( IOException e )
155 		{
156 			log.error( e.getMessage(), e );
157 		}
158 	}
159 
160 	protected void createContent( StringBuilder content )
161 	{
162 		content.replace( content.indexOf( PROJECT_FILE_NAME ), content.indexOf( PROJECT_FILE_NAME )
163 				+ PROJECT_FILE_NAME.length(), projectFile.getName() );
164 
165 		content.replace( content.indexOf( SOAPUI_SETTINGS ), content.indexOf( SOAPUI_SETTINGS )
166 				+ SOAPUI_SETTINGS.length(),
167 				settingsFile != null && settingsFile.exists() && settingsFile.isFile() ? "WEB-INF/soapui/"
168 						+ settingsFile.getName() : "");//settingsFile.getAbsolutePath() );
169 		content.replace( content.indexOf( MOCKSERVICE_ENDPOINT ), content.indexOf( MOCKSERVICE_ENDPOINT )
170 				+ MOCKSERVICE_ENDPOINT.length(), localEndpoint );
171 
172 		if( !includeActions )
173 			content.replace( content.indexOf( "WEB-INF/actions" ), content.indexOf( "WEB-INF/actions" )
174 					+ "WEB-INF/actions".length(), "" );
175 		if( !includeListeners )
176 			content.replace( content.indexOf( "WEB-INF/listeners" ), content.indexOf( "WEB-INF/listeners" )
177 					+ "WEB-INF/listeners".length(), "" );
178 		if( !enableWebUI )
179 			content.replace( content.indexOf( "<param-value>true</param-value>" ), content
180 					.indexOf( "<param-value>true</param-value>" )
181 					+ "<param-value>true</param-value>".length(), "<param-value>false</param-value>" );
182 	}
183 
184 	protected boolean prepareWarFile()
185 	{
186 		// create file system first
187 		if( createWarFileSystem() )
188 		{
189 			// copy all from bin/../lib to soapui.home/war/WEB-INF/lib/
190 			File fromDir = new File( System.getProperty( "soapui.home" ), ".." + File.separator + "lib" );
191 			JarPackager.copyAllFromTo( fromDir, lib, new FileFilter()
192 			{
193 
194 				public boolean accept( File pathname )
195 				{
196 					return pathname.getName().indexOf( "servlet" ) == -1;
197 				}
198 			} );
199 
200 			if( includeExt )
201 			{
202 				// copy all from bin/ext to soapui.home/war/WEB-INF/lib/
203 				fromDir = new File( System.getProperty( "soapui.home" ), "ext" );
204 				JarPackager.copyAllFromTo( fromDir, lib, null );
205 			}
206 
207 			// copy soapui jar to soapui.home/war/WEB-INF/lib/
208 			File soapUIHome = new File( System.getProperty( "soapui.home" ) );
209 			String[] mainJar = soapUIHome.list( new FilenameFilter()
210 			{
211 				public boolean accept( File dir, String name )
212 				{
213 					if( name.toLowerCase().startsWith( "soapui" ) && name.toLowerCase().endsWith( ".jar" ) )
214 						return true;
215 					return false;
216 				}
217 			} );
218 
219 			fromDir = new File( System.getProperty( "soapui.home" ), mainJar[0] );
220 			JarPackager.copyFileToDir( fromDir, lib );
221 			// copy project and settings file to bin/war/WEB-INF/soapui/
222 			JarPackager.copyFileToDir( projectFile, soapuiDir );
223 			if( settingsFile != null && settingsFile.exists() && settingsFile.isFile() )
224 				JarPackager.copyFileToDir( settingsFile, soapuiDir );
225 
226 			// actions
227 			if( includeActions )
228 			{
229 				fromDir = new File( System.getProperty( "soapui.ext.actions" ) );
230 				JarPackager.copyAllFromTo( fromDir, actionsDir, null );
231 			}
232 			// listeners
233 			if( includeListeners )
234 			{
235 				fromDir = new File( System.getProperty( "soapui.ext.listeners" ) );
236 				JarPackager.copyAllFromTo( fromDir, listenersDir, null );
237 			}
238 
239 			copyWarResource( "header_logo.jpg" );
240 			copyWarResource( "stylesheet.css" );
241 
242 			return true;
243 		}
244 		return false;
245 	}
246 
247 	private void copyWarResource( String resource )
248 	{
249 		try
250 		{
251 			Tools.writeAll( new FileOutputStream( new File( warDir, resource ) ), SoapUI.class
252 					.getResourceAsStream( "/com/eviware/soapui/resources/mockaswar/" + resource ) );
253 		}
254 		catch( Exception e )
255 		{
256 			e.printStackTrace();
257 		}
258 	}
259 
260 	protected boolean createWarFileSystem()
261 	{
262 		if( warDir.isDirectory() )
263 		{
264 			webInf = new File( warDir, "WEB-INF" );
265 			if( !( webInf.mkdir() || webInf.exists() ) )
266 			{
267 				UISupport.showErrorMessage( "Could not create directory " + webInf.getAbsolutePath() );
268 				return false;
269 			}
270 			else
271 			{
272 				clearDir( webInf );
273 				lib = new File( webInf, "lib" );
274 				if( !( lib.mkdir() || lib.exists() ) )
275 				{
276 					UISupport.showErrorMessage( "Could not create directory " + lib.getAbsolutePath() );
277 					return false;
278 				}
279 				soapuiDir = new File( webInf, "soapui" );
280 				if( !( soapuiDir.mkdir() || soapuiDir.exists() ) )
281 				{
282 					UISupport.showErrorMessage( "Could not create directory " + soapuiDir.getAbsolutePath() );
283 					return false;
284 				}
285 				clearDir( soapuiDir );
286 
287 				if( includeActions )
288 				{
289 					actionsDir = new File( webInf, "actions" );
290 					if( !( actionsDir.mkdirs() || actionsDir.exists() ) )
291 					{
292 						UISupport.showErrorMessage( "Could not create directory " + actionsDir.getAbsolutePath() );
293 						return false;
294 					}
295 					clearDir( actionsDir );
296 				}
297 				if( includeListeners )
298 				{
299 					listenersDir = new File( webInf, "listeners" );
300 					if( !( listenersDir.mkdirs() || listenersDir.exists() ) )
301 					{
302 						UISupport.showErrorMessage( "Could not create directory " + listenersDir.getAbsolutePath() );
303 						return false;
304 					}
305 					clearDir( listenersDir );
306 				}
307 				return true;
308 			}
309 		}
310 		else
311 		{
312 			UISupport.showErrorMessage( warDir.getName() + " need to be directory!" );
313 			return false;
314 		}
315 	}
316 
317 	/***
318 	 * Deletes all files, just files, in directory
319 	 * 
320 	 * @param dir
321 	 */
322 	protected void clearDir( File dir )
323 	{
324 		for( File file : dir.listFiles() )
325 			if( file.isFile() )
326 				file.delete();
327 	}
328 
329 }