Exemplo n.º 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;
  }
Exemplo n.º 2
0
 @SuppressWarnings("unchecked")
 public static Long getExpressValue(Map map, String expressionStr) throws ParseException {
   Long expValue = 0L;
   map = instanlExpressMap(map);
   if (!StringUtils.isNullObject(expressionStr)) {
     JexlEngine jexl = JexlManager.getJexlEngine();
     Expression expression = jexl.createExpression(expressionStr);
     JexlContext jexlContext = new MapContext(map);
     Object obj = (Object) expression.evaluate(jexlContext);
     if (null != obj) {
       BigDecimal b = new BigDecimal(obj.toString());
       b = b.setScale(0, BigDecimal.ROUND_HALF_UP);
       expValue = b.longValue();
     }
   }
   return expValue;
 }
Exemplo n.º 3
0
  /**
   * Function that looks for expressions to parse. It parses backwards to capture embedded
   * expressions
   *
   * @param value
   * @param last
   * @param jexl
   * @return
   */
  private static String resolveVariableExpression(String value, int last, JexlEngine jexl) {
    int lastIndex = value.lastIndexOf("$(", last);
    if (lastIndex == -1) {
      return value;
    }

    // Want to check that everything is well formed, and that
    // we properly capture $( ...(...)...).
    int bracketCount = 0;
    int nextClosed = lastIndex + 2;
    for (; nextClosed < value.length(); ++nextClosed) {
      if (value.charAt(nextClosed) == '(') {
        bracketCount++;
      } else if (value.charAt(nextClosed) == ')') {
        bracketCount--;
        if (bracketCount == -1) {
          break;
        }
      }
    }

    if (nextClosed == value.length()) {
      throw new IllegalArgumentException("Expression " + value + " not well formed.");
    }

    String innerExpression = value.substring(lastIndex + 2, nextClosed);
    Object result = null;
    try {
      Expression e = jexl.createExpression(innerExpression);
      result = e.evaluate(new MapContext());
    } catch (JexlException e) {
      throw new IllegalArgumentException(
          "Expression " + value + " not well formed. " + e.getMessage(), e);
    }

    if (result == null) {
      // for backward compatibility it is best to return value
      return value;
    }

    String newValue =
        value.substring(0, lastIndex) + result.toString() + value.substring(nextClosed + 1);
    return resolveVariableExpression(newValue, lastIndex, jexl);
  }
Exemplo n.º 4
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();
    }
  }