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.plugin.surefire.booterclient.output;
20  
21  import java.io.File;
22  
23  import org.apache.maven.surefire.api.util.internal.DumpFileUtils;
24  
25  import static java.lang.String.format;
26  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMPSTREAM_FILENAME;
27  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMPSTREAM_FILENAME_FORMATTER;
28  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMP_FILENAME;
29  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMP_FILENAME_FORMATTER;
30  import static org.apache.maven.plugin.surefire.SurefireHelper.EVENTS_BINARY_DUMP_FILENAME_FORMATTER;
31  
32  /**
33   * Reports errors to dump file.
34   * Used only within java process of the plugin itself and not the forked JVM.
35   */
36  public final class InPluginProcessDumpSingleton {
37      private static final InPluginProcessDumpSingleton SINGLETON = new InPluginProcessDumpSingleton();
38  
39      private InPluginProcessDumpSingleton() {}
40  
41      public static InPluginProcessDumpSingleton getSingleton() {
42          return SINGLETON;
43      }
44  
45      public synchronized File dumpStreamException(Throwable t, String msg, File reportsDirectory, int jvmRun) {
46          File dump = newDumpStreamFile(reportsDirectory, jvmRun);
47          DumpFileUtils.dumpException(t, msg == null ? "null" : msg, dump);
48          return dump;
49      }
50  
51      public synchronized void dumpStreamException(Throwable t, String msg, File reportsDirectory) {
52          DumpFileUtils.dumpException(t, msg == null ? "null" : msg, newDumpStreamFile(reportsDirectory));
53      }
54  
55      public synchronized File dumpStreamText(String msg, File reportsDirectory, int jvmRun) {
56          File dump = newDumpStreamFile(reportsDirectory, jvmRun);
57          DumpFileUtils.dumpText(msg == null ? "null" : msg, dump);
58          return dump;
59      }
60  
61      public synchronized void dumpStreamText(String msg, File reportsDirectory) {
62          DumpFileUtils.dumpText(msg == null ? "null" : msg, newDumpStreamFile(reportsDirectory));
63      }
64  
65      public synchronized void dumpException(Throwable t, String msg, File reportsDirectory, int jvmRun) {
66          File dump = newDumpFile(reportsDirectory, jvmRun);
67          DumpFileUtils.dumpException(t, msg == null ? "null" : msg, dump);
68      }
69  
70      public synchronized void dumpException(Throwable t, String msg, File reportsDirectory) {
71          File dump = newDumpFile(reportsDirectory);
72          DumpFileUtils.dumpException(t, msg == null ? "null" : msg, dump);
73      }
74  
75      public File getEventStreamBinaryFile(File reportsDirectory, int jvmRun) {
76          reportsDirectory.mkdirs();
77          return new File(reportsDirectory, format(EVENTS_BINARY_DUMP_FILENAME_FORMATTER, jvmRun));
78      }
79  
80      private File newDumpStreamFile(File reportsDirectory) {
81          return new File(reportsDirectory, DUMPSTREAM_FILENAME);
82      }
83  
84      private static File newDumpStreamFile(File reportsDirectory, int jvmRun) {
85          return new File(reportsDirectory, format(DUMPSTREAM_FILENAME_FORMATTER, jvmRun));
86      }
87  
88      private static File newDumpFile(File reportsDirectory, int jvmRun) {
89          return new File(reportsDirectory, format(DUMP_FILENAME_FORMATTER, jvmRun));
90      }
91  
92      private static File newDumpFile(File reportsDirectory) {
93          return new File(reportsDirectory, DUMP_FILENAME);
94      }
95  }