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

info.kwarc.sally4.registration.impl.ClientManagerImpl Maven / Gradle / Ivy

The newest version!
package info.kwarc.sally4.registration.impl;

import info.kwarc.sally4.activemq.ActiveMQService;
import info.kwarc.sally4.components.TemplatingComponent;
import info.kwarc.sally4.docmanager.MessageParser;
import info.kwarc.sally4.registration.SallyClient;
import info.kwarc.sally4.registration.models.ClientManagerModel;
import info.kwarc.sally4.servlet.SallyServlet;

import java.util.HashMap;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.DataFormat;
import org.apache.felix.ipojo.Factory;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is the adapter class that manages all clients handled by Sally. 
 * @author Constantin Jucovschi
 *
 */
@Component
@Instantiate
public class ClientManagerImpl extends RouteBuilder {
	public static final String sallyRegisterQueue = "sally_register";

	@Requires(filter="(factory.name=SallyDocFactory)")
	private Factory factory;
	
	@Requires(optional=true)
	SallyClient[] docs;

	ClientManagerModel clientManager;
	
	@Requires 	
	SallyServlet servlet;

	@Requires 	
	ActiveMQService activeMQ;

	CamelContext camelContext;
	Logger log;

	@Requires
	MessageParser parser;

	public HashMap prepareDocManagement() {
		HashMap result = new HashMap();
		result.put("routes", docs);
		return result;
	}

	@Override
	public void configure() throws Exception {
		DataFormat core = parser.getFormat("core");

		from("sallyservlet:///")
		.to("freemarker:index.html");
		
		from("sallyservlet:///docmanager")
		.bean(method(this, "prepareDocManagement"))
		.to("freemarker:docmanager.html");

		from("activemq:queue:"+sallyRegisterQueue)
		.to("log:registermsg")
		.unmarshal(core)
		.bean(method(clientManager, "registerDocument"))			
		.marshal(core);
	}

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

	@Validate
	public void start() throws Exception{
		clientManager = new ClientManagerModel(factory);

		camelContext = new DefaultCamelContext();
		camelContext.addComponent("activemq", activeMQ.getDefaultActiveMQConnection());
		camelContext.addComponent("freemarker", new TemplatingComponent("templates/", getClass().getClassLoader()));
		camelContext.addComponent("sallyservlet", servlet.getCamelComponent());
		camelContext.addRoutes(this);
		camelContext.start();
	}

	@Invalidate
	public void stop() throws Exception {
		camelContext.stop();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy