View Javadoc
1   package org.apache.maven.plugins.dependency.utils.markers;
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 java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugin.MojoExecutionException;
27  
28  /**
29   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
30   */
31  public class SourcesFileMarkerHandler
32      extends DefaultFileMarkerHandler
33  {
34  
35      boolean resolved;
36  
37      /**
38       * @param markerFilesDirectory the marker files directory.
39       */
40      public SourcesFileMarkerHandler( File markerFilesDirectory )
41      {
42          super( markerFilesDirectory );
43      }
44  
45      /**
46       * @param artifact {@link Artifact}
47       * @param markerFilesDirectory marker files directory.
48       * @param isResolved true/false.
49       */ 
50      public SourcesFileMarkerHandler( Artifact artifact, File markerFilesDirectory, boolean isResolved )
51      {
52          super( artifact, markerFilesDirectory );
53          this.resolved = isResolved;
54      }
55  
56      /**
57       * Returns properly formatted File
58       * 
59       * @return File object for marker. The file is not guaranteed to exist.
60       */
61      @Override
62      public File getMarkerFile()
63      {
64          return getMarkerFile( this.resolved );
65      }
66  
67      /**
68       * Get MarkerFile, exposed for unit testing purposes
69       * 
70       * @param res resolved or not.
71       * @return marker file for this artifact.
72       */
73      protected File getMarkerFile( boolean res )
74      {
75          String suffix;
76          if ( res )
77          {
78              suffix = ".resolved";
79          }
80          else
81          {
82              suffix = ".unresolved";
83          }
84  
85          return new File( this.markerFilesDirectory, this.artifact.getId().replace( ':', '-' ) + suffix );
86      }
87  
88      /**
89       * Tests whether the file or directory denoted by this abstract pathname exists.
90       * 
91       * @return <code>true</code> if and only if the file or directory denoted by this abstract pathname exists;
92       *         <code>false</code> otherwise
93       * @throws MojoExecutionException If a security manager exists and its <code>{@link
94       *          java.lang.SecurityManager#checkRead(java.lang.String)}</code> method denies read access to the file or
95       *             directory
96       */
97      @Override
98      public boolean isMarkerSet()
99          throws MojoExecutionException
100     {
101         File marker = getMarkerFile();
102 
103         File marker2 = getMarkerFile( !this.resolved );
104 
105         return marker.exists() || marker2.exists();
106     }
107 
108     @Override
109     public boolean isMarkerOlder( Artifact theArtifact )
110         throws MojoExecutionException
111     {
112         File marker = getMarkerFile();
113         if ( marker.exists() )
114         {
115             return theArtifact.getFile().lastModified() > marker.lastModified();
116         }
117         else
118         {
119             marker = getMarkerFile( !this.resolved );
120             if ( marker.exists() )
121             {
122                 return theArtifact.getFile().lastModified() > marker.lastModified();
123             }
124             else
125             {
126                 // if the marker doesn't exist, we want to copy so assume it is
127                 // infinitely older
128                 return true;
129             }
130         }
131     }
132 
133     @Override
134     public void setMarker()
135         throws MojoExecutionException
136     {
137         File marker = getMarkerFile();
138 
139         // get the other file if it exists.
140         File clearMarker = getMarkerFile( !this.resolved );
141         // create marker file
142         try
143         {
144             marker.getParentFile().mkdirs();
145         }
146         catch ( NullPointerException e )
147         {
148             // parent is null, ignore it.
149         }
150 
151         try
152         {
153             marker.createNewFile();
154             // clear the other file if it exists.
155             if ( clearMarker.exists() )
156             {
157                 if ( !clearMarker.delete() )
158                 {
159                     clearMarker.deleteOnExit();
160                 }
161             }
162         }
163         catch ( IOException e )
164         {
165             throw new MojoExecutionException( "Unable to create Marker: " + marker.getAbsolutePath(), e );
166         }
167     }
168 
169     /**
170      * Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the
171      * directory must be empty in order to be deleted.
172      * 
173      * @return <code>true</code> if and only if the file or directory is successfully deleted; <code>false</code>
174      *         otherwise
175      * @throws SecurityException If a security manager exists and its <code>{@link
176      *          java.lang.SecurityManager#checkDelete}</code> method denies delete access to the file
177      */
178     @Override
179     public boolean clearMarker()
180         throws MojoExecutionException
181     {
182         File marker = getMarkerFile();
183         File marker2 = getMarkerFile( !this.resolved );
184         boolean markResult = marker.delete();
185         boolean mark2Result = marker2.delete();
186         return markResult || mark2Result;
187     }
188 
189     /**
190      * @return Returns the resolved.
191      */
192     public boolean isResolved()
193     {
194         return this.resolved;
195     }
196 
197     /**
198      * @param isResolved The resolved to set.
199      */
200     public void setResolved( boolean isResolved )
201     {
202         this.resolved = isResolved;
203     }
204 }