KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > MavenPluginCollector


1 package org.apache.maven.plugin;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.model.Plugin;
20 import org.apache.maven.plugin.descriptor.PluginDescriptor;
21 import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent;
22 import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener;
23 import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
24 import org.codehaus.plexus.logging.AbstractLogEnabled;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 public class MavenPluginCollector
33     extends AbstractLogEnabled
34     implements ComponentDiscoveryListener
35 {
36
37     private Set JavaDoc pluginsInProcess = new HashSet JavaDoc();
38
39     private Map JavaDoc pluginDescriptors = new HashMap JavaDoc();
40
41     private Map JavaDoc pluginIdsByPrefix = new HashMap JavaDoc();
42
43     // ----------------------------------------------------------------------
44
// Mojo discovery
45
// ----------------------------------------------------------------------
46
public void componentDiscovered( ComponentDiscoveryEvent event )
47     {
48         ComponentSetDescriptor componentSetDescriptor = event.getComponentSetDescriptor();
49
50         if ( componentSetDescriptor instanceof PluginDescriptor )
51         {
52             PluginDescriptor pluginDescriptor = (PluginDescriptor) componentSetDescriptor;
53             
54             // TODO: see comment in getPluginDescriptor
55
String JavaDoc key = Plugin.constructKey( pluginDescriptor.getGroupId(), pluginDescriptor.getArtifactId() );
56             
57             if ( !pluginsInProcess.contains( key ) )
58             {
59                 pluginsInProcess.add( key );
60
61                 pluginDescriptors.put( key, pluginDescriptor );
62
63                 // TODO: throw an (not runtime) exception if there is a prefix overlap - means doing so elsewhere
64
// we also need to deal with multiple versions somehow - currently, first wins
65
if ( !pluginIdsByPrefix.containsKey( pluginDescriptor.getGoalPrefix() ) )
66                 {
67                     pluginIdsByPrefix.put( pluginDescriptor.getGoalPrefix(), pluginDescriptor );
68                 }
69             }
70         }
71     }
72
73     public PluginDescriptor getPluginDescriptor( Plugin plugin )
74     {
75         // TODO: include version, but can't do this in the plugin manager as it is not resolved to the right version
76
// at that point. Instead, move the duplication check to the artifact container, or store it locally based on
77
// the unresolved version?
78
return (PluginDescriptor) pluginDescriptors.get( plugin.getKey() );
79     }
80
81     public boolean isPluginInstalled( Plugin plugin )
82     {
83         // TODO: see comment in getPluginDescriptor
84
return pluginDescriptors.containsKey( plugin.getKey() );
85     }
86
87     public PluginDescriptor getPluginDescriptorForPrefix( String JavaDoc prefix )
88     {
89         return (PluginDescriptor) pluginIdsByPrefix.get( prefix );
90     }
91
92     public void flushPluginDescriptor( Plugin plugin )
93     {
94         pluginsInProcess.remove( plugin.getKey() );
95         pluginDescriptors.remove( plugin.getKey() );
96         
97         for ( Iterator JavaDoc it = pluginIdsByPrefix.entrySet().iterator(); it.hasNext(); )
98         {
99             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
100             
101             if ( plugin.getKey().equals( entry.getValue() ) )
102             {
103                 it.remove();
104             }
105         }
106     }
107
108 }
109
Popular Tags