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