Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package org.apache.kafka.common.security.kerberos;
  2.  
  3. import javax.security.auth.Subject;
  4. import javax.security.auth.login.LoginException;
  5. import java.io.IOException;
  6. import java.util.Map;
  7.  
  8. import org.apache.kafka.common.security.JaasUtils;
  9. import org.apache.kafka.common.KafkaException;
  10. import org.apache.kafka.common.Configurable;
  11. import org.apache.zookeeper.Login;
  12.  
  13.  
  14. public class LoginManager implements Configurable {
  15. public enum Mode { CLIENT, SERVER };
  16. private Login login;
  17. private final String serviceName;
  18. private final String loginContext;
  19. private final Mode mode;
  20. volatile private static LoginManager INSTANCE;
  21.  
  22. private LoginManager(Mode mode) throws IOException, LoginException {
  23. this.mode = mode;
  24. if (mode == Mode.SERVER)
  25. this.loginContext = JaasUtils.LOGIN_CONTEXT_SERVER;
  26. else
  27. this.loginContext = JaasUtils.LOGIN_CONTEXT_CLIENT;
  28. this.serviceName = JaasUtils.jaasConfig(loginContext, JaasUtils.SERVICE_NAME);
  29. }
  30.  
  31. public static final LoginManager getLoginManager(Mode mode) throws IOException, LoginException {
  32. if(INSTANCE != null && INSTANCE.mode.equals(mode)) {
  33. return INSTANCE;
  34. } else if (INSTANCE != null && !INSTANCE.mode.equals(mode)) {
  35. throw new IllegalArgumentException("cant change mode once initialized");
  36. } else synchronized (LoginManager.class) {
  37. if(INSTANCE == null) {
  38. INSTANCE = new LoginManager(mode);
  39. }
  40. }
  41. return INSTANCE;
  42. }
  43.  
  44. @Override
  45. public void configure(Map<String, ?> configs) throws KafkaException {
  46. try {
  47. login = new Login(loginContext);
  48. login.startThreadIfNeeded();
  49. } catch (Exception e) {
  50. throw new KafkaException(e);
  51. }
  52. }
  53.  
  54. public Subject subject() {
  55. return login.subject();
  56. }
  57.  
  58. public String serviceName() {
  59. return serviceName;
  60. }
  61.  
  62. public void close() {
  63. login.shutdown();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement