1
2
3
4
5
6
7
8
9
10
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 File[] filez = getAllFilesFrom( webInf ).toArray( new File[0] );
96 JarPackager.createJarArchive( warFile, warDir, filez );
97 }
98 }
99 return null;
100 }
101 };
102 try
103 {
104 progressDialog.run( warWorker );
105 }
106 catch( Exception e )
107 {
108 log.error( e.getMessage(), e );
109 }
110
111 }
112
113 private ArrayList<File> getAllFilesFrom( File dir )
114 {
115 ArrayList<File> result = new ArrayList<File>();
116 if( dir.isDirectory() )
117 {
118 result.addAll( Arrays.asList( dir.listFiles() ) );
119 ArrayList<File> toAdd = new ArrayList<File>();
120 for( File f : result )
121 {
122 if( f.isDirectory() )
123 toAdd.addAll( getAllFilesFrom( f ) );
124 }
125 result.addAll( toAdd );
126 }
127 return result;
128 }
129
130 protected void createWebXml()
131 {
132 URL url = SoapUI.class.getResource( "/com/eviware/soapui/resources/mockaswar/web.xml" );
133 try
134 {
135 BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream() ) );
136 String inputLine;
137 StringBuilder content = new StringBuilder();
138
139 while( ( inputLine = in.readLine() ) != null )
140 content.append( inputLine + "\n" );
141
142 createContent( content );
143
144 BufferedWriter out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File( webInf,
145 "web.xml" ) ) ) );
146 out.write( content.toString() );
147 out.flush();
148 out.close();
149 }
150 catch( IOException e )
151 {
152 log.error( e.getMessage(), e );
153 }
154 }
155
156 protected void createContent( StringBuilder content )
157 {
158 content.replace( content.indexOf( PROJECT_FILE_NAME ), content.indexOf( PROJECT_FILE_NAME )
159 + PROJECT_FILE_NAME.length(), projectFile.getName() );
160 content.replace( content.indexOf( SOAPUI_SETTINGS ), content.indexOf( SOAPUI_SETTINGS )
161 + SOAPUI_SETTINGS.length(),
162 settingsFile != null && settingsFile.exists() && settingsFile.isFile() ? settingsFile.getAbsolutePath()
163 : "" );
164 content.replace( content.indexOf( MOCKSERVICE_ENDPOINT ), content.indexOf( MOCKSERVICE_ENDPOINT )
165 + MOCKSERVICE_ENDPOINT.length(), localEndpoint );
166
167 if( !includeActions )
168 content.replace( content.indexOf( "WEB-INF/actions" ), content.indexOf( "WEB-INF/actions" )
169 + "WEB-INF/actions".length(), "" );
170 if( !includeListeners )
171 content.replace( content.indexOf( "WEB-INF/listeners" ), content.indexOf( "WEB-INF/listeners" )
172 + "WEB-INF/listeners".length(), "" );
173 if( !enableWebUI )
174 content.replace( content.indexOf( "<param-value>true</param-value>" ), content
175 .indexOf( "<param-value>true</param-value>" )
176 + "<param-value>true</param-value>".length(), "<param-value>false</param-value>" );
177 }
178
179 protected boolean prepareWarFile()
180 {
181
182 if( createWarFileSystem() )
183 {
184
185 File fromDir = new File( System.getProperty( "soapui.home" ), ".." + File.separator + "lib" );
186 JarPackager.copyAllFromTo( fromDir, lib, new FileFilter()
187 {
188
189 public boolean accept( File pathname )
190 {
191 return pathname.getName().indexOf( "servlet" ) == -1;
192 }
193 } );
194
195 if( includeExt )
196 {
197
198 fromDir = new File( System.getProperty( "soapui.home" ), "ext" );
199 JarPackager.copyAllFromTo( fromDir, lib, null );
200 }
201
202
203 File soapUIHome = new File( System.getProperty( "soapui.home" ) );
204 String[] mainJar = soapUIHome.list( new FilenameFilter()
205 {
206
207 public boolean accept( File dir, String name )
208 {
209 if( name.toLowerCase().startsWith( "soapui" ) && name.toLowerCase().endsWith( ".jar" ) )
210 return true;
211 return false;
212 }
213 } );
214 fromDir = new File( System.getProperty( "soapui.home" ), mainJar[0] );
215 JarPackager.copyFileToDir( fromDir, lib );
216
217 JarPackager.copyFileToDir( projectFile, soapuiDir );
218 if( settingsFile != null && settingsFile.exists() && settingsFile.isFile() )
219 JarPackager.copyFileToDir( settingsFile, soapuiDir );
220
221
222 if( includeActions )
223 {
224 fromDir = new File( System.getProperty( "soapui.ext.actions" ) );
225 JarPackager.copyAllFromTo( fromDir, actionsDir, null );
226 }
227
228 if( includeListeners )
229 {
230 fromDir = new File( System.getProperty( "soapui.ext.listeners" ) );
231 JarPackager.copyAllFromTo( fromDir, listenersDir, null );
232 }
233
234 copyWarResource( "header_logo.jpg" );
235 copyWarResource( "stylesheet.css" );
236
237 return true;
238 }
239 return false;
240 }
241
242 private void copyWarResource( String resource )
243 {
244 try
245 {
246 Tools.writeAll( new FileOutputStream( new File( warDir, resource ) ), SoapUI.class
247 .getResourceAsStream( "/com/eviware/soapui/resources/mockaswar/" + resource ) );
248 }
249 catch( Exception e )
250 {
251 e.printStackTrace();
252 }
253 }
254
255 protected boolean createWarFileSystem()
256 {
257 if( warDir.isDirectory() )
258 {
259 webInf = new File( warDir, "WEB-INF" );
260 if( !( webInf.mkdir() || webInf.exists() ) )
261 {
262 UISupport.showErrorMessage( "Could not create directory " + webInf.getAbsolutePath() );
263 return false;
264 }
265 else
266 {
267 clearDir(webInf);
268 lib = new File( webInf, "lib" );
269 if( !( lib.mkdir() || lib.exists() ) )
270 {
271 UISupport.showErrorMessage( "Could not create directory " + lib.getAbsolutePath() );
272 return false;
273 }
274 soapuiDir = new File( webInf, "soapui" );
275 if( !( soapuiDir.mkdir() || soapuiDir.exists() ) )
276 {
277 UISupport.showErrorMessage( "Could not create directory " + soapuiDir.getAbsolutePath() );
278 return false;
279 }
280 clearDir( soapuiDir );
281
282 if( includeActions )
283 {
284 actionsDir = new File( webInf, "actions" );
285 if( !( actionsDir.mkdirs() || actionsDir.exists() ) )
286 {
287 UISupport.showErrorMessage( "Could not create directory " + actionsDir.getAbsolutePath() );
288 return false;
289 }
290 clearDir( actionsDir );
291 }
292 if( includeListeners )
293 {
294 listenersDir = new File( webInf, "listeners" );
295 if( !( listenersDir.mkdirs() || listenersDir.exists() ) )
296 {
297 UISupport.showErrorMessage( "Could not create directory " + listenersDir.getAbsolutePath() );
298 return false;
299 }
300 clearDir( listenersDir );
301 }
302 return true;
303 }
304 }
305 else
306 {
307 UISupport.showErrorMessage( warDir.getName() + " need to be directory!" );
308 return false;
309 }
310 }
311
312 /***
313 * Deletes all files, just files, in directory
314 *
315 * @param dir
316 */
317 protected void clearDir( File dir )
318 {
319 for( File file : dir.listFiles())
320 if( file.isFile())
321 file.delete();
322 }
323
324 }