1 19 20 package org.netbeans.modules.web.monitor.client; 21 22 import java.io.FileNotFoundException ; 23 import java.io.InputStreamReader ; 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.StringWriter ; 27 import java.util.Enumeration ; 28 import java.util.StringTokenizer ; 29 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.swing.SwingUtilities ; 36 37 import org.openide.filesystems.FileAlreadyLockedException; 38 import org.openide.filesystems.FileLock; 39 import org.openide.filesystems.FileObject; 40 41 import org.netbeans.modules.web.monitor.data.Constants; 42 43 46 47 public class PutTransaction extends HttpServlet { 48 49 private static FileObject currDir = null; 50 private static boolean debug = false; 51 52 private ServletConfig servletConfig = null; 53 54 public void doPost(HttpServletRequest req, HttpServletResponse res) 55 throws ServletException , IOException { 56 57 if(debug) log("doPost"); if(currDir == null) { 59 try { 60 currDir = Controller.getCurrDir(); 61 } 62 catch(FileNotFoundException ex) { 63 if(debug) log("Couldn't write the transaction data"); return; 66 } 67 } 68 69 72 String id = req.getQueryString(); 73 if(id == null || id.length() == 0) { 74 if(debug) log("Bad request, exiting..."); return; 76 } 77 78 id = id.substring(0, id.indexOf(Constants.Punctuation.itemSep)); 79 80 if(debug) log(" Trying to add the transaction"); FileObject fo = null; 82 83 try { 84 if(debug) log(" Before creating the file"); fo = currDir.createData(id, "xml"); if(debug) log(" After creating the file"); } 88 catch(IOException ioex) { 89 if(debug) log(" Could not create the file, exiting..."); 90 return; 91 } 92 FileLock lock = null; 93 try { 94 lock = fo.lock(); 95 if(debug) log(" Got the lock"); } 97 catch(FileAlreadyLockedException falex) { 98 if(debug) log(" Couldn't get a file lock, exiting..."); return; 100 } 101 102 boolean success = false; 103 try { 104 PrintWriter fout = new PrintWriter (fo.getOutputStream(lock)); 105 try { 106 InputStreamReader isr = new InputStreamReader (req.getInputStream()); 107 try { 108 char[] charBuf = new char[4096]; 109 int numChars; 110 111 while((numChars = isr.read(charBuf, 0, 4096)) != -1) { 112 fout.write(charBuf, 0, numChars); 113 } 114 } finally { 115 isr.close(); 116 } 117 } finally { 118 fout.close(); 119 } 120 success = true; 121 if(debug) log("...success"); } 123 catch(IOException ioex) { 124 if (debug) { 125 log("Failed to read/write the record:"); 126 log(ioex); 127 } 128 } 129 finally { 130 lock.releaseLock(); 131 132 try { 133 res.setContentType("text/plain"); PrintWriter out = res.getWriter(); 135 try { 136 out.println(Constants.Comm.ACK); 137 } finally { 138 out.close(); 139 } 140 } catch(Exception ex) { 141 } 143 } 144 final boolean success2 = success; 145 final String id2 = id; 146 SwingUtilities.invokeLater(new Runnable () { 148 public void run () { 149 if(success2) { 150 MonitorAction.addTransaction(id2); 151 } 152 }}); 153 } 154 155 public void doGet(HttpServletRequest req, HttpServletResponse res) 157 throws ServletException , IOException { 158 159 if(debug) log("doGet"); 161 PrintWriter out = res.getWriter(); 162 try { 163 out.println("Shouldn't use GET for this!"); } 166 catch (Exception e) { 167 if(debug) log(e.getMessage()); 168 } 169 try { out.close(); } catch(Exception ex) {} 170 } 171 172 173 177 public void init(ServletConfig servletConfig) { 178 179 this.servletConfig = servletConfig; 180 if(debug) log("init"); } 182 183 public void log(String msg) { 184 System.out.println("PutTransaction::" + msg); 186 } 187 188 public void log(Throwable t) { 189 log(getStackTrace(t)); 190 } 191 192 193 public static String getStackTrace(Throwable t) { 194 195 String stackTrace = null; 196 197 try { 198 StringWriter sw = new StringWriter (); 199 PrintWriter pw = new PrintWriter (sw); 200 t.printStackTrace(pw); 201 pw.close(); 202 sw.close(); 203 stackTrace = sw.getBuffer().toString(); 204 } 205 catch(Exception ex) {} 206 return stackTrace; 207 } 208 209 } 211 212 213 | Popular Tags |