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
|
/*******************************************************************************
* Copyright (c) 2008-2010 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.core.internal.embedder;
import java.io.File;
import org.eclipse.core.runtime.IProgressMonitor;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.index.artifact.Gav;
import org.apache.maven.index.artifact.GavCalculator;
import org.apache.maven.index.artifact.M2GavCalculator;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
import org.apache.maven.wagon.repository.Repository;
import org.eclipse.m2e.core.core.MavenLogger;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.embedder.ILocalRepositoryListener;
/**
* @author Eugene Kuleshov
*/
final class WagonTransferListenerAdapter extends AbstractTransferListenerAdapter implements TransferListener {
// TODO this is just wrong!
private final GavCalculator gavCalculator = new M2GavCalculator();
WagonTransferListenerAdapter(MavenImpl maven, IProgressMonitor monitor) {
super(maven, monitor);
}
public void transferInitiated(TransferEvent e) {
// System.err.println( "init "+e.getWagon().getRepository()+"/"+e.getResource().getName());
transferInitiated((String) null);
}
public void transferStarted(TransferEvent e) {
StringBuilder sb = new StringBuilder();
if(e.getWagon() != null && e.getWagon().getRepository() != null) {
Wagon wagon = e.getWagon();
Repository repository = wagon.getRepository();
String repositoryId = repository.getId();
sb.append(repositoryId).append(" : "); //$NON-NLS-1$
}
sb.append(e.getResource().getName());
transferStarted(sb.toString());
}
public void transferProgress(TransferEvent e, byte[] buffer, int length) {
long total = e.getResource().getContentLength();
String artifactUrl = e.getWagon().getRepository() + "/" + e.getResource().getName(); //$NON-NLS-1$
transferProgress(artifactUrl, total, length);
}
public void transferCompleted(TransferEvent e) {
String artifactUrl = e.getWagon().getRepository() + "/" + e.getResource().getName(); //$NON-NLS-1$
transferCompleted(artifactUrl);
notifyLocalRepositoryListeners(e);
}
public void transferError(TransferEvent e) {
transferError(e.getWagon().getRepository() + "/" + e.getResource().getName(), e.getException()); //$NON-NLS-1$
}
public void debug(String message) {
// System.err.println( "debug "+message);
}
private void notifyLocalRepositoryListeners(TransferEvent e) {
try {
ArtifactRepository localRepository = maven.getLocalRepository();
if (!(localRepository.getLayout() instanceof DefaultRepositoryLayout)) {
return;
}
String repoBasepath = new File(localRepository.getBasedir()).getCanonicalPath();
File artifactFile = e.getLocalFile();
if (artifactFile == null) {
return;
}
String artifactPath = artifactFile.getCanonicalPath();
if (!artifactPath.startsWith(repoBasepath)) {
return;
}
artifactPath = artifactPath.substring(repoBasepath.length());
Gav gav = gavCalculator.pathToGav(artifactPath);
ArtifactKey artifactKey = new ArtifactKey(gav.getGroupId(), gav.getArtifactId(), gav.getVersion(), gav.getClassifier());
File repoBasedir = new File(localRepository.getBasedir()).getCanonicalFile();
for (ILocalRepositoryListener listener : maven.getLocalRepositoryListeners()) {
listener.artifactInstalled(repoBasedir, artifactKey, artifactFile);
}
} catch (Exception ex) {
MavenLogger.log("Could not notify local repository listeners", ex);
}
}
}
|