View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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;
14  
15  import com.eviware.soapui.config.SoapuiSettingsDocumentConfig;
16  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
17  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
18  import com.eviware.soapui.model.settings.Settings;
19  import com.eviware.soapui.monitor.MockEngine;
20  import com.eviware.soapui.settings.*;
21  import com.eviware.soapui.support.ClasspathHacker;
22  import com.eviware.soapui.support.StringUtils;
23  import com.eviware.soapui.support.Tools;
24  import com.eviware.soapui.support.action.SoapUIActionRegistry;
25  import com.eviware.soapui.support.listener.SoapUIListenerRegistry;
26  import com.eviware.soapui.support.types.StringList;
27  import org.apache.commons.ssl.OpenSSL;
28  import org.apache.log4j.Logger;
29  import org.apache.log4j.xml.DOMConfigurator;
30  
31  import javax.swing.*;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.IOException;
35  import java.io.UnsupportedEncodingException;
36  import java.net.URL;
37  import java.security.GeneralSecurityException;
38  
39  /***
40   * Initializes core objects. Transform to a Spring "ApplicationContext"?
41   * 
42   * @author ole.matzura
43   */
44  
45  public class DefaultSoapUICore implements SoapUICore
46  {
47  	public static Logger log;
48  
49  	private boolean logIsInitialized;
50  	private String root;
51  	protected SoapuiSettingsDocumentConfig settingsDocument;
52  	private MockEngine mockEngine;
53  	private XmlBeansSettingsImpl settings;
54  	private SoapUIListenerRegistry listenerRegistry;
55  	private SoapUIActionRegistry actionRegistry;
56  
57  	private String settingsFile;
58  
59  	private String password;
60  
61  	public static DefaultSoapUICore createDefault()
62  	{
63  		return new DefaultSoapUICore(null, DEFAULT_SETTINGS_FILE);
64  	}
65  
66  	public DefaultSoapUICore()
67  	{
68  	}
69  
70  	public DefaultSoapUICore(String root)
71  	{
72  		this.root = root;
73  	}
74  
75  	public DefaultSoapUICore(String root, String settingsFile)
76  	{
77  		this(root);
78  		init(settingsFile);
79  	}
80  
81  	public DefaultSoapUICore(String root, String settingsFile, String password)
82  	{
83  		this(root);
84  		this.password = password;
85  		init(settingsFile);
86  	}
87  
88  	public void init(String settingsFile)
89  	{
90  		initLog();
91  
92  		SoapUI.setSoapUICore(this);
93  
94  		loadExternalLibraries();
95  		initSettings(settingsFile == null ? DEFAULT_SETTINGS_FILE : settingsFile);
96  
97  		initCoreComponents();
98  		initExtensions(getExtensionClassLoader());
99  
100 		SoapVersion.Soap11.equals(SoapVersion.Soap12);
101 
102 	}
103 
104 	protected void initExtensions(ClassLoader extensionClassLoader)
105 	{
106 		String extDir = System.getProperty("soapui.ext.listeners");
107 		addExternalListeners(extDir != null ? extDir : root == null ? "listeners" : root + File.separatorChar
108 				+ "listeners", extensionClassLoader);
109 	}
110 
111 	protected ClassLoader getExtensionClassLoader()
112 	{
113 		return SoapUI.class.getClassLoader();
114 	}
115 
116 	protected void initCoreComponents()
117 	{
118 	}
119 
120 	public String getRoot()
121 	{
122 		if (root == null || root.length() == 0)
123 			root = System.getProperty("soapui.home");
124 		return root;
125 	}
126 
127 	protected Settings initSettings(String fileName)
128 	{
129 
130 		try
131 		{
132 			File settingsFile = root == null ? new File(System.getProperty("soapui.home"), fileName) : new File(new File(
133 					root), fileName);
134 			if (!settingsFile.exists())
135 			{
136 				if (settingsDocument == null)
137 				{
138 					log.info("Creating new settings at [" + settingsFile.getAbsolutePath() + "]");
139 					settingsDocument = SoapuiSettingsDocumentConfig.Factory.newInstance();
140 				}
141 			}
142 			else
143 			{
144 				settingsDocument = SoapuiSettingsDocumentConfig.Factory.parse(settingsFile);
145 
146 				byte[] encryptedContent = settingsDocument.getSoapuiSettings().getEncryptedContent();
147 				if (encryptedContent != null)
148 				{
149 					char[] password = null;
150 					if (this.password == null)
151 					{
152 						// swing element -!! uh!
153 						JPasswordField passwordField = new JPasswordField();
154 						JLabel qLabel = new JLabel("Password");
155 						JOptionPane.showConfirmDialog(null, new Object[] { qLabel, passwordField }, "Global Settings",
156 								JOptionPane.OK_CANCEL_OPTION);
157 						password = passwordField.getPassword();
158 					}
159 					else
160 					{
161 						password = this.password.toCharArray();
162 					}
163 
164 					byte[] data = OpenSSL.decrypt("des3", password, encryptedContent);
165 					try
166 					{
167 						settingsDocument = SoapuiSettingsDocumentConfig.Factory.parse(new String(data, "UTF-8"));
168 					}
169 					catch (Exception e)
170 					{
171 						log.warn("Wrong password.");
172 						JOptionPane.showMessageDialog(null, "Wrong password, creating backup settings file [ "
173 								+ settingsFile.getAbsolutePath() + ".bak.xml. ]\nSwitch to default settings.",
174 								"Error - Wrong Password", JOptionPane.ERROR_MESSAGE);
175 						settingsDocument.save(new File(settingsFile.getAbsolutePath() + ".bak.xml"));
176 						throw e;
177 					}
178 				}
179 
180 				log.info("initialized soapui-settings from [" + settingsFile.getAbsolutePath() + "]");
181 			}
182 		}
183 		catch (Exception e)
184 		{
185 			log.warn("Failed to load settings from [" + e.getMessage() + "], creating new");
186 			settingsDocument = SoapuiSettingsDocumentConfig.Factory.newInstance();
187 		}
188 
189 		if (settingsDocument.getSoapuiSettings() == null)
190 		{
191 			settingsDocument.addNewSoapuiSettings();
192 			settings = new XmlBeansSettingsImpl(null, null, settingsDocument.getSoapuiSettings());
193 
194 			initDefaultSettings(settings);
195 		}
196 		else
197 		{
198 			settings = new XmlBeansSettingsImpl(null, null, settingsDocument.getSoapuiSettings());
199 		}
200 
201 		this.settingsFile = fileName;
202 
203 		if (!settings.isSet(WsdlSettings.EXCLUDED_TYPES))
204 		{
205 			StringList list = new StringList();
206 			list.add("schema@http://www.w3.org/2001/XMLSchema");
207 			settings.setString(WsdlSettings.EXCLUDED_TYPES, list.toXml());
208 		}
209 
210 		if (!settings.isSet(WsdlSettings.NAME_WITH_BINDING))
211 		{
212 			settings.setBoolean(WsdlSettings.NAME_WITH_BINDING, true);
213 		}
214 
215 		if (!settings.isSet(HttpSettings.MAX_CONNECTIONS_PER_HOST))
216 		{
217 			settings.setLong(HttpSettings.MAX_CONNECTIONS_PER_HOST, 500);
218 		}
219 
220 		if (!settings.isSet(HttpSettings.MAX_TOTAL_CONNECTIONS))
221 		{
222 			settings.setLong(HttpSettings.MAX_TOTAL_CONNECTIONS, 2000);
223 		}
224 
225 		if (!settings.isSet(UISettings.AUTO_SAVE_PROJECTS_ON_EXIT))
226 		{
227 			settings.setBoolean(UISettings.AUTO_SAVE_PROJECTS_ON_EXIT, true);
228 		}
229 
230 		if (!settings.isSet(UISettings.SHOW_DESCRIPTIONS))
231 		{
232 			settings.setBoolean(UISettings.SHOW_DESCRIPTIONS, true);
233 		}
234 
235 		if (!settings.isSet(WsaSettings.USE_DEFAULT_RELATES_TO))
236 		{
237 			settings.setBoolean(WsaSettings.USE_DEFAULT_RELATES_TO, true);
238 		}
239 
240 		if (!settings.isSet(WsaSettings.USE_DEFAULT_RELATIONSHIP_TYPE))
241 		{
242 			settings.setBoolean(WsaSettings.USE_DEFAULT_RELATIONSHIP_TYPE, true);
243 		}
244 
245 		return settings;
246 	}
247 
248 	/*
249 	 * (non-Javadoc)
250 	 * 
251 	 * @see com.eviware.soapui.SoapUICore#importSettings(java.io.File)
252 	 */
253 	public void importSettings(File file) throws Exception
254 	{
255 		File toFile = null;
256 		if (file != null)
257 		{
258 			log.info("Importing preferences from [" + file.getAbsolutePath() + "]");
259 			toFile = new File(root == null ? System.getProperty("soapui.home") : root, file.getName());
260 			Tools.copyFile(file, toFile, true);
261 			initSettings(toFile.getName());
262 		}
263 	}
264 
265 	/*
266 	 * (non-Javadoc)
267 	 * 
268 	 * @see com.eviware.soapui.SoapUICore#getSettings()
269 	 */
270 	public Settings getSettings()
271 	{
272 		if (settings == null)
273 		{
274 			initSettings(DEFAULT_SETTINGS_FILE);
275 		}
276 
277 		return settings;
278 	}
279 
280 	protected void initDefaultSettings(Settings settings2)
281 	{
282 		settings.setBoolean(WsdlSettings.CACHE_WSDLS, true);
283 		settings.setBoolean(WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES, true);
284 
285 		settings.setBoolean(HttpSettings.INCLUDE_REQUEST_IN_TIME_TAKEN, true);
286 		settings.setBoolean(HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN, true);
287 
288 		settings.setString(UISettings.AUTO_SAVE_INTERVAL, "0");
289 		settings.setBoolean(UISettings.SHOW_STARTUP_PAGE, true);
290 
291 		settings.setBoolean(WsaSettings.SOAP_ACTION_OVERRIDES_WSA_ACTION, false);
292 		settings.setBoolean(WsaSettings.USE_DEFAULT_RELATIONSHIP_TYPE, true);
293 		settings.setBoolean(WsaSettings.USE_DEFAULT_RELATES_TO, true);
294 		settings.setBoolean(WsaSettings.OVERRIDE_EXISTING_HEADERS, false);
295 		settings.setBoolean(WsaSettings.ENABLE_FOR_OPTIONAL, false);
296 	}
297 
298 	/*
299 	 * (non-Javadoc)
300 	 * 
301 	 * @see com.eviware.soapui.SoapUICore#saveSettings()
302 	 */
303 	public String saveSettings() throws Exception
304 	{
305 		if (settingsFile == null)
306 			settingsFile = DEFAULT_SETTINGS_FILE;
307 
308 		File file = root == null ? new File(settingsFile) : new File(new File(root),
309 				settingsFile);
310 
311 		SoapuiSettingsDocumentConfig settingsDocument = (SoapuiSettingsDocumentConfig) this.settingsDocument.copy();
312 		String password = settings.getString(SecuritySettings.SHADOW_PASSWORD, null);
313 
314 		if (password != null && password.length() > 0)
315 		{
316 			try
317 			{
318 				byte[] data = settingsDocument.xmlText().getBytes();
319 				byte[] encryptedData = OpenSSL.encrypt("des3", password.toCharArray(), data);
320 				settingsDocument.setSoapuiSettings(null);
321 				settingsDocument.getSoapuiSettings().setEncryptedContent(encryptedData);
322 			}
323 			catch (UnsupportedEncodingException e)
324 			{
325 				log.error("Encryption error", e);
326 			}
327 			catch (IOException e)
328 			{
329 				log.error("Encryption error", e);
330 			}
331 			catch (GeneralSecurityException e)
332 			{
333 				log.error("Encryption error", e);
334 			}
335 		}
336 
337 		settingsDocument.save(file);
338 		log.info("Settings saved to [" + file.getAbsolutePath() + "]");
339 		return file.getAbsolutePath();
340 	}
341 
342 	public String getSettingsFile()
343 	{
344 		return settingsFile;
345 	}
346 
347 	public void setSettingsFile(String settingsFile)
348 	{
349 		this.settingsFile = settingsFile;
350 	}
351 
352 	protected void initLog()
353 	{
354 		if (!logIsInitialized)
355 		{
356 			File log4jconfig = root == null ? new File("soapui-log4j.xml") : new File(new File(root), "soapui-log4j.xml");
357 			if (log4jconfig.exists())
358 			{
359 				System.out.println("Configuring log4j from [" + log4jconfig.getAbsolutePath() + "]");
360 				DOMConfigurator.configureAndWatch(log4jconfig.getAbsolutePath(), 5000);
361 			}
362 			else
363 			{
364 				URL url = getClass().getResource("/com/eviware/soapui/resources/conf/soapui-log4j.xml");
365 				if (url != null)
366 				{
367 					DOMConfigurator.configure(url);
368 				}
369 				else
370 					System.err.println("Missing soapui-log4j.xml configuration");
371 			}
372 
373 			logIsInitialized = true;
374 
375 			log = Logger.getLogger(DefaultSoapUICore.class);
376 		}
377 	}
378 
379 	protected void loadExternalLibraries()
380 	{
381 		try
382 		{
383 			String extDir = System.getProperty("soapui.ext.libraries");
384 
385 			File dir = extDir != null ? new File(extDir) : StringUtils.isNullOrEmpty(root) ? new File("ext") : new File(
386 					new File(root), "ext");
387 
388 			if (dir.exists() && dir.isDirectory())
389 			{
390 				File[] files = dir.listFiles();
391 				for (File file : files)
392 				{
393 					if (file.getName().toLowerCase().endsWith(".jar"))
394 					{
395 						ClasspathHacker.addFile(file);
396 					}
397 				}
398 			}
399 			else
400 			{
401 				log.warn("Missing folder [" + dir.getAbsolutePath() + "] for external libraries");
402 			}
403 		}
404 		catch (Exception e)
405 		{
406 			log.error(e.toString());
407 		}
408 	}
409 
410 	/*
411 	 * (non-Javadoc)
412 	 * 
413 	 * @see com.eviware.soapui.SoapUICore#getMockEngine()
414 	 */
415 	public MockEngine getMockEngine()
416 	{
417 		if (mockEngine == null)
418 			mockEngine = new MockEngine();
419 
420 		return mockEngine;
421 	}
422 
423 	/*
424 	 * (non-Javadoc)
425 	 * 
426 	 * @see com.eviware.soapui.SoapUICore#getListenerRegistry()
427 	 */
428 	public SoapUIListenerRegistry getListenerRegistry()
429 	{
430 		if (listenerRegistry == null)
431 			initListenerRegistry();
432 
433 		return listenerRegistry;
434 	}
435 
436 	protected void initListenerRegistry()
437 	{
438 		listenerRegistry = new SoapUIListenerRegistry(null);
439 	}
440 
441 	/*
442 	 * (non-Javadoc)
443 	 * 
444 	 * @see com.eviware.soapui.SoapUICore#getActionRegistry()
445 	 */
446 	public SoapUIActionRegistry getActionRegistry()
447 	{
448 		if (actionRegistry == null)
449 			actionRegistry = initActionRegistry();
450 
451 		return actionRegistry;
452 	}
453 
454 	protected SoapUIActionRegistry initActionRegistry()
455 	{
456 		return new SoapUIActionRegistry(DefaultSoapUICore.class
457 				.getResourceAsStream("/com/eviware/soapui/resources/conf/soapui-actions.xml"));
458 	}
459 
460 	protected void addExternalListeners(String folder, ClassLoader classLoader)
461 	{
462 		File[] actionFiles = new File(folder).listFiles();
463 		if (actionFiles != null)
464 		{
465 			for (File actionFile : actionFiles)
466 			{
467 				if (actionFile.isDirectory())
468 				{
469 					addExternalListeners(actionFile.getAbsolutePath(), classLoader);
470 					continue;
471 				}
472 
473 				if (!actionFile.getName().toLowerCase().endsWith("-listeners.xml"))
474 					continue;
475 
476 				try
477 				{
478 					log.info("Adding listeners from [" + actionFile.getAbsolutePath() + "]");
479 
480 					SoapUI.getListenerRegistry().addConfig(new FileInputStream(actionFile), classLoader);
481 				}
482 				catch (Exception e)
483 				{
484 					SoapUI.logError(e);
485 				}
486 			}
487 		}
488 	}
489 }