KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > amazon > search > AsynchronousClient


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package sample.amazon.search;
18
19 import org.apache.axis2.Constants;
20 import org.apache.axis2.addressing.AddressingConstants;
21 import org.apache.axis2.addressing.EndpointReference;
22 import org.apache.axis2.clientapi.Call;
23 import org.apache.axis2.context.MessageContext;
24 import org.apache.axis2.description.OperationDescription;
25 import org.apache.axis2.engine.AxisFault;
26 import org.apache.axis2.om.OMOutput;
27
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.stream.XMLOutputFactory;
30 import javax.xml.stream.XMLStreamException;
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.net.MalformedURLException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 /**
39  * This class implements main() method and sendMsg() methods,
40  * check the availability of License key
41  * Builds call objects and set it's properties,
42  * Also keep few important variables as static and protected those frequently need by other classes
43  *
44  * @auther Gayan Asanka (gayan@opensource.lk)
45  */

46
47 public class AsynchronousClient {
48
49     /**
50      * Query parameter
51      */

52     protected static String JavaDoc search = "";
53
54     /**
55      * Query parameters sent with the last request
56      */

57     protected static String JavaDoc prevSearch = "";
58
59     /**
60      * License key
61      */

62     protected static String JavaDoc amazonkey;
63
64     /**
65      * properties file to store the license key
66      */

67     protected static Properties JavaDoc prop;
68
69     /**
70      * maximum results per page
71      */

72     protected static String JavaDoc maxResults = String.valueOf(2);
73
74     /**
75      * when this is set, thread sends a new request
76      */

77     protected static boolean doSearch = false;
78
79     public static void main(String JavaDoc[] args) {
80         GUIHandler gui;
81         Thread JavaDoc linkThread, guiThread;
82         LinkFollower page;
83         File JavaDoc propertyFile;
84
85         page = new LinkFollower(); //this object used to build the linkfollower thread
86
LinkFollower.showURL = false;
87         gui = new GUIHandler();
88
89         /*check the license key, if it is not there, ask user to enter the key*/
90
91         prop = new Properties JavaDoc();
92         try {
93             String JavaDoc workingDir = System.getProperty("user.dir");
94             propertyFile = new File JavaDoc(workingDir + File.separator + "samples" + File.separator +
95                     "key.properties");
96             propertyFile.createNewFile();
97             prop.load(new FileInputStream JavaDoc(propertyFile));
98             amazonkey = prop.getProperty("amazonKey");
99             System.out.println("key is " + amazonkey);
100             if (amazonkey == null) {
101                 gui.setKey();
102             }
103             prop = null; //Useless from now onwards
104

105         } catch (IOException JavaDoc e) {
106             e.printStackTrace();
107         }
108         gui.buildFrame(); //GUI is built and desplayed
109

110         linkThread = new Thread JavaDoc(page);
111         guiThread = new Thread JavaDoc(gui);
112
113         guiThread.start();
114         linkThread.start();
115         guiThread.run(); //To listen to the GUI and send messages
116
linkThread.run(); //To detect hyperLink actions and open Simple Web Browser
117
}
118
119     /**
120      * constructor
121      */

122     public AsynchronousClient() {
123
124     }
125
126     /**
127      * Method sendMsg()
128      * build the Call Object
129      * Set the URL : To use TCP monitor comment the line where url is hard coded
130      * and uncomment the following line
131      * Get built the messageContext
132      * Invoke the sending operation
133      *
134      * @throws AxisFault
135      */

136     public static void sendMsg() throws AxisFault {
137         search.trim();
138         prevSearch = search;
139         Call call = new Call();
140         URL JavaDoc url = null;
141         try {
142             url = new URL JavaDoc("http", "soap.amazon.com", "/onca/soap?Service=AlexaWebInfoService");
143
144             /** Uncomment the folowing to use TCP Monitor, and comment the above */
145
146             //url = new URL("http", "localhost",8080, "/onca/soap?Service=AlexaWebInfoService");
147

148         } catch (MalformedURLException JavaDoc e) {
149             e.printStackTrace();
150         }
151         call.setTo(new EndpointReference(AddressingConstants.WSA_TO, url.toString()));
152         MessageContext requestContext = ClientUtil.getMessageContext();
153         try {
154             call.setTransportInfo(Constants.TRANSPORT_HTTP, Constants.TRANSPORT_HTTP, false);
155
156             System.out.println("Sending the Async message ....");
157
158            OMOutput omOutput = new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter
159                     (System.out));
160             requestContext.getEnvelope().serialize(omOutput);
161             omOutput.flush();
162
163             System.out.println();
164             QName JavaDoc opName = new QName JavaDoc("urn:GoogleSearch", "doGoogleSearch");
165             OperationDescription opdesc = new OperationDescription(opName);
166             call.invokeNonBlocking(opdesc, requestContext, new ClientCallbackHandler());
167         } catch (AxisFault e1) {
168             e1.printStackTrace();
169         } catch (XMLStreamException e) {
170             e.printStackTrace();
171         }
172         System.out.println("Message sent and the client thread is returned....");
173     }
174 }
175
176
177
178
179
180
Popular Tags