View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }