001package org.apache.maven.wagon.observers; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.wagon.events.SessionEvent; 023import org.apache.maven.wagon.events.SessionListener; 024import org.apache.maven.wagon.events.TransferEvent; 025import org.apache.maven.wagon.events.TransferListener; 026 027import java.io.PrintStream; 028 029/** 030 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 031 * 032 */ 033public class Debug 034 implements SessionListener, TransferListener 035{ 036 private PrintStream out; 037 038 long timestamp; 039 040 long transfer; 041 042 public Debug() 043 { 044 this( System.out ); 045 } 046 047 public Debug( PrintStream out ) 048 { 049 this.out = out; 050 } 051 052 /** 053 * @see SessionListener#sessionOpening(SessionEvent) 054 */ 055 public void sessionOpening( final SessionEvent sessionEvent ) 056 { 057 //out.println( .getUrl() + " - Session: Opening "); 058 } 059 060 /** 061 * @see SessionListener#sessionOpened(SessionEvent) 062 */ 063 public void sessionOpened( final SessionEvent sessionEvent ) 064 { 065 out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Opened " ); 066 } 067 068 /** 069 * @see SessionListener#sessionDisconnecting(SessionEvent) 070 */ 071 public void sessionDisconnecting( final SessionEvent sessionEvent ) 072 { 073 out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Disconnecting " ); 074 075 } 076 077 /** 078 * @see SessionListener#sessionDisconnected(SessionEvent) 079 */ 080 public void sessionDisconnected( final SessionEvent sessionEvent ) 081 { 082 out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Disconnected" ); 083 } 084 085 /** 086 * @see SessionListener#sessionConnectionRefused(SessionEvent) 087 */ 088 public void sessionConnectionRefused( final SessionEvent sessionEvent ) 089 { 090 out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Connection refused" ); 091 092 } 093 094 /** 095 * @see SessionListener#sessionLoggedIn(SessionEvent) 096 */ 097 public void sessionLoggedIn( final SessionEvent sessionEvent ) 098 { 099 out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Logged in" ); 100 101 } 102 103 /** 104 * @see SessionListener#sessionLoggedOff(SessionEvent) 105 */ 106 public void sessionLoggedOff( final SessionEvent sessionEvent ) 107 { 108 out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Logged off" ); 109 110 } 111 112 /** 113 * @see TransferListener#debug(String) 114 */ 115 public void debug( final String message ) 116 { 117 out.println( message ); 118 119 } 120 121 public void transferInitiated( TransferEvent transferEvent ) 122 { 123 // This space left intentionally blank 124 } 125 126 /** 127 * @see TransferListener#transferStarted(TransferEvent) 128 */ 129 public void transferStarted( final TransferEvent transferEvent ) 130 { 131 timestamp = transferEvent.getTimestamp(); 132 133 transfer = 0; 134 135 if ( transferEvent.getRequestType() == TransferEvent.REQUEST_GET ) 136 { 137 final String message = "Downloading: " + transferEvent.getResource().getName() + " from " 138 + transferEvent.getWagon().getRepository().getUrl(); 139 140 out.println( message ); 141 } 142 else 143 { 144 final String message = "Uploading: " + transferEvent.getResource().getName() + " to " 145 + transferEvent.getWagon().getRepository().getUrl(); 146 147 out.println( message ); 148 149 } 150 } 151 152 /** 153 * @see TransferListener#transferProgress(TransferEvent,byte[],int) 154 */ 155 public void transferProgress( final TransferEvent transferEvent, byte[] buffer, int length ) 156 { 157 158 out.print( "#" ); 159 //String data = new String( transferEvent.getData(),0, transferEvent.getDataLength()); 160 //out.println(data); 161 transfer += length; 162 } 163 164 /** 165 * @see TransferListener#transferCompleted(TransferEvent) 166 */ 167 public void transferCompleted( final TransferEvent transferEvent ) 168 { 169 final double duration = (double) ( transferEvent.getTimestamp() - timestamp ) / 1000; 170 171 out.println(); 172 173 final String message = "Transfer finished. " + transfer + " bytes copied in " + duration + " seconds"; 174 175 out.println( message ); 176 177 } 178 179 /** 180 * @see TransferListener#transferError(TransferEvent) 181 */ 182 public void transferError( final TransferEvent transferEvent ) 183 { 184 out.println( " Transfer error: " + transferEvent.getException() ); 185 186 } 187 188 /** 189 * @see SessionListener#sessionError(SessionEvent) 190 */ 191 public void sessionError( final SessionEvent sessionEvent ) 192 { 193 out.println( " Session error: " + sessionEvent.getException() ); 194 195 } 196 197 public PrintStream getOut() 198 { 199 return out; 200 } 201 202}