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  
18  import com.eviware.soapui.config.SoapuiSettingsDocumentConfig;
19  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.SwingToolHost;
20  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.attachments.AttachmentsInspectorFactory;
21  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.aut.AutInspectorFactory;
22  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.httpheaders.HttpHeadersInspectorFactory;
23  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.registry.XmlInspectorRegistry;
24  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.script.ScriptInspectorFactory;
25  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.ssl.SSLInspectorFactory;
26  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.wss.WssInspectorFactory;
27  import com.eviware.soapui.model.settings.Settings;
28  import com.eviware.soapui.model.workspace.Workspace;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.x.form.XFormFactory;
31  import com.eviware.x.impl.swing.SwingFormFactory;
32  
33  public class SwingSoapUICore extends DefaultSoapUICore
34  {
35  	public SwingSoapUICore()
36  	{
37  		super();
38  	}
39  	
40  	public SwingSoapUICore( String settingsFile )
41  	{
42  		super( null, settingsFile );
43  	}
44  
45  	public void prepareUI()
46  	{
47  		UISupport.setToolHost( new SwingToolHost() );
48        XFormFactory.Factory.instance = new SwingFormFactory();
49  	}
50  	
51  	public void afterStartup(Workspace workspace)
52  	{
53  		XmlInspectorRegistry inspectorRegistry = XmlInspectorRegistry.getInstance();
54  		inspectorRegistry.addFactory( new ScriptInspectorFactory() );
55  		inspectorRegistry.addFactory( new AutInspectorFactory() );
56  		inspectorRegistry.addFactory( new HttpHeadersInspectorFactory() );
57  		inspectorRegistry.addFactory( new AttachmentsInspectorFactory() );
58  		inspectorRegistry.addFactory( new SSLInspectorFactory() );
59  		inspectorRegistry.addFactory( new WssInspectorFactory() );
60  		
61  		addExternalActions( getRoot() == null ? "actions" : getRoot() + File.separatorChar + "actions",	
62  					getExtensionClassLoader() );
63  	}
64  
65  	@Override
66  	protected Settings initSettings( String fileName )
67  	{
68  		String fn = fileName;
69  		
70  		if( !new File( fileName ).exists() )
71  		{
72  			try
73  			{
74  				fileName = importSettingsOnStartup( fileName );
75  			}
76  			catch( Exception e )
77  			{
78  				e.printStackTrace();
79  			}
80  		}
81  		
82  		Settings result = super.initSettings( fileName );
83  		
84  		if( !fileName.equals( fn ))
85  			setSettingsFile( fn );
86  		
87  		return result;
88  	}
89  	
90  	protected String importSettingsOnStartup( String fileName ) throws Exception
91  	{
92  	   if( UISupport.getDialogs().confirm("Missing soapUI Settings, import from existing installation?", "Import Preferences") )
93  		{
94  			while( true )
95  			{
96  			   File settingsFile = UISupport.getFileDialogs().open( null, "Import Preferences", ".xml", "soapUI settings XML", fileName );
97  				if( settingsFile != null )
98  				{
99  					try
100 					{
101 						SoapuiSettingsDocumentConfig.Factory.parse( settingsFile );
102 						log.info( "imported soapui-settings from [" + settingsFile.getAbsolutePath() + "]" );
103 						return settingsFile.getAbsolutePath();
104 					}
105 					catch( Exception e )
106 					{
107                   if( !UISupport.getDialogs().confirm(
108                         "Error loading settings from [" + settingsFile.getAbsolutePath() + "]\r\nspecify another?",
109                         "Error Importing" ) )
110 						{
111 							break;
112 						}
113 					}
114 				}
115 			}
116 		}
117 		
118 		return fileName;
119 	}
120 
121 	private void addExternalActions( String folder, ClassLoader classLoader )
122 	{
123 		File[] actionFiles = new File( folder ).listFiles();
124       if( actionFiles != null )
125       {
126 	      for( File actionFile : actionFiles )
127 	      {
128 	      	if( actionFile.isDirectory() )
129 	      	{
130 	      		addExternalActions( actionFile.getAbsolutePath(), classLoader );
131 	      		continue;
132 	      	}
133 	      	
134 	      	if( !actionFile.getName().toLowerCase().endsWith( "-actions.xml" ))
135 	      		continue;
136 	      	
137 	      	try
138 				{
139 	      		log.info( "Adding actions from [" + actionFile.getAbsolutePath() + "]" );
140 					
141 					SoapUI.getActionRegistry().addConfig( new FileInputStream( actionFile ), 
142 								classLoader);
143 				}
144 				catch( Exception e )
145 				{
146 					SoapUI.logError( e );
147 				}
148 	      }
149       }
150 	}
151 }