View Javadoc

1   package org.apache.maven.lifecycle.internal;
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.project.MavenProject;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  /**
33   * Handles all concurrency-related logging.
34   * <p/>
35   * The logging/diagnostic needs of a concurrent build are different from a linear build. This
36   * delta required to analyze a concurrent build is located here.
37   * <p/>
38   * NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
39   * 
40   * @since 3.0
41   * @author Kristian Rosenvold
42   */
43  public class ConcurrentBuildLogger
44  {
45      private final long startTime;
46  
47      private final Map<MavenProject, Thread> threadMap = new ConcurrentHashMap<MavenProject, Thread>();
48  
49      public ConcurrentBuildLogger()
50      {
51          startTime = System.currentTimeMillis();
52      }
53  
54  
55      List<BuildLogItem> items = Collections.synchronizedList( new ArrayList<BuildLogItem>() );
56  
57      public BuildLogItem createBuildLogItem( MavenProject project, ExecutionPlanItem current )
58      {
59          threadMap.put( project, Thread.currentThread() );
60          BuildLogItem result = new BuildLogItem( project, current );
61          items.add( result );
62          return result;
63      }
64  
65      public String toString()
66      {
67          StringBuilder result = new StringBuilder();
68          for ( Map.Entry<MavenProject, Thread> mavenProjectThreadEntry : threadMap.entrySet() )
69          {
70              result.append( mavenProjectThreadEntry.getKey().getName() );
71              result.append( " ran on " );
72              result.append( mavenProjectThreadEntry.getValue().getName() );
73              result.append( "\n" );
74          }
75  
76          for ( BuildLogItem builtLogItem : items )
77          {
78              result.append( builtLogItem.toString( startTime ) );
79              result.append( "\n" );
80          }
81          return result.toString();
82      }
83  
84      public String toGraph()
85      {
86          StringBuilder result = new StringBuilder();
87  
88          Map<MavenProject, Collection<BuildLogItem>> multiMap = new HashMap<MavenProject, Collection<BuildLogItem>>();
89          for ( BuildLogItem builtLogItem : items )
90          {
91              MavenProject project = builtLogItem.getProject();
92              Collection<BuildLogItem> bag = multiMap.get( project );
93              if ( bag == null )
94              {
95                  bag = new ArrayList<BuildLogItem>();
96                  multiMap.put( project, bag );
97              }
98              bag.add( builtLogItem );
99          }
100 
101         result.append( "digraph build" );
102         result.append( " {\n " );
103 
104         for ( MavenProject mavenProject : multiMap.keySet() )
105         {
106             final Collection<BuildLogItem> builtLogItems = multiMap.get( mavenProject );
107             result.append( "   subgraph " );
108             result.append( mavenProject.getArtifactId() );
109             result.append( "   {\n" );
110 
111             for ( BuildLogItem builtLogItem : builtLogItems )
112             {
113                 result.append( builtLogItem.toGraph( startTime ) );
114             }
115 
116             result.append( "\n   }\n" );
117         }
118 
119         result.append( "\n}\n " );
120         return result.toString();
121     }
122 
123 }