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.impl.wsdl.monitor;
14  
15  import org.mortbay.jetty.bio.SocketConnector;
16  import org.mortbay.jetty.security.SslSocketConnector;
17  import org.mortbay.jetty.servlet.Context;
18  import org.mortbay.jetty.servlet.ServletHolder;
19  import org.mortbay.thread.BoundedThreadPool;
20  
21  import com.eviware.soapui.impl.wsdl.actions.monitor.SoapMonitorAction;
22  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.TunnelServlet;
23  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.ProxyServlet;
24  import com.eviware.soapui.impl.wsdl.monitor.jettyproxy.Server;
25  import com.eviware.soapui.model.settings.Settings;
26  import com.eviware.soapui.support.UISupport;
27  
28  public class SoapMonitorEngineImpl implements SoapMonitorEngine
29  {
30  
31  	Server server = new Server();
32  	SocketConnector connector = new SocketConnector();
33  	private SslSocketConnector sslConnector;
34  	private String sslEndpoint = null;
35  	private boolean proxyOrTunnel = true;
36  
37  	public boolean isRunning()
38  	{
39  		return server.isRunning();
40  	}
41  
42  	public void start(SoapMonitor soapMonitor, int localPort)
43  	{
44  
45  		Settings settings = soapMonitor.getProject().getSettings();
46  		BoundedThreadPool threadPool = new BoundedThreadPool();
47  		threadPool.setMaxThreads(100);
48  		server.setThreadPool(threadPool);
49  		Context context = new Context(server, "/", 0);
50  
51  		if (sslEndpoint != null)
52  		{
53  			sslConnector = new SslSocketConnector();
54  			sslConnector.setKeystore(settings.getString(SoapMonitorAction.LaunchForm.SSLTUNNEL_KEYSTORE, ""));
55  			sslConnector.setPassword(settings.getString(SoapMonitorAction.LaunchForm.SSLTUNNEL_PASSWORD, ""));
56  			sslConnector.setKeyPassword(settings.getString(SoapMonitorAction.LaunchForm.SSLTUNNEL_KEYPASSWORD, ""));
57  			sslConnector.setTruststore(settings.getString(SoapMonitorAction.LaunchForm.SSLTUNNEL_TRUSTSTORE, ""));
58  			sslConnector.setTrustPassword(settings.getString(SoapMonitorAction.LaunchForm.SSLTUNNEL_TRUSTSTORE_PASSWORD, ""));
59  			sslConnector.setMaxIdleTime(30000);
60  			sslConnector.setNeedClientAuth(false);
61  			sslConnector.setPort(localPort);
62  
63  			server.addConnector(sslConnector);
64  			context.addServlet(new ServletHolder(new TunnelServlet(soapMonitor, sslEndpoint)), "/");
65  			proxyOrTunnel = false;
66  		}
67  		else
68  		{
69  			proxyOrTunnel = true;
70  			connector.setPort(localPort);
71  			server.addConnector(connector);
72  			context.addServlet(new ServletHolder(new ProxyServlet(soapMonitor)), "/");
73  		}
74  		try
75  		{
76  			server.start();
77  		}
78  		catch (Exception e)
79  		{
80  			UISupport.showErrorMessage("Error starting monitor: " + e.getMessage());
81  		}
82  
83  	}
84  
85  	public void stop()
86  	{
87  
88  		try
89  		{
90  			if (server != null)
91  			{
92  				server.stop();
93  			}
94  		}
95  		catch (Exception e)
96  		{
97  			e.printStackTrace();
98  		}
99  		finally
100 		{
101 			if (server != null)
102 			{
103 				server.destroy();
104 			}
105 		}
106 
107 	}
108 
109 	protected void setSslEndpoint(String sslEndpoint)
110 	{
111 		this.sslEndpoint = sslEndpoint;
112 	}
113 
114 	/*
115 	 * @return true if proxy, false if ssl tunnel
116 	 * (non-Javadoc)
117 	 * @see com.eviware.soapui.impl.wsdl.monitor.SoapMonitorEngine#isProxy()
118 	 */
119 	public boolean isProxy()
120 	{
121 		return proxyOrTunnel;
122 	}
123 
124 }