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