1 | |
package org.kuali.maven.wagon; |
2 | |
|
3 | |
import java.net.URI; |
4 | |
import java.net.URISyntaxException; |
5 | |
|
6 | |
import org.apache.maven.wagon.events.SessionEvent; |
7 | |
import org.apache.maven.wagon.events.SessionListener; |
8 | |
import org.apache.maven.wagon.events.TransferEvent; |
9 | |
import org.apache.maven.wagon.events.TransferListener; |
10 | |
import org.slf4j.Logger; |
11 | |
import org.slf4j.LoggerFactory; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | 0 | public class S3Listener implements TransferListener, SessionListener { |
20 | 0 | final Logger log = LoggerFactory.getLogger(S3Listener.class); |
21 | 0 | SimpleFormatter formatter = new SimpleFormatter(); |
22 | 0 | SessionTracker sessionTracker = new SessionTracker(); |
23 | |
|
24 | |
public void transferCompleted(final TransferEvent transferEvent) { |
25 | 0 | TransferTracker tt = sessionTracker.getCurrentTransfer(); |
26 | 0 | tt.setCompleted(System.currentTimeMillis()); |
27 | |
|
28 | |
|
29 | 0 | } |
30 | |
|
31 | |
public void transferError(final TransferEvent transferEvent) { |
32 | 0 | log.error("Transfer error: " + transferEvent.getException(), transferEvent.getException()); |
33 | 0 | } |
34 | |
|
35 | |
public void transferInitiated(final TransferEvent transferEvent) { |
36 | 0 | sessionTracker.addTransfer(new TransferTracker()); |
37 | 0 | TransferTracker tt = sessionTracker.getCurrentTransfer(); |
38 | 0 | tt.setInitiated(System.currentTimeMillis()); |
39 | 0 | } |
40 | |
|
41 | |
public void transferProgress(final TransferEvent transferEvent, final byte[] buffer, final int length) { |
42 | |
|
43 | 0 | if (length == -1) { |
44 | 0 | return; |
45 | |
} |
46 | 0 | TransferTracker tt = sessionTracker.getCurrentTransfer(); |
47 | 0 | int byteCount = tt.getByteCount() + length; |
48 | 0 | tt.setByteCount(byteCount); |
49 | 0 | } |
50 | |
|
51 | |
public void transferStarted(final TransferEvent transferEvent) { |
52 | 0 | TransferTracker tt = sessionTracker.getCurrentTransfer(); |
53 | 0 | tt.setStarted(System.currentTimeMillis()); |
54 | 0 | if (transferEvent.getRequestType() == TransferEvent.REQUEST_GET) { |
55 | 0 | log.info("Downloading: " + getURI(transferEvent)); |
56 | |
} else { |
57 | 0 | log.info("Uploading: " + getURI(transferEvent)); |
58 | |
} |
59 | |
|
60 | 0 | } |
61 | |
|
62 | |
protected String getURI(final TransferEvent event) { |
63 | 0 | return getNormalizedURI(event.getWagon().getRepository().getUrl() + "/" + event.getResource().getName()); |
64 | |
} |
65 | |
|
66 | |
protected String getNormalizedURI(final String uri) { |
67 | |
try { |
68 | 0 | URI rawUri = new URI(uri); |
69 | 0 | return rawUri.normalize().toString(); |
70 | 0 | } catch (URISyntaxException e) { |
71 | 0 | return uri; |
72 | |
} |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void sessionOpening(final SessionEvent sessionEvent) { |
79 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public void sessionOpened(final SessionEvent sessionEvent) { |
86 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
87 | 0 | sessionTracker.setOpened(System.currentTimeMillis()); |
88 | |
|
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public void sessionDisconnecting(final SessionEvent sessionEvent) { |
95 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
96 | 0 | sessionTracker.setDisconnecting(System.currentTimeMillis()); |
97 | |
|
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public void sessionDisconnected(final SessionEvent sessionEvent) { |
104 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
105 | |
|
106 | 0 | sessionTracker.setDisconnected(System.currentTimeMillis()); |
107 | 0 | int transferCount = sessionTracker.getTransfers().size(); |
108 | 0 | long byteCount = 0; |
109 | 0 | long transferElapsed = 0; |
110 | 0 | for (TransferTracker tt : sessionTracker.getTransfers()) { |
111 | 0 | byteCount += tt.getByteCount(); |
112 | 0 | transferElapsed += tt.getCompleted() - tt.getStarted(); |
113 | |
} |
114 | 0 | long elapsed = sessionTracker.getDisconnected() - sessionTracker.getOpened(); |
115 | 0 | StringBuilder sb = new StringBuilder(); |
116 | 0 | sb.append("Transfers: " + transferCount); |
117 | 0 | sb.append(" Time: " + formatter.getTime(elapsed)); |
118 | 0 | sb.append(" Amount: " + formatter.getSize(byteCount)); |
119 | 0 | sb.append(" Rate: " + formatter.getRate(transferElapsed, byteCount)); |
120 | 0 | sb.append(" Throughput: " + formatter.getRate(elapsed, byteCount)); |
121 | 0 | log.info(sb.toString()); |
122 | 0 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public void sessionConnectionRefused(final SessionEvent sessionEvent) { |
128 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
129 | 0 | log.warn(sessionEvent.getWagon().getRepository().getUrl() + " - Connection refused"); |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public void sessionLoggedIn(final SessionEvent sessionEvent) { |
136 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
137 | 0 | sessionTracker.setLoggedIn(System.currentTimeMillis()); |
138 | 0 | log.info("Logged in - " + sessionEvent.getWagon().getRepository().getHost()); |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void sessionLoggedOff(final SessionEvent sessionEvent) { |
145 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
146 | 0 | sessionTracker.setLoggedOff(System.currentTimeMillis()); |
147 | 0 | log.info("Logged off - " + sessionEvent.getWagon().getRepository().getHost()); |
148 | 0 | } |
149 | |
|
150 | |
public void sessionError(final SessionEvent sessionEvent) { |
151 | 0 | sessionTracker.addSessionEvent(sessionEvent); |
152 | 0 | log.error("Session error: " + sessionEvent.getException(), sessionEvent.getException()); |
153 | 0 | } |
154 | |
|
155 | |
public void debug(final String message) { |
156 | 0 | log.debug(message); |
157 | 0 | } |
158 | |
|
159 | |
} |