001: /******************************************************************************
002: * ResponderPROFILE.java
003: * ****************************************************************************/package org.openlaszlo.servlets.responders;
004:
005: import java.io.*;
006: import javax.servlet.*;
007: import javax.servlet.http.*;
008: import javax.servlet.ServletOutputStream;
009: import org.openlaszlo.compiler.Compiler;
010: import org.openlaszlo.compiler.CompilationEnvironment;
011: import org.openlaszlo.utils.LZHttpUtils;
012: import org.openlaszlo.media.MimeType;
013: import org.openlaszlo.sc.ScriptCompiler;
014: import org.openlaszlo.server.LPS;
015: import org.openlaszlo.utils.FileUtils;
016: import java.util.*;
017: import org.apache.log4j.Logger;
018:
019: // We don't expect a lot of use of this, for simplicity, we make it
020: // single-threaded (so the open file is shared without collisions)
021: public final class ResponderPROFILE extends Responder implements
022: SingleThreadModel {
023: private static Logger mLogger = Logger
024: .getLogger(ResponderPROFILE.class);
025: private static OutputStream output;
026:
027: private HashMap datachunks;
028:
029: protected void respondImpl(HttpServletRequest req,
030: HttpServletResponse res) throws IOException {
031: synchronized (this ) {
032: String command = req.getParameter("command");
033: String seqparam = req.getParameter("seqnum");
034:
035: mLogger.info("PROFILE_OUTPUT = " + command + " ["
036: + seqparam != null ? seqparam : "] " + output);
037: if (output != null) {
038: if ("data".equals(command)) {
039: mLogger.info("PROFILE_DATA");
040:
041: Integer seqnum = new Integer(0);
042: try {
043: seqnum = new Integer(seqparam);
044: } catch (NumberFormatException e) {
045: mLogger.info("PROFILE couldn't parse seqnum: "
046: + seqparam);
047: }
048:
049: ByteArrayOutputStream dout = new ByteArrayOutputStream();
050: // Create a data chunk for this sequence number
051: this .datachunks.put(seqnum, dout);
052: String pdata = req.getParameter("pdata");
053: byte data[] = pdata.getBytes();
054: // Copy the data into the chunk
055: mLogger.info("PROFILE_DATA storing chunk #"
056: + seqnum + ", write len: " + data.length);
057: dout.write(data, 0, data.length);
058: respondWithMessage(res, "Data");
059: } else if ("close".equals(command)) {
060: mLogger.info("PROFILE_CLOSE");
061:
062: String nitems = req.getParameter("nchunks");
063: int nchunks = 0;
064:
065: try {
066: nchunks = Integer.parseInt(nitems);
067: } catch (NumberFormatException e) {
068: mLogger
069: .info("PROFILE couldn't parse nchunks arg: "
070: + nitems);
071: }
072: mLogger.info("PROFILE client close expects "
073: + nitems + " data chunks");
074:
075: // Copy data chunks in sequence order. Count index from zero
076: // up to the number of expected messages.
077: int k = 0;
078: for (k = 0; k < nchunks; k++) {
079: Integer i = new Integer(k);
080: if (!datachunks.containsKey(i)) {
081: mLogger.info("PROFILE missing data chunk "
082: + k + ", expected " + nchunks);
083: continue;
084: }
085: ByteArrayOutputStream b = (ByteArrayOutputStream) datachunks
086: .get(i);
087: byte bdata[] = b.toByteArray();
088: // copy message to output stream
089: mLogger.info("PROFILE_DATA write chunk #" + i
090: + ", length:" + bdata.length);
091: output.write(bdata, 0, bdata.length);
092: }
093:
094: output.flush();
095: output.close();
096: output = null;
097: respondWithMessage(res, "Closed");
098: }
099: } else if ("open".equals(command)) {
100: // getRealPath?
101: String uri = LZHttpUtils.getRealPath(mContext, req);
102: this .datachunks = new HashMap();
103:
104: // try to clean up a little
105: try {
106: if (output != null) {
107: output.close();
108: }
109: } catch (Exception e) {
110: }
111:
112: // Modicum of security: Only profile apps that exist
113: if ((new File(uri)).exists()) {
114: uri = uri.substring(0, uri.lastIndexOf('.'));
115: File file = new File(uri + ".profiler");
116: mLogger.info("PROFILE_OPEN " + file);
117: file.createNewFile();
118: output = new FileOutputStream(file);
119: respondWithMessage(res, "Open");
120: }
121: }
122: }
123: }
124:
125: public int getMimeType() {
126: return MIME_TYPE_XML;
127: }
128: }
129:
130: /*
131: * Copyright 2006 Laszlo Systems, Inc. All Rights Reserved. Use is
132: * subject to license terms.
133: */
|