summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/branchConfig/NewRootBunch.java
blob: 2c7444ed85b03024f222b2a25e9d93022d2b6cc5 (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
/*
 * 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.branchConfig;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManagerQueue;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vcs.CalledInBackground;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.ui.VcsBalloonProblemNotifier;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Consumer;
import com.intellij.util.PairConsumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.integrate.SvnBranchItem;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// synch is here
public class NewRootBunch implements SvnBranchConfigManager {
  private final static Logger LOG = Logger.getInstance("#org.jetbrains.idea.svn.branchConfig.NewRootBunch");
  private final Object myLock = new Object();
  private final Project myProject;
  private final ProgressManagerQueue myBranchesLoader;
  private final Map<VirtualFile, InfoStorage<SvnBranchConfigurationNew>> myMap;

  public NewRootBunch(final Project project, ProgressManagerQueue branchesLoader) {
    myProject = project;
    myBranchesLoader = branchesLoader;
    myMap = new HashMap<VirtualFile, InfoStorage<SvnBranchConfigurationNew>>();
  }

  public void updateForRoot(@NotNull final VirtualFile root, @NotNull final InfoStorage<SvnBranchConfigurationNew> config,
                            @Nullable final PairConsumer<SvnBranchConfigurationNew, SvnBranchConfigurationNew> callbackOnUpdate) {
    synchronized (myLock) {
      final InfoStorage<SvnBranchConfigurationNew> existing = myMap.get(root);
      if (existing == null) {
        myMap.put(root, config);
        if (callbackOnUpdate != null) {
          callbackOnUpdate.consume(null, config.getValue());
        }
      } else {
        existing.accept(config, callbackOnUpdate);
      }
    }
  }

  public void updateBranches(@NotNull final VirtualFile root, @NotNull final String branchesParent,
                             @NotNull final InfoStorage<List<SvnBranchItem>> items) {
    synchronized (myLock) {
      final InfoStorage<SvnBranchConfigurationNew> existing = myMap.get(root);
      if (existing == null) {
        LOG.info("cannot update branches, branches parent not found: " + branchesParent);
      } else {
        existing.getValue().updateBranch(branchesParent, items);
      }
    }
  }

  @NotNull
  public SvnBranchConfigurationNew getConfig(@NotNull final VirtualFile root) {
    synchronized (myLock) {
      final InfoStorage<SvnBranchConfigurationNew> value = myMap.get(root);
      final SvnBranchConfigurationNew result;
      if (value == null) {
        result = new SvnBranchConfigurationNew();
        myMap.put(root, new InfoStorage<SvnBranchConfigurationNew>(result, InfoReliability.empty));
        myBranchesLoader.run(new DefaultBranchConfigInitializer(myProject, this, root));
      } else {
        result = value.getValue();
      }
      return result;
    }
  }

  public void reloadBranches(@NotNull final VirtualFile root, @NotNull final String branchParentUrl,
                             final Consumer<List<SvnBranchItem>> callback) {
    ApplicationManager.getApplication().executeOnPooledThread(new BranchesLoadRunnable(myProject, this, branchParentUrl,
                                                                                       InfoReliability.setByUser, root, callback, true));
  }

  @Nullable
  @CalledInBackground
  public SVNURL getWorkingBranchWithReload(final SVNURL svnurl, final VirtualFile root) {
    final Ref<SVNURL> result = new Ref<SVNURL>();
    try {
      final SvnBranchConfigurationNew configuration = myMap.get(root).getValue();
      final String group = configuration.getGroupToLoadToReachUrl(svnurl);
      final Runnable runnable = new Runnable() {
        public void run() {
          final SvnBranchConfigurationNew reloadedConfiguration = myMap.get(root).getValue();
          try {
            result.set(reloadedConfiguration.getWorkingBranch(svnurl));
          }
          catch (SVNException e) {
            //
          }
        }
      };

      if (group == null) {
        runnable.run();
      } else {
        new BranchesLoadRunnable(myProject, this, group, InfoReliability.setByUser, root,
                                 new Consumer<List<SvnBranchItem>>() {
                                   public void consume(List<SvnBranchItem> svnBranchItems) {
                                     runnable.run();
                                   }
                                 }, true).run();
      }
    }
    catch (SVNException e) {
      //
    }
    return result.get();
  }

  public static class BranchesLoadRunnable implements Runnable {
    private final Project myProject;
    private final SvnBranchConfigManager myBunch;
    private final VirtualFile myRoot;
    @Nullable
    private final Consumer<List<SvnBranchItem>> myCallback;
    private final String myUrl;
    private final InfoReliability myInfoReliability;
    private boolean myPassive;

    public BranchesLoadRunnable(final Project project,
                                final SvnBranchConfigManager bunch,
                                final String url,
                                final InfoReliability infoReliability,
                                final VirtualFile root,
                                @Nullable final Consumer<List<SvnBranchItem>> callback,
                                boolean passive) {
      myProject = project;
      myBunch = bunch;
      myUrl = url;
      myInfoReliability = infoReliability;
      myRoot = root;
      myCallback = callback;
      myPassive = passive;
    }

    public void run() {
      boolean callbackCalled = false;
      try {
        final List<SvnBranchItem> items = BranchesLoader.loadBranches(myProject, myUrl, myPassive);
        myBunch.updateBranches(myRoot, myUrl, new InfoStorage<List<SvnBranchItem>>(items, myInfoReliability));
        if (myCallback != null) {
          myCallback.consume(items);
          callbackCalled = true;
        }
      }
      catch (VcsException e) {
        showError(e);
      }
      catch (SVNException e) {
        showError(e);
      }
      finally {
        // callback must be called by contract
        if (myCallback != null && (! callbackCalled)) {
          myCallback.consume(null);
        }
      }
    }

    private void showError(Exception e) {
      // already logged inside
      if (InfoReliability.setByUser.equals(myInfoReliability)) {
        VcsBalloonProblemNotifier.showOverChangesView(myProject, "Branches load error: " + e.getMessage(), MessageType.ERROR);
      }
    }
  }

  private static class DefaultBranchConfigInitializer implements Runnable {
    private final Project myProject;
    private final SvnBranchConfigManager myBunch;
    private final VirtualFile myRoot;

    private DefaultBranchConfigInitializer(final Project project, final SvnBranchConfigManager bunch, final VirtualFile root) {
      myProject = project;
      myRoot = root;
      myBunch = bunch;
    }

    public void run() {
      final SvnBranchConfigurationNew result = DefaultConfigLoader.loadDefaultConfiguration(myProject, myRoot);
      if (result != null) {
        final Application application = ApplicationManager.getApplication();
        for (String url : result.getBranchUrls()) {
          application.executeOnPooledThread(new BranchesLoadRunnable(myProject, myBunch, url, InfoReliability.defaultValues, myRoot, null,
                                                                     true));
        }
        myBunch.updateForRoot(myRoot, new InfoStorage<SvnBranchConfigurationNew>(result, InfoReliability.defaultValues), null);
      }
    }
  }

  public Map<VirtualFile, SvnBranchConfigurationNew> getMapCopy() {
    synchronized (myLock) {
      final Map<VirtualFile, SvnBranchConfigurationNew> result = new HashMap<VirtualFile, SvnBranchConfigurationNew>();
      for (VirtualFile vf : myMap.keySet()) {
        result.put(vf, myMap.get(vf).getValue());
      }
      return result;
    }
  }
}