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