View Javadoc
1   package org.apache.maven.demo.extension;
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 java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.maven.eventspy.AbstractEventSpy;
26  import org.apache.maven.eventspy.EventSpy;
27  import org.apache.maven.execution.ExecutionEvent;
28  import org.apache.maven.plugin.MojoExecution;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.DefaultPlexusContainer;
31  import org.codehaus.plexus.classworlds.ClassWorld;
32  import org.codehaus.plexus.classworlds.realm.ClassRealm;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.eclipse.aether.RepositoryEvent;
35  
36  /**
37   * Event Spy demo (since Maven 3.0.2, with <a href="https://issues.apache.org/jira/browse/MNG-4936">MNG-4936</a>).
38   * <p>Notice: event spies are not activated from POM build extensions.
39   */
40  //@Named( "demo" )
41  //@Singleton
42  @Component( role = EventSpy.class, hint = "demo" ) // using Plexus Component annotations for Maven 3.0.x
43  public class EventSpyDemo
44      extends AbstractEventSpy
45  {
46      private Map<String, Integer> events = new HashMap<String, Integer>();
47  
48      private DefaultPlexusContainer container;
49  
50      public void init( Context context )
51          throws Exception
52      {
53          System.err.println( "EventSpyDemo init:" );
54          for ( Map.Entry<String, Object> entry : context.getData().entrySet() )
55          {
56              System.err.println( "             init context: - " + entry.getKey() + " = " + entry.getValue() );
57          }
58          container = (DefaultPlexusContainer) context.getData().get( "plexus" );
59          dump( container.getClassWorld() );
60      }
61  
62      public void onEvent( Object event )
63          throws Exception
64      {
65          if ( event instanceof ExecutionEvent )
66          {
67              ExecutionEvent exec = (ExecutionEvent) event;
68              MavenProject project = exec.getProject();
69              MojoExecution mojoExec = exec.getMojoExecution();
70              Exception ex = exec.getException();
71              System.err.println( "EventSpyDemo onEvent: ExecutionEvent " + exec.getType()
72                  + ( project == null ? "" : ( " " + project.getId() ) )
73                  + ( mojoExec == null ? "" : ( " " + mojoExec ) )
74                  + ( ex == null ? "" : ( " " + ex ) ) );
75          }
76          else if ( event instanceof RepositoryEvent )
77          {
78              RepositoryEvent repo = (RepositoryEvent) event;
79              if ( repo.getType() == RepositoryEvent.EventType.ARTIFACT_RESOLVING
80                  || repo.getType() == RepositoryEvent.EventType.ARTIFACT_RESOLVED )
81              {
82                // don't display these evens: too many artifacts resolved along execution
83              }
84              else
85              {
86                System.err.println( "EventSpyDemo onEvent: RepositoryEvent " + ( (RepositoryEvent) event ).getType() );
87              }
88          }
89          else
90          {
91              System.err.println( "EventSpyDemo onEvent: " + event.getClass().getName() );
92          }
93          Integer count = events.get( event.getClass().getName() );
94          if ( count == null )
95          {
96              count = 1;
97          }
98          else
99          {
100             count++;
101         }
102         events.put( event.getClass().getName(), count );
103     }
104 
105     public void close()
106         throws Exception
107     {
108         System.err.println( "EventSpyDemo close:" );
109         for ( Map.Entry<String, Integer> entry : events.entrySet() )
110         {
111             System.err.println( "             - " + entry.getValue() + " " + entry.getKey() );
112         }
113         //dump( container.getClassWorld() );
114     }
115 
116     private void dump( ClassWorld cw )
117     {
118         for ( ClassRealm cr : cw.getRealms() )
119         {
120             System.err.println( "EventSpyDemo - ClassRealm " + cr.getId() );
121             cr.display( System.err );
122         }
123     }
124 }