PageRenderTime 134ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/bitbucket/connectors/jetbrains/BitbucketSettings.java

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 89 lines | 68 code | 14 blank | 7 comment | 6 complexity | 4e7c80def9f29af1578c7af41dd6f802 MD5 | raw file
  1. package org.bitbucket.connectors.jetbrains;
  2. import com.intellij.openapi.components.PersistentStateComponent;
  3. import com.intellij.openapi.components.ServiceManager;
  4. import com.intellij.openapi.components.State;
  5. import com.intellij.openapi.components.Storage;
  6. import com.intellij.openapi.util.PasswordUtil;
  7. import com.intellij.openapi.util.text.StringUtil;
  8. import org.jdom.Element;
  9. import org.jetbrains.annotations.NotNull;
  10. /**
  11. * User: leha2000
  12. * Date: Apr 7, 2011
  13. * Time: 1:01:55 PM
  14. */
  15. @State(
  16. name = "BitbucketSettings",
  17. storages = {
  18. @Storage(
  19. id = "main",
  20. file = "$APP_CONFIG$/bitbucket_settings.xml"
  21. )}
  22. )
  23. public class BitbucketSettings implements PersistentStateComponent<Element> {
  24. private static final String SETTINGS_TAG = "BitbucketSettings";
  25. private static final String LOGIN = "Login";
  26. private static final String PASSWORD = "Password";
  27. private String myLogin;
  28. private String myPassword;
  29. public static BitbucketSettings getInstance() {
  30. return ServiceManager.getService(BitbucketSettings.class);
  31. }
  32. public Element getState() {
  33. if (StringUtil.isEmptyOrSpaces(myLogin) && StringUtil.isEmptyOrSpaces(myPassword)) {
  34. return null;
  35. }
  36. final Element element = new Element(SETTINGS_TAG);
  37. element.setAttribute(LOGIN, getLogin());
  38. element.setAttribute(PASSWORD, getEncodedPassword());
  39. return element;
  40. }
  41. public String getEncodedPassword() {
  42. return PasswordUtil.encodePassword(getPassword());
  43. }
  44. public void setEncodedPassword(final String password) {
  45. try {
  46. setPassword(PasswordUtil.decodePassword(password));
  47. }
  48. catch (NumberFormatException e) {
  49. // do nothing
  50. }
  51. }
  52. public void loadState(@NotNull final Element element) {
  53. try {
  54. setLogin(element.getAttributeValue(LOGIN));
  55. setEncodedPassword(element.getAttributeValue(PASSWORD));
  56. }
  57. catch (Exception e) {
  58. // ignore
  59. }
  60. }
  61. @NotNull
  62. public String getLogin() {
  63. return myLogin != null ? myLogin : "";
  64. }
  65. @NotNull
  66. public String getPassword() {
  67. return myPassword != null ? myPassword : "";
  68. }
  69. public void setLogin(String login) {
  70. myLogin = login != null ? login : "";
  71. }
  72. public void setPassword(String password) {
  73. myPassword = password != null ? password : "";
  74. }
  75. }