Example #1
0
    public String getCacheString() {
      StringBuilder sb = new StringBuilder();

      sb.append(name).append(":");

      for (Map.Entry<String, String> tagEntry : tags.entries()) {
        sb.append(tagEntry.getKey()).append("=");
        sb.append(tagEntry.getValue()).append(":");
      }

      return (sb.toString());
    }
Example #2
0
    @Override
    public Metric deserialize(
        JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
        throws JsonParseException {
      JsonObject jsonObject = jsonElement.getAsJsonObject();

      String name = null;
      if (jsonObject.get("name") != null) name = jsonObject.get("name").getAsString();

      boolean exclude_tags = false;
      if (jsonObject.get("exclude_tags") != null)
        exclude_tags = jsonObject.get("exclude_tags").getAsBoolean();

      TreeMultimap<String, String> tags = TreeMultimap.create();
      JsonElement jeTags = jsonObject.get("tags");
      if (jeTags != null) {
        JsonObject joTags = jeTags.getAsJsonObject();
        int count = 0;
        for (Map.Entry<String, JsonElement> tagEntry : joTags.entrySet()) {
          String context = "tags[" + count + "]";
          if (tagEntry.getKey().isEmpty())
            throw new ContextualJsonSyntaxException(context, "name must not be empty");

          if (tagEntry.getValue().isJsonArray()) {
            for (JsonElement element : tagEntry.getValue().getAsJsonArray()) {
              if (element.isJsonNull() || element.getAsString().isEmpty())
                throw new ContextualJsonSyntaxException(
                    context + "." + tagEntry.getKey(), "value must not be null or empty");
              tags.put(tagEntry.getKey(), element.getAsString());
            }
          } else {
            if (tagEntry.getValue().isJsonNull() || tagEntry.getValue().getAsString().isEmpty())
              throw new ContextualJsonSyntaxException(
                  context + "." + tagEntry.getKey(), "value must not be null or empty");
            tags.put(tagEntry.getKey(), tagEntry.getValue().getAsString());
          }
          count++;
        }
      }

      Metric ret = new Metric(name, exclude_tags, tags);

      JsonElement limit = jsonObject.get("limit");
      if (limit != null) ret.setLimit(limit.getAsInt());

      return (ret);
    }
Example #3
0
  private void deserializeProperties(
      String context, JsonObject jsonObject, String name, Object object)
      throws QueryException, BeanValidationException {
    Set<Map.Entry<String, JsonElement>> props = jsonObject.entrySet();
    for (Map.Entry<String, JsonElement> prop : props) {
      String property = prop.getKey();
      if (property.equals("name")) continue;

      PropertyDescriptor pd = null;
      try {
        pd = getPropertyDescriptor(object.getClass(), property);
      } catch (IntrospectionException e) {
        logger.error("Introspection error on " + object.getClass(), e);
      }

      if (pd == null) {
        String msg =
            "Property '"
                + property
                + "' was specified for object '"
                + name
                + "' but no matching setter was found on '"
                + object.getClass()
                + "'";

        throw new QueryException(msg);
      }

      Class propClass = pd.getPropertyType();

      Object propValue;
      try {
        propValue = m_gson.fromJson(prop.getValue(), propClass);
        validateObject(propValue, context + "." + property);
      } catch (ContextualJsonSyntaxException e) {
        throw new BeanValidationException(
            new SimpleConstraintViolation(e.getContext(), e.getMessage()), context);
      } catch (NumberFormatException e) {
        throw new BeanValidationException(
            new SimpleConstraintViolation(property, e.getMessage()), context);
      }

      Method method = pd.getWriteMethod();
      if (method == null) {
        String msg =
            "Property '"
                + property
                + "' was specified for object '"
                + name
                + "' but no matching setter was found on '"
                + object.getClass().getName()
                + "'";

        throw new QueryException(msg);
      }

      try {
        method.invoke(object, propValue);
      } catch (Exception e) {
        logger.error("Invocation error: ", e);
        String msg =
            "Call to "
                + object.getClass().getName()
                + ":"
                + method.getName()
                + " failed with message: "
                + e.getMessage();

        throw new QueryException(msg);
      }
    }
  }