All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.kwarc.sally4.mhw.base.impl.MHWorkerSelector Maven / Gradle / Ivy

The newest version!
package info.kwarc.sally4.mhw.base.impl;

import info.kwarc.sally4.activemq.ActiveMQService;
import info.kwarc.sally4.components.TemplatingComponent;
import info.kwarc.sally4.mhw.base.IMathHubDocument;
import info.kwarc.sally4.processors.FileServeProcessor;
import info.kwarc.sally4.registration.ClientManager;
import info.kwarc.sally4.servlet.SallyServlet;

import org.apache.camel.builder.RouteBuilder;
import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Requires;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Instantiate
public class MHWorkerSelector extends RouteBuilder {
	Logger log;

	@Requires
	SallyServlet servlet;

	@Requires
	ActiveMQService activeMQ;

	@Requires 
	ClientManager docMan;
	
	MathHubWorkerAssigner assignerModel;

	@Requires(optional=true)
	IMathHubDocument [] mathHubDocuments;

	boolean started = false;

	public MHWorkerSelector() {
		log = LoggerFactory.getLogger(getClass());
	}

	@Bind(aggregate=true, optional=true)
	public void addPlanetaryClient(IMathHubDocument planetaryClients) {

	}

	/*
	public HashMap getMathHubWorkers(@Header(QueryParser.QueryMapHeader) HashMap query) {
		HashMap data = new HashMap();
		String userid = query.get("uid");
		String worker = assignerModel.getWorkerAssignedToUser(userid);
		if (worker !=null) {
			data.put("assigned", true);
			ArrayList w = new ArrayList();
			w.add(assignerModel.getWorkerFromId(worker));
			data.put("mhworkers", w);			
		} else {
			data.put("assigned", false);
			data.put("mhworkers", assignerModel.getFreeWorkers());			
		}
		data.put("uid", userid);
		return data;
	}

	public HashMap setMHW(@Header(QueryParser.QueryMapHeader) HashMap query) throws Exception {
		HashMap data = new HashMap();
		String uid = query.get("uid");
		String mhwqueue = query.get("mhwqueue");
		String action = query.get("action");
		if (action.equals("connect")) {
			assignerModel.assignWorkerToUser(mhwqueue, uid);			
		}
		if (action.equals("disconnect")) {
			assignerModel.unassignWorkerFromUser(mhwqueue, uid);
		}
		return data;
	}

	CamelContext resourceContext;

	@Validate
	public void start() {
		assignerModel = new MathHubWorkerAssigner();

		resourceContext = new DefaultCamelContext();
		resourceContext.addComponent("sallyservlet", activeMQ.getDefaultActiveMQConnection());
		try {
			resourceContext.addRoutes(this);
			resourceContext.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		started = true;
		for (IWorker doc : bindRequests) {
			bindMathHubWorker(doc);
		}
	}

	@Invalidate
	public void stop() {
		try {
			resourceContext.stop();
			for (PlanetaryDoc client : planetaryClients) {
				RemoveService rm = new RemoveService();
				rm.setId("mhwsettings");
				client.stopService(rm);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	*/


	@Override
	public void configure() throws Exception {
		getContext().addComponent("freemarker", new TemplatingComponent("templates/", getClass().getClassLoader()));
		//		getContext().addComponent("freemarker", new FreemarkerComponent());
		from("sallyservlet:///mhw/libs?matchOnUriPrefix=true")
		.process(new FileServeProcessor("libs", getClass().getClassLoader() ));

		from("sallyservlet:///mhw/mhwsettings")
		.bean(method(this, "getMathHubWorkers"))
		//		.to("freemarker:file:///home/costea/workspace_sally4/sally4/mhw-sally-service/src/main/resources/templates/mhwsettings.html");
		.to("freemarker:mhwsettings.html");

		from("sallyservlet:///mhw/setmhw")
		.bean(method(this, "setMHW"))
		//		.to("freemarker:file:///home/costea/workspace_sally4/sally4/mhw-sally-service/src/main/resources/templates/mhwset.html");
		.to("freemarker:mhwset.html");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy