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.util.internal.DumpFileUtils;
23  
24  import java.io.File;
25  
26  import static org.apache.maven.surefire.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  
44      private DumpErrorSingleton()
45      {
46      }
47  
48      public static DumpErrorSingleton getSingleton()
49      {
50          return SINGLETON;
51      }
52  
53      public synchronized void init( File reportsDir, String dumpFileName )
54      {
55          dumpFile = createDumpFile( reportsDir, dumpFileName );
56          dumpStreamFile = createDumpStreamFile( reportsDir, dumpFileName );
57      }
58  
59      public synchronized void dumpException( Throwable t, String msg )
60      {
61          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, dumpFile );
62      }
63  
64      public synchronized void dumpException( Throwable t )
65      {
66          DumpFileUtils.dumpException( t, dumpFile );
67      }
68  
69      public synchronized void dumpText( String msg )
70      {
71          DumpFileUtils.dumpText( msg == null ? "null" : msg, dumpFile );
72      }
73  
74      public synchronized void dumpStreamException( Throwable t, String msg )
75      {
76          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, dumpStreamFile );
77      }
78  
79      public synchronized void dumpStreamException( Throwable t )
80      {
81          DumpFileUtils.dumpException( t, dumpStreamFile );
82      }
83  
84      public synchronized void dumpStreamText( String msg )
85      {
86          DumpFileUtils.dumpText( msg == null ? "null" : msg, dumpStreamFile );
87      }
88  
89      private File createDumpFile( File reportsDir, String dumpFileName )
90      {
91          return newDumpFile( reportsDir, dumpFileName + DUMP_FILE_EXT );
92      }
93  
94      private File createDumpStreamFile( File reportsDir, String dumpFileName )
95      {
96          return newDumpFile( reportsDir, dumpFileName + DUMPSTREAM_FILE_EXT );
97      }
98  }