예제 #1
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;
 }
예제 #2
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);
  }
  /**
   * Method for creating JexlVCMatchExp from input walker arguments mapping from names to exps.
   * These two arrays contain the name associated with each JEXL expression. initializeMatchExps
   * will parse each expression and return a list of JexlVCMatchExp, in order, that correspond to
   * the names and exps. These are suitable input to match() below.
   *
   * @param names_and_exps mapping of names to expressions
   * @return list of matches
   */
  public static List<JexlVCMatchExp> initializeMatchExps(Map<String, String> names_and_exps) {
    List<JexlVCMatchExp> exps = new ArrayList<JexlVCMatchExp>();

    for (Map.Entry<String, String> elt : names_and_exps.entrySet()) {
      String name = elt.getKey();
      String expStr = elt.getValue();

      if (name == null || expStr == null)
        throw new IllegalArgumentException(
            "Cannot create null expressions : " + name + " " + expStr);
      try {
        Expression exp = engine.createExpression(expStr);
        exps.add(new JexlVCMatchExp(name, exp));
      } catch (Exception e) {
        throw new UserException.BadArgumentValue(
            name,
            "Invalid expression used ("
                + expStr
                + "). Please see the JEXL docs for correct syntax.");
      }
    }

    return exps;
  }
예제 #4
0
 static {
   jexlEngine.setCache(1);
   jexlEngine.setLenient(false);
   jexlEngine.setSilent(false);
 }
예제 #5
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();
    }
  }
 static {
   engine.setSilent(false); // will throw errors now for selects that don't evaluate properly
   engine.setLenient(false);
 }