All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jcustenborder.kafka.connect.jmx.ObjectNameAndAttributes Maven / Gradle / Ivy

The newest version!
/**
 * Copyright © 2017 Jeremy Custenborder ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jcustenborder.kafka.connect.jmx;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.connect.errors.ConnectException;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class ObjectNameAndAttributes {
  public final ObjectName objectName;
  public final Set attributes;
  public final boolean isWildCard;


  private ObjectNameAndAttributes(ObjectName objectName, Set attributes, boolean isWildCard) {
    this.objectName = objectName;
    this.attributes = attributes;
    this.isWildCard = isWildCard;
  }

  public static final ObjectNameAndAttributes parse(String s) throws MalformedObjectNameException {
    Preconditions.checkNotNull(s, "input cannot be null.");
    final String input;
    try {
      input = URLDecoder.decode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
    Pattern pattern = Pattern.compile("^(.+)\\|(.+)$");
    Matcher matcher = pattern.matcher(input);
    Preconditions.checkState(
        matcher.matches(),
        "Could not match '%s' with pattern '%s'",
        input,
        pattern.pattern()
    );
    final String objectNameInput = matcher.group(1);
    final String attributesInput = matcher.group(2);
    final ObjectName objectName = new ObjectName(objectNameInput);
    final boolean isWildCard;
    final String[] attributes;
    if (attributesInput.equals("*")) {
      attributes = new String[0];
      isWildCard = true;
    } else {
      attributes = attributesInput.split("\\s*;\\s*,");
      isWildCard = false;
      Preconditions.checkState(attributes.length > 0, "At least one attribute must be specified.");
    }

    return new ObjectNameAndAttributes(objectName, ImmutableSet.copyOf(attributes), isWildCard);
  }


  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this)
        .add("objectName", this.objectName)
        .add("attributes", this.attributes)
        .add("isWildCard", this.isWildCard)
        .toString();
  }

  public static class Validator implements ConfigDef.Validator {
    @Override
    public void ensureValid(String name, Object value) {
      Preconditions.checkState(
          value instanceof List,
          "'%s' must be a String,",
          name
      );
      final List input = (List) value;

      for (String a : input) {
        try {
          ObjectNameAndAttributes result = parse(a);
        } catch (Exception e) {
          throw new ConnectException("Could not parse", e);
        }
      }
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy