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( "Java version: " + System.getProperty( "java.version", "<unknown java version>" ) );
77  
78          stdout.println( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) );
79  
80          stdout.println( "Default locale: " + Locale.getDefault() + ", platform encoding: "
81              + System.getProperty( "file.encoding", "<unknown encoding>" ) );
82  
83          stdout.println( "OS name: \"" + Os.OS_NAME + "\" version: \"" + Os.OS_VERSION + "\" arch: \"" + Os.OS_ARCH
84              + "\" Family: \"" + Os.OS_FAMILY + "\"" );
85      }
86  
87      private static String reduce( String s )
88      {
89          return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
90      }
91  
92  
93      private static void stats( Date start, Logger logger )
94      {
95          Date finish = new Date();
96  
97          long time = finish.getTime() - start.getTime();
98  
99          logger.info( "Total time: " + formatTime( time ) );
100 
101         logger.info( "Finished at: " + finish );
102 
103         //noinspection CallToSystemGC
104         System.gc();
105 
106         Runtime r = Runtime.getRuntime();
107 
108         logger.info( "Final Memory: " + ( r.totalMemory() - r.freeMemory() ) / MB + "M/" + r.totalMemory() / MB + "M" );
109     }
110 
111     private static String formatTime( long ms )
112     {
113         long secs = ms / MS_PER_SEC;
114 
115         long min = secs / SEC_PER_MIN;
116 
117         secs = secs % SEC_PER_MIN;
118 
119         String msg = "";
120 
121         if ( min > 1 )
122         {
123             msg = min + " minutes ";
124         }
125         else if ( min == 1 )
126         {
127             msg = "1 minute ";
128         }
129 
130         if ( secs > 1 )
131         {
132             msg += secs + " seconds";
133         }
134         else if ( secs == 1 )
135         {
136             msg += "1 second";
137         }
138         else if ( min == 0 )
139         {
140             msg += "< 1 second";
141         }
142         return msg;
143     }
144 
145     private static String getFormattedTime( long time )
146     {
147         String pattern = "s.SSS's'";
148         if ( time / 60000L > 0 )
149         {
150             pattern = "m:s" + pattern;
151             if ( time / 3600000L > 0 )
152             {
153                 pattern = "H:m" + pattern;
154             }
155         }
156         DateFormat fmt = new SimpleDateFormat( pattern );
157         fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
158         return fmt.format( new Date( time ) );
159     }
160 
161     static Properties getBuildProperties()
162     {
163         Properties properties = new Properties();
164         InputStream resourceAsStream = null;
165         try
166         {
167             resourceAsStream = MavenCli.class.getResourceAsStream( "/org/apache/maven/messages/build.properties" );
168 
169             if ( resourceAsStream != null )
170             {
171                 properties.load( resourceAsStream );
172             }
173         }
174         catch ( IOException e )
175         {
176             System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
177         }
178         finally
179         {
180             IOUtil.close( resourceAsStream );
181         }
182 
183         return properties;
184     }
185 
186     public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
187     {
188         if ( logger == null )
189         {
190             logger = new PrintStreamLogger( System.out );
191         }
192 
193         if ( showStackTrace )
194         {
195             logger.error( message, e );
196         }
197         else
198         {
199             logger.error( message );
200 
201             if ( e != null )
202             {
203                 logger.error( e.getMessage() );
204 
205                 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
206                 {
207                     logger.error( "Caused by: " + cause.getMessage() );
208                 }
209             }
210         }
211     }
212 
213 }