| /* |
| * Copyright 2000-2012 JetBrains s.r.o. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package git4idea.config; |
| |
| import com.intellij.openapi.components.*; |
| import com.intellij.util.xmlb.annotations.MapAnnotation; |
| import com.intellij.util.xmlb.annotations.Property; |
| import gnu.trove.THashMap; |
| |
| import java.util.Map; |
| import java.util.TreeMap; |
| |
| /** |
| * This class stores information related to SSH connections |
| */ |
| @State( |
| name = "SSHConnectionSettings", |
| storages = {@Storage( |
| file = StoragePathMacros.APP_CONFIG + "/security.xml")}) |
| public class SSHConnectionSettings implements PersistentStateComponent<SSHConnectionSettings.State> { |
| /** |
| * The last successful hosts, the entries are sorted to save on efforts on sorting during saving and loading |
| */ |
| THashMap<String, String> myLastSuccessful = new THashMap<String, String>(); |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public State getState() { |
| State s = new State(); |
| s.setLastSuccessful(new TreeMap<String, String>(myLastSuccessful)); |
| return s; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public void loadState(State state) { |
| myLastSuccessful.clear(); |
| myLastSuccessful.putAll(state.getLastSuccessful()); |
| } |
| |
| /** |
| * Get last successful authentication method |
| * |
| * @param userName the key in format user@host |
| * @return the last successful stored authentication method or null |
| */ |
| public String getLastSuccessful(String userName) { |
| return myLastSuccessful.get(userName); |
| } |
| |
| /** |
| * Get last successful authentication method |
| * |
| * @param userName the key in format user@host |
| * @param method the last successful stored authentication method (null or empty string if entry should be dropped) |
| */ |
| public void setLastSuccessful(String userName, String method) { |
| if (null == method || method.length() == 0) { |
| myLastSuccessful.remove(userName); |
| } |
| else { |
| myLastSuccessful.put(userName, method); |
| } |
| } |
| |
| /** |
| * @return the service instance |
| */ |
| public static SSHConnectionSettings getInstance() { |
| return ServiceManager.getService(SSHConnectionSettings.class); |
| } |
| |
| /** |
| * The state for the settings |
| */ |
| public static class State { |
| /** |
| * The last successful authentications |
| */ |
| private Map<String, String> myLastSuccessful = new TreeMap<String, String>(); |
| |
| /** |
| * @return the last successful authentications |
| */ |
| @Property(surroundWithTag = false) |
| @MapAnnotation(surroundKeyWithTag = false, surroundValueWithTag = false, surroundWithTag = false, |
| entryTagName = "successfulAuthentication", keyAttributeName = "user", valueAttributeName = "method") |
| public Map<String, String> getLastSuccessful() { |
| return myLastSuccessful; |
| } |
| |
| /** |
| * Set last successful authentications |
| * |
| * @param map from user hosts to methods |
| */ |
| public void setLastSuccessful(Map<String, String> map) { |
| myLastSuccessful = map; |
| } |
| } |
| } |