KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > handlers > common > SmallNumberHandler


1 package demo.handlers.common;
2
3 import java.util.Map JavaDoc;
4 import javax.xml.bind.JAXBContext;
5 import javax.xml.bind.JAXBException;
6 import javax.xml.ws.LogicalMessage;
7 import javax.xml.ws.ProtocolException;
8 import javax.xml.ws.handler.LogicalHandler;
9 import javax.xml.ws.handler.LogicalMessageContext;
10 import javax.xml.ws.handler.MessageContext;
11 import org.objectweb.handlers.types.AddNumbers;
12 import org.objectweb.handlers.types.AddNumbersResponse;
13
14
15
16 /**
17  * handles addition of small numbers.
18  */

19 public class SmallNumberHandler implements LogicalHandler<LogicalMessageContext> {
20
21
22     // Implementation of javax.xml.ws.handler.Handler
23

24     public final boolean handleMessage(LogicalMessageContext messageContext) {
25         System.out.println("LogicalMessageHandler handleMessage called");
26
27         try {
28             boolean outbound = (Boolean JavaDoc)messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
29             
30             if (outbound) {
31                 // get the LogicalMessage from our context
32
//
33
LogicalMessage msg = messageContext.getMessage();
34
35                 // check the payload, if its an AddNumbers request, we'll intervene
36
//
37
JAXBContext jaxbContext = JAXBContext.newInstance(AddNumbers.class);
38                 Object JavaDoc payload = msg.getPayload(jaxbContext);
39                 
40                 if (payload instanceof AddNumbers) {
41                     AddNumbers req = (AddNumbers)payload;
42
43                     // now, if the arguments are small, let's do the calculation here
44
//
45
int a = req.getArg0();
46                     int b = req.getArg1();
47                     
48                     if (isSmall(a) && isSmall(b)) {
49                         int answer = a + b;
50                     
51                         System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
52                         // ok, we've done the calculation, so build the
53
// response and set it as the payload of the message
54
AddNumbersResponse resp = new AddNumbersResponse();
55                         resp.setReturn(answer);
56                         msg.setPayload(resp, jaxbContext);
57
58                         // finally, return false, indicating that request
59
// processing stops here and our answer will be
60
// returned to the client
61
return false;
62                     }
63                 }
64             }
65             return true;
66         } catch (JAXBException ex) {
67             throw new ProtocolException(ex);
68         }
69
70     }
71
72     public final boolean handleFault(LogicalMessageContext messageContext) {
73         System.out.println("LogicalMessageHandler handleFault called");
74         System.out.println(messageContext);
75         
76         return true;
77     }
78
79     public void close(MessageContext ctx) {
80         System.out.println("LogicalHandler close called");
81     }
82
83     public void init(Map JavaDoc config) {
84         System.out.println("LogicalHandler init called");
85     }
86
87     public void destroy() {
88         System.out.println("LogicalHandler close called");
89     }
90
91     private boolean isSmall(int i) {
92         return i > 0 && i <= 10;
93     }
94 }
95
Popular Tags