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());
}
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");
}
}
}
}
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);
}
}
}
Aggregations