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.Collections;
26  import java.util.List;
27  
28  /**
29   * @since 3.0
30   * @author Kristian Rosenvold
31   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
32   */
33  public class BuildLogItem
34  {
35      private final ExecutionPlanItem executionPlanItem;
36  
37      private final MavenProject project;
38  
39      private final long startTime;
40  
41      private long endTime;
42  
43      private final List<DependencyLogEntry> dependencies =
44          Collections.synchronizedList( new ArrayList<DependencyLogEntry>() );
45  
46      public BuildLogItem( MavenProject project, ExecutionPlanItem executionPlanItem )
47      {
48          this.executionPlanItem = executionPlanItem;
49          this.project = project;
50          startTime = System.currentTimeMillis();
51  
52      }
53  
54  
55      public MavenProject getProject()
56      {
57          return project;
58      }
59  
60      public void setComplete()
61      {
62          endTime = System.currentTimeMillis();
63      }
64  
65      public void addWait( MavenProject upstreamProject, ExecutionPlanItem inSchedule, long startWait )
66      {
67          long now = System.currentTimeMillis();
68          dependencies.add( new DependencyLogEntry( upstreamProject, inSchedule, startWait, now, null ) );
69      }
70  
71      public void addDependency( MavenProject upstreamProject, String message )
72      {
73          dependencies.add( new DependencyLogEntry( upstreamProject, message ) );
74      }
75  
76      public String toString( long rootStart )
77      {
78          StringBuilder result = new StringBuilder();
79          result.append( String.format( "%1d  %2d ", startTime - rootStart, endTime - rootStart ) );
80          result.append( project.getName() );
81          result.append( " " );
82          result.append( getMojoExecutionDescription( executionPlanItem ) );
83          if ( dependencies.size() > 0 )
84          {
85              result.append( "\n" );
86              for ( DependencyLogEntry waitLogEntry : dependencies )
87              {
88                  result.append( "    " );
89                  result.append( waitLogEntry.toString() );
90                  result.append( "\n" );
91              }
92          }
93          return result.toString();
94      }
95  
96  
97      public Object toGraph( long rootStart )
98      {
99          StringBuilder result = new StringBuilder();
100         if ( dependencies.size() > 0 )
101         {
102             for ( DependencyLogEntry waitLogEntry : dependencies )
103             {
104                 result.append( "        " );
105                 result.append( nodeKey( project, executionPlanItem ) );
106                 result.append( " ->   " );
107                 result.append( waitLogEntry.toNodeKey() );
108                 result.append( waitLogEntry.toNodeDescription( rootStart ) );
109                 result.append( "\n" );
110             }
111         }
112         else
113         {
114             result.append( "        " );
115             result.append( nodeKey( project, executionPlanItem ) );
116             result.append( "\n" );
117         }
118         return result.toString();
119     }
120 
121     private static String nodeKey( MavenProject mavenProject, ExecutionPlanItem executionPlanItem )
122     {
123         String key = mavenProject.getArtifactId();
124         if ( executionPlanItem != null )
125         {
126             key += "_" + getMojoExecutionDescription( executionPlanItem );
127         }
128         return key.replace( ".", "_" ).replace( ":", "_" );
129     }
130 
131     private static String getMojoExecutionDescription( ExecutionPlanItem executionPlanItem )
132     {
133         if ( executionPlanItem.getMojoExecution() != null )
134         {
135             return executionPlanItem.getMojoExecution().getArtifactId() + getLifeCyclePhase( executionPlanItem );
136         }
137         else
138         {
139             return "";
140         }
141     }
142 
143     private static String getLifeCyclePhase( ExecutionPlanItem executionPlanItem )
144     {
145         return executionPlanItem.getLifecyclePhase() != null ? "[" + executionPlanItem.getLifecyclePhase() + "]" : "";
146     }
147 
148 
149     class DependencyLogEntry
150     {
151         private final ExecutionPlanItem executionPlanItem;
152 
153         private final MavenProject upstreamProject;
154 
155         private final Long start;
156 
157         private final Long stop;
158 
159         private final String message;
160 
161         DependencyLogEntry( MavenProject upstreamProject, ExecutionPlanItem executionPlanItem, Long start, Long stop,
162                             String message )
163         {
164             this.upstreamProject = upstreamProject;
165             this.executionPlanItem = executionPlanItem;
166             this.start = start;
167             this.stop = stop;
168             this.message = message;
169         }
170 
171         DependencyLogEntry( MavenProject upstreamProject, String message )
172         {
173             this( upstreamProject, null, null, null, message );
174         }
175 
176         public String toString()
177         {
178             return upstreamProject.getName() + ":" + getExecutionPlanItem() + getElapsed() + getMessage();
179         }
180 
181         public String toNodeKey()
182         {
183             return nodeKey( upstreamProject, executionPlanItem );
184         }
185 
186         public String toNodeDescription( long rootStart )
187         {
188             return "";
189         }
190 
191 
192         private String getMessage()
193         {
194             return message != null ? message : "";
195         }
196 
197         private String getExecutionPlanItem()
198         {
199             if ( executionPlanItem != null )
200             {
201                 return getMojoExecutionDescription( executionPlanItem );
202             }
203             else
204             {
205                 return "";
206             }
207         }
208 
209         private String getElapsed()
210         {
211             if ( start != null && stop != null )
212             {
213                 long elapsed = stop - start;
214                 return elapsed > 0 ? ", wait=" + elapsed : "";
215             }
216             return "";
217         }
218     }
219 
220 }