summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/commandLine/BaseUpdateCommandListener.java
blob: eb80bad0c70f1811677fd2e6234c50d3f79a81a6 (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
package org.jetbrains.idea.svn.commandLine;

import com.intellij.execution.process.ProcessOutputTypes;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vcs.VcsException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.ISVNEventHandler;
import org.tmatesoft.svn.core.wc.SVNEvent;

import java.io.File;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Konstantin Kolosovsky.
 */
public class BaseUpdateCommandListener extends LineCommandAdapter {

  @NotNull
  private final UpdateOutputLineConverter converter;

  @Nullable
  private final ISVNEventHandler handler;

  @NotNull
  private final AtomicReference<SVNException> exception;

  public BaseUpdateCommandListener(@NotNull File base, @Nullable ISVNEventHandler handler) {
    this.handler = handler;
    this.converter = new UpdateOutputLineConverter(base);
    exception = new AtomicReference<SVNException>();
  }

  @Override
  public void onLineAvailable(String line, Key outputType) {
    if (ProcessOutputTypes.STDOUT.equals(outputType)) {
      final SVNEvent event = converter.convert(line);
      if (event != null) {
        beforeHandler(event);
        try {
          callHandler(event);
        }
        catch (SVNException e) {
          cancel();
          exception.set(e);
        }
      }
    }
  }

  private void callHandler(SVNEvent event) throws SVNException {
    if (handler != null) {
      handler.handleEvent(event, 0.5);
    }
  }

  public void throwIfException() throws SVNException {
    SVNException e = exception.get();

    if (e != null) {
      throw e;
    }
  }

  public void throwWrappedIfException() throws VcsException {
    SVNException e = exception.get();

    if (e != null) {
      throw new VcsException(e);
    }
  }

  protected void beforeHandler(@NotNull SVNEvent event) {
  }
}