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.apache.maven.repository.legacy;
20  
21  import java.util.IdentityHashMap;
22  import java.util.Map;
23  
24  import org.apache.maven.repository.ArtifactTransferEvent;
25  import org.apache.maven.repository.ArtifactTransferListener;
26  import org.apache.maven.repository.ArtifactTransferResource;
27  import org.apache.maven.wagon.events.TransferEvent;
28  import org.apache.maven.wagon.events.TransferListener;
29  import org.apache.maven.wagon.repository.Repository;
30  import org.apache.maven.wagon.resource.Resource;
31  
32  /**
33   * TransferListenerAdapter
34   */
35  @Deprecated
36  public class TransferListenerAdapter implements TransferListener {
37  
38      private final ArtifactTransferListener listener;
39  
40      private final Map<Resource, ArtifactTransferResource> artifacts;
41  
42      private final Map<Resource, Long> transfers;
43  
44      public static TransferListener newAdapter(ArtifactTransferListener listener) {
45          if (listener == null) {
46              return null;
47          } else {
48              return new TransferListenerAdapter(listener);
49          }
50      }
51  
52      private TransferListenerAdapter(ArtifactTransferListener listener) {
53          this.listener = listener;
54          this.artifacts = new IdentityHashMap<>();
55          this.transfers = new IdentityHashMap<>();
56      }
57  
58      public void debug(String message) {}
59  
60      public void transferCompleted(TransferEvent transferEvent) {
61          ArtifactTransferEvent event = wrap(transferEvent);
62  
63          Long transferred;
64          synchronized (transfers) {
65              transferred = transfers.remove(transferEvent.getResource());
66          }
67          if (transferred != null) {
68              event.setTransferredBytes(transferred);
69          }
70  
71          synchronized (artifacts) {
72              artifacts.remove(transferEvent.getResource());
73          }
74  
75          listener.transferCompleted(event);
76      }
77  
78      public void transferError(TransferEvent transferEvent) {
79          synchronized (transfers) {
80              transfers.remove(transferEvent.getResource());
81          }
82          synchronized (artifacts) {
83              artifacts.remove(transferEvent.getResource());
84          }
85      }
86  
87      public void transferInitiated(TransferEvent transferEvent) {
88          listener.transferInitiated(wrap(transferEvent));
89      }
90  
91      public void transferProgress(TransferEvent transferEvent, byte[] buffer, int length) {
92          Long transferred;
93          synchronized (transfers) {
94              transferred = transfers.get(transferEvent.getResource());
95              if (transferred == null) {
96                  transferred = (long) length;
97              } else {
98                  transferred = transferred + length;
99              }
100             transfers.put(transferEvent.getResource(), transferred);
101         }
102 
103         ArtifactTransferEvent event = wrap(transferEvent);
104         event.setDataBuffer(buffer);
105         event.setDataOffset(0);
106         event.setDataLength(length);
107         event.setTransferredBytes(transferred);
108 
109         listener.transferProgress(event);
110     }
111 
112     public void transferStarted(TransferEvent transferEvent) {
113         listener.transferStarted(wrap(transferEvent));
114     }
115 
116     private ArtifactTransferEvent wrap(TransferEvent event) {
117         if (event == null) {
118             return null;
119         } else {
120             String wagon = event.getWagon().getClass().getName();
121 
122             ArtifactTransferResource artifact = wrap(event.getWagon().getRepository(), event.getResource());
123 
124             ArtifactTransferEvent evt;
125             if (event.getException() != null) {
126                 evt = new ArtifactTransferEvent(wagon, event.getException(), event.getRequestType(), artifact);
127             } else {
128                 evt = new ArtifactTransferEvent(wagon, event.getEventType(), event.getRequestType(), artifact);
129             }
130 
131             evt.setLocalFile(event.getLocalFile());
132 
133             return evt;
134         }
135     }
136 
137     private ArtifactTransferResource wrap(Repository repository, Resource resource) {
138         if (resource == null) {
139             return null;
140         } else {
141             synchronized (artifacts) {
142                 ArtifactTransferResource artifact = artifacts.get(resource);
143 
144                 if (artifact == null) {
145                     artifact = new MavenArtifact(repository.getUrl(), resource);
146                     artifacts.put(resource, artifact);
147                 }
148 
149                 return artifact;
150             }
151         }
152     }
153 }