org.objectweb.petals.engine.pojo.SamplePojoService Maven / Gradle / Ivy
package org.objectweb.petals.engine.pojo;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jbi.component.ComponentContext;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.NormalizedMessage;
import javax.jbi.servicedesc.ServiceEndpoint;
import javax.xml.namespace.QName;
import org.objectweb.petals.component.common.MEPConstants;
import org.objectweb.petals.component.common.util.MessageExchangeWrapper;
import org.objectweb.petals.component.common.util.SourceHelper;
public class SamplePojoService {
private Logger logger;
private ComponentContext ctx;
private DeliveryChannel channel;
public void setChannel(DeliveryChannel channel) {
this.channel = channel;
}
public void setCtx(ComponentContext ctx) {
this.ctx = ctx;
}
public void setLogger(Logger logger) {
this.logger = logger;
}
public boolean onExchange(MessageExchangeWrapper exchange) throws Exception {
NormalizedMessage in = exchange.getInMessage();
if (in != null) {
String msg = SourceHelper.createString(in.getContent());
logger.log(Level.INFO, "The SamplePojo received:\n" + msg);
}
// calls helloworld
callHelloworld();
// answer
URI pattern = exchange.getPattern();
if (pattern.equals(MEPConstants.IN_OUT_PATTERN.value())
|| pattern.equals(MEPConstants.IN_OPTIONAL_OUT_PATTERN.value())) {
NormalizedMessage out = exchange.createMessage();
out.setContent(SourceHelper
.createSource("sample "));
exchange.setMessage(out, "out");
return true;
} else {
return false;
}
}
public void init() {
logger.log(Level.INFO, "SamplePojo inits.");
}
public void start() {
logger.log(Level.INFO, "SamplePojo starts.");
}
public void stop() {
logger.log(Level.INFO, "SamplePojo stops.");
}
private void callHelloworld() throws Exception {
logger.log(Level.INFO,
"The SamplePojo calls helloworld Service if it exists...");
QName helloworldSrv = new QName("http://petals.objectweb.org/",
"HelloworldService");
ServiceEndpoint[] helloworldEps = ctx
.getEndpointsForService(helloworldSrv);
if (helloworldEps != null && helloworldEps.length > 0) {
logger
.log(Level.INFO,
"The SamplePojo found the helloworld Service. Send a message to it.");
InOut exchange = channel.createExchangeFactory()
.createInOutExchange();
NormalizedMessage in = exchange.createMessage();
in
.setContent(SourceHelper
.createSource(" sample-pojo say hello "));
exchange.setInMessage(in);
exchange.setOperation(new QName("sayHello"));
exchange.setEndpoint(helloworldEps[0]);
channel.sendSync(exchange);
NormalizedMessage out = exchange.getOutMessage();
String s_out = SourceHelper.createString(out.getContent());
logger.log(Level.INFO,
"The SamplePojo received from the helloworld Service :\n"
+ s_out);
} else {
logger.log(Level.INFO,
"The SamplePojo did not found the helloworld Service.");
}
}
}