View Javadoc
1   package org.apache.maven.wagon.observers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.wagon.events.SessionEvent;
23  import org.apache.maven.wagon.events.SessionListener;
24  import org.apache.maven.wagon.events.TransferEvent;
25  import org.apache.maven.wagon.events.TransferListener;
26  
27  import java.io.PrintStream;
28  
29  /**
30   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
31   *
32   */
33  public class Debug
34      implements SessionListener, TransferListener
35  {
36      private PrintStream out;
37  
38      long timestamp;
39  
40      long transfer;
41  
42      public Debug()
43      {
44          this( System.out );
45      }
46  
47      public Debug( PrintStream out )
48      {
49          this.out = out;
50      }
51  
52      /**
53       * @see SessionListener#sessionOpening(SessionEvent)
54       */
55      public void sessionOpening( final SessionEvent sessionEvent )
56      {
57          //out.println( .getUrl() + " - Session: Opening  ");
58      }
59  
60      /**
61       * @see SessionListener#sessionOpened(SessionEvent)
62       */
63      public void sessionOpened( final SessionEvent sessionEvent )
64      {
65          out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Opened  " );
66      }
67  
68      /**
69       * @see SessionListener#sessionDisconnecting(SessionEvent)
70       */
71      public void sessionDisconnecting( final SessionEvent sessionEvent )
72      {
73          out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Disconnecting  " );
74  
75      }
76  
77      /**
78       * @see SessionListener#sessionDisconnected(SessionEvent)
79       */
80      public void sessionDisconnected( final SessionEvent sessionEvent )
81      {
82          out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Disconnected" );
83      }
84  
85      /**
86       * @see SessionListener#sessionConnectionRefused(SessionEvent)
87       */
88      public void sessionConnectionRefused( final SessionEvent sessionEvent )
89      {
90          out.println( sessionEvent.getWagon().getRepository().getUrl() + " - Session: Connection refused" );
91  
92      }
93  
94      /**
95       * @see SessionListener#sessionLoggedIn(SessionEvent)
96       */
97      public void sessionLoggedIn( final SessionEvent sessionEvent )
98      {
99          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 }