1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37
38
39
40
41
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 }