1 package org.apache.maven.demo.extension;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
41
42 @Component( role = EventSpy.class, hint = "demo" )
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
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
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 }