1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui;
14
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.net.URL;
18
19 import org.apache.log4j.Logger;
20 import org.apache.log4j.xml.DOMConfigurator;
21
22 import com.eviware.soapui.config.SoapuiSettingsDocumentConfig;
23 import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
24 import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
25 import com.eviware.soapui.model.settings.Settings;
26 import com.eviware.soapui.monitor.MockEngine;
27 import com.eviware.soapui.settings.HttpSettings;
28 import com.eviware.soapui.settings.UISettings;
29 import com.eviware.soapui.settings.WsdlSettings;
30 import com.eviware.soapui.support.ClasspathHacker;
31 import com.eviware.soapui.support.StringUtils;
32 import com.eviware.soapui.support.action.SoapUIActionRegistry;
33 import com.eviware.soapui.support.listener.SoapUIListenerRegistry;
34 import com.eviware.soapui.support.types.StringList;
35
36 /***
37 * Initializes core objects. Transform to a Spring "ApplicationContext"?
38 *
39 * @author ole.matzura
40 */
41
42 public class DefaultSoapUICore implements SoapUICore
43 {
44 public static Logger log;
45
46 private boolean logIsInitialized;
47 private String root;
48 private SoapuiSettingsDocumentConfig settingsDocument;
49 private MockEngine mockEngine;
50 private XmlBeansSettingsImpl settings;
51 private SoapUIListenerRegistry listenerRegistry;
52 private SoapUIActionRegistry actionRegistry;
53
54 private String settingsFile;
55
56 public static DefaultSoapUICore createDefault()
57 {
58 return new DefaultSoapUICore( null, DEFAULT_SETTINGS_FILE );
59 }
60
61 public DefaultSoapUICore()
62 {
63 }
64
65 public DefaultSoapUICore( String root )
66 {
67 this.root = root;
68 }
69
70 public DefaultSoapUICore( String root, String settingsFile )
71 {
72 this( root );
73 init( settingsFile );
74 }
75
76 public void init( String settingsFile )
77 {
78 SoapUI.setSoapUICore( this );
79
80 initLog();
81 loadExternalLibraries();
82 initSettings( settingsFile == null ? DEFAULT_SETTINGS_FILE : settingsFile );
83 initCoreComponents();
84 initExtensions( getExtensionClassLoader() );
85
86 SoapVersion.Soap11.equals( SoapVersion.Soap12 );
87 }
88
89 protected void initExtensions( ClassLoader extensionClassLoader )
90 {
91 addExternalListeners( root == null ? "listeners" : root + File.separatorChar + "listeners", extensionClassLoader );
92 }
93
94 protected ClassLoader getExtensionClassLoader()
95 {
96 return SoapUI.class.getClassLoader();
97 }
98
99 protected void initCoreComponents()
100 {
101 }
102
103 public String getRoot()
104 {
105 return root;
106 }
107
108 protected Settings initSettings( String fileName )
109 {
110 try
111 {
112 File settingsFile = new File( fileName );
113 if( !settingsFile.exists() )
114 {
115 if( settingsDocument == null )
116 {
117 log.info( "Creating new settings at [" + settingsFile.getAbsolutePath() + "]" );
118 settingsDocument = SoapuiSettingsDocumentConfig.Factory.newInstance();
119 }
120 }
121 else
122 {
123 settingsDocument = SoapuiSettingsDocumentConfig.Factory.parse( settingsFile );
124 log.info( "initialized soapui-settings from [" + settingsFile.getAbsolutePath() + "]" );
125 }
126 }
127 catch( Exception e )
128 {
129 log.warn( "Failed to load settings from [" + e.getMessage() + "], creating new" );
130 settingsDocument = SoapuiSettingsDocumentConfig.Factory.newInstance();
131 }
132
133 if( settingsDocument.getSoapuiSettings() == null )
134 {
135 settingsDocument.addNewSoapuiSettings();
136 settings = new XmlBeansSettingsImpl( null, null, settingsDocument.getSoapuiSettings() );
137
138 initDefaultSettings( settings );
139 }
140 else
141 {
142 settings = new XmlBeansSettingsImpl( null, null, settingsDocument.getSoapuiSettings() );
143 }
144
145 this.settingsFile = fileName;
146
147 if( !settings.isSet( WsdlSettings.EXCLUDED_TYPES ))
148 {
149 StringList list = new StringList();
150 list.add( "schema@http://www.w3.org/2001/XMLSchema");
151 settings.setString( WsdlSettings.EXCLUDED_TYPES, list.toXml() );
152 }
153
154 if( !settings.isSet( WsdlSettings.NAME_WITH_BINDING ))
155 {
156 settings.setBoolean( WsdlSettings.NAME_WITH_BINDING, true );
157 }
158
159 if( !settings.isSet( HttpSettings.MAX_CONNECTIONS_PER_HOST ))
160 {
161 settings.setLong( HttpSettings.MAX_CONNECTIONS_PER_HOST, 500 );
162 }
163
164 if( !settings.isSet( HttpSettings.MAX_TOTAL_CONNECTIONS ))
165 {
166 settings.setLong( HttpSettings.MAX_TOTAL_CONNECTIONS, 2000 );
167 }
168
169 return settings;
170 }
171
172
173
174
175 public void importSettings( File file ) throws Exception
176 {
177 if( file != null )
178 {
179 log.info( "Importing preferences from [" + file.getAbsolutePath() + "]" );
180 SoapuiSettingsDocumentConfig doc = SoapuiSettingsDocumentConfig.Factory.parse( file );
181 settings.setConfig( doc.getSoapuiSettings() );
182 }
183 }
184
185
186
187
188 public Settings getSettings()
189 {
190 if( settings == null )
191 {
192 initSettings( DEFAULT_SETTINGS_FILE );
193 }
194
195 return settings;
196 }
197
198 protected void initDefaultSettings(Settings settings2)
199 {
200 settings.setBoolean( WsdlSettings.CACHE_WSDLS, true );
201 settings.setBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES, true );
202
203 settings.setBoolean( HttpSettings.INCLUDE_REQUEST_IN_TIME_TAKEN, true );
204 settings.setBoolean( HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN, true );
205
206 settings.setString( UISettings.AUTO_SAVE_INTERVAL, "0" );
207 }
208
209
210
211
212 public String saveSettings() throws Exception
213 {
214 File file = new File( settingsFile );
215 settingsDocument.save( file );
216 log.info( "Settings saved to [" + file.getAbsolutePath() + "]" );
217 return file.getAbsolutePath();
218 }
219
220 public String getSettingsFile()
221 {
222 return settingsFile;
223 }
224
225 public void setSettingsFile( String settingsFile )
226 {
227 this.settingsFile = settingsFile;
228 }
229
230 protected void initLog()
231 {
232 if( !logIsInitialized )
233 {
234 File log4jconfig = root == null ? new File( "soapui-log4j.xml" ) : new File( new File( root ), "soapui-log4j.xml" );
235 if( log4jconfig.exists() )
236 {
237 System.out.println( "Configuring log4j from [" + log4jconfig.getAbsolutePath() + "]" );
238 DOMConfigurator.configureAndWatch( log4jconfig.getAbsolutePath(), 5000 );
239 }
240 else
241 {
242 URL log4jUrl = SoapUI.class.getResource( "/soapui-log4j.xml" );
243 System.out.println( "Configuring log4j from [" + log4jUrl + "]" );
244 DOMConfigurator.configure( log4jUrl );
245 }
246
247 logIsInitialized = true;
248
249 log = Logger.getLogger( DefaultSoapUICore.class );
250 }
251 }
252
253 protected void loadExternalLibraries()
254 {
255 try
256 {
257 File dir = StringUtils.isNullOrEmpty( root ) ? new File( "ext" ) : new File( new File( root ), "ext" );
258
259 if( dir.exists() && dir.isDirectory() )
260 {
261 File[] files = dir.listFiles();
262 for( File file : files )
263 {
264 if( file.getName().toLowerCase().endsWith( ".jar" ))
265 {
266 ClasspathHacker.addFile( file );
267 }
268 }
269 }
270 else
271 {
272 log.warn( "Missing folder [" + dir.getAbsolutePath() + "] for external libraries" );
273 }
274 }
275 catch( Exception e )
276 {
277 log.error( e.toString() );
278 }
279 }
280
281
282
283
284 public MockEngine getMockEngine()
285 {
286 if( mockEngine == null )
287 mockEngine = new MockEngine();
288
289 return mockEngine;
290 }
291
292
293
294
295 public SoapUIListenerRegistry getListenerRegistry()
296 {
297 if( listenerRegistry == null )
298 initListenerRegistry();
299
300 return listenerRegistry;
301 }
302
303 protected void initListenerRegistry()
304 {
305 listenerRegistry = new SoapUIListenerRegistry( DefaultSoapUICore.class.getResourceAsStream( "/soapui-listeners.xml" ));
306 }
307
308
309
310
311 public SoapUIActionRegistry getActionRegistry()
312 {
313 if( actionRegistry == null )
314 initActionRegistry();
315
316 return actionRegistry;
317 }
318
319 protected void initActionRegistry()
320 {
321 actionRegistry = new SoapUIActionRegistry( DefaultSoapUICore.class.getResourceAsStream( "/soapui-actions.xml" ));
322 }
323
324 protected void addExternalListeners( String folder, ClassLoader classLoader )
325 {
326 File[] actionFiles = new File( folder ).listFiles();
327 if( actionFiles != null )
328 {
329 for( File actionFile : actionFiles )
330 {
331 if( actionFile.isDirectory() )
332 {
333 addExternalListeners( actionFile.getAbsolutePath(), classLoader );
334 continue;
335 }
336
337 if( !actionFile.getName().toLowerCase().endsWith( "-listeners.xml" ))
338 continue;
339
340 try
341 {
342 log.info( "Adding listeners from [" + actionFile.getAbsolutePath() + "]" );
343
344 SoapUI.getListenerRegistry().addConfig( new FileInputStream( actionFile ),
345 classLoader);
346 }
347 catch( Exception e )
348 {
349 SoapUI.logError( e );
350 }
351 }
352 }
353 }
354 }