summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/svnkit/SvnKitManager.java
blob: 7571000267977d574b0fff24699ca8eaaa2f050c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright 2000-2014 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.svnkit;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.SvnConfiguration;
import org.jetbrains.idea.svn.SvnHttpAuthMethodsDefaultChecker;
import org.jetbrains.idea.svn.SvnVcs;
import org.jetbrains.idea.svn.auth.SvnAuthenticationManager;
import org.jetbrains.idea.svn.svnkit.lowLevel.PrimitivePool;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.util.jna.SVNJNAUtil;
import org.tmatesoft.svn.core.internal.wc.admin.SVNAdminArea14;
import org.tmatesoft.svn.core.internal.wc.admin.SVNAdminAreaFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.*;
import org.tmatesoft.svn.util.SVNDebugLog;

/**
 * @author Konstantin Kolosovsky.
 */
public class SvnKitManager {

  private static final Logger LOG = SvnVcs.wrapLogger(Logger.getInstance(SvnKitManager.class));

  @NonNls public static final String LOG_PARAMETER_NAME = "javasvn.log";
  @NonNls public static final String TRACE_NATIVE_CALLS = "javasvn.log.native";
  @NonNls public static final String SVNKIT_HTTP_SSL_PROTOCOLS = "svnkit.http.sslProtocols";

  @Nullable private static String ourExplicitlySetSslProtocols;

  @NotNull private final SvnVcs myVcs;
  @NotNull private final Project myProject;
  @NotNull private final SvnConfiguration myConfiguration;

  static {
    System.setProperty("svnkit.log.native.calls", "true");

    final SvnKitDebugLogger
      logger = new SvnKitDebugLogger(Boolean.getBoolean(LOG_PARAMETER_NAME), Boolean.getBoolean(TRACE_NATIVE_CALLS), LOG);
    SVNDebugLog.setDefaultLog(logger);

    SVNJNAUtil.setJNAEnabled(true);
    SvnHttpAuthMethodsDefaultChecker.check();

    SVNAdminAreaFactory.setSelector(new SvnKitAdminAreaFactorySelector());

    DAVRepositoryFactory.setup();
    SVNRepositoryFactoryImpl.setup();
    FSRepositoryFactory.setup();

    // non-optimized writing is fast enough on Linux/MacOS, and somewhat more reliable
    if (SystemInfo.isWindows) {
      SVNAdminArea14.setOptimizedWritingEnabled(true);
    }

    if (!SVNJNAUtil.isJNAPresent()) {
      LOG.warn("JNA is not found by svnkit library");
    }

    ourExplicitlySetSslProtocols = System.getProperty(SVNKIT_HTTP_SSL_PROTOCOLS);
  }

  public SvnKitManager(@NotNull SvnVcs vcs) {
    myVcs = vcs;
    myProject = myVcs.getProject();
    myConfiguration = myVcs.getSvnConfiguration();

    refreshSSLProperty();
  }

  @Nullable
  public static String getExplicitlySetSslProtocols() {
    return ourExplicitlySetSslProtocols;
  }

  public static boolean isSSLProtocolExplicitlySet() {
    return ourExplicitlySetSslProtocols != null;
  }

  public void refreshSSLProperty() {
    if (isSSLProtocolExplicitlySet()) return;

    if (SvnConfiguration.SSLProtocols.all.equals(myConfiguration.getSslProtocols())) {
      System.clearProperty(SVNKIT_HTTP_SSL_PROTOCOLS);
    }
    else if (SvnConfiguration.SSLProtocols.sslv3.equals(myConfiguration.getSslProtocols())) {
      System.setProperty(SVNKIT_HTTP_SSL_PROTOCOLS, "SSLv3");
    }
    else if (SvnConfiguration.SSLProtocols.tlsv1.equals(myConfiguration.getSslProtocols())) {
      System.setProperty(SVNKIT_HTTP_SSL_PROTOCOLS, "TLSv1");
    }
  }

  public void activate() {
    if (SystemInfo.isWindows) {
      if (!SVNJNAUtil.isJNAPresent()) {
        Notifications.Bus.notify(new Notification(myVcs.getDisplayName(), "Subversion plugin: no JNA",
                                                  "A problem with JNA initialization for SVNKit library. Encryption is not available.",
                                                  NotificationType.WARNING), myProject);
      }
      else if (!SVNJNAUtil.isWinCryptEnabled()) {
        Notifications.Bus.notify(new Notification(myVcs.getDisplayName(), "Subversion plugin: no encryption",
                                                  "A problem with encryption module (Crypt32.dll) initialization for SVNKit library. " +
                                                  "Encryption is not available.",
                                                  NotificationType.WARNING
        ), myProject);
      }
    }
  }

  @NotNull
  public ISVNOptions getSvnOptions() {
    return myConfiguration.getOptions(myProject);
  }

  @NotNull
  public SVNRepository createRepository(String url) throws SVNException {
    return createRepository(SVNURL.parseURIEncoded(url));
  }

  @NotNull
  public SVNRepository createRepository(@NotNull SVNURL url) throws SVNException {
    SVNRepository repository = SVNRepositoryFactory.create(url);
    repository.setAuthenticationManager(getAuthenticationManager());
    repository.setTunnelProvider(getSvnOptions());

    return repository;
  }

  @NotNull
  private ISVNRepositoryPool getPool() {
    return getPool(getAuthenticationManager());
  }

  @NotNull
  private ISVNRepositoryPool getPool(@NotNull ISVNAuthenticationManager manager) {
    if (myProject.isDisposed()) {
      throw new ProcessCanceledException();
    }
    return new PrimitivePool(manager, getSvnOptions());
  }

  @NotNull
  public SVNUpdateClient createUpdateClient() {
    return setupClient(new SVNUpdateClient(getPool(), getSvnOptions()));
  }

  @NotNull
  public SVNUpdateClient createUpdateClient(@NotNull ISVNAuthenticationManager manager) {
    return setupClient(new SVNUpdateClient(getPool(manager), getSvnOptions()), manager);
  }

  @NotNull
  public SVNStatusClient createStatusClient() {
    SVNStatusClient client = new SVNStatusClient(getPool(), getSvnOptions());
    client.setIgnoreExternals(false);

    return setupClient(client);
  }

  @NotNull
  public SVNWCClient createWCClient() {
    return setupClient(new SVNWCClient(getPool(), getSvnOptions()));
  }

  @NotNull
  public SVNWCClient createWCClient(@NotNull ISVNAuthenticationManager manager) {
    return setupClient(new SVNWCClient(getPool(manager), getSvnOptions()), manager);
  }

  @NotNull
  public SVNCopyClient createCopyClient() {
    return setupClient(new SVNCopyClient(getPool(), getSvnOptions()));
  }

  @NotNull
  public SVNMoveClient createMoveClient() {
    return setupClient(new SVNMoveClient(getPool(), getSvnOptions()));
  }

  @NotNull
  public SVNLogClient createLogClient() {
    return setupClient(new SVNLogClient(getPool(), getSvnOptions()));
  }

  @NotNull
  public SVNLogClient createLogClient(@NotNull ISVNAuthenticationManager manager) {
    return setupClient(new SVNLogClient(getPool(manager), getSvnOptions()), manager);
  }

  @NotNull
  public SVNCommitClient createCommitClient() {
    return setupClient(new SVNCommitClient(getPool(), getSvnOptions()));
  }

  @NotNull
  public SVNDiffClient createDiffClient() {
    return setupClient(new SVNDiffClient(getPool(), getSvnOptions()));
  }

  @NotNull
  public SVNChangelistClient createChangelistClient() {
    return setupClient(new SVNChangelistClient(getPool(), getSvnOptions()));
  }

  @NotNull
  private SvnAuthenticationManager getAuthenticationManager() {
    return myConfiguration.getAuthenticationManager(myVcs);
  }

  @NotNull
  private <T extends SVNBasicClient> T setupClient(@NotNull T client) {
    return setupClient(client, getAuthenticationManager());
  }

  @NotNull
  private static <T extends SVNBasicClient> T setupClient(@NotNull T client, @NotNull ISVNAuthenticationManager manager) {
    client.getOperationsFactory().setAuthenticationManager(manager);

    return client;
  }
}