/*-
 *******************************************************************************
 * Copyright (c) 2011, 2015 Diamond Light Source Ltd.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Matthew Gerring - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.dawnsci.remotedataset.server;

import java.util.HashMap;
import java.util.Map;


import org.eclipse.dawnsci.remotedataset.server.event.EventServlet;
import org.eclipse.dawnsci.remotedataset.server.event.FileMonitorSocket;
import org.eclipse.dawnsci.remotedataset.server.info.InfoServlet;
import org.eclipse.dawnsci.remotedataset.server.slice.SliceServlet;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

/**
 * This object is designed to start the server and 
 * can be used as a spring object for instance.
 * 
 *  
    {@literal <bean id="dataServer" class="org.eclipse.dawnsci.remotedataset.server.DataServer" init-method="start">}
    {@literal    <property name="port"      value="8690" />}
    {@literal </bean>}

 * @author Matthew Gerring
 *
 */
public class DataServer extends PortServer {
	
	
	public DataServer() {
         	
	}
	
	/**
	 * Method replaces main(...) when running things with OSGi
	 * 
	 * Not called on GDA server will will probably start the server by calling
	 */
	@Override
	public Object start(IApplicationContext context) throws Exception {

		final Map      args          = context.getArguments();
		final String[] configuration = (String[])args.get("application.args");
        
		Map<String, String> conf = new HashMap<String, String>(7);
		for (int i = 0; i < configuration.length; i++) {
			final String pkey = configuration[i];
			if (pkey.startsWith("-")) {
				conf.put(pkey.substring(1), configuration[i+1]);
			}
		}
      
    	if (conf.containsKey("port")) {
    		setPort(Integer.parseInt(conf.get("port").toString()));
    	} 
    	start(); // blocking
    	
    	return server;// We are done with this application now.
	}

	public void start() throws Exception {
		start(false);
	}

	public void start(boolean block) throws Exception {
		
		this.server = new Server();
		ServerConnector connector = new ServerConnector(server);
		connector.setPort(getPort());
		connector.setReuseAddress(true);
		server.addConnector(connector);   
		
		// We enable sessions on the server so that 
		// we can cache LoaderFactories to a given session.
		// The loader factory therefore needs a non-global 
		// data soft reference cache.
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
		context.setContextPath("/");
		server.setHandler(context);
      
        // Make individual servlets
        // Slicing (large data in binary http)
		ServletHolder holderSlice = new ServletHolder("slice", SliceServlet.class);
		context.addServlet(holderSlice, "/slice/*");
		
		// Doing events, like data changing shape.
		// FIXME Should not be needed
		WebSocketHandler wsHandler = new WebSocketHandler() {
			@Override
			public void configure(WebSocketServletFactory factory) {
				factory.register(FileMonitorSocket.class);
			}
		};
		context.setHandler(wsHandler);
		// FIXME End should not be needed.		
		ServletHolder holderInfo = new ServletHolder("info", InfoServlet.class);
		context.addServlet(holderInfo, "/info/*");
     
		// Events json objects to notifyu of problems.
		ServletHolder holderEvent = new ServletHolder("event", EventServlet.class);
		context.addServlet(holderEvent, "/event/*");
		
	    server.start();
	    if (block) server.join();

	}

}

@@ -1984,16 +1984,48 @@
 tion %7B%0A%0A
+%09%09@SuppressWarnings(%22rawtypes%22)%0A
 %09%09final 
@@ -2526,16 +2526,20 @@
  %09start(
+true
 ); // bl
@@ -2634,16 +2634,31 @@
 id start
+WithoutBlocking
 () throw
@@ -3997,17 +3997,16 @@
 o notify
-u
  of prob

Allow DataServer to start up as an application or a Spring bean