KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > PutTransaction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.monitor.client;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.servlet.ServletConfig JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServlet JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
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 /*
44  * Put a transaction
45  */

46   
47 public class PutTransaction extends HttpServlet JavaDoc {
48
49     private static FileObject currDir = null;
50     private static boolean debug = false;
51      
52     private ServletConfig JavaDoc servletConfig = null;
53
54     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
55     throws ServletException JavaDoc, IOException JavaDoc {
56      
57     if(debug) log("doPost"); //NOI18N
58
if(currDir == null) {
59         try {
60         currDir = Controller.getCurrDir();
61         }
62         catch(FileNotFoundException JavaDoc ex) {
63         // PENDING report this error properly
64
if(debug) log("Couldn't write the transaction data"); //NOI18N
65
return;
66         }
67     }
68
69     // As soon as you get the parameters, you've gotten an input
70
// string for this. Don't do that.
71

72     String JavaDoc id = req.getQueryString();
73     if(id == null || id.length() == 0) {
74         if(debug) log("Bad request, exiting..."); //NOI18N
75
return;
76     }
77
78     id = id.substring(0, id.indexOf(Constants.Punctuation.itemSep));
79
80     if(debug) log(" Trying to add the transaction"); //NOI18N
81
FileObject fo = null;
82      
83     try {
84         if(debug) log(" Before creating the file"); //NOI18N
85
fo = currDir.createData(id, "xml"); //NOI18N
86
if(debug) log(" After creating the file"); //NOI18N
87
}
88     catch(IOException JavaDoc 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"); //NOI18N
96
}
97     catch(FileAlreadyLockedException falex) {
98         if(debug) log(" Couldn't get a file lock, exiting..."); //NOI18N
99
return;
100     }
101
102     boolean success = false;
103     try {
104         PrintWriter JavaDoc fout = new PrintWriter JavaDoc(fo.getOutputStream(lock));
105         try {
106             InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(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"); //NOI18N
122
}
123     catch(IOException JavaDoc 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"); //NOI18N
134
PrintWriter JavaDoc out = res.getWriter();
135             try {
136                 out.println(Constants.Comm.ACK);
137             } finally {
138                 out.close();
139             }
140         } catch(Exception JavaDoc ex) {
141             // It doesn't actually matter if this goes wrong
142
}
143     }
144         final boolean success2 = success;
145         final String JavaDoc id2 = id;
146         // window system code must be run in AWT thread
147
SwingUtilities.invokeLater(new Runnable JavaDoc() {
148             public void run () {
149                 if(success2) {
150             MonitorAction.addTransaction(id2);
151         }
152         }});
153     }
154
155     // PENDING - deal better with this
156
public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
157     throws ServletException JavaDoc, IOException JavaDoc {
158
159     if(debug) log("doGet"); //NOI18N
160

161     PrintWriter JavaDoc out = res.getWriter();
162     try {
163         //out.println(id);
164
out.println("Shouldn't use GET for this!"); //NOI18N
165
}
166     catch (Exception JavaDoc e) {
167         if(debug) log(e.getMessage());
168     }
169     try { out.close(); } catch(Exception JavaDoc ex) {}
170     }
171
172
173     /**
174      * Init method for this filter
175      *
176      */

177     public void init(ServletConfig JavaDoc servletConfig) {
178
179     this.servletConfig = servletConfig;
180     if(debug) log("init"); //NOI18N
181
}
182     
183     public void log(String JavaDoc msg) {
184     System.out.println("PutTransaction::" + msg); //NOI18N
185

186     }
187
188     public void log(Throwable JavaDoc t) {
189     log(getStackTrace(t));
190     }
191
192
193     public static String JavaDoc getStackTrace(Throwable JavaDoc t) {
194
195     String JavaDoc stackTrace = null;
196         
197     try {
198         StringWriter JavaDoc sw = new StringWriter JavaDoc();
199         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
200         t.printStackTrace(pw);
201         pw.close();
202         sw.close();
203         stackTrace = sw.getBuffer().toString();
204     }
205     catch(Exception JavaDoc ex) {}
206     return stackTrace;
207     }
208
209 } //PutTransaction.java
210

211
212
213
Popular Tags