KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > extensibility > ExtensibilityQueryBindingImpl


1 /**
2  * ExtensibilityQueryBindingImpl.java
3  *
4  */

5
6 package test.wsdl.extensibility;
7
8 import org.apache.axis.components.logger.LogFactory;
9 import org.apache.axis.message.MessageElement;
10 import org.apache.axis.message.Text;
11 import org.apache.commons.logging.Log;
12
13 import javax.xml.namespace.QName JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringWriter JavaDoc;
16 import java.rmi.RemoteException JavaDoc;
17 import java.util.Calendar JavaDoc;
18
19 public class ExtensibilityQueryBindingImpl implements ExtensibilityQueryPortType {
20     private final static String JavaDoc[] books = new String JavaDoc[] { "The Grid", "The Oxford Dictionary" };
21     private final static String JavaDoc[] subjects = new String JavaDoc[] { "Computer Science", "English" };
22     protected static Log log =
23             LogFactory.getLog(ExtensibilityQueryBindingImpl.class.getName());
24
25     public ExtensibilityType query(ExtensibilityType query) throws RemoteException JavaDoc {
26         ExtensibilityType result = new ExtensibilityType();
27         Object JavaDoc obj = null;
28         try {
29             obj = query.get_any()[0].getObjectValue(BookType.class);
30         } catch (Exception JavaDoc e) {
31             StringWriter JavaDoc writer = new StringWriter JavaDoc();
32             PrintWriter JavaDoc out = new PrintWriter JavaDoc(writer);
33             log.error("Error converting query: " + writer.toString());
34             throw new RemoteException JavaDoc(e.toString());
35         }
36         log.error("Incoming MessageContext " + obj + " : " + query.get_any()[0].toString());
37         if (obj instanceof BookType) {
38             BookType bookQuery = (BookType)obj;
39             String JavaDoc subject = bookQuery.getSubject();
40             if (!"all".equals(subject)) {
41                 throw new RemoteException JavaDoc("ExtensibilityQueryBindingImpl: Book subject query should be all, instead was " + subject);
42             }
43             ResultListType resultList = new ResultListType();
44             QueryResultType[] queryResult = new QueryResultType[books.length];
45             for (int i = 0; i < books.length; i++) {
46                 queryResult[i] = new QueryResultType();
47                 queryResult[i].setName(subjects[i]);
48                 queryResult[i].setStatus(StatusType.MORE);
49                 queryResult[i].setValue(books[i]);
50                 queryResult[i].setTime(Calendar.getInstance());
51                 queryResult[i].setQueryType(new QName JavaDoc("urn:QueryType","BookQuery"));
52             }
53             resultList.setResult(queryResult);
54             QName JavaDoc elementName = _QueryResultElement.getTypeDesc().getFields()[0].getXmlName();
55             MessageElement me = new MessageElement(elementName.getNamespaceURI(), elementName.getLocalPart(), resultList);
56             log.debug("Outgoing message: " + me.toString());
57             result.set_any(new MessageElement [] { me });
58         } else {
59             throw new RemoteException JavaDoc("Failed to get FindBooksQueryExpressionElement. Got: " + obj);
60         }
61         return result;
62     }
63     
64     public ExtensibilityType mixedQuery(ExtensibilityType query)
65         throws RemoteException JavaDoc {
66         MessageElement [] elements = query.get_any();
67         if (elements == null) {
68             throw new RemoteException JavaDoc("No any");
69         }
70         if (elements.length != 3) {
71             throw new RemoteException JavaDoc("Expected: 3 got: " + elements.length +
72                                       " element");
73         }
74         
75         String JavaDoc expected = "123 456";
76         String JavaDoc received = elements[0].toString();
77         
78         if (!expected.equals(received)) {
79             throw new RemoteException JavaDoc("Expected: " + expected +
80                                       " received: " + received);
81         }
82         
83         Object JavaDoc obj = null;
84         try {
85             obj = elements[1].getObjectValue(BookType.class);
86         } catch (Exception JavaDoc e) {
87             throw new RemoteException JavaDoc("Failed to deserialize", e);
88         }
89         BookType bookQuery = (BookType)obj;
90         String JavaDoc subject = bookQuery.getSubject();
91         if (!"all".equals(subject)) {
92             throw new RemoteException JavaDoc("ExtensibilityQueryBindingImpl: Book subject query should be all, instead was " + subject);
93         }
94         
95         expected = "789";
96         received = elements[2].toString();
97         
98         if (!expected.equals(received)) {
99             throw new RemoteException JavaDoc("Expected: " + expected +
100                                       " received: " + received);
101         }
102         
103         ExtensibilityType reply = new ExtensibilityType();
104         
105         MessageElement [] replyElements = new MessageElement[2];
106         
107         BookType book = new BookType();
108         book.setSubject("gotAll");
109         QName JavaDoc elementName = _FindBooksQueryExpressionElement.getTypeDesc().getFields()[0].getXmlName();
110         replyElements[0] = new MessageElement(elementName.getNamespaceURI(), elementName.getLocalPart(), book);
111         replyElements[1] = new MessageElement(new Text("ABCD"));
112         
113         reply.set_any(replyElements);
114         
115         return reply;
116     }
117 }
118
Popular Tags