Search in sources :

Example 1 with Call

use of org.directwebremoting.extend.Call 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;
    }
}
Also used : Call(org.directwebremoting.extend.Call) Future(edu.emory.mathcs.backport.java.util.concurrent.Future) Reply(org.directwebremoting.extend.Reply) ExecutionException(edu.emory.mathcs.backport.java.util.concurrent.ExecutionException) Replies(org.directwebremoting.extend.Replies) TimeoutException(edu.emory.mathcs.backport.java.util.concurrent.TimeoutException)

Example 2 with Call

use of org.directwebremoting.extend.Call 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;
}
Also used : Call(org.directwebremoting.extend.Call) Reply(org.directwebremoting.extend.Reply) Replies(org.directwebremoting.extend.Replies)

Example 3 with Call

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

the class BaseCallMarshaller method marshallInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.extend.Marshaller#marshallInbound(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
public Calls marshallInbound(HttpServletRequest request, HttpServletResponse response) throws IOException, ServerException {
    // We must parse the parameters before we setup the conduit because it's
    // only after doing this that we know the scriptSessionId
    WebContext webContext = WebContextFactory.get();
    Batch batch = (Batch) request.getAttribute(ATTRIBUTE_BATCH);
    if (batch == null) {
        batch = new Batch(request, crossDomainSessionSecurity, allowGetForSafariButMakeForgeryEasier, sessionCookieName);
        // Save calls for retry exception
        request.setAttribute(ATTRIBUTE_BATCH, batch);
    }
    // Various bits of the Batch need to be stashed away places
    storeParsedRequest(request, webContext, batch);
    Calls calls = batch.getCalls();
    // Debug the environment
    if (log.isDebugEnabled() && calls.getCallCount() > 0) {
        // We can just use 0 because they are all shared
        InboundContext inctx = (InboundContext) batch.getInboundContexts().get(0);
        StringBuffer buffer = new StringBuffer();
        for (Iterator it = inctx.getInboundVariableNames(); it.hasNext(); ) {
            String key = (String) it.next();
            InboundVariable value = inctx.getInboundVariable(key);
            if (key.startsWith(ProtocolConstants.INBOUND_CALLNUM_PREFIX) && key.indexOf(ProtocolConstants.INBOUND_CALLNUM_SUFFIX + ProtocolConstants.INBOUND_KEY_ENV) != -1) {
                buffer.append(key);
                buffer.append('=');
                buffer.append(value.toString());
                buffer.append(", ");
            }
        }
        if (buffer.length() > 0) {
            log.debug("Environment:  " + buffer.toString());
        }
    }
    callLoop: for (int callNum = 0; callNum < calls.getCallCount(); callNum++) {
        Call call = calls.getCall(callNum);
        InboundContext inctx = (InboundContext) batch.getInboundContexts().get(callNum);
        // 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());
        // Which method are we using?
        Method method = findMethod(call, inctx);
        if (method == null) {
            String name = call.getScriptName() + '.' + call.getMethodName();
            String error = Messages.getString("BaseCallMarshaller.UnknownMethod", name);
            log.warn("Marshalling exception: " + error);
            call.setMethod(null);
            call.setParameters(null);
            call.setException(new IllegalArgumentException(error));
            continue callLoop;
        }
        call.setMethod(method);
        // Check this method is accessible
        accessControl.assertExecutionIsPossible(creator, call.getScriptName(), method);
        // Convert all the parameters to the correct types
        Object[] params = new Object[method.getParameterTypes().length];
        for (int j = 0; j < method.getParameterTypes().length; j++) {
            try {
                Class paramType = method.getParameterTypes()[j];
                InboundVariable param = inctx.getParameter(callNum, j);
                TypeHintContext incc = new TypeHintContext(converterManager, method, j);
                params[j] = converterManager.convertInbound(paramType, param, inctx, incc);
            } catch (MarshallException ex) {
                log.warn("Marshalling exception", ex);
                call.setMethod(null);
                call.setParameters(null);
                call.setException(ex);
                continue callLoop;
            }
        }
        call.setParameters(params);
    }
    return calls;
}
Also used : Call(org.directwebremoting.extend.Call) WebContext(org.directwebremoting.WebContext) TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundContext(org.directwebremoting.extend.InboundContext) Calls(org.directwebremoting.extend.Calls) InboundVariable(org.directwebremoting.extend.InboundVariable) Creator(org.directwebremoting.extend.Creator) Method(java.lang.reflect.Method) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator)

Example 4 with Call

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

the class Batch method parseParameters.

/**
 * Fish out the important parameters
 * @throws ServerException If the parsing of input parameter fails
 */
protected void parseParameters() throws ServerException {
    Map paramMap = getAllParameters();
    calls = new Calls();
    // Work out how many calls are in this packet
    String callStr = (String) paramMap.remove(ProtocolConstants.INBOUND_CALL_COUNT);
    int callCount;
    try {
        callCount = Integer.parseInt(callStr);
    } catch (NumberFormatException ex) {
        throw new ServerException(Messages.getString("BaseCallMarshaller.BadCallCount"));
    }
    // Extract the ids, scriptnames and methodnames
    for (int callNum = 0; callNum < callCount; callNum++) {
        Call call = new Call();
        calls.addCall(call);
        InboundContext inctx = new InboundContext();
        inboundContexts.add(inctx);
        String prefix = ProtocolConstants.INBOUND_CALLNUM_PREFIX + callNum + ProtocolConstants.INBOUND_CALLNUM_SUFFIX;
        // The special values
        String callId = (String) paramMap.remove(prefix + ProtocolConstants.INBOUND_KEY_ID);
        call.setCallId(callId);
        if (!LocalUtil.isLetterOrDigitOrUnderline(callId)) {
            throw new SecurityException("Call IDs may only contain Java Identifiers");
        }
        String scriptName = (String) paramMap.remove(prefix + ProtocolConstants.INBOUND_KEY_SCRIPTNAME);
        call.setScriptName(scriptName);
        if (!LocalUtil.isLetterOrDigitOrUnderline(scriptName)) {
            throw new SecurityException("Script names may only contain Java Identifiers");
        }
        String methodName = (String) paramMap.remove(prefix + ProtocolConstants.INBOUND_KEY_METHODNAME);
        call.setMethodName(methodName);
        if (!LocalUtil.isLetterOrDigitOrUnderline(methodName)) {
            throw new SecurityException("Method names may only contain Java Identifiers");
        }
        // Look for parameters to this method
        for (Iterator it = paramMap.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            if (key.startsWith(prefix)) {
                String data = (String) entry.getValue();
                String[] split = ParseUtil.splitInbound(data);
                String value = split[LocalUtil.INBOUND_INDEX_VALUE];
                String type = split[LocalUtil.INBOUND_INDEX_TYPE];
                inctx.createInboundVariable(callNum, key, type, value);
                it.remove();
            }
        }
    }
    String batchId = (String) paramMap.remove(ProtocolConstants.INBOUND_KEY_BATCHID);
    calls.setBatchId(batchId);
    if (!LocalUtil.isLetterOrDigitOrUnderline(batchId)) {
        throw new SecurityException("Batch IDs may only contain Java Identifiers");
    }
    httpSessionId = (String) paramMap.remove(ProtocolConstants.INBOUND_KEY_HTTP_SESSIONID);
    scriptSessionId = (String) paramMap.remove(ProtocolConstants.INBOUND_KEY_SCRIPT_SESSIONID);
    page = (String) paramMap.remove(ProtocolConstants.INBOUND_KEY_PAGE);
    for (Iterator it = paramMap.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();
        String key = (String) entry.getKey();
        String value = (String) entry.getValue();
        if (key.startsWith(ProtocolConstants.INBOUND_KEY_METADATA)) {
            spareParameters.put(key.substring(ProtocolConstants.INBOUND_KEY_METADATA.length()), value);
        }
    }
}
Also used : Call(org.directwebremoting.extend.Call) ServerException(org.directwebremoting.extend.ServerException) InboundContext(org.directwebremoting.extend.InboundContext) Calls(org.directwebremoting.extend.Calls) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Call (org.directwebremoting.extend.Call)4 Iterator (java.util.Iterator)2 Calls (org.directwebremoting.extend.Calls)2 InboundContext (org.directwebremoting.extend.InboundContext)2 Replies (org.directwebremoting.extend.Replies)2 Reply (org.directwebremoting.extend.Reply)2 ExecutionException (edu.emory.mathcs.backport.java.util.concurrent.ExecutionException)1 Future (edu.emory.mathcs.backport.java.util.concurrent.Future)1 TimeoutException (edu.emory.mathcs.backport.java.util.concurrent.TimeoutException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WebContext (org.directwebremoting.WebContext)1 Creator (org.directwebremoting.extend.Creator)1 InboundVariable (org.directwebremoting.extend.InboundVariable)1 MarshallException (org.directwebremoting.extend.MarshallException)1 ServerException (org.directwebremoting.extend.ServerException)1 TypeHintContext (org.directwebremoting.extend.TypeHintContext)1