use of org.directwebremoting.extend.Reply in project ma-core-public by infiniteautomation.
the class ParallelDefaultRemoter method execute.
/**
* Execute a set of remote calls in parallel and generate set of reply data
* for later conversion to whatever wire protocol we are using today.
* @param calls The set of calls to execute in parallel
* @return A set of reply data objects
*/
public Replies execute(Calls calls) {
Replies replies = new Replies(calls.getBatchId());
Future[] future = new Future[calls.getCallCount()];
if (calls.getCallCount() == 1) {
return super.execute(calls);
} else {
for (int callNum = 0; callNum < calls.getCallCount(); callNum++) {
Call call = calls.getCall(callNum);
future[callNum] = executorService.submit(new OneCall(call));
}
for (int callNum = 0; callNum < calls.getCallCount(); callNum++) {
try {
Reply reply = (Reply) future[callNum].get(this.timeout, TimeUnit.MILLISECONDS);
replies.addReply(reply);
} catch (InterruptedException ex) {
log.warn("Method execution failed: ", ex);
replies.addReply(new Reply(calls.getCall(callNum).getCallId(), null, ex));
} catch (ExecutionException ex) {
log.warn("Method execution failed: ", ex);
replies.addReply(new Reply(calls.getCall(callNum).getCallId(), null, ex));
} catch (TimeoutException ex) {
log.warn("Method execution failed: ", ex);
replies.addReply(new Reply(calls.getCall(callNum).getCallId(), null, ex));
}
}
return replies;
}
}
use of org.directwebremoting.extend.Reply 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.Reply in project ma-core-public by infiniteautomation.
the class DefaultRemoter method execute.
/* (non-Javadoc)
* @see org.directwebremoting.Remoter#execute(org.directwebremoting.Calls)
*/
public Replies execute(Calls calls) {
Replies replies = new Replies(calls.getBatchId());
int callCount = calls.getCallCount();
if (callCount > maxCallCount) {
log.error("Call count for batch exceeds maxCallCount. Add an init-param of maxCallCount to increase this limit");
throw new SecurityException("Call count for batch is too high");
}
for (int callNum = 0; callNum < callCount; callNum++) {
Call call = calls.getCall(callNum);
Reply reply = execute(call);
replies.addReply(reply);
}
return replies;
}
use of org.directwebremoting.extend.Reply in project ma-core-public by infiniteautomation.
the class DefaultRemoter method execute.
/**
* Execute a single call object
* @param call The call to execute
* @return A Reply to the Call
*/
public Reply execute(Call call) {
try {
Method method = call.getMethod();
if (method == null || call.getException() != null) {
return new Reply(call.getCallId(), null, call.getException());
}
// Get a list of the available matching methods with the coerced
// parameters that we will use to call it if we choose to use that
// method.
Creator creator = creatorManager.getCreator(call.getScriptName());
// We don't need to check accessControl.getReasonToNotExecute()
// because the checks are made by the doExec method, but we do check
// if we can display it
accessControl.assertExecutionIsPossible(creator, call.getScriptName(), method);
// Get ourselves an object to execute a method on unless the
// method is static
Object object = null;
String scope = creator.getScope();
boolean create = false;
if (!Modifier.isStatic(method.getModifiers())) {
WebContext webcx = WebContextFactory.get();
// Check the various scopes to see if it is there
if (scope.equals(Creator.APPLICATION)) {
object = webcx.getServletContext().getAttribute(call.getScriptName());
} else if (scope.equals(Creator.SESSION)) {
object = webcx.getSession().getAttribute(call.getScriptName());
} else if (scope.equals(Creator.SCRIPT)) {
object = webcx.getScriptSession().getAttribute(call.getScriptName());
} else if (scope.equals(Creator.REQUEST)) {
object = webcx.getHttpServletRequest().getAttribute(call.getScriptName());
}
// If we don't have an object the call the creator
if (object == null) {
create = true;
object = creator.getInstance();
}
// Remember it for next time
if (create) {
if (scope.equals(Creator.APPLICATION)) {
// This might also be done at application startup by
// DefaultCreatorManager.addCreator(String, Creator)
webcx.getServletContext().setAttribute(call.getScriptName(), object);
} else if (scope.equals(Creator.SESSION)) {
webcx.getSession().setAttribute(call.getScriptName(), object);
} else if (scope.equals(Creator.SCRIPT)) {
webcx.getScriptSession().setAttribute(call.getScriptName(), object);
} else if (scope.equals(Creator.REQUEST)) {
webcx.getHttpServletRequest().setAttribute(call.getScriptName(), object);
}
// Creator.PAGE scope means we create one every time anyway
}
}
// Some debug
log.info("Exec: " + call.getScriptName() + "." + call.getMethodName() + "()");
if (log.isDebugEnabled()) {
StringBuffer buffer = new StringBuffer();
if (create) {
buffer.append("--Object created, ");
if (!scope.equals(Creator.PAGE)) {
buffer.append(" stored in ");
buffer.append(scope);
} else {
buffer.append(" not stored");
}
} else {
buffer.append("--Object found in ");
buffer.append(scope);
}
buffer.append(". ");
// It would be good to debug the params but it's not easy
// buffer.append("Call params (");
// for (int j = 0; j < inctx.getParameterCount(callNum); j++)
// {
// if (j != 0)
// {
// buffer.append(", ");
// }
// InboundVariable param = inctx.getParameter(callNum, j);
// buffer.append(param.toString());
// }
// buffer.append(") ");
buffer.append("id=");
buffer.append(call.getCallId());
log.debug(buffer.toString());
}
// Execute the filter chain method.toString()
final Iterator it = ajaxFilterManager.getAjaxFilters(call.getScriptName());
AjaxFilterChain chain = new AjaxFilterChain() {
public Object doFilter(Object obj, Method meth, Object[] p) throws Exception {
AjaxFilter next = (AjaxFilter) it.next();
return next.doFilter(obj, meth, p, this);
}
};
Object reply = chain.doFilter(object, method, call.getParameters());
return new Reply(call.getCallId(), reply);
} catch (InvocationTargetException ex) {
// Allow Jetty RequestRetry exception to propogate to container
Continuation.rethrowIfContinuation(ex);
log.warn("Method execution failed: ", ex.getTargetException());
return new Reply(call.getCallId(), null, ex.getTargetException());
} catch (Exception ex) {
// Allow Jetty RequestRetry exception to propogate to container
Continuation.rethrowIfContinuation(ex);
log.warn("Method execution failed: ", ex);
return new Reply(call.getCallId(), null, ex);
}
}
Aggregations