1 64 65 package com.jcorporate.expresso.ext.dbobj; 66 67 import org.apache.log4j.Logger; 68 69 import java.io.BufferedReader ; 70 import java.io.IOException ; 71 import java.io.InputStreamReader ; 72 import java.util.StringTokenizer ; 73 74 75 80 public class URLReader 81 extends Thread { 82 private BufferedReader dis = null; 83 private String expectString = null; 84 private boolean succeeded = false; 85 private String failureReason = "Timed Out"; 86 private String sessionId = null; 87 private static Logger log = Logger.getLogger(URLReader.class); 88 private boolean printOutput = false; 89 private boolean requireSession = false; 90 91 98 public URLReader(InputStreamReader newDis, String newExpect, 99 boolean newPrint) { 100 dis = new BufferedReader (newDis); 101 expectString = newExpect; 102 printOutput = newPrint; 103 } 104 105 106 public String getSessionId() { 107 return sessionId; 108 } 109 110 public void setRequireSession(boolean newRequireSession) { 111 requireSession = newRequireSession; 112 } 113 114 117 public String getFailureReason() { 118 return failureReason; 119 } 120 121 124 public boolean completedSuccessfully() { 125 return succeeded; 126 } 127 128 132 public void run() { 133 boolean returnVal = false; 134 StringBuffer wholeReply = new StringBuffer (); 135 136 if (dis == null) { 137 failureReason = "No connection to server:DataInputStream dis is null"; 138 139 return; 140 } 141 try { 142 143 int linesRead = 0; 146 147 String OneLine = dis.readLine(); 149 150 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 } 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 stk = new StringTokenizer (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 } 188 189 if (wholeReply.toString().indexOf(expectString) > 0) { 190 returnVal = true; 191 } 192 193 dis.close(); 194 195 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 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 } 223 224 } 225 226 | Popular Tags |