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 java.io.IOException;
23  import java.io.InputStream;
24  import java.io.PrintStream;
25  import java.text.DateFormat;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Locale;
29  import java.util.Properties;
30  import java.util.TimeZone;
31  
32  import org.codehaus.plexus.logging.Logger;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.Os;
35  
36  /**
37   * Utility class used to report errors, statistics, application version info, etc.
38   *
39   * @author jdcasey
40   *
41   */
42  public final class CLIReportingUtils
43  {
44  
45      public static final long MB = 1024 * 1024;
46  
47      public static final int MS_PER_SEC = 1000;
48  
49      public static final int SEC_PER_MIN = 60;
50  
51      public static void showVersion( PrintStream stdout )
52      {
53          Properties properties = getBuildProperties();
54  
55          String timestamp = reduce( properties.getProperty( "timestamp" ) );
56          String version = reduce( properties.getProperty( "version" ) );
57          String rev = reduce( properties.getProperty( "buildNumber" ) );
58  
59          String msg = "Apache Maven ";
60          msg += ( version != null ? version : "<version unknown>" );
61          if ( rev != null || timestamp != null )
62          {
63              msg += " (";
64              msg += ( rev != null ? "r" + rev : "" );
65              if ( timestamp != null )
66              {
67                  SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" );
68                  String ts = fmt.format( new Date( Long.valueOf( timestamp ).longValue() ) );
69                  msg += ( rev != null ? "; " : "" ) + ts;
70              }
71              msg += ")";
72          }
73  
74          stdout.println( msg );
75  
76          stdout.println( "Maven home: " + System.getProperty( "maven.home", "<unknown maven home>" ) );
77  
78          stdout.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" )
79              + ", vendor: " + System.getProperty( "java.vendor", "<unknown vendor>" ) );
80  
81          stdout.println( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) );
82  
83          stdout.println( "Default locale: " + Locale.getDefault() + ", platform encoding: "
84              + System.getProperty( "file.encoding", "<unknown encoding>" ) );
85  
86          stdout.println( "OS name: \"" + Os.OS_NAME + "\", version: \"" + Os.OS_VERSION + "\", arch: \"" + Os.OS_ARCH
87              + "\", family: \"" + Os.OS_FAMILY + "\"" );
88      }
89  
90      private static String reduce( String s )
91      {
92          return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
93      }
94  
95  
96      private static void stats( Date start, Logger logger )
97      {
98          Date finish = new Date();
99  
100         long time = finish.getTime() - start.getTime();
101 
102         logger.info( "Total time: " + formatTime( time ) );
103 
104         logger.info( "Finished at: " + finish );
105 
106         //noinspection CallToSystemGC
107         System.gc();
108 
109         Runtime r = Runtime.getRuntime();
110 
111         logger.info( "Final Memory: " + ( r.totalMemory() - r.freeMemory() ) / MB + "M/" + r.totalMemory() / MB + "M" );
112     }
113 
114     private static String formatTime( long ms )
115     {
116         long secs = ms / MS_PER_SEC;
117 
118         long min = secs / SEC_PER_MIN;
119 
120         secs = secs % SEC_PER_MIN;
121 
122         String msg = "";
123 
124         if ( min > 1 )
125         {
126             msg = min + " minutes ";
127         }
128         else if ( min == 1 )
129         {
130             msg = "1 minute ";
131         }
132 
133         if ( secs > 1 )
134         {
135             msg += secs + " seconds";
136         }
137         else if ( secs == 1 )
138         {
139             msg += "1 second";
140         }
141         else if ( min == 0 )
142         {
143             msg += "< 1 second";
144         }
145         return msg;
146     }
147 
148     private static String getFormattedTime( long time )
149     {
150         String pattern = "s.SSS's'";
151         if ( time / 60000L > 0 )
152         {
153             pattern = "m:s" + pattern;
154             if ( time / 3600000L > 0 )
155             {
156                 pattern = "H:m" + pattern;
157             }
158         }
159         DateFormat fmt = new SimpleDateFormat( pattern );
160         fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
161         return fmt.format( new Date( time ) );
162     }
163 
164     static Properties getBuildProperties()
165     {
166         Properties properties = new Properties();
167         InputStream resourceAsStream = null;
168         try
169         {
170             resourceAsStream = MavenCli.class.getResourceAsStream( "/org/apache/maven/messages/build.properties" );
171 
172             if ( resourceAsStream != null )
173             {
174                 properties.load( resourceAsStream );
175             }
176         }
177         catch ( IOException e )
178         {
179             System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
180         }
181         finally
182         {
183             IOUtil.close( resourceAsStream );
184         }
185 
186         return properties;
187     }
188 
189     public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
190     {
191         if ( logger == null )
192         {
193             logger = new PrintStreamLogger( System.out );
194         }
195 
196         if ( showStackTrace )
197         {
198             logger.error( message, e );
199         }
200         else
201         {
202             logger.error( message );
203 
204             if ( e != null )
205             {
206                 logger.error( e.getMessage() );
207 
208                 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
209                 {
210                     logger.error( "Caused by: " + cause.getMessage() );
211                 }
212             }
213         }
214     }
215 
216 }