View Javadoc

1   package org.apache.maven.cli;
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.WagonConstants;
23  import org.apache.maven.wagon.events.TransferEvent;
24  import org.apache.maven.wagon.events.TransferListener;
25  import org.codehaus.plexus.logging.AbstractLogEnabled;
26  import org.codehaus.plexus.logging.Logger;
27  
28  import java.io.PrintStream;
29  
30  /**
31   * Abstract console download progress meter.
32   * 
33   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
34   * @version $Id: AbstractConsoleDownloadMonitor.java 747773 2009-02-25 13:35:29Z brett $
35   * @since 2.0.5
36   */
37  public abstract class AbstractConsoleDownloadMonitor
38      extends AbstractLogEnabled
39      implements TransferListener
40  {
41      private Logger logger;
42  
43      PrintStream out = System.out;
44  
45      public AbstractConsoleDownloadMonitor()
46      {
47      }
48  
49      public AbstractConsoleDownloadMonitor( Logger logger )
50      {
51          this.logger = logger;
52      }
53  
54      public void transferInitiated( TransferEvent transferEvent )
55      {
56          String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
57  
58          String url = transferEvent.getWagon().getRepository().getUrl();
59  
60          // TODO: can't use getLogger() because this isn't currently instantiated as a component
61          out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
62      }
63  
64      /**
65       * Do nothing
66       */
67      public void transferStarted( TransferEvent transferEvent )
68      {
69          // This space left intentionally blank
70      }
71  
72      /**
73       * Do nothing
74       */
75      public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
76      {
77          // This space left intentionally blank
78      }
79  
80      public void transferCompleted( TransferEvent transferEvent )
81      {
82          String line = createCompletionLine( transferEvent );
83          out.println( line );
84      }
85  
86      protected String createCompletionLine( TransferEvent transferEvent )
87      {
88          String line;
89          long contentLength = transferEvent.getResource().getContentLength();
90          if ( contentLength != WagonConstants.UNKNOWN_LENGTH )
91          {
92              StringBuffer buf = new StringBuffer();
93              String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
94              buf.append( contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b" );
95              String name = transferEvent.getResource().getName();
96              name = name.substring( name.lastIndexOf( '/' ) + 1, name.length() );
97              buf.append( " " );
98              buf.append( type );
99              buf.append( "  (" );
100             buf.append( name );
101             buf.append( ")" );
102             line = buf.toString();
103         }
104         else
105         {
106             line = "";
107         }
108         return line;
109     }
110 
111     public void transferError( TransferEvent transferEvent )
112     {
113         // these errors should already be handled elsewhere by Maven since they all result in an exception from Wagon
114         if ( logger != null )
115         {
116             Exception exception = transferEvent.getException();
117             logger.debug( exception.getMessage(), exception );
118         }
119     }
120 
121     /**
122      * Do nothing
123      */
124     public void debug( String message )
125     {
126         if ( logger != null )
127         {
128             logger.debug( message );
129         }
130     }
131 
132 }