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