From 6ea7182fd8c5277d8311547494c7d940430e6e7c Mon Sep 17 00:00:00 2001 From: irengrig Date: Mon, 21 Dec 2009 11:45:05 +0300 Subject: [PATCH] IDEADEV-42031 (Sporadic failures on subversion project update). do not write credentials so often --- .../src/org/jetbrains/idea/svn/SvnAuthEquals.java | 58 ++++++++++++++++++++++ .../idea/svn/SvnAuthenticationManager.java | 33 +++++++++++- .../src/org/jetbrains/idea/svn/Wrapper.java | 28 +++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthEquals.java create mode 100644 plugins/svn4idea/src/org/jetbrains/idea/svn/Wrapper.java diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthEquals.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthEquals.java new file mode 100644 index 0000000000..ec1e9f09dd --- /dev/null +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthEquals.java @@ -0,0 +1,58 @@ +/* + * Copyright 2000-2009 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 org.jetbrains.idea.svn; + +import com.intellij.openapi.util.Comparing; +import org.tmatesoft.svn.core.auth.*; + +public class SvnAuthEquals { + private SvnAuthEquals() { + } + + public static boolean equals(final SVNAuthentication a1, final SVNAuthentication a2) { + if (a1 == a2) return true; + if (a1 == null || a2 == null) return false; + if (! Comparing.equal(a1.getKind(), a2.getKind())) return false; + if (! Comparing.equal(a1.getUserName(), a2.getUserName())) return false; + + final Class a1Class = a1.getClass(); + if (! a1Class.equals(a2.getClass())) return false; + + if (SVNUserNameAuthentication.class.equals(a1Class)) return true; + if (SVNPasswordAuthentication.class.equals(a1Class)) { + return Comparing.equal(((SVNPasswordAuthentication) a1).getPassword(), ((SVNPasswordAuthentication) a2).getPassword()); + } + if (SVNSSLAuthentication.class.equals(a1Class)) { + if (! Comparing.equal(((SVNSSLAuthentication) a1).getCertificateFile(), ((SVNSSLAuthentication) a2).getCertificateFile())) return false; + return Comparing.equal(((SVNSSLAuthentication) a1).getPassword(), ((SVNSSLAuthentication) a2).getPassword()); + } + if (SVNSSHAuthentication.class.equals(a1Class)) { + if (! Comparing.equal(((SVNSSHAuthentication) a1).getPrivateKeyFile(), ((SVNSSHAuthentication) a2).getPrivateKeyFile())) return false; + if (! Comparing.equal(((SVNSSHAuthentication) a1).getPassphrase(), ((SVNSSHAuthentication) a2).getPassphrase())) return false; + if (! Comparing.equal(((SVNSSHAuthentication) a1).getPortNumber(), ((SVNSSHAuthentication) a2).getPortNumber())) return false; + return Comparing.equal(((SVNSSHAuthentication) a1).getPassword(), ((SVNSSHAuthentication) a2).getPassword()); + } + return false; + } + + public static int hashCode(final SVNAuthentication a) { + int result = a.getKind().hashCode(); + if (a.getUserName() != null) { + result = (31 * result) + a.getUserName().hashCode(); + } + return result; + } +} diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthenticationManager.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthenticationManager.java index 31d2e8f789..4d6b92ade4 100644 --- a/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthenticationManager.java +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/SvnAuthenticationManager.java @@ -19,6 +19,7 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vcs.changes.ui.ChangesViewBalloonProblemNotifier; import com.intellij.openapi.ui.MessageType; +import com.intellij.util.containers.SoftHashMap; import com.intellij.util.net.HttpConfigurable; import org.tmatesoft.svn.core.SVNErrorMessage; import org.tmatesoft.svn.core.SVNException; @@ -60,11 +61,14 @@ public class SvnAuthenticationManager extends DefaultSVNAuthenticationManager { } private static class PersistentAuthenticationProviderProxy implements ISVNAuthenticationProvider, IPersistentAuthenticationProvider { + private final Map myRewritePreventer; + private static final long ourRefreshInterval = 6000 * 1000; private final ISVNAuthenticationProvider myDelegate; private Project myProject; private PersistentAuthenticationProviderProxy(final ISVNAuthenticationProvider delegate) { myDelegate = delegate; + myRewritePreventer = new SoftHashMap(); } public void setProject(Project project) { @@ -83,7 +87,13 @@ public class SvnAuthenticationManager extends DefaultSVNAuthenticationManager { public void saveAuthentication(final SVNAuthentication auth, final String kind, final String realm) throws SVNException { try { - ((IPersistentAuthenticationProvider) myDelegate).saveAuthentication(auth, kind, realm); + final SvnAuthWrapperEqualable newKey = new SvnAuthWrapperEqualable(auth); + final Long recent = myRewritePreventer.get(newKey); + final long currTime = System.currentTimeMillis(); + if (recent == null || ((recent != null) && ((currTime - recent.longValue()) > ourRefreshInterval))) { + ((IPersistentAuthenticationProvider) myDelegate).saveAuthentication(auth, kind, realm); + myRewritePreventer.put(newKey, currTime); + } } catch (final SVNException e) { // show notification so that user was aware his credentials were not saved @@ -280,4 +290,25 @@ public class SvnAuthenticationManager extends DefaultSVNAuthenticationManager { } return null; } + + private static class SvnAuthWrapperEqualable extends Wrapper { + private SvnAuthWrapperEqualable(SVNAuthentication svnAuthentication) { + super(svnAuthentication); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (obj instanceof SvnAuthWrapperEqualable) { + return SvnAuthEquals.equals(this.getT(), ((SvnAuthWrapperEqualable) obj).getT()); + } + return false; + } + + @Override + public int hashCode() { + return SvnAuthEquals.hashCode(getT()); + } + } } diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/Wrapper.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/Wrapper.java new file mode 100644 index 0000000000..877af9f3d7 --- /dev/null +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/Wrapper.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2009 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 org.jetbrains.idea.svn; + +public class Wrapper { + private final T myT; + + public Wrapper(T t) { + myT = t; + } + + public T getT() { + return myT; + } +} -- 2.11.4.GIT