1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import org.eclipse.aether.RepositorySystemSession;
22 import org.eclipse.aether.transfer.AbstractTransferListener;
23 import org.eclipse.aether.transfer.TransferCancelledException;
24 import org.eclipse.aether.transfer.TransferEvent;
25 import org.eclipse.aether.transfer.TransferListener;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import static java.util.Objects.requireNonNull;
30
31 class SafeTransferListener extends AbstractTransferListener {
32
33 private static final Logger LOGGER = LoggerFactory.getLogger(SafeTransferListener.class);
34
35 private final TransferListener listener;
36
37 public static TransferListener wrap(RepositorySystemSession session) {
38 TransferListener listener = session.getTransferListener();
39 if (listener == null) {
40 return null;
41 }
42 return new SafeTransferListener(listener);
43 }
44
45 protected SafeTransferListener(RepositorySystemSession session) {
46 this(session.getTransferListener());
47 }
48
49 private SafeTransferListener(TransferListener listener) {
50 this.listener = listener;
51 }
52
53 private void logError(TransferEvent event, Throwable e) {
54 LOGGER.debug(
55 "Failed to dispatch transfer event '{}' to {}",
56 event,
57 listener.getClass().getCanonicalName(),
58 e);
59 }
60
61 @Override
62 public void transferInitiated(TransferEvent event) throws TransferCancelledException {
63 requireNonNull(event, "event cannot be null");
64 if (listener != null) {
65 try {
66 listener.transferInitiated(event);
67 } catch (RuntimeException | LinkageError e) {
68 logError(event, e);
69 }
70 }
71 }
72
73 @Override
74 public void transferStarted(TransferEvent event) throws TransferCancelledException {
75 requireNonNull(event, "event cannot be null");
76 if (listener != null) {
77 try {
78 listener.transferStarted(event);
79 } catch (RuntimeException | LinkageError e) {
80 logError(event, e);
81 }
82 }
83 }
84
85 @Override
86 public void transferProgressed(TransferEvent event) throws TransferCancelledException {
87 requireNonNull(event, "event cannot be null");
88 if (listener != null) {
89 try {
90 listener.transferProgressed(event);
91 } catch (RuntimeException | LinkageError e) {
92 logError(event, e);
93 }
94 }
95 }
96
97 @Override
98 public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
99 requireNonNull(event, "event cannot be null");
100 if (listener != null) {
101 try {
102 listener.transferCorrupted(event);
103 } catch (RuntimeException | LinkageError e) {
104 logError(event, e);
105 }
106 }
107 }
108
109 @Override
110 public void transferSucceeded(TransferEvent event) {
111 requireNonNull(event, "event cannot be null");
112 if (listener != null) {
113 try {
114 listener.transferSucceeded(event);
115 } catch (RuntimeException | LinkageError e) {
116 logError(event, e);
117 }
118 }
119 }
120
121 @Override
122 public void transferFailed(TransferEvent event) {
123 requireNonNull(event, "event cannot be null");
124 if (listener != null) {
125 try {
126 listener.transferFailed(event);
127 } catch (RuntimeException | LinkageError e) {
128 logError(event, e);
129 }
130 }
131 }
132 }