Coverage Report - org.apache.maven.model.converter.relocators.AbstractPluginRelocator
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPluginRelocator
0%
0/62
0%
0/30
2,083
 
 1  
 package org.apache.maven.model.converter.relocators;
 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.Model;
 23  
 import org.apache.maven.model.Plugin;
 24  
 import org.apache.maven.model.ReportPlugin;
 25  
 import org.apache.maven.model.converter.ConverterListener;
 26  
 import org.apache.maven.model.converter.ModelUtils;
 27  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 28  
 
 29  
 import java.util.ArrayList;
 30  
 import java.util.Iterator;
 31  
 import java.util.List;
 32  
 
 33  
 /**
 34  
  * A general implementation of the <code>PluginRelocator</code> interface.
 35  
  *
 36  
  * @author Dennis Lundberg
 37  
  * @version $Id: AbstractPluginRelocator.java 661727 2008-05-30 14:21:49Z bentmann $
 38  
  */
 39  0
 public abstract class AbstractPluginRelocator
 40  
     extends AbstractLogEnabled
 41  
     implements PluginRelocator
 42  
 {
 43  0
     private List listeners = new ArrayList();
 44  
 
 45  
     /**
 46  
      * If there is no replacement for this plugin, you can have the plugin
 47  
      * removed from the v4 pom by returning <code>null</code> from this method
 48  
      * and from getNewGroupId().
 49  
      *
 50  
      * @return The artifactId of the new Maven 2 plugin
 51  
      */
 52  
     public abstract String getNewArtifactId();
 53  
 
 54  
     /**
 55  
      * If there is no replacement for this plugin, you can have the plugin
 56  
      * removed from the v4 pom by returning <code>null</code> from this method
 57  
      * and from getNewArtifactId().
 58  
      *
 59  
      * @return The groupId of the new Maven 2 plugin
 60  
      */
 61  
     public abstract String getNewGroupId();
 62  
 
 63  
     /**
 64  
      * <strong>Note:</strong> Because we are working on the recently converted
 65  
      * Maven 2 model, this method must return the artifactId that is in the
 66  
      * model, after the model has been converted.
 67  
      *
 68  
      * @return The artifactId of the Maven 1 plugin.
 69  
      * @see org.apache.maven.model.converter.PomV3ToV4Translator#translateDependencies(java.util.List)
 70  
      */
 71  
     public abstract String getOldArtifactId();
 72  
 
 73  
     /**
 74  
      * <strong>Note:</strong> Because we are working on the recently converted
 75  
      * Maven 2 model, this method must return the groupId that is in the model,
 76  
      * after the model has been converted.
 77  
      * <p/>
 78  
      * Feel free to overload this method if your plugin has a different groupId.
 79  
      * </p>
 80  
      *
 81  
      * @return The groupId of the Maven 1 plugin.
 82  
      * @see org.apache.maven.model.converter.PomV3ToV4Translator#translateDependencies(java.util.List)
 83  
      */
 84  
     public String getOldGroupId()
 85  
     {
 86  0
         return "org.apache.maven.plugins";
 87  
     }
 88  
 
 89  
     /**
 90  
      * {@inheritDoc}
 91  
      */
 92  
     public void relocate( Model v4Model )
 93  
     {
 94  
         // Relocate build plugins
 95  0
         Plugin oldBuildPlugin = ModelUtils.findBuildPlugin( v4Model, getOldGroupId(), getOldArtifactId() );
 96  0
         if ( oldBuildPlugin != null )
 97  
         {
 98  0
             if ( getNewArtifactId() == null && getNewGroupId() == null )
 99  
             {
 100  
                 // Remove the old plugin
 101  0
                 v4Model.getBuild().getPlugins().remove( oldBuildPlugin );
 102  0
                 sendInfoMessage( "Removing build plugin " + getOldGroupId() + ":" + getOldArtifactId() );
 103  0
                 fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
 104  
             }
 105  
             else
 106  
             {
 107  0
                 Plugin newPlugin = ModelUtils.findBuildPlugin( v4Model, getNewGroupId(), getNewArtifactId() );
 108  0
                 if ( newPlugin == null )
 109  
                 {
 110  
                     // The new plugin does not exist, relocate the old one
 111  0
                     oldBuildPlugin.setArtifactId( getNewArtifactId() );
 112  0
                     oldBuildPlugin.setGroupId( getNewGroupId() );
 113  0
                     sendInfoMessage( "Relocating build plugin " + getOldGroupId() + ":" + getOldArtifactId() );
 114  0
                     fireRelocatePluginEvent( getOldGroupId(), getOldArtifactId(), getNewGroupId(), getNewArtifactId() );
 115  
                 }
 116  
                 else
 117  
                 {
 118  
                     // The new plugin already exist, remove the old one
 119  0
                     v4Model.getBuild().getPlugins().remove( oldBuildPlugin );
 120  0
                     sendInfoMessage( "Removing old build plugin " + getOldGroupId() + ":" + getOldArtifactId()
 121  
                         + " because the new one already exist" );
 122  0
                     fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
 123  
                 }
 124  
             }
 125  
         }
 126  
 
 127  
         // Relocate report plugins
 128  0
         ReportPlugin oldReportPlugin = ModelUtils.findReportPlugin( v4Model, getOldGroupId(), getOldArtifactId() );
 129  0
         if ( oldReportPlugin != null )
 130  
         {
 131  0
             if ( getNewArtifactId() == null && getNewGroupId() == null )
 132  
             {
 133  
                 // Remove the old plugin
 134  0
                 v4Model.getReporting().getPlugins().remove( oldReportPlugin );
 135  0
                 sendInfoMessage( "Removing report plugin " + getOldGroupId() + ":" + getOldArtifactId() );
 136  0
                 fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
 137  
             }
 138  
             else
 139  
             {
 140  0
                 ReportPlugin newPlugin = ModelUtils.findReportPlugin( v4Model, getNewGroupId(), getNewArtifactId() );
 141  0
                 if ( newPlugin == null )
 142  
                 {
 143  
                     // The new plugin does not exist, relocate the old one
 144  0
                     oldReportPlugin.setArtifactId( getNewArtifactId() );
 145  0
                     oldReportPlugin.setGroupId( getNewGroupId() );
 146  0
                     sendInfoMessage( "Relocating report plugin " + getOldGroupId() + ":" + getOldArtifactId() );
 147  0
                     fireRelocateReportEvent( getOldGroupId(), getOldArtifactId(), getNewGroupId(), getNewArtifactId() );
 148  
                 }
 149  
                 else
 150  
                 {
 151  
                     // The new plugin already exist, remove the old one
 152  0
                     v4Model.getReporting().getPlugins().remove( oldReportPlugin );
 153  0
                     sendInfoMessage( "Removing old report plugin " + getOldGroupId() + ":" + getOldArtifactId()
 154  
                         + " because the new one already exist" );
 155  0
                     fireRemovePluginEvent( getOldGroupId(), getOldArtifactId() );
 156  
                 }
 157  
             }
 158  
         }
 159  0
     }
 160  
 
 161  
     public void addListener( ConverterListener listener )
 162  
     {
 163  0
         if ( !listeners.contains( listener ) )
 164  
         {
 165  0
             listeners.add( listener );
 166  
         }
 167  0
     }
 168  
 
 169  
     public void addListeners( List listeners )
 170  
     {
 171  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 172  
         {
 173  0
             ConverterListener listener = (ConverterListener) i.next();
 174  0
             addListener( listener );
 175  
         }
 176  0
     }
 177  
 
 178  
     private void sendInfoMessage( String message )
 179  
     {
 180  0
         getLogger().info( message );
 181  
 
 182  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 183  
         {
 184  0
             ConverterListener listener = (ConverterListener) i.next();
 185  0
             listener.info( message );
 186  
         }
 187  0
     }
 188  
 
 189  
     private void fireRelocatePluginEvent( String oldGroupId, String oldArtifactId, String newGroupId,
 190  
                                           String newArtifactId )
 191  
     {
 192  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 193  
         {
 194  0
             ConverterListener listener = (ConverterListener) i.next();
 195  0
             listener.relocatePluginEvent( oldGroupId, oldArtifactId, newGroupId, newArtifactId );
 196  
         }
 197  0
     }
 198  
 
 199  
     private void fireRelocateReportEvent( String oldGroupId, String oldArtifactId, String newGroupId,
 200  
                                           String newArtifactId )
 201  
     {
 202  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 203  
         {
 204  0
             ConverterListener listener = (ConverterListener) i.next();
 205  0
             listener.relocateReportEvent( oldGroupId, oldArtifactId, newGroupId, newArtifactId );
 206  
         }
 207  0
     }
 208  
 
 209  
     private void fireRemovePluginEvent( String groupId, String artifactId )
 210  
     {
 211  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 212  
         {
 213  0
             ConverterListener listener = (ConverterListener) i.next();
 214  0
             listener.removePluginEvent( groupId, artifactId );
 215  
         }
 216  0
     }
 217  
 
 218  
     private void fireRemoveReportEvent( String groupId, String artifactId )
 219  
     {
 220  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 221  
         {
 222  0
             ConverterListener listener = (ConverterListener) i.next();
 223  0
             listener.removeReportEvent( groupId, artifactId );
 224  
         }
 225  0
     }
 226  
 }