public EvalResult apply(Evaluator e, Continuation c, Value v) {
   if (v instanceof DynamicException) {
     return c.apply(v);
   } else {
     return c.apply(apply(v));
   }
 }
  /**
   * Resolve to context named by 'name'. Returns true if at named context (i.e. 'name' is empty
   * name). Returns false otherwise, and sets Continuation on parts of 'name' not yet resolved.
   */
  protected boolean resolve_to_context(Name name, Continuation cont) throws NamingException {
    String target = name.toString();

    StringHeadTail ht = c_parseComponent(target, cont);
    String tail = ht.getTail();
    String head = ht.getHead();

    if (debug > 0)
      System.out.println("RESOLVE TO CONTEXT(" + target + ") = {" + head + ", " + tail + "}");

    if (head == null) {
      // something is wrong; no name at all
      InvalidNameException e = new InvalidNameException();
      throw cont.fillInException(e);
    }
    if (!isEmpty(head)) {
      // if there is head is a non-empty name
      // this means more resolution to be done
      try {
        Object headCtx = a_lookup(head, cont);
        //              System.out.println("answer " + headCtx);
        if (headCtx != null) cont.setContinue(headCtx, head, this, (tail == null ? "" : tail));
        else if (cont.isContinue()) cont.appendRemainingComponent(tail);
      } catch (NamingException e) {
        e.appendRemainingComponent(tail);
        throw e;
      }
    } else {
      cont.setSuccess(); // clear
      return true;
    }
    return false;
  }
  /**
   * Resolves to penultimate context named by 'name'. Returns true if penultimate context has been
   * reached (i.e. name only has one atomic component left). Returns false otherwise, and sets
   * Continuation to parts of name not yet resolved.
   */
  protected boolean resolve_to_penultimate_context(Name name, Continuation cont)
      throws NamingException {
    String target = name.toString();

    if (debug > 0) System.out.println("RESOLVE TO PENULTIMATE" + target);

    StringHeadTail ht = c_parseComponent(target, cont);
    String tail = ht.getTail();
    String head = ht.getHead();
    if (head == null) {
      // something is wrong; no name at all
      InvalidNameException e = new InvalidNameException();
      throw cont.fillInException(e);
    }

    if (!isEmpty(tail)) {
      // more components; hence not at penultimate context yet
      try {
        Object headCtx = a_lookup(head, cont);
        if (headCtx != null) cont.setContinue(headCtx, head, this, tail);
        else if (cont.isContinue()) cont.appendRemainingComponent(tail);
      } catch (NamingException e) {
        e.appendRemainingComponent(tail);
        throw e;
      }
    } else {
      // already at penultimate context
      cont.setSuccess(); // clear
      return true;
    }
    return false;
  }
  /**
   * Method which returns the previous entry for the given entry
   *
   * @param entry The entry to fetch the previous entry for
   * @command The command to return the result to
   */
  protected void getPreviousEntry(LogEntry entry, Continuation command) {
    if (entry == entries[0]) {
      getPreviousEntry(
          new StandardContinuation(command) {
            public void receiveResult(Object o) {
              if (o instanceof CoalescedLogEntry) {
                LogEntry[] otherEntries = ((CoalescedLogEntry) o).getEntries();
                parent.receiveResult(otherEntries[otherEntries.length - 1]);
              } else {
                parent.receiveResult(o);
              }
            }
          });
    } else {
      for (int i = 1; i < entries.length; i++)
        if (entries[i] == entry) {
          command.receiveResult(entries[i - 1]);
          return;
        }

      if (entry == null) {
        command.receiveResult(entries[entries.length - 1]);
        return;
      }

      command.receiveException(
          new IllegalArgumentException("ERROR: Could not find previous entry for " + entry));
    }
  }
 /**
  * Marks a var as referenced, recursing into any values of this var that we skipped.
  *
  * @return True if this variable had not been referenced before.
  */
 private boolean markReferencedVar(Var var) {
   if (referenced.add(var)) {
     for (Continuation c : continuations.get(var)) {
       c.apply();
     }
     return true;
   }
   return false;
 }
  /**
   * Resolves the nns for 'name' when the named context is acting as an intermediate context.
   *
   * <p>For a system that supports junctions, this would be equilvalent to a_lookup(name, cont);
   * because for junctions, an intermediate slash simply signifies a syntactic separator.
   *
   * <p>For a system that supports implicit nns, this would be equivalent to a_lookup_nns(name,
   * cont); because for implicit nns, a slash always signifies the implicit nns, regardless of
   * whether it is intermediate or trailing.
   *
   * <p>By default this method supports junctions, and also allows for an implicit nns to be
   * dynamically determined through the use of the "nns" reference (see a_processJunction_nns()).
   * Contexts that implement implicit nns directly should provide an appropriate override.
   */
  protected Object a_resolveIntermediate_nns(String name, Continuation cont)
      throws NamingException {
    try {
      final Object obj = a_lookup(name, cont);

      // Do not append "" to Continuation 'cont' even if set
      // because the intention is to ignore the nns

      //
      if (obj != null && getClass().isInstance(obj)) {
        // If "obj" is in the same type as this object, it must
        // not be a junction. Continue the lookup with "/".

        cont.setContinueNNS(obj, name, this);
        return null;

      } else if (obj != null && !(obj instanceof Context)) {
        // obj is not even a context, so try to find its nns
        // dynamically by constructing a Reference containing obj.
        RefAddr addr =
            new RefAddr("nns") {
              public Object getContent() {
                return obj;
              }

              private static final long serialVersionUID = -3399518522645918499L;
            };
        Reference ref = new Reference("java.lang.Object", addr);

        // Resolved name has trailing slash to indicate nns
        CompositeName resName = new CompositeName();
        resName.add(name);
        resName.add(""); // add trailing slash

        // Set continuation leave it to
        // PartialCompositeContext.getPCContext() to throw CPE.
        // Do not use setContinueNNS() because we've already
        // consumed "/" (i.e., moved it to resName).

        cont.setContinue(ref, resName, this);
        return null;

      } else {
        return obj;
      }

    } catch (NamingException e) {
      e.appendRemainingComponent(""); // add nns back
      throw e;
    }
  }
  /** Release the continuation */
  public void release() {
    if (continuation != null) {
      try {
        continuation.release();
        ctx.releaseManaged();
      } catch (InterruptedException e) {
        return;
      }
      ctx = null;

      continuation = null;
      script = null;
      scriptClosure = null;

      try {
        getContext().getWriter().flush();
      } catch (Exception e) {
        Util.printStackTrace(e);
      }
      try {
        getContext().getErrorWriter().flush();
      } catch (Exception e) {
        Util.printStackTrace(e);
      }
    }
  }
  @Override
  public Continuation apply(final ContainerRequest request) {
    final RoutingContext rc = routingContextProvider.get();
    rc.pushTemplates(resourceTemplate, methodTemplate);

    return Continuation.of(request);
  }
  @Override
  public Continuation apply(final ContainerRequest request) {
    final RoutingContext routingCtx = injector.inject(RoutingContext.class);

    Object subResource = getResource(routingCtx);
    if (subResource == null) {
      throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    if (subResource.getClass().isAssignableFrom(Class.class)) {
      final Class<?> clazz = (Class<?>) subResource;
      SingletonResourceBinder singletonResourceFactory =
          injector.inject(SingletonResourceBinder.class);
      singletonResourceFactory.bindResourceClassAsSingleton(clazz);
      subResource = injector.inject(clazz);
    }

    // TODO: what to do with the issues?
    final Resource subResourceModel =
        Resource.builder(subResource, new LinkedList<ResourceModelIssue>()).build();
    runtimeModelBuilder.process(subResourceModel, true);

    // TODO: implement generated sub-resource methodAcceptorPair caching
    routingCtx.pushMatchedResource(subResource);
    Router subResourceAcceptor = runtimeModelBuilder.buildModel(true);
    return Continuation.of(request, subResourceAcceptor);
  }
  @Override
  public Continuation apply(final RequestProcessingContext processingContext) {
    final RoutingContext rc = processingContext.routingContext();
    rc.pushMatchResult(new SingleMatchResult("/" + processingContext.request().getPath(false)));

    return Continuation.of(processingContext, rootRouter);
  }
  /** Resolves to nns associated with 'name' and set Continuation to the result. */
  protected void resolve_to_nns_and_continue(Name name, Continuation cont) throws NamingException {
    if (debug > 0) System.out.println("RESOLVE TO NNS AND CONTINUE" + name.toString());

    if (resolve_to_penultimate_context_nns(name, cont)) {
      Object nns = a_lookup_nns(name.toString(), cont);
      if (nns != null) cont.setContinue(nns, name, this);
    }
  }
 /**
  * This function is used when implementing a naming system that supports junctions. For example,
  * when the a_bind_nns(name, newobj) method is invoked, that means the caller is attempting to
  * bind the object 'newobj' to the nns of 'name'. For context that supports junctions, 'name'
  * names a junction and is pointing to the root of another naming system, which in turn might have
  * an nns. This means that a_bind_nns() should first resolve 'name' and attempt to continue the
  * operation in the context named by 'name'. (i.e. bind to the nns of the context named by
  * 'name'). If name is already empty, then throw NameNotFoundException because this context by
  * default does not have any nns.
  */
 protected void a_processJunction_nns(String name, Continuation cont) throws NamingException {
   if (name.equals("")) {
     NameNotFoundException e = new NameNotFoundException();
     cont.setErrorNNS(this, name);
     throw cont.fillInException(e);
   }
   try {
     // lookup name to continue operation in nns
     Object target = a_lookup(name, cont);
     if (cont.isContinue()) cont.appendRemainingComponent(""); // add nns back
     else {
       cont.setContinueNNS(target, name, this);
     }
   } catch (NamingException e) {
     e.appendRemainingComponent(""); // add nns back
     throw e;
   }
 }
Exemple #13
0
  @Override
  public Value value(final QueryContext ctx) throws QueryException {

    Expr fun = func;
    Var[] args = args(ctx);
    do {
      // cache arguments, evaluate function and reset variable scope
      final VarStack cs = addArgs(ctx, args);
      ctx.tailCalls = 0;
      try {
        return fun.value(ctx);
      } catch (final Continuation c) {
        fun = c.getFunc();
        args = c.getArgs();
      } finally {
        ctx.vars.reset(cs);
      }
    } while (true);
  }
 protected Object c_lookup(Name name, Continuation cont) throws NamingException {
   Object ret = null;
   if (resolve_to_penultimate_context(name, cont)) {
     ret = a_lookup(name.toString(), cont);
     if (ret != null && ret instanceof LinkRef) {
       cont.setContinue(ret, name, this);
       ret = null;
     }
   }
   return ret;
 }
  /**
   * This function is similar to resolve_to_penultimate_context() except it should only be called by
   * the nns() functions. This function fixes any exception or continuations so that it will have
   * the proper nns name.
   */
  protected boolean resolve_to_penultimate_context_nns(Name name, Continuation cont)
      throws NamingException {
    try {
      if (debug > 0) System.out.println("RESOLVE TO PENULTIMATE NNS" + name.toString());
      boolean answer = resolve_to_penultimate_context(name, cont);

      // resolve_to_penultimate_context() only calls a_lookup().
      // Any continuation it sets is lacking the nns, so
      // we need to add it back
      if (cont.isContinue()) cont.appendRemainingComponent("");

      return answer;
    } catch (NamingException e) {
      // resolve_to_penultimate_context() only calls a_lookup().
      // Any exceptions it throws is lacking the nns, so
      // we need to add it back.
      e.appendRemainingComponent("");
      throw e;
    }
  }
Exemple #16
0
 /**
  * The heart of the event processing loop. This is where the events are actually evaluated.
  *
  * @param next - the event to evaluate.
  * @throws SimulationException - if an exception occurs during the evaluation of the event.
  */
 protected void evaluate(EventImpl next) throws SimulationException {
   currentEvent = next;
   currentTime = currentEvent.getTime();
   Continuation continuation = currentEvent.getContinuation();
   if (continuation != null) {
     returnFrame = continuation.getFrame();
     caller = continuation.getCaller();
   }
   Throwable exception = null;
   Object result = null;
   try {
     if (eventLog != null) {
       eventLog.info(currentEvent.toString());
     }
     result = currentEvent.invoke();
   } catch (SimulationEnd e) {
     throw e;
   } catch (Throwable e) {
     if (caller == null || blockingEvent != null) {
       throw new SimulationException(e);
     }
     exception = e;
   } finally {
     currentEvent = null;
   }
   if (blockingEvent != null) {
     continuingEvent.setContinuation(new Continuation(caller, currentFrame));
     blockingEvent.setContinuation(new Continuation(continuingEvent));
     post(blockingEvent);
     blockingEvent = null;
     continuingEvent = null;
     currentFrame = null;
   } else if (caller != null) {
     post(caller.resume(currentTime, result, exception));
   }
   caller = null;
   returnFrame = null;
 }
 protected Object c_lookup_nns(Name name, Continuation cont) throws NamingException {
   if (_contextType == _ATOMIC) {
     Object ret = null;
     if (resolve_to_penultimate_context_nns(name, cont)) {
       ret = a_lookup_nns(name.toString(), cont);
       if (ret != null && ret instanceof LinkRef) {
         cont.setContinue(ret, name, this);
         ret = null;
       }
     }
     return ret;
   } else {
     return super.c_lookup_nns(name, cont);
   }
 }
 @JRubyMethod(
     name = {"call", "[]"},
     rest = true)
 public IRubyObject call(ThreadContext context, IRubyObject[] args) {
   if (disabled) {
     RubyKernel.raise(
         context,
         context.runtime.getThreadError(),
         new IRubyObject[] {
           context.runtime.newString("continuations can not be called from outside their scope")
         },
         Block.NULL_BLOCK);
   }
   continuation.args = args;
   throw continuation;
 }
  /**
   * This function is used when implementing a naming system that supports junctions. For example,
   * when the a_list_nns(newobj) method is invoked, that means the caller is attempting to list the
   * the nns context of of this context. For a context that supports junctions, it by default does
   * not have any nns. Consequently, a NameNotFoundException is thrown.
   */
  protected void a_processJunction_nns(Continuation cont) throws NamingException {

    // Construct a new Reference that contains this context.
    RefAddr addr =
        new RefAddr("nns") {
          public Object getContent() {
            return AtomicContext.this;
          }

          private static final long serialVersionUID = 3449785852664978312L;
        };
    Reference ref = new Reference("java.lang.Object", addr);

    // Set continuation leave it to PartialCompositeContext.getPCContext()
    // to throw the exception.
    // Do not use setContinueNNS() because we've are
    // setting relativeResolvedName to "/".
    cont.setContinue(ref, _NNS_NAME, this);
  }
Exemple #20
0
 @Override
 public Continuation apply(ContainerRequest data) {
   return Continuation.of(transformation.apply(data), children);
 }
Exemple #21
0
 @Override
 public Continuation apply(ContainerRequest request) {
   return Continuation.of(request);
 }
 /*
  * Obtain a PHP instance for url.
  */
 protected final Object doEval(Reader reader, ScriptContext context) throws Exception {
   continuation = getContinuation(reader, context);
   return continuation.getPhpScript();
 }