View Javadoc
1   package org.apache.maven.surefire.api.booter;
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.api.util.internal.DumpFileUtils;
23  
24  import java.io.File;
25  
26  import static org.apache.maven.surefire.api.util.internal.DumpFileUtils.newDumpFile;
27  
28  /**
29   * Dumps lost commands and caused exceptions in forked JVM. <br>
30   * Fail-safe.
31   *
32   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
33   * @since 2.20
34   */
35  public final class DumpErrorSingleton
36  {
37      public static final String DUMP_FILE_EXT = ".dump";
38      public static final String DUMPSTREAM_FILE_EXT = ".dumpstream";
39      private static final DumpErrorSingleton SINGLETON = new DumpErrorSingleton();
40  
41      private File dumpFile;
42      private File dumpStreamFile;
43      private File binaryDumpStreamFile;
44  
45      private DumpErrorSingleton()
46      {
47      }
48  
49      public static DumpErrorSingleton getSingleton()
50      {
51          return SINGLETON;
52      }
53  
54      public synchronized void init( File reportsDir, String dumpFileName )
55      {
56          dumpFile = createDumpFile( reportsDir, dumpFileName );
57          dumpStreamFile = createDumpStreamFile( reportsDir, dumpFileName );
58          String fileNameWithoutExtension =
59              dumpFileName.contains( "." ) ? dumpFileName.substring( 0, dumpFileName.lastIndexOf( '.' ) ) : dumpFileName;
60          binaryDumpStreamFile = newDumpFile( reportsDir, fileNameWithoutExtension + "-commands.bin" );
61      }
62  
63      public synchronized File dumpException( Throwable t, String msg )
64      {
65          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, dumpFile );
66          return dumpFile;
67      }
68  
69      public synchronized File dumpException( Throwable t )
70      {
71          DumpFileUtils.dumpException( t, dumpFile );
72          return dumpFile;
73      }
74  
75      public synchronized File dumpText( String msg )
76      {
77          DumpFileUtils.dumpText( msg == null ? "null" : msg, dumpFile );
78          return dumpFile;
79      }
80  
81      public synchronized File dumpStreamException( Throwable t, String msg )
82      {
83          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, dumpStreamFile );
84          return dumpStreamFile;
85      }
86  
87      public synchronized File dumpStreamException( Throwable t )
88      {
89          DumpFileUtils.dumpException( t, dumpStreamFile );
90          return dumpStreamFile;
91      }
92  
93      public synchronized File dumpStreamText( String msg )
94      {
95          DumpFileUtils.dumpText( msg == null ? "null" : msg, dumpStreamFile );
96          return dumpStreamFile;
97      }
98  
99      public File getCommandStreamBinaryFile()
100     {
101         return binaryDumpStreamFile;
102     }
103 
104     private File createDumpFile( File reportsDir, String dumpFileName )
105     {
106         return newDumpFile( reportsDir, dumpFileName + DUMP_FILE_EXT );
107     }
108 
109     private File createDumpStreamFile( File reportsDir, String dumpFileName )
110     {
111         return newDumpFile( reportsDir, dumpFileName + DUMPSTREAM_FILE_EXT );
112     }
113 }