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

org.coos.messaging.impl.DefaultEndpointExtension Maven / Gradle / Ivy

package org.coos.messaging.impl;


import org.coos.messaging.ExchangePattern;
import org.coos.messaging.Link;
import org.coos.messaging.Message;
import org.coos.messaging.MessageContext;
import org.coos.messaging.ProcessorException;
import org.coos.messaging.ProcessorInterruptException;
import org.coos.messaging.Service;
import org.coos.messaging.impl.DefaultProcessor;
import org.coos.messaging.util.URIHelper;

public abstract class DefaultEndpointExtension extends DefaultProcessor implements Service{

	private boolean started = false;
	private boolean stopped = false;
	
	public abstract void process(Message msg);
	public abstract void processInbound(Message msg);
	public abstract void processOutbound(Message msg);
	public abstract void startup();
	public abstract void shutdown();
	
	public void start() throws Exception {
		if (!started) {
			startup();
			started = true;
			stopped = false;
		}
	}
	
	public void stop() throws Exception {
		if (stopped) {
			shutdown();
			stopped = true;
			started = false;
		}
	}
	
	/**
	 * Helper method that sends a message in reply to a request message
	 * 
	 * @param response A new message that will be sent in reply to the request message
	 * @param request The message that requests a reply
	 * @throws ProcessorException
	 */
	public void reply(Message request, Message response) throws ProcessorException {
		//Get current link
		Link curLink = request.getMessageContext().getCurrentLink();

		Link replyLink = null;

		if(curLink.isOutLink()){
			//If this is an outlink, reverse the response and send it back on inlink of current channel
			replyLink = request.getMessageContext().getCurrentChannel().getInLink();
		} else {
			//If this is an inlink, reverse the response and send it back on outlink of current channel
			replyLink = request.getMessageContext().getCurrentChannel().getOutLink();
		}
		
		//The receiver URI
		response.setReceiverEndpointUri(request.getSenderEndpointUri());

		//The sender URI
		response.setSenderEndpointUri(request.getReceiverEndpointUri());
		
		if(request.getHeader(Message.EXCHANGE_PATTERN).equals("OutIn")){
			//Must contain the exchange id of the originating message to reach the sender
			response.setHeader(Message.EXCHANGE_ID, request.getHeader(Message.EXCHANGE_ID));
			//Must be set to InOut to reply to an OutIn
			response.setHeader(Message.EXCHANGE_PATTERN, ExchangePattern.InOut);
		}
		
		//process the message
		replyLink.processMessage(response);
	}

	public void processMessage(Message msg) throws ProcessorException {
		URIHelper helper = new URIHelper(msg.getReceiverEndpointUri());

		if(helper.getPath().startsWith("/"+getProperty("endpoint_extension_name"))) {
			process(msg);
			throw new ProcessorInterruptException("Arrived at endpoint extension: "+getProperty("endpoint_extension_name"));
		}

		//Get the current link
		MessageContext mc = msg.getMessageContext();
		Link curLink = mc.getCurrentLink();
		
		if(curLink.isOutLink())
			processOutbound(msg);
		else
			processInbound(msg);
		
		
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy