Search in sources :

Example 1 with ScriptConduit

use of org.directwebremoting.extend.ScriptConduit in project ma-core-public by infiniteautomation.

the class BaseCallMarshaller method marshallOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Marshaller#marshallOutbound(org.directwebremoting.Replies, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
public void marshallOutbound(Replies replies, HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the output stream and setup the mimetype
    response.setContentType(getOutboundMimeType());
    PrintWriter out;
    if (log.isDebugEnabled()) {
        // This might be considered evil - altering the program flow
        // depending on the log status, however DebuggingPrintWriter is
        // very thin and only about logging
        out = new DebuggingPrintWriter("", response.getWriter());
    } else {
        out = response.getWriter();
    }
    // The conduit to pass on reverse ajax scripts
    ScriptConduit conduit = new CallScriptConduit(out);
    // Setup a debugging prefix
    if (out instanceof DebuggingPrintWriter) {
        DebuggingPrintWriter dpw = (DebuggingPrintWriter) out;
        dpw.setPrefix("out(" + conduit.hashCode() + "): ");
    }
    // Send the script prefix (if any)
    sendOutboundScriptPrefix(out, replies.getBatchId());
    // From the call to addScriptConduit() there could be 2 threads writing
    // to 'out' so we synchronize on 'out' to make sure there are no
    // clashes
    RealScriptSession scriptSession = (RealScriptSession) WebContextFactory.get().getScriptSession();
    out.println(ProtocolConstants.SCRIPT_CALL_INSERT);
    scriptSession.writeScripts(conduit);
    out.println(ProtocolConstants.SCRIPT_CALL_REPLY);
    String batchId = replies.getBatchId();
    for (int i = 0; i < replies.getReplyCount(); i++) {
        Reply reply = replies.getReply(i);
        String callId = reply.getCallId();
        try {
            // The existance of a throwable indicates that something went wrong
            if (reply.getThrowable() != null) {
                Throwable ex = reply.getThrowable();
                EnginePrivate.remoteHandleException(conduit, batchId, callId, ex);
                log.warn("--Erroring: batchId[" + batchId + "] message[" + ex.toString() + ']');
            } else {
                Object data = reply.getReply();
                EnginePrivate.remoteHandleCallback(conduit, batchId, callId, data);
            }
        } catch (IOException ex) {
            // We're a bit stuck we died half way through writing so
            // we can't be sure the browser can react to the failure.
            // Since we can no longer do output we just log and end
            log.error("--Output Error: batchId[" + batchId + "] message[" + ex.toString() + ']', ex);
        } catch (MarshallException ex) {
            EnginePrivate.remoteHandleMarshallException(conduit, batchId, callId, ex);
            log.warn("--MarshallException: batchId=" + batchId + " class=" + ex.getConversionType().getName(), ex);
        } catch (Exception ex) {
            // This is a bit of a "this can't happen" case so I am a bit
            // nervous about sending the exception to the client, but we
            // want to avoid silently dying so we need to do something.
            EnginePrivate.remoteHandleException(conduit, batchId, callId, ex);
            log.error("--MarshallException: batchId=" + batchId + " message=" + ex.toString());
        }
    }
    sendOutboundScriptSuffix(out, replies.getBatchId());
}
Also used : IOException(java.io.IOException) MarshallException(org.directwebremoting.extend.MarshallException) IOException(java.io.IOException) ServerException(org.directwebremoting.extend.ServerException) DebuggingPrintWriter(org.directwebremoting.util.DebuggingPrintWriter) ScriptConduit(org.directwebremoting.extend.ScriptConduit) MarshallException(org.directwebremoting.extend.MarshallException) Reply(org.directwebremoting.extend.Reply) RealScriptSession(org.directwebremoting.extend.RealScriptSession) PrintWriter(java.io.PrintWriter) DebuggingPrintWriter(org.directwebremoting.util.DebuggingPrintWriter)

Example 2 with ScriptConduit

use of org.directwebremoting.extend.ScriptConduit in project ma-core-public by infiniteautomation.

the class DefaultScriptSession method addScript.

/* (non-Javadoc)
     * @see org.directwebremoting.ScriptSession#addScript(java.lang.String)
     */
public void addScript(ScriptBuffer script) {
    checkNotInvalidated();
    if (script == null) {
        throw new NullPointerException("null script");
    }
    // First we try to add the script to an existing conduit
    synchronized (this.scripts) {
        if (conduits.size() == 0) {
            // There are no conduits, just store it until there are
            scripts.add(script);
        // log.debug("- No conduits. Adding script to waiting list");
        } else {
            // Try all the conduits, starting with the first
            boolean written = false;
            // The conduit.addScript call is an external call which eventually makes its way back here
            // and into the removeScriptConduit method.  Since removeScriptConduit may modify the conduits
            // collection we need to make a protective copy here to prevent ConcurrentModExceptions.
            List conduitsList;
            synchronized (conduits) {
                conduitsList = new ArrayList(conduits);
            }
            for (Iterator it = conduitsList.iterator(); !written && it.hasNext(); ) {
                ScriptConduit conduit = (ScriptConduit) it.next();
                try {
                    written = conduit.addScript(script);
                // log.debug("- Adding script to conduit (written=" + written + "): " + conduit);
                } catch (Exception ex) {
                    it.remove();
                    log.debug("Failed to write to ScriptConduit, removing from list: " + conduit);
                }
            }
            if (!written) {
                scripts.add(script);
            // log.debug("- No conduits passed it on. Adding script to waiting list");
            }
        }
    }
}
Also used : ScriptConduit(org.directwebremoting.extend.ScriptConduit) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) MarshallException(org.directwebremoting.extend.MarshallException)

Example 3 with ScriptConduit

use of org.directwebremoting.extend.ScriptConduit in project ma-core-public by infiniteautomation.

the class DefaultScriptSession method debug.

/**
 * Some debug output
 */
private void debug() {
    if (log.isDebugEnabled()) {
        log.debug("Known ScriptConduits:");
        for (Iterator it = conduits.iterator(); it.hasNext(); ) {
            ScriptConduit c = (ScriptConduit) it.next();
            log.debug("- " + c);
        }
    }
}
Also used : ScriptConduit(org.directwebremoting.extend.ScriptConduit) Iterator(java.util.Iterator)

Aggregations

ScriptConduit (org.directwebremoting.extend.ScriptConduit)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 MarshallException (org.directwebremoting.extend.MarshallException)2 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RealScriptSession (org.directwebremoting.extend.RealScriptSession)1 Reply (org.directwebremoting.extend.Reply)1 ServerException (org.directwebremoting.extend.ServerException)1 DebuggingPrintWriter (org.directwebremoting.util.DebuggingPrintWriter)1