content: package com.aliyun.openservices.kafka.ons;
  
  import java.util.Properties;
  
  public class JavaKafkaConfigurer {
  
      private static Properties properties;
  
      public static boolean isEmpty(String str) {
         if (null == str) {
             return true;
         }
         if (0 == str.trim().length()) {
             return true;
         }
  
         return false;
      }
  
      public static void configureSasl() {
          //如果用-D或者其它方式设置过，这里不再设置
          Properties kafkaProperties = getKafkaProperties();
          if (null == System.getProperty("java.security.auth.login.config")
                  && isEmpty(kafkaProperties.getProperty("sasl.username"))
                  && isEmpty(kafkaProperties.getProperty("sasl.password"))) {
              //请注意将XXX修改为自己的路径
              //这个路径必须是一个文件系统可读的路径，不能被打包到jar中
              System.setProperty("java.security.auth.login.config", getKafkaProperties().getProperty("java.security.auth.login.config"));
          }
      }
  
      public synchronized static Properties getKafkaProperties() {
          if (null != properties) {
              return properties;
          }
          //获取配置文件kafka.properties的内容
          Properties kafkaProperties = new Properties();
          try {
              kafkaProperties.load(KafkaProducerDemo.class.getClassLoader().getResourceAsStream("kafka.properties"));
          } catch (Exception e) {
              //没加载到文件，程序要考虑退出
              e.printStackTrace();
          }
          properties = kafkaProperties;
          return kafkaProperties;
      }
  
  
  }
  