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.surefire.util.internal.DumpFileUtils;
23  
24  import java.io.File;
25  
26  import static java.lang.String.format;
27  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMP_FILENAME;
28  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMP_FILENAME_FORMATTER;
29  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMPSTREAM_FILENAME;
30  import static org.apache.maven.plugin.surefire.SurefireHelper.DUMPSTREAM_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  {
38      private static final InPluginProcessDumpSingleton SINGLETON = new InPluginProcessDumpSingleton();
39  
40      private InPluginProcessDumpSingleton()
41      {
42      }
43  
44      public static InPluginProcessDumpSingleton getSingleton()
45      {
46          return SINGLETON;
47      }
48  
49      public synchronized File dumpStreamException( Throwable t, String msg, File reportsDirectory, int jvmRun )
50      {
51          File dump = newDumpStreamFile( reportsDirectory, jvmRun );
52          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, dump );
53          return dump;
54      }
55  
56      public synchronized void dumpStreamException( Throwable t, String msg, File reportsDirectory )
57      {
58          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, newDumpStreamFile( reportsDirectory ) );
59      }
60  
61      public synchronized File dumpStreamText( String msg, File reportsDirectory, int jvmRun )
62      {
63          File dump = newDumpStreamFile( reportsDirectory, jvmRun );
64          DumpFileUtils.dumpText( msg == null ? "null" : msg, dump );
65          return dump;
66      }
67  
68      public synchronized void dumpStreamText( String msg, File reportsDirectory )
69      {
70          DumpFileUtils.dumpText( msg == null ? "null" : msg, newDumpStreamFile( reportsDirectory ) );
71      }
72  
73      public synchronized void dumpException( Throwable t, String msg, File reportsDirectory, int jvmRun )
74      {
75          File dump = newDumpFile( reportsDirectory, jvmRun );
76          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, dump );
77      }
78  
79      public synchronized void dumpException( Throwable t, String msg, File reportsDirectory )
80      {
81          File dump = newDumpFile( reportsDirectory );
82          DumpFileUtils.dumpException( t, msg == null ? "null" : msg, dump );
83      }
84  
85      private File newDumpStreamFile( File reportsDirectory )
86      {
87          return new File( reportsDirectory, DUMPSTREAM_FILENAME );
88      }
89  
90      private static File newDumpStreamFile( File reportsDirectory, int jvmRun )
91      {
92          return new File( reportsDirectory, format( DUMPSTREAM_FILENAME_FORMATTER, jvmRun ) );
93      }
94  
95      private static File newDumpFile( File reportsDirectory, int jvmRun )
96      {
97          return new File( reportsDirectory, format( DUMP_FILENAME_FORMATTER, jvmRun ) );
98      }
99  
100     private static File newDumpFile( File reportsDirectory )
101     {
102         return new File( reportsDirectory, DUMP_FILENAME );
103     }
104 }