KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > dbobj > URLReader


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.dbobj;
66
67 import org.apache.log4j.Logger;
68
69 import java.io.BufferedReader JavaDoc;
70 import java.io.IOException JavaDoc;
71 import java.io.InputStreamReader JavaDoc;
72 import java.util.StringTokenizer JavaDoc;
73
74
75 /**
76  * Created on March 20, 2001, 9:00 AM
77  *
78  * @author Michael Nash
79  */

80 public class URLReader
81         extends Thread JavaDoc {
82     private BufferedReader JavaDoc dis = null;
83     private String JavaDoc expectString = null;
84     private boolean succeeded = false;
85     private String JavaDoc failureReason = "Timed Out";
86     private String JavaDoc sessionId = null;
87     private static Logger log = Logger.getLogger(URLReader.class);
88     private boolean printOutput = false;
89     private boolean requireSession = false;
90
91     /**
92      * Creates new URLReader
93      *
94      * @param newDis a Data Input Stream
95      * @param newExpect String that is expected
96      * @param newPrint should output be printed
97      */

98     public URLReader(InputStreamReader JavaDoc newDis, String JavaDoc newExpect,
99                      boolean newPrint) {
100         dis = new BufferedReader JavaDoc(newDis);
101         expectString = newExpect;
102         printOutput = newPrint;
103     } /* URLReader(DataInputStream, String) */
104
105
106     public String JavaDoc getSessionId() {
107         return sessionId;
108     }
109
110     public void setRequireSession(boolean newRequireSession) {
111         requireSession = newRequireSession;
112     }
113
114     /**
115      * @return The reason for failure as java.lang.String
116      */

117     public String JavaDoc getFailureReason() {
118         return failureReason;
119     } /* getFailureReason() */
120
121     /**
122      * @return true if the test had completed successfully
123      */

124     public boolean completedSuccessfully() {
125         return succeeded;
126     } /* completedSuccessfully() */
127
128     /**
129      * Read from the specified URL, looking for the "ExpectString". If you find it,
130      * return true, if not - return false
131      */

132     public void run() {
133         boolean returnVal = false;
134         StringBuffer JavaDoc wholeReply = new StringBuffer JavaDoc();
135
136         if (dis == null) {
137             failureReason = "No connection to server:DataInputStream dis is null";
138
139             return;
140         }
141         try {
142
143             //System.out.println(myName + ":Bytes available from stream:"
144
// + dis.available());
145
int linesRead = 0;
146
147             //System.out.println(myName + ":Reading line");
148
String JavaDoc OneLine = dis.readLine();
149
150             //System.out.println("Read (initial) from server:" + OneLine);
151
if (OneLine == null) {
152                 failureReason = "Server never sent EOF";
153
154                 return;
155             }
156             while (OneLine != null) {
157                 if (printOutput) {
158                     System.out.println(OneLine);
159                 }
160
161                 wholeReply.append(OneLine + " ");
162
163                 if (!OneLine.equals("")) {
164                     linesRead++;
165                 } /* if */
166
167
168                 OneLine = dis.readLine();
169
170                 if (OneLine != null) {
171                     if (OneLine.indexOf("jsessionid|") > 0) {
172                         log.info("Found line with jsessionid:'" + OneLine +
173                                 "'");
174
175                         StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(OneLine, "|");
176                         stk.nextToken();
177
178                         if (stk.hasMoreTokens()) {
179                             sessionId = stk.nextToken();
180                             System.out.println("Session assigned:'" +
181                                     sessionId + "'");
182                         } else {
183                             log.error("No second token found! Session info not coded correctly");
184                         }
185                     }
186                 }
187             } /* while */
188
189             if (wholeReply.toString().indexOf(expectString) > 0) {
190                 returnVal = true;
191             }
192
193             dis.close();
194
195             //Log.log(myName, "Read " + linesRead + " lines from server", "");
196
if ((requireSession) && (sessionId == null)) {
197                 System.out.println("Never got session established. Output was:");
198                 System.out.println("-----Output Begins----");
199                 System.out.println(wholeReply.toString());
200                 System.out.println("-----Output Ends----");
201             }
202             if (!returnVal) {
203                 System.out.println("Expect string:'" + expectString + "'");
204                 failureReason = "Never received the expected string '" +
205                         expectString + "'. Got '";
206                 failureReason = failureReason + wholeReply.toString();
207                 failureReason = failureReason + "' instead. (" + linesRead +
208                         " lines read)";
209
210                 return;
211             }
212         } catch (IOException JavaDoc ie) {
213             log.error("I/O Exception during test:" + ie);
214             failureReason = "I/O Exception reading URL:" + ie.getMessage();
215             succeeded = false;
216             System.gc();
217
218             return;
219         }
220
221         succeeded = true;
222     } /* run() */
223
224 } /* URLReader */
225
226 /* URLReader */
Popular Tags