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