View Javadoc
1   package org.apache.maven.surefire.util.internal;
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.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.PrintWriter;
27  import java.io.Writer;
28  import java.text.SimpleDateFormat;
29  import java.util.Date;
30  
31  import static java.nio.charset.StandardCharsets.UTF_8;
32  
33  /**
34   * Dumps a text or exception in dump file.
35   * Each call logs a date when it was written to the dump file.
36   *
37   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38   * @since 2.20
39   */
40  public final class DumpFileUtils
41  {
42      private DumpFileUtils()
43      {
44          throw new IllegalStateException( "no instantiable constructor" );
45      }
46  
47      /**
48       * New dump file. Synchronized object appears in main memory and perfectly visible in other threads.
49       *
50       * @param reportsDir    only report directory
51       * @param dumpFileName    dump file name
52       */
53      public static synchronized File newDumpFile( File reportsDir, String dumpFileName )
54      {
55          return new File( reportsDir, dumpFileName );
56      }
57  
58      public static void dumpException( Throwable t, File dumpFile )
59      {
60          dumpException( t, null, dumpFile );
61      }
62  
63      public static void dumpException( Throwable t, String msg, File dumpFile )
64      {
65          try
66          {
67              if ( t != null && dumpFile != null
68                           && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
69              {
70                  try ( PrintWriter pw = new PrintWriter( createWriter( dumpFile ) ) )
71                  {
72                      if ( msg != null )
73                      {
74                          pw.append( msg )
75                                  .append( StringUtils.NL );
76                      }
77                      t.printStackTrace( pw );
78                      pw.append( StringUtils.NL )
79                        .append( StringUtils.NL );
80                  }
81              }
82          }
83          catch ( Exception e )
84          {
85              // do nothing
86          }
87      }
88  
89      public static void dumpText( String msg, File dumpFile )
90      {
91          try
92          {
93              if ( msg != null && dumpFile != null
94                           && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
95              {
96                  try ( Writer writer = createWriter( dumpFile ) )
97                  {
98                      writer.append( msg )
99                              .append( StringUtils.NL )
100                             .append( StringUtils.NL );
101                 }
102             }
103         }
104         catch ( Exception e )
105         {
106             // do nothing
107         }
108     }
109 
110     public static String newFormattedDateFileName()
111     {
112         return new SimpleDateFormat( "yyyy-MM-dd'T'HH-mm-ss_SSS" ).format( new Date() );
113     }
114 
115     private static Writer createWriter( File dumpFile ) throws IOException
116     {
117         return new OutputStreamWriter( new FileOutputStream( dumpFile, true ), UTF_8 )
118                        .append( "# Created at " )
119                        .append( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS" ).format( new Date() ) )
120                        .append( StringUtils.NL );
121     }
122 
123     private static boolean mkdirs( File dumpFile )
124     {
125         File dir = dumpFile.getParentFile();
126         return dir.exists() || dir.mkdirs();
127     }
128 }