View Javadoc

1   package org.apache.maven.plugin.dependency.utils.filters;
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  /**
23   * 
24   */
25  
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.dependency.fromConfiguration.ArtifactItem;
33  import org.apache.maven.plugin.dependency.utils.markers.MarkerHandler;
34  import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;
35  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
36  
37  /**
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   * @version $Id: MarkerFileFilter.java 728546 2008-12-21 22:56:51Z bentmann $
40   */
41  public class MarkerFileFilter
42      extends AbstractArtifactsFilter
43      implements ArtifactItemFilter
44  {
45  
46      boolean overWriteReleases;
47  
48      boolean overWriteSnapshots;
49  
50      boolean overWriteIfNewer;
51  
52      MarkerHandler handler;
53  
54      public MarkerFileFilter( boolean overWriteReleases, boolean overWriteSnapshots, boolean overWriteIfNewer,
55                              MarkerHandler handler )
56      {
57          this.overWriteReleases = overWriteReleases;
58          this.overWriteSnapshots = overWriteSnapshots;
59          this.overWriteIfNewer = overWriteIfNewer;
60          this.handler = handler;
61      }
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see org.apache.mojo.dependency.utils.filters.ArtifactsFilter#filter(java.util.Set,
67       *      org.apache.maven.plugin.logging.Log)
68       */
69      public Set filter( Set artifacts )
70          throws ArtifactFilterException
71      {
72          Set result = new HashSet();
73  
74          Iterator iter = artifacts.iterator();
75         
76          while ( iter.hasNext() )
77          {
78              Artifact artifact = (Artifact) iter.next();
79              if ( isArtifactIncluded( new ArtifactItem( artifact ) ) )
80              {
81                  result.add( artifact );
82              }
83          }
84          return result;
85      }
86  
87      public boolean isArtifactIncluded( ArtifactItem item )
88        throws ArtifactFilterException
89      {
90          Artifact artifact = item.getArtifact();
91          boolean overWrite = false;
92          boolean result = false;
93          if ( ( artifact.isSnapshot() && this.overWriteSnapshots )
94              || ( !artifact.isSnapshot() && this.overWriteReleases ) )
95          {
96              overWrite = true;
97          }
98  
99          handler.setArtifact( artifact );
100 
101         try
102         {
103             if ( overWrite || ( !handler.isMarkerSet() || ( overWriteIfNewer && handler.isMarkerOlder( artifact ) ) ) )
104             {
105                 result = true;
106             }
107         }
108         catch ( MojoExecutionException e )
109         {
110             throw new ArtifactFilterException (e.getMessage(),e);
111         }
112 
113         return result;
114     }
115 
116     /**
117      * @return Returns the overWriteReleases.
118      */
119     public boolean isOverWriteReleases()
120     {
121         return this.overWriteReleases;
122     }
123 
124     /**
125      * @param overWriteReleases
126      *            The overWriteReleases to set.
127      */
128     public void setOverWriteReleases( boolean overWriteReleases )
129     {
130         this.overWriteReleases = overWriteReleases;
131     }
132 
133     /**
134      * @return Returns the overWriteSnapshots.
135      */
136     public boolean isOverWriteSnapshots()
137     {
138         return this.overWriteSnapshots;
139     }
140 
141     /**
142      * @param overWriteSnapshots
143      *            The overWriteSnapshots to set.
144      */
145     public void setOverWriteSnapshots( boolean overWriteSnapshots )
146     {
147         this.overWriteSnapshots = overWriteSnapshots;
148     }
149 
150     /**
151      * @return Returns the overWriteIfNewer.
152      */
153     public boolean isOverWriteIfNewer()
154     {
155         return this.overWriteIfNewer;
156     }
157 
158     /**
159      * @param overWriteIfNewer
160      *            The overWriteIfNewer to set.
161      */
162     public void setOverWriteIfNewer( boolean overWriteIfNewer )
163     {
164         this.overWriteIfNewer = overWriteIfNewer;
165     }
166 }