KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > pojo > SamplePojoService


1 package org.objectweb.petals.engine.pojo;
2
3 import java.net.URI JavaDoc;
4 import java.util.logging.Level JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import javax.jbi.component.ComponentContext;
8 import javax.jbi.messaging.DeliveryChannel;
9 import javax.jbi.messaging.InOut;
10 import javax.jbi.messaging.NormalizedMessage;
11 import javax.jbi.servicedesc.ServiceEndpoint;
12 import javax.xml.namespace.QName JavaDoc;
13
14 import org.objectweb.petals.component.common.MEPConstants;
15 import org.objectweb.petals.component.common.util.MessageExchangeWrapper;
16 import org.objectweb.petals.component.common.util.SourceHelper;
17
18 public class SamplePojoService {
19
20     private Logger JavaDoc logger;
21
22     private ComponentContext ctx;
23
24     private DeliveryChannel channel;
25
26     public void setChannel(DeliveryChannel channel) {
27         this.channel = channel;
28     }
29
30     public void setCtx(ComponentContext ctx) {
31         this.ctx = ctx;
32     }
33
34     public void setLogger(Logger JavaDoc logger) {
35         this.logger = logger;
36     }
37
38     public boolean onExchange(MessageExchangeWrapper exchange) throws Exception JavaDoc {
39         NormalizedMessage in = exchange.getInMessage();
40         if (in != null) {
41             String JavaDoc msg = SourceHelper.createString(in.getContent());
42             logger.log(Level.INFO, "The SamplePojo received:\n" + msg);
43         }
44
45         // calls helloworld
46
callHelloworld();
47
48         // answer
49
URI JavaDoc pattern = exchange.getPattern();
50         if (pattern.equals(MEPConstants.IN_OUT_PATTERN.value())
51             || pattern.equals(MEPConstants.IN_OPTIONAL_OUT_PATTERN.value())) {
52             NormalizedMessage out = exchange.createMessage();
53             out.setContent(SourceHelper
54                 .createSource("<pojo-response>sample</pojo-response>"));
55             exchange.setMessage(out, "out");
56             return true;
57         } else {
58             return false;
59         }
60     }
61
62     public void init() {
63         logger.log(Level.INFO, "SamplePojo inits.");
64     }
65
66     public void start() {
67         logger.log(Level.INFO, "SamplePojo starts.");
68     }
69
70     public void stop() {
71         logger.log(Level.INFO, "SamplePojo stops.");
72     }
73
74     private void callHelloworld() throws Exception JavaDoc {
75
76         logger.log(Level.INFO,
77             "The SamplePojo calls helloworld Service if it exists...");
78
79         QName JavaDoc helloworldSrv = new QName JavaDoc("http://petals.objectweb.org/",
80             "HelloworldService");
81         ServiceEndpoint[] helloworldEps = ctx
82             .getEndpointsForService(helloworldSrv);
83
84         if (helloworldEps != null && helloworldEps.length > 0) {
85             logger
86                 .log(Level.INFO,
87                     "The SamplePojo found the helloworld Service. Send a message to it.");
88
89             InOut exchange = channel.createExchangeFactory()
90                 .createInOutExchange();
91             NormalizedMessage in = exchange.createMessage();
92             in
93                 .setContent(SourceHelper
94                     .createSource("<sample-pojo> sample-pojo say hello</sample-pojo>"));
95             exchange.setInMessage(in);
96             exchange.setOperation(new QName JavaDoc("sayHello"));
97             exchange.setEndpoint(helloworldEps[0]);
98             channel.sendSync(exchange);
99
100             NormalizedMessage out = exchange.getOutMessage();
101             String JavaDoc s_out = SourceHelper.createString(out.getContent());
102             logger.log(Level.INFO,
103                 "The SamplePojo received from the helloworld Service :\n"
104                     + s_out);
105
106         } else {
107             logger.log(Level.INFO,
108                 "The SamplePojo did not found the helloworld Service.");
109
110         }
111     }
112 }
113
Popular Tags