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            out.println( "" );
143        }
144        else
145        {
146            final String message = "Uploading: " + transferEvent.getResource().getName() + " to "
147                + transferEvent.getWagon().getRepository().getUrl();
148
149            out.println( message );
150
151            out.println( "" );
152
153        }
154    }
155
156    /**
157     * @see TransferListener#transferProgress(TransferEvent,byte[],int)
158     */
159    public void transferProgress( final TransferEvent transferEvent, byte[] buffer, int length )
160    {
161
162        out.print( "#" );
163        //String data = new String( transferEvent.getData(),0, transferEvent.getDataLength());
164        //out.println(data);
165        transfer += length;
166    }
167
168    /**
169     * @see TransferListener#transferCompleted(TransferEvent)
170     */
171    public void transferCompleted( final TransferEvent transferEvent )
172    {
173        final double duration = (double) ( transferEvent.getTimestamp() - timestamp ) / 1000;
174
175        out.println();
176
177        final String message = "Transfer finished. " + transfer + " bytes copied in " + duration + " seconds";
178
179        out.println( message );
180
181    }
182
183    /**
184     * @see TransferListener#transferError(TransferEvent)
185     */
186    public void transferError( final TransferEvent transferEvent )
187    {
188        out.println( " Transfer error: " + transferEvent.getException() );
189
190    }
191
192    /**
193     * @see SessionListener#sessionError(SessionEvent)
194     */
195    public void sessionError( final SessionEvent sessionEvent )
196    {
197        out.println( " Session error: " + sessionEvent.getException() );
198
199    }
200
201    public PrintStream getOut()
202    {
203        return out;
204    }
205
206}