View Javadoc

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