View Javadoc

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