Ejemplo n.º 1
0
    @Override
    public Construct execs(Target t, Environment env, Script parent, ParseTree... nodes) {
      if (nodes.length < 5) {
        throw new ConfigRuntimeException(
            "bind accepts 5 or more parameters", ExceptionType.InsufficientArgumentsException, t);
      }
      Construct name = parent.seval(nodes[0], env);
      Construct options = parent.seval(nodes[1], env);
      Construct prefilter = parent.seval(nodes[2], env);
      Construct event_obj = parent.eval(nodes[3], env);
      IVariableList custom_params = new IVariableList();
      for (int a = 0; a < nodes.length - 5; a++) {
        Construct var = parent.eval(nodes[4 + a], env);
        if (!(var instanceof IVariable)) {
          throw new ConfigRuntimeException(
              "The custom parameters must be ivariables", ExceptionType.CastException, t);
        }
        IVariable cur = (IVariable) var;
        custom_params.set(
            env.getEnv(GlobalEnv.class).GetVarList().get(cur.getName(), cur.getTarget()));
      }
      Environment newEnv = env;
      try {
        newEnv = env.clone();
      } catch (Exception e) {
      }
      newEnv.getEnv(GlobalEnv.class).SetVarList(custom_params);
      ParseTree tree = nodes[nodes.length - 1];

      // Check to see if our arguments are correct
      if (!(options instanceof CNull || options instanceof CArray)) {
        throw new ConfigRuntimeException(
            "The options must be an array or null", ExceptionType.CastException, t);
      }
      if (!(prefilter instanceof CNull || prefilter instanceof CArray)) {
        throw new ConfigRuntimeException(
            "The prefilters must be an array or null", ExceptionType.CastException, t);
      }
      if (!(event_obj instanceof IVariable)) {
        throw new ConfigRuntimeException(
            "The event object must be an IVariable", ExceptionType.CastException, t);
      }
      CString id;
      if (options instanceof CNull) {
        options = null;
      }
      if (prefilter instanceof CNull) {
        prefilter = null;
      }
      Event event;
      try {
        BoundEvent be =
            new BoundEvent(
                name.val(),
                (CArray) options,
                (CArray) prefilter,
                ((IVariable) event_obj).getName(),
                newEnv,
                tree,
                t);
        EventUtils.RegisterEvent(be);
        id = new CString(be.getId(), t);
        event = EventList.getEvent(be.getEventName());
      } catch (EventException ex) {
        throw new ConfigRuntimeException(ex.getMessage(), ExceptionType.BindException, t);
      }

      // Set up our bind counter, but only if the event is supposed to be added to the counter
      if (event.addCounter()) {
        synchronized (bindCounter) {
          if (bindCounter.get() == 0) {
            env.getEnv(GlobalEnv.class).GetDaemonManager().activateThread(null);
            StaticLayer.GetConvertor()
                .addShutdownHook(
                    new Runnable() {

                      @Override
                      public void run() {
                        synchronized (bindCounter) {
                          bindCounter.set(0);
                        }
                      }
                    });
          }
          bindCounter.incrementAndGet();
        }
      }
      return id;
    }