View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.booter;
20  
21  import java.io.File;
22  
23  import org.apache.maven.surefire.api.util.internal.DumpFileUtils;
24  
25  import static org.apache.maven.surefire.api.util.internal.DumpFileUtils.newDumpFile;
26  
27  /**
28   * Dumps lost commands and caused exceptions in forked JVM. <br>
29   * Fail-safe.
30   *
31   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
32   * @since 2.20
33   */
34  public final class DumpErrorSingleton {
35      public static final String DUMP_FILE_EXT = ".dump";
36      public static final String DUMPSTREAM_FILE_EXT = ".dumpstream";
37      private static final DumpErrorSingleton SINGLETON = new DumpErrorSingleton();
38  
39      private File dumpFile;
40      private File dumpStreamFile;
41      private File binaryDumpStreamFile;
42  
43      private DumpErrorSingleton() {}
44  
45      public static DumpErrorSingleton getSingleton() {
46          return SINGLETON;
47      }
48  
49      public synchronized void init(File reportsDir, String dumpFileName) {
50          dumpFile = createDumpFile(reportsDir, dumpFileName);
51          dumpStreamFile = createDumpStreamFile(reportsDir, dumpFileName);
52          String fileNameWithoutExtension =
53                  dumpFileName.contains(".") ? dumpFileName.substring(0, dumpFileName.lastIndexOf('.')) : dumpFileName;
54          binaryDumpStreamFile = newDumpFile(reportsDir, fileNameWithoutExtension + "-commands.bin");
55      }
56  
57      public synchronized File dumpException(Throwable t, String msg) {
58          DumpFileUtils.dumpException(t, msg == null ? "null" : msg, dumpFile);
59          return dumpFile;
60      }
61  
62      public synchronized File dumpException(Throwable t) {
63          DumpFileUtils.dumpException(t, dumpFile);
64          return dumpFile;
65      }
66  
67      public synchronized File dumpText(String msg) {
68          DumpFileUtils.dumpText(msg == null ? "null" : msg, dumpFile);
69          return dumpFile;
70      }
71  
72      public synchronized File dumpStreamException(Throwable t, String msg) {
73          DumpFileUtils.dumpException(t, msg == null ? "null" : msg, dumpStreamFile);
74          return dumpStreamFile;
75      }
76  
77      public synchronized File dumpStreamException(Throwable t) {
78          DumpFileUtils.dumpException(t, dumpStreamFile);
79          return dumpStreamFile;
80      }
81  
82      public synchronized File dumpStreamText(String msg) {
83          DumpFileUtils.dumpText(msg == null ? "null" : msg, dumpStreamFile);
84          return dumpStreamFile;
85      }
86  
87      public File getCommandStreamBinaryFile() {
88          return binaryDumpStreamFile;
89      }
90  
91      private File createDumpFile(File reportsDir, String dumpFileName) {
92          return newDumpFile(reportsDir, dumpFileName + DUMP_FILE_EXT);
93      }
94  
95      private File createDumpStreamFile(File reportsDir, String dumpFileName) {
96          return newDumpFile(reportsDir, dumpFileName + DUMPSTREAM_FILE_EXT);
97      }
98  }