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