// Copyright 2010 NexJ Systems Inc. This software is licensed under the terms of the Eclipse Public License 1.0
package nexj.core.integration;
import java.util.Iterator;
import nexj.core.integration.io.ObjectInput;
import nexj.core.meta.integration.Channel;
import nexj.core.meta.integration.MessageTable;
import nexj.core.meta.integration.service.Binding;
import nexj.core.rpc.TransferObject;
import nexj.core.runtime.InvocationContext;
import nexj.core.util.Logger;
/**
* Generic receiver implementation.
*/
public class Receiver
{
// associations
/**
* The class logger.
*/
protected final static Logger s_logger = Logger.getLogger(Receiver.class);
// operations
/**
* @return The class logger.
*/
protected Logger getLogger()
{
return s_logger;
}
/**
* Determines if a channel is bound to a service.
* @param channel The channel.
* @param context The invocation context.
* @return True if the channel is bound.
*/
public boolean isBound(Channel channel, InvocationContext context)
{
return channel.getBindingCount() != 0;
}
/**
* Processes a message received on a specified channel.
* @param tobj The message.
* @param channel The channel, on which the message has been received.
* @param context The invocation context.
*/
public void receive(TransferObject tobj, Channel channel, InvocationContext context) throws IntegrationException
{
getLogger().dump(tobj);
context.getUnitOfWork().checkLicense();
Object body = tobj.findValue(Sender.BODY);
MessageTable table = channel.getMessageTable();
Iterator itr = null;
if (table != null && table.getFormat() != null)
{
tobj = ((MessageParser)table.getFormat().getParser().getInstance(context))
.parse((body instanceof Input) ? (Input)body : new ObjectInput(body), table);
itr = channel.getBindingIterator(table.getMessage(tobj.getClassName()));
}
else
{
if (body instanceof Input)
{
tobj.setValue(Sender.BODY, ((Input)body).getObject());
}
itr = channel.getBindingIterator();
}
while (itr.hasNext())
{
context.getMachine().invoke(((Binding)itr.next()).getFunction(), tobj, (Object[])null);
}
}
}