View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  package com.eviware.soapui.impl.wsdl.actions.project;
13  
14  import java.io.File;
15  
16  import com.eviware.soapui.SoapUI;
17  import com.eviware.soapui.actions.SoapUIPreferencesAction;
18  import com.eviware.soapui.impl.wsdl.WsdlProject;
19  import com.eviware.soapui.integration.impl.CajoClient;
20  import com.eviware.soapui.settings.LoadUISettings;
21  import com.eviware.soapui.support.UISupport;
22  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
23  
24  public class StartLoadUI extends AbstractSoapUIAction<WsdlProject>
25  {
26  	private static final String SH = ".sh";
27  	private static final String BAT = ".bat";
28  	private static final String COMMAND = ".command";
29  	private static final String LOADUI_LAUNCH_EXTENSION = ( UISupport.isWindows() ? BAT : UISupport.isMac() ? COMMAND
30  			: SH );
31  	private static final String LOADUI = "loadUI";
32  	public static final String SOAPUI_ACTION_ID = "StartLoadUI";
33  	public static final String LOADUI_LAUNCH_TITLE = "Launch loadUI";
34  	public static final String LOADUI_LAUNCH_QUESTION = "For this action you have to launch loadUI. Launch it now?";
35  
36  	public StartLoadUI()
37  	{
38  		super( "Start loadUI", "Start loadUI application" );
39  	}
40  
41  	public void perform( WsdlProject project, Object param )
42  	{
43  		String loadUIBatPath = getLoadUIPath();
44  		startLoadUI( loadUIBatPath );
45  	}
46  
47  	public static Process launchLoadUI()
48  	{
49  		String loadUILaunchPath = getLoadUIPath();
50  		return startLoadUI( loadUILaunchPath );
51  	}
52  
53  	private static Process startLoadUI( String loadUILaunchPath )
54  	{
55  		if( CajoClient.getInstance().testConnection() )
56  		{
57  			try
58  			{
59  				CajoClient.getInstance().invoke( "bringToFront", null );
60  				return null;
61  			}
62  			catch( Exception e )
63  			{
64  				SoapUI.log.error( "Error while invoke cajo server in loadui ", e );
65  			}
66  		}
67  
68  		try
69  		{
70  			while( !( new File( loadUILaunchPath ) ).exists() )
71  			{
72  				UISupport.showInfoMessage( "No loadUI" + LOADUI_LAUNCH_EXTENSION + " in path:\"" + loadUILaunchPath + "\"" );
73  				if( UISupport.getMainFrame() != null )
74  				{
75  					if( SoapUIPreferencesAction.getInstance().show( SoapUIPreferencesAction.LOADUI_SETTINGS ) )
76  					{
77  						loadUILaunchPath = getLoadUIPath();
78  					}
79  					else
80  					{
81  						return null;
82  					}
83  				}
84  			}
85  			String[] commandsWin = new String[] { "cmd.exe", "/c", LOADUI + LOADUI_LAUNCH_EXTENSION };
86  			String[] commandsLinux = new String[] { "sh", LOADUI + LOADUI_LAUNCH_EXTENSION };
87  			String[] commandsMac = new String[] { "sh", LOADUI + LOADUI_LAUNCH_EXTENSION };
88  
89  			ProcessBuilder pb = new ProcessBuilder( UISupport.isWindows() ? commandsWin : UISupport.isMac() ? commandsMac
90  					: commandsLinux );
91  			pb.directory( new File( SoapUI.getSettings().getString( LoadUISettings.LOADUI_PATH, "" ) ) );
92  			Process p = pb.start();
93  			return p;
94  		}
95  		catch( Exception e )
96  		{
97  			SoapUI.logError( e );
98  		}
99  		return null;
100 	}
101 
102 	private static String getLoadUIPath()
103 	{
104 		return SoapUI.getSettings().getString( LoadUISettings.LOADUI_PATH, "" ) + File.separator + LOADUI
105 				+ LOADUI_LAUNCH_EXTENSION;
106 	}
107 
108 	public static boolean testCajoConnection()
109 	{
110 		return CajoClient.getInstance().testConnection();
111 	}
112 
113 }