示例#1
0
  /** {@inheritDoc} */
  @Override
  public boolean apply(@Nullable T1 t1, @Nullable T2 t2) {
    lazyCompile();

    if (expr != null) {
      JexlContext ctx = new MapContext();

      ctx.set(var1, t1);
      ctx.set(var2, t2);

      for (Map.Entry<String, Object> e : map.entrySet()) {
        ctx.set(e.getKey(), e.getValue());
      }

      try {
        Object obj = expr.evaluate(ctx);

        if (obj instanceof Boolean) {
          return (Boolean) obj;
        }
      } catch (Exception ex) {
        throw F.wrap(ex);
      }
    }

    return false;
  }
示例#2
0
  private void execute(String config) throws ConnectionException, IOException {

    NotificationConfig c = NotificationConfig.fromXml(new File(config));

    MessageFilter dupFilter = DuplicateMessageFilter.createFilter();

    GdcRESTApiWrapper rest = null;
    try {
      for (NotificationMessage m : c.getMessages()) {
        String dupFilterKind = m.getDupFilterKind();
        if (dupFilterKind != null && dupFilterKind.length() > 0) {
          if (!dupFilter.filter(m.getMessage(), dupFilterKind)) {
            l.debug("Message filtered out by the dup kind filter.");
            l.info("Message filtered out by the dup kind filter.");
            continue;
          }
        }
        rest = new GdcRESTApiWrapper(cliParams.getHttpConfig());
        rest.login();
        Expression e = null;
        JexlEngine jexl = new JexlEngine();
        e = jexl.createExpression(m.getCondition());
        JexlContext jc = new MapContext();
        List<Metric> metrics = m.getMetrics();
        double[] values = null;
        if (metrics != null && metrics.size() > 0) {
          values = new double[metrics.size()];
          for (int i = 0; i < metrics.size(); i++) {
            values[i] = rest.computeMetric(metrics.get(i).getUri());
            jc.set(metrics.get(i).getAlias(), new Double(values[i]));
          }
        }
        String[] texts = null;
        List<Report> reports = m.getReports();
        if (reports != null && reports.size() > 0) {
          texts = new String[reports.size()];
          for (int i = 0; i < reports.size(); i++) {
            texts[i] = rest.computeReport(reports.get(i).getUri());
          }
        }
        boolean result = decide(e.evaluate(jc));
        if (result) {
          NotificationTransport t = selectTransport(m.getUri());
          String msg = m.getMessage();
          if (values != null && values.length > 0 && metrics != null && metrics.size() > 0) {
            for (int i = 0; i < metrics.size(); i++) {
              String fmt = metrics.get(i).getFormat();
              if (fmt == null || fmt.length() <= 0) fmt = DEFAULT_FORMAT;
              DecimalFormat df = new DecimalFormat(fmt);
              msg = msg.replace("%" + metrics.get(i).getAlias() + "%", df.format(values[i]));
            }
          }
          if (texts != null && texts.length > 0 && reports != null && reports.size() > 0) {
            for (int i = 0; i < reports.size(); i++) {
              msg = msg.replace("%" + reports.get(i).getAlias() + "%", texts[i]);
            }
          }
          String dupFilterExact = m.getDupFilterExact();
          if (dupFilterExact != null && dupFilterExact.length() > 0) {
            if (!dupFilter.filter(msg, dupFilterExact)) {
              l.debug("Message filtered out by the dup exact filter.");
              l.info("Message filtered out by the dup exact filter.");
              continue;
            }
          }
          String fmt = m.getMessageTimestampFormat();
          if (fmt != null && fmt.length() > 0) t.send(msg + " (at " + getTimestamp(fmt) + ")");
          else t.send(msg);
          dupFilter.update(msg);
          dupFilter.update(m.getMessage());
          l.info("Notification sent.");
        }
      }
      dupFilter.save();
      rest.logout();
    } catch (Exception e) {
      throw new IOException(e);
    } finally {
      if (rest != null) rest.logout();
    }
  }