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