org.apache.kafka.common.utils.Utils.newInstance()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(12.6k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中org.apache.kafka.common.utils.Utils.newInstance()方法的一些代码示例,展示了Utils.newInstance()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.newInstance()方法的具体详情如下:
包路径:org.apache.kafka.common.utils.Utils
类名称:Utils
方法名:newInstance

Utils.newInstance介绍

[英]Instantiate the class
[中]实例化这个类

代码示例

代码示例来源:origin: apache/kafka

private void createClientCallbackHandler(Map<String, ?> configs) {
  @SuppressWarnings("unchecked")
  Class<? extends AuthenticateCallbackHandler> clazz = (Class<? extends AuthenticateCallbackHandler>) configs.get(SaslConfigs.SASL_CLIENT_CALLBACK_HANDLER_CLASS);
  if (clazz == null)
    clazz = clientCallbackHandlerClass();
  AuthenticateCallbackHandler callbackHandler = Utils.newInstance(clazz);
  saslCallbackHandlers.put(clientSaslMechanism, callbackHandler);
}

代码示例来源:origin: apache/kafka

/**
 * Look up the class by name and instantiate it.
 * @param klass class name
 * @param base super class of the class to be instantiated
 * @param <T> the type of the base class
 * @return the new instance
 */
public static <T> T newInstance(String klass, Class<T> base) throws ClassNotFoundException {
  return Utils.newInstance(loadClass(klass, base));
}

代码示例来源:origin: apache/kafka

@SuppressWarnings("deprecation")
private static org.apache.kafka.common.security.auth.PrincipalBuilder createPrincipalBuilder(
    Class<?> principalBuilderClass, Map<String, ?> configs) {
  org.apache.kafka.common.security.auth.PrincipalBuilder principalBuilder;
  if (principalBuilderClass == null)
    principalBuilder = new org.apache.kafka.common.security.auth.DefaultPrincipalBuilder();
  else
    principalBuilder = (org.apache.kafka.common.security.auth.PrincipalBuilder) Utils.newInstance(principalBuilderClass);
  principalBuilder.configure(configs);
  return principalBuilder;
}

代码示例来源:origin: apache/kafka

if (klass instanceof String) {
  try {
    o = Utils.newInstance((String) klass, t);
  } catch (ClassNotFoundException e) {
    throw new KafkaException(klass + " ClassNotFoundException exception occurred", e);
  o = Utils.newInstance((Class<?>) klass);
} else
  throw new KafkaException("List contains element of type " + klass.getClass().getName() + ", expected String or Class");

代码示例来源:origin: apache/kafka

/**
 * Get a configured instance of the give class specified by the given configuration key. If the object implements
 * Configurable configure it using the configuration.
 *
 * @param key The configuration key for the class
 * @param t The interface the class should implement
 * @return A configured instance of the class
 */
public <T> T getConfiguredInstance(String key, Class<T> t) {
  Class<?> c = getClass(key);
  if (c == null)
    return null;
  Object o = Utils.newInstance(c);
  if (!t.isInstance(o))
    throw new KafkaException(c.getName() + " is not an instance of " + t.getName());
  if (o instanceof Configurable)
    ((Configurable) o).configure(originals());
  return t.cast(o);
}

代码示例来源:origin: apache/kafka

private LoginManager(JaasContext jaasContext, String saslMechanism, Map<String, ?> configs,
           LoginMetadata<?> loginMetadata) throws LoginException {
  this.loginMetadata = loginMetadata;
  this.login = Utils.newInstance(loginMetadata.loginClass);
  loginCallbackHandler = Utils.newInstance(loginMetadata.loginCallbackClass);
  loginCallbackHandler.configure(configs, saslMechanism, jaasContext.configurationEntries());
  login.configure(configs, jaasContext.name(), jaasContext.configuration(), loginCallbackHandler);
  login.login();
}

代码示例来源:origin: apache/kafka

@SuppressWarnings("deprecation")
public static KafkaPrincipalBuilder createPrincipalBuilder(Map<String, ?> configs,
                              TransportLayer transportLayer,
                              Authenticator authenticator,
                              KerberosShortNamer kerberosShortNamer,
                              SslPrincipalMapper sslPrincipalMapper) {
  Class<?> principalBuilderClass = (Class<?>) configs.get(BrokerSecurityConfigs.PRINCIPAL_BUILDER_CLASS_CONFIG);
  final KafkaPrincipalBuilder builder;
  if (principalBuilderClass == null || principalBuilderClass == DefaultKafkaPrincipalBuilder.class) {
    builder = new DefaultKafkaPrincipalBuilder(kerberosShortNamer, sslPrincipalMapper);
  } else if (KafkaPrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
    builder = (KafkaPrincipalBuilder) Utils.newInstance(principalBuilderClass);
  } else if (org.apache.kafka.common.security.auth.PrincipalBuilder.class.isAssignableFrom(principalBuilderClass)) {
    org.apache.kafka.common.security.auth.PrincipalBuilder oldPrincipalBuilder =
        createPrincipalBuilder(principalBuilderClass, configs);
    builder = DefaultKafkaPrincipalBuilder.fromOldPrincipalBuilder(authenticator, transportLayer,
        oldPrincipalBuilder, kerberosShortNamer);
  } else {
    throw new InvalidConfigurationException("Type " + principalBuilderClass.getName() + " is not " +
        "an instance of " + org.apache.kafka.common.security.auth.PrincipalBuilder.class.getName() + " or " +
        KafkaPrincipalBuilder.class.getName());
  }
  if (builder instanceof Configurable)
    ((Configurable) builder).configure(configs);
  return builder;
}

代码示例来源:origin: apache/kafka

private void createServerCallbackHandlers(Map<String, ?> configs) throws ClassNotFoundException {
  for (String mechanism : jaasContexts.keySet()) {
    AuthenticateCallbackHandler callbackHandler;
    String prefix = ListenerName.saslMechanismPrefix(mechanism);
    @SuppressWarnings("unchecked")
    Class<? extends AuthenticateCallbackHandler> clazz =
        (Class<? extends AuthenticateCallbackHandler>) configs.get(prefix + BrokerSecurityConfigs.SASL_SERVER_CALLBACK_HANDLER_CLASS);
    if (clazz != null)
      callbackHandler = Utils.newInstance(clazz);
    else if (mechanism.equals(PlainSaslServer.PLAIN_MECHANISM))
      callbackHandler = new PlainServerCallbackHandler();
    else if (ScramMechanism.isScram(mechanism))
      callbackHandler = new ScramServerCallbackHandler(credentialCache.cache(mechanism, ScramCredential.class), tokenCache);
    else if (mechanism.equals(OAuthBearerLoginModule.OAUTHBEARER_MECHANISM))
      callbackHandler = new OAuthBearerUnsecuredValidatorCallbackHandler();
    else
      callbackHandler = new SaslServerCallbackHandler();
    saslCallbackHandlers.put(mechanism, callbackHandler);
  }
}

代码示例来源:origin: org.apache.kafka/connect-runtime

protected static <T> T newPlugin(Class<T> klass) {
  try {
    return Utils.newInstance(klass);
  } catch (Throwable t) {
    throw new ConnectException("Instantiation error", t);
  }
}

代码示例来源:origin: rayokota/kafka-graphs

public static <T> T getConfiguredInstance(Class<T> cls, Map<String, Object> configs) {
  if (cls == null)
    return null;
  Object o = Utils.newInstance(cls);
  if (o instanceof Configurable)
    ((Configurable) o).configure(configs);
  return cls.cast(o);
}

代码示例来源:origin: spring-cloud/spring-cloud-stream-binder-kafka

private Serde<?> getValueSerde(String valueSerdeString) throws ClassNotFoundException {
    Serde<?> valueSerde;
    if (StringUtils.hasText(valueSerdeString)) {
      valueSerde = Utils.newInstance(valueSerdeString, Serde.class);
    }
    else {
      valueSerde = this.binderConfigurationProperties.getConfiguration().containsKey("default.value.serde") ?
          Utils.newInstance(this.binderConfigurationProperties.getConfiguration().get("default.value.serde"), Serde.class) : Serdes.ByteArray();
    }
    return valueSerde;
  }
}

代码示例来源:origin: spring-cloud/spring-cloud-stream-binder-kafka

private Serde<?> getKeySerde(String keySerdeString) {
  Serde<?> keySerde;
  try {
    if (StringUtils.hasText(keySerdeString)) {
      keySerde = Utils.newInstance(keySerdeString, Serde.class);
    }
    else {
      keySerde = this.binderConfigurationProperties.getConfiguration().containsKey("default.key.serde") ?
          Utils.newInstance(this.binderConfigurationProperties.getConfiguration().get("default.key.serde"), Serde.class) : Serdes.ByteArray();
    }
    keySerde.configure(this.streamConfigGlobalProperties, true);
  }
  catch (ClassNotFoundException ex) {
    throw new IllegalStateException("Serde class not found: ", ex);
  }
  return keySerde;
}

代码示例来源:origin: org.apache.kafka/connect-runtime

/**
 * Get an instance of the give class specified by the given configuration key.
 *
 * @param key The configuration key for the class
 * @param t The interface the class should implement
 * @return A instance of the class
 */
private <T> T getInstance(AbstractConfig config, String key, Class<T> t) {
  Class<?> c = config.getClass(key);
  if (c == null) {
    return null;
  }
  // Instantiate the class, but we don't know if the class extends the supplied type
  Object o = Utils.newInstance(c);
  if (!t.isInstance(o)) {
    throw new KafkaException(c.getName() + " is not an instance of " + t.getName());
  }
  return t.cast(o);
}

代码示例来源:origin: org.apache.kafka/kafka-streams

@SuppressWarnings("unchecked")
@Override
public void configure(final Map<String, ?> configs, final boolean isKey) {
  if (inner == null) {
    final String propertyName = isKey ? StreamsConfig.DEFAULT_WINDOWED_KEY_SERDE_INNER_CLASS : StreamsConfig.DEFAULT_WINDOWED_VALUE_SERDE_INNER_CLASS;
    final String value = (String) configs.get(propertyName);
    try {
      inner = Serde.class.cast(Utils.newInstance(value, Serde.class)).serializer();
      inner.configure(configs, isKey);
    } catch (final ClassNotFoundException e) {
      throw new ConfigException(propertyName, value, "Serde class " + value + " could not be found.");
    }
  }
}

代码示例来源:origin: org.apache.kafka/kafka-streams

@SuppressWarnings("unchecked")
@Override
public void configure(final Map<String, ?> configs, final boolean isKey) {
  if (inner == null) {
    final String propertyName = isKey ? StreamsConfig.DEFAULT_WINDOWED_KEY_SERDE_INNER_CLASS : StreamsConfig.DEFAULT_WINDOWED_VALUE_SERDE_INNER_CLASS;
    final String value = (String) configs.get(propertyName);
    try {
      inner = Serde.class.cast(Utils.newInstance(value, Serde.class)).serializer();
      inner.configure(configs, isKey);
    } catch (final ClassNotFoundException e) {
      throw new ConfigException(propertyName, value, "Serde class " + value + " could not be found.");
    }
  }
}

代码示例来源:origin: org.apache.kafka/kafka-streams

@SuppressWarnings("unchecked")
@Override
public void configure(final Map<String, ?> configs, final boolean isKey) {
  if (inner == null) {
    final String propertyName = isKey ? StreamsConfig.DEFAULT_WINDOWED_KEY_SERDE_INNER_CLASS : StreamsConfig.DEFAULT_WINDOWED_VALUE_SERDE_INNER_CLASS;
    final String value = (String) configs.get(propertyName);
    try {
      inner = Serde.class.cast(Utils.newInstance(value, Serde.class)).deserializer();
      inner.configure(configs, isKey);
    } catch (final ClassNotFoundException e) {
      throw new ConfigException(propertyName, value, "Serde class " + value + " could not be found.");
    }
  }
}

代码示例来源:origin: org.apache.kafka/kafka-streams

@SuppressWarnings("unchecked")
@Override
public void configure(final Map<String, ?> configs, final boolean isKey) {
  if (inner == null) {
    final String propertyName = isKey ? StreamsConfig.DEFAULT_WINDOWED_KEY_SERDE_INNER_CLASS : StreamsConfig.DEFAULT_WINDOWED_VALUE_SERDE_INNER_CLASS;
    final String value = (String) configs.get(propertyName);
    try {
      inner = Serde.class.cast(Utils.newInstance(value, Serde.class)).deserializer();
      inner.configure(configs, isKey);
    } catch (final ClassNotFoundException e) {
      throw new ConfigException(propertyName, value, "Serde class " + value + " could not be found.");
    }
  }
}

代码示例来源:origin: linkedin/li-apache-kafka-clients

public <T> T getConfiguredInstance(String key, Class<T> t, Producer<byte[], byte[]> producer) {
 Class<?> c = getClass(key);
 if (c == null) {
  return null;
 }
 Object o = Utils.newInstance(c);
 if (!t.isInstance(o)) {
  throw new KafkaException(c.getName() + " is not an instance of " + t.getName());
 }
 if (o instanceof Configurable) {
  ((Configurable) o).configure(configsWithCurrentProducer(producer));
 }
 return t.cast(o);
}

代码示例来源:origin: me.jeffshaw.kafka/kafka-clients

/**
 * Get a configured instance of the give class specified by the given configuration key. If the object implements
 * Configurable configure it using the configuration.
 * 
 * @param key The configuration key for the class
 * @param t The interface the class should implement
 * @return A configured instance of the class
 */
public <T> T getConfiguredInstance(String key, Class<T> t) {
  Class<?> c = getClass(key);
  if (c == null)
    return null;
  Object o = Utils.newInstance(c);
  if (!t.isInstance(o))
    throw new KafkaException(c.getName() + " is not an instance of " + t.getName());
  if (o instanceof Configurable)
    ((Configurable) o).configure(this.originals);
  return t.cast(o);
}

代码示例来源:origin: homeaway/stream-registry

public static SchemaManager loadSchemaManager(StreamRegistryConfiguration configuration) {
  SchemaManagerConfig schemaManagerConfig = configuration.getSchemaManagerConfig();
  Preconditions.checkNotNull(schemaManagerConfig, "schema manager config cannot be null");
  String schemaManagerClass = schemaManagerConfig.getClassName();
  Preconditions.checkState(schemaManagerClass != null && !schemaManagerClass.isEmpty(),
      "schema manager class must be defined");
  Preconditions.checkState(schemaManagerConfig.getProperties() != null
      && schemaManagerConfig.getProperties().containsKey(SCHEMA_REGISTRY_URL_CONFIG),
      "schemaManagerConfig properties must define schema.registry.url");
  try {
    SchemaManager schemaManager = Utils.newInstance(schemaManagerClass, SchemaManager.class);
    schemaManager.configure(schemaManagerConfig.getProperties());
    return schemaManager;
  } catch (ClassNotFoundException e) {
    throw new IllegalStateException("Error loading SchemaManager from configuration", e);
  }
}

相关文章