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 org.apache.maven.surefire.report.ReporterConfiguration;
23  
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStreamWriter;
28  import java.io.PrintWriter;
29  import java.io.Writer;
30  import java.text.SimpleDateFormat;
31  import java.util.Date;
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      public static synchronized File newDumpFile( String dumpFileName, ReporterConfiguration configuration )
51      {
52          return new File( configuration.getReportsDirectory(), dumpFileName );
53      }
54  
55      public static void dumpException( Throwable t, File dumpFile )
56      {
57          dumpException( t, null, dumpFile );
58      }
59  
60      public static void dumpException( Throwable t, String msg, File dumpFile )
61      {
62          try
63          {
64              if ( t != null && dumpFile != null
65                           && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
66              {
67                  Writer fw = createWriter( dumpFile );
68                  if ( msg != null )
69                  {
70                      fw.append( msg )
71                              .append( StringUtils.NL );
72                  }
73                  PrintWriter pw = new PrintWriter( fw );
74                  t.printStackTrace( pw );
75                  pw.flush();
76                  fw.append( StringUtils.NL )
77                    .append( StringUtils.NL )
78                    .close();
79              }
80          }
81          catch ( Exception e )
82          {
83              // do nothing
84          }
85      }
86  
87      public static void dumpText( String msg, File dumpFile )
88      {
89          try
90          {
91              if ( msg != null && dumpFile != null
92                           && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
93              {
94                  createWriter( dumpFile )
95                          .append( msg )
96                          .append( StringUtils.NL )
97                          .append( StringUtils.NL )
98                          .close();
99              }
100         }
101         catch ( Exception e )
102         {
103             // do nothing
104         }
105     }
106 
107     public static String newFormattedDateFileName()
108     {
109         return new SimpleDateFormat( "yyyy-MM-dd'T'HH-mm-ss_SSS" ).format( new Date() );
110     }
111 
112     private static Writer createWriter( File dumpFile ) throws IOException
113     {
114         return new OutputStreamWriter( new FileOutputStream( dumpFile, true ), "UTF-8" )
115                        .append( "# Created on " )
116                        .append( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS" ).format( new Date() ) )
117                        .append( StringUtils.NL );
118     }
119 
120     private static boolean mkdirs( File dumpFile )
121     {
122         File dir = dumpFile.getParentFile();
123         return dir.exists() || dir.mkdirs();
124     }
125 }